Blame view

build/vite/plugin/html.ts 1.12 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
import type { ViteEnv } from '../../utils';
vben authored
7
vben authored
8
import html from 'vite-plugin-html';
9
10
11
12

import pkg from '../../../package.json';
import { GLOB_CONFIG_FILE_NAME } from '../../constant';
vben authored
13
export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
14
  const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
vben authored
15
vben authored
16
  const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`;
17
vben authored
18
19
20
21
  const getAppConfigSrc = () => {
    return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`;
  };
vben authored
22
23
24
  const htmlPlugin: Plugin[] = html({
    minify: isBuild,
    inject: {
vben authored
25
      // Inject data into ejs template
vben authored
26
27
28
      injectData: {
        title: VITE_GLOB_APP_TITLE,
      },
vben authored
29
      // Embed the generated app.config.js file
vben authored
30
31
32
33
34
      tags: isBuild
        ? [
            {
              tag: 'script',
              attrs: {
vben authored
35
                src: getAppConfigSrc(),
vben authored
36
37
38
39
              },
            },
          ]
        : [],
40
41
    },
  });
vben authored
42
  return htmlPlugin;
43
}