updateHtml.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { readFileSync, writeFileSync, existsSync } from 'fs-extra';
  2. import viteConfig, { htmlConfig } from '../../vite.config';
  3. import { getCwdPath, successConsole, errorConsole } from '../utils';
  4. import { GLOB_CONFIG_FILE_NAME } from '../constant';
  5. import { hmScript } from './hm';
  6. const pkg = require('../../package.json');
  7. const { title, addHm, cdnConf, useCdn } = htmlConfig;
  8. function injectTitle(html: string, htmlTitle: string) {
  9. if (/<\/title>/.test(html)) {
  10. return html.replace(/<\/title>/, `${htmlTitle}</title>`);
  11. }
  12. return html;
  13. }
  14. function injectConfigScript(html: string) {
  15. const tag = `\t\t<script src='${viteConfig.base || './'}${GLOB_CONFIG_FILE_NAME}?v=${
  16. pkg.version
  17. }-${new Date().getTime()}'></script>`;
  18. if (/<\/head>/.test(html)) {
  19. return html.replace(/<\/head>/, `${tag}\n\t\t</head>`);
  20. }
  21. return html;
  22. }
  23. function injectHmScript(html: string) {
  24. if (/<head>/.test(html)) {
  25. return html.replace(/<head>/, `<head>\n${hmScript}`);
  26. }
  27. return html;
  28. }
  29. function injectCdnCss(html: string) {
  30. if (!cdnConf) return html;
  31. const { css } = cdnConf;
  32. if (!css || css.length === 0) return html;
  33. let cdnCssTag = '';
  34. for (const cssLink of css) {
  35. cdnCssTag += `<link rel="stylesheet" href="${cssLink}">`;
  36. }
  37. if (/<\/head>/.test(html)) {
  38. return html.replace(/<\/head>/, `${cdnCssTag}\n\t\t</head>`);
  39. }
  40. return html;
  41. }
  42. function injectCdnjs(html: string) {
  43. if (!cdnConf) return html;
  44. const { js } = cdnConf;
  45. if (!js || js.length === 0) return html;
  46. let cdnJsTag = '';
  47. for (const src of js) {
  48. // TODO
  49. // <script type="importmap">
  50. // { "imports": {
  51. // "vue": "https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0/vue.esm-browser.js",
  52. // "vue-router": "https://cdnjs.cloudflare.com/ajax/libs/vue-router/4.0.0-alpha.13/vue-router.esm.js",
  53. // "vuex": "https://cdnjs.cloudflare.com/ajax/libs/vuex/4.0.0-beta.2/vuex.esm-browser.js"
  54. // } }
  55. // </script>
  56. cdnJsTag += `\t<script type="text/javascript" src="${src}"></script>\n`;
  57. }
  58. if (/<\/body>/.test(html)) {
  59. return html.replace(/<\/body>/, `${cdnJsTag}\n</body>`);
  60. }
  61. return html;
  62. }
  63. export async function runUpdateHtml() {
  64. const outDir = viteConfig.outDir || 'dist';
  65. const indexPath = getCwdPath(outDir, 'index.html');
  66. if (!existsSync(`${indexPath}`)) {
  67. return;
  68. }
  69. try {
  70. let processedHtml = '';
  71. const rawHtml = readFileSync(indexPath, 'utf-8');
  72. processedHtml = rawHtml;
  73. processedHtml = injectTitle(processedHtml, title);
  74. processedHtml = injectConfigScript(processedHtml);
  75. if (addHm) {
  76. processedHtml = injectHmScript(processedHtml);
  77. }
  78. if (useCdn) {
  79. processedHtml = injectCdnCss(processedHtml);
  80. processedHtml = injectCdnjs(processedHtml);
  81. }
  82. writeFileSync(indexPath, processedHtml);
  83. successConsole('Update Html Successfully!');
  84. } catch (error) {
  85. errorConsole('Update Html Error\n' + error);
  86. }
  87. }