html.ts
959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Plugin } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
const GLOB_CONFIG_FILE_NAME = '_app.config.js'
export function configHtmlPlugin(env: ViteEnv, isBuild: boolean): Plugin {
const { VITE_GLOB_APP_TITLE, VITE_GLOB_PUBLIC_PATH } = env
const getAppConfigSrc = () => {
const path = VITE_GLOB_PUBLIC_PATH
return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${Date.now()}`
}
const htmlPlugin = createHtmlPlugin({
minify: isBuild,
inject: {
data: {
title: VITE_GLOB_APP_TITLE,
},
tags: isBuild ? [
{
tag: 'script',
attrs: { src: getAppConfigSrc() }
}
] : []
}
})
return {
name: 'html-plugin',
transformIndexHtml: (code, ctx) => {
return {
html: code,
tags: isBuild ? [
{
tag: 'script',
attrs: { src: getAppConfigSrc() }
}
] : []
}
},
}
}