html.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Plugin to minimize and use ejs template syntax in index.html.
  3. * https://github.com/anncwb/vite-plugin-html
  4. */
  5. import type { Plugin } from 'vite';
  6. import type { ViteEnv } from '../../utils';
  7. import html from 'vite-plugin-html';
  8. import pkg from '../../../package.json';
  9. import { GLOB_CONFIG_FILE_NAME } from '../../constant';
  10. export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
  11. const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
  12. const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`;
  13. const getAppConfigSrc = () => {
  14. return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`;
  15. };
  16. const htmlPlugin: Plugin[] = html({
  17. minify: isBuild,
  18. inject: {
  19. // Inject data into ejs template
  20. injectData: {
  21. title: VITE_GLOB_APP_TITLE,
  22. },
  23. // Embed the generated app.config.js file
  24. tags: isBuild
  25. ? [
  26. {
  27. tag: 'script',
  28. attrs: {
  29. src: getAppConfigSrc(),
  30. },
  31. },
  32. ]
  33. : [],
  34. },
  35. });
  36. return htmlPlugin;
  37. }