html.ts
1.11 KB
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
46
47
48
49
50
51
52
53
54
55
56
import { HtmlTagDescriptor, 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() }
  //       }
  //     ] : []
  //   }
  // })
  const tags:HtmlTagDescriptor[] = [
    {
      tag: 'title',
      children: VITE_GLOB_APP_TITLE
    }
  ]
  if (isBuild) {
    tags.push(
      {
        tag: 'script',
        attrs: { src: getAppConfigSrc() }
      }
    )
  }
  return {
    name: 'html-plugin',
    transformIndexHtml: (code, ctx) => {
    
      return {
        html: code,
        tags
      }
    },
  }
}