html.ts 910 B

123456789101112131415161718192021222324252627282930313233
  1. import type { Plugin } from 'vite';
  2. import html from 'vite-plugin-html';
  3. import { ViteEnv } from '../../utils';
  4. // @ts-ignore
  5. import pkg from '../../../package.json';
  6. import { GLOB_CONFIG_FILE_NAME } from '../../constant';
  7. export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
  8. const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
  9. const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`;
  10. const htmlPlugin: Plugin[] = html({
  11. minify: isBuild,
  12. inject: {
  13. injectData: {
  14. title: VITE_GLOB_APP_TITLE,
  15. },
  16. tags: isBuild
  17. ? [
  18. {
  19. tag: 'script',
  20. attrs: {
  21. src: `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${
  22. pkg.version
  23. }-${new Date().getTime()}`,
  24. },
  25. },
  26. ]
  27. : [],
  28. },
  29. });
  30. return htmlPlugin;
  31. }