polyfill.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.initFixedEsbuildInitTSConfck = exports.rewriteCompilerSfcParse = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  10. const shared_1 = require("@vue/shared");
  11. /**
  12. * TODO 临时重写,解决 @vitejs/plugin-vue 的 Bug
  13. */
  14. function rewriteCompilerSfcParse() {
  15. // @ts-ignore
  16. const compilerSfc = require((0, uni_cli_shared_1.resolveBuiltIn)('@vue/compiler-sfc'));
  17. const { parse } = compilerSfc;
  18. let extname_vue = process.env.UNI_APP_X === 'true' ? uni_cli_shared_1.X_EXTNAME_VUE : uni_cli_shared_1.EXTNAME_VUE;
  19. compilerSfc.parse = (source, options) => {
  20. if (options?.filename) {
  21. const extname = path_1.default.extname(options.filename);
  22. // wxs、filter、renderjs
  23. if (extname && !extname_vue.includes(extname)) {
  24. const tag = extname.slice(1);
  25. source = `<${tag}>` + source + `</${tag}>`;
  26. }
  27. }
  28. return parse(source, options);
  29. };
  30. }
  31. exports.rewriteCompilerSfcParse = rewriteCompilerSfcParse;
  32. /**
  33. * 已废弃,交由 rewriteExistsSyncHasRootFile 实现,因为新的 vite 版本在 configResolved 中重写已经晚了
  34. * 解决 HBuilderX 项目未包含 package.json 时,initTSConfck 可能导致查找过慢,或递归目录时权限不足报错
  35. * 即:未包含 package.json 时,直接移除 initTSConfck 相关逻辑
  36. * @param inputDir
  37. * @returns
  38. */
  39. function initFixedEsbuildInitTSConfck(inputDir) {
  40. if (!(0, uni_cli_shared_1.isInHBuilderX)()) {
  41. return [];
  42. }
  43. if (fs_1.default.existsSync(path_1.default.resolve(inputDir, 'package.json'))) {
  44. return [];
  45. }
  46. // 'vite:esbuild', 'vite:esbuild-transpile' initTSConfck
  47. const existsSync = fs_1.default.existsSync;
  48. // 根目录 lerna.json 路径
  49. const lernaJsonPath = (0, uni_cli_shared_1.normalizePath)(path_1.default.resolve(inputDir, 'lerna.json'));
  50. return [
  51. {
  52. name: 'uni:fixed-HBuilderX-initTSConfck-before',
  53. enforce: 'pre',
  54. configResolved() {
  55. // 在 HBuilderX 项目中,缺少 package.json 时, 确保 searchForWorkspaceRoot 使用项目根目录, 否则 initTSConfck 会查找很费时,且可能访问目录权限报错
  56. // https://github.com/vitejs/vite/blob/43b7b78b1834a4c7128d8a5d987f66a4defcbd93/packages/vite/src/node/plugins/esbuild.ts#L407
  57. fs_1.default.existsSync = (p) => {
  58. if ((0, shared_1.isString)(p)) {
  59. // 访问根目录 lerna.json 时,直接返回 true
  60. // https://github.com/vitejs/vite/blob/43b7b78b1834a4c7128d8a5d987f66a4defcbd93/packages/vite/src/node/server/searchRoot.ts#L35
  61. if (p.endsWith('lerna.json') &&
  62. lernaJsonPath === (0, uni_cli_shared_1.normalizePath)(p)) {
  63. return true;
  64. }
  65. }
  66. return existsSync(p);
  67. };
  68. },
  69. },
  70. ];
  71. }
  72. exports.initFixedEsbuildInitTSConfck = initFixedEsbuildInitTSConfck;