2020-12-13 21:17:37 +08:00
|
|
|
import type { Plugin } from 'vite';
|
|
|
|
import ViteHtmlPlugin from 'vite-plugin-html';
|
|
|
|
import { isProdFn, isSiteMode, ViteEnv } from '../../utils';
|
|
|
|
|
|
|
|
import { hmScript } from '../hm';
|
|
|
|
// @ts-ignore
|
|
|
|
import pkg from '../../../package.json';
|
|
|
|
import { GLOB_CONFIG_FILE_NAME } from '../../constant';
|
|
|
|
|
2020-12-22 22:13:03 +08:00
|
|
|
export function setupHtmlPlugin(
|
|
|
|
plugins: Plugin[],
|
|
|
|
env: ViteEnv,
|
|
|
|
mode: 'development' | 'production'
|
|
|
|
) {
|
2020-12-13 21:17:37 +08:00
|
|
|
const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
|
|
|
|
|
|
|
|
const htmlPlugin = ViteHtmlPlugin({
|
|
|
|
// html title
|
|
|
|
title: VITE_GLOB_APP_TITLE,
|
2020-12-22 22:13:03 +08:00
|
|
|
minify: isProdFn(mode),
|
2020-12-13 21:17:37 +08:00
|
|
|
options: {
|
2020-12-22 22:13:03 +08:00
|
|
|
publicPath: VITE_PUBLIC_PATH,
|
2020-12-13 21:17:37 +08:00
|
|
|
// Package and insert additional configuration files
|
2020-12-22 22:13:03 +08:00
|
|
|
injectConfig: isProdFn(mode)
|
2020-12-13 21:17:37 +08:00
|
|
|
? `<script src='${VITE_PUBLIC_PATH || './'}${GLOB_CONFIG_FILE_NAME}?v=${
|
|
|
|
pkg.version
|
|
|
|
}-${new Date().getTime()}'></script>`
|
|
|
|
: '',
|
|
|
|
// Insert Baidu statistics code
|
|
|
|
hmScript: isSiteMode() ? hmScript : '',
|
|
|
|
title: VITE_GLOB_APP_TITLE,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
plugins.push(htmlPlugin);
|
|
|
|
return plugins;
|
|
|
|
}
|