html.ts 1.11 KB
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
      }
    },

  }
}