2021-02-09 23:47:14 +08:00
|
|
|
/**
|
|
|
|
* Plugin to minimize and use ejs template syntax in index.html.
|
|
|
|
* https://github.com/anncwb/vite-plugin-html
|
|
|
|
*/
|
2020-12-13 21:17:37 +08:00
|
|
|
import type { Plugin } from 'vite';
|
2021-02-09 23:47:14 +08:00
|
|
|
|
2021-01-09 23:28:52 +08:00
|
|
|
import html from 'vite-plugin-html';
|
2020-12-13 21:17:37 +08:00
|
|
|
|
2021-03-18 06:19:12 +08:00
|
|
|
import pkg from '../../../package.json';
|
2020-12-13 21:17:37 +08:00
|
|
|
import { GLOB_CONFIG_FILE_NAME } from '../../constant';
|
|
|
|
|
2021-01-09 23:28:52 +08:00
|
|
|
export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
|
2020-12-13 21:17:37 +08:00
|
|
|
const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
|
2021-02-09 23:47:14 +08:00
|
|
|
|
2021-04-01 22:34:31 +08:00
|
|
|
const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`;
|
2021-01-22 22:42:27 +08:00
|
|
|
|
2021-02-09 23:47:14 +08:00
|
|
|
const getAppConfigSrc = () => {
|
|
|
|
return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`;
|
|
|
|
};
|
|
|
|
|
2021-01-09 23:28:52 +08:00
|
|
|
const htmlPlugin: Plugin[] = html({
|
|
|
|
minify: isBuild,
|
|
|
|
inject: {
|
2021-02-09 23:47:14 +08:00
|
|
|
// Inject data into ejs template
|
2021-01-09 23:28:52 +08:00
|
|
|
injectData: {
|
|
|
|
title: VITE_GLOB_APP_TITLE,
|
|
|
|
},
|
2021-02-09 23:47:14 +08:00
|
|
|
// Embed the generated app.config.js file
|
2021-01-09 23:28:52 +08:00
|
|
|
tags: isBuild
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
tag: 'script',
|
|
|
|
attrs: {
|
2021-02-09 23:47:14 +08:00
|
|
|
src: getAppConfigSrc(),
|
2021-01-09 23:28:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: [],
|
2020-12-13 21:17:37 +08:00
|
|
|
},
|
|
|
|
});
|
2021-01-09 23:28:52 +08:00
|
|
|
return htmlPlugin;
|
2020-12-13 21:17:37 +08:00
|
|
|
}
|