html.ts 902 B

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