html.ts 669 Bytes
import { createHtmlPlugin } from 'vite-plugin-html'

const GLOB_CONFIG_FILE_NAME = '_app.config.js'

export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
  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 htmlPlugin
}