Blame view

build/vite/plugin/html.ts 1.07 KB
vben authored
1
2
3
4
/**
 * Plugin to minimize and use ejs template syntax in index.html.
 * https://github.com/anncwb/vite-plugin-html
 */
5
import type { Plugin } from 'vite';
vben authored
6
vben authored
7
import html from 'vite-plugin-html';
8
Vben authored
9
import pkg from '../../../package.json';
10
11
import { GLOB_CONFIG_FILE_NAME } from '../../constant';
vben authored
12
export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
13
  const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
vben authored
14
15
  const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`;
16
vben authored
17
18
19
20
  const getAppConfigSrc = () => {
    return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`;
  };
vben authored
21
22
23
  const htmlPlugin: Plugin[] = html({
    minify: isBuild,
    inject: {
vben authored
24
      // Inject data into ejs template
vben authored
25
26
27
      injectData: {
        title: VITE_GLOB_APP_TITLE,
      },
vben authored
28
      // Embed the generated app.config.js file
vben authored
29
30
31
32
33
      tags: isBuild
        ? [
            {
              tag: 'script',
              attrs: {
vben authored
34
                src: getAppConfigSrc(),
vben authored
35
36
37
38
              },
            },
          ]
        : [],
39
40
    },
  });
vben authored
41
  return htmlPlugin;
42
}