html.ts 959 Bytes
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() }
          }
        ] : []
      }
    },

  }
}