Blame view

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