index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createTransformIndexHtml = void 0;
  4. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  5. function createTransformIndexHtml() {
  6. let warned = false;
  7. return async function (html) {
  8. const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR);
  9. const title = manifestJson.h5?.title || manifestJson.name || '';
  10. const isX = process.env.UNI_APP_X === 'true';
  11. if (isX) {
  12. // 兼容旧版本模板
  13. const mainJs = ` src="/main.js"`;
  14. const mainTs = ` src="/main.ts"`;
  15. const main = ` src="/main"`;
  16. let oldMain = '';
  17. if (html.includes(mainJs)) {
  18. oldMain = mainJs;
  19. }
  20. else if (html.includes(mainTs)) {
  21. oldMain = mainTs;
  22. }
  23. if (oldMain) {
  24. html = html.replace(oldMain, main);
  25. if (!warned) {
  26. warned = true;
  27. console.warn(`当前项目根目录 index.html 未兼容 uni-app x 的 web 平台,请将里边的${oldMain} 调整为${main}。`);
  28. }
  29. }
  30. }
  31. return {
  32. html: html.replace(/<title>(.*?)<\/title>/, `<title>${title}</title>`),
  33. tags: process.env.NODE_ENV === 'development'
  34. ? [
  35. {
  36. tag: 'script',
  37. children: `if (typeof globalThis === 'undefined') {
  38. window.globalThis = window
  39. }`,
  40. injectTo: 'head-prepend',
  41. },
  42. ]
  43. : [],
  44. };
  45. };
  46. }
  47. exports.createTransformIndexHtml = createTransformIndexHtml;