pagesJson.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.uniPagesJsonPlugin = exports.getNVueCssPaths = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const debug_1 = __importDefault(require("debug"));
  9. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  10. const entry_1 = require("./entry");
  11. const uni_i18n_1 = require("@dcloudio/uni-i18n");
  12. const debugPagesJson = (0, debug_1.default)('uni:pages-json');
  13. const nvueCssPathsCache = new Map();
  14. function getNVueCssPaths(config) {
  15. return nvueCssPathsCache.get(config);
  16. }
  17. exports.getNVueCssPaths = getNVueCssPaths;
  18. function uniPagesJsonPlugin(options) {
  19. let resolvedConfig;
  20. const platform = process.env.UNI_PLATFORM;
  21. const inputDir = process.env.UNI_INPUT_DIR;
  22. return (0, uni_cli_shared_1.defineUniPagesJsonPlugin)((opts) => {
  23. return {
  24. name: 'uni:mp-pages-json',
  25. enforce: 'pre',
  26. configResolved(config) {
  27. resolvedConfig = config;
  28. },
  29. transform(code, id) {
  30. if (!opts.filter(id)) {
  31. return null;
  32. }
  33. this.addWatchFile(path_1.default.resolve(inputDir, 'pages.json'));
  34. (0, uni_cli_shared_1.getLocaleFiles)(path_1.default.resolve(inputDir, 'locale')).forEach((filepath) => {
  35. this.addWatchFile(filepath);
  36. });
  37. const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir);
  38. const { appJson, pageJsons, nvuePages } = (0, uni_cli_shared_1.parseMiniProgramPagesJson)(code, platform, {
  39. debug: !!manifestJson.debug,
  40. darkmode: options.app.darkmode,
  41. networkTimeout: manifestJson.networkTimeout,
  42. subpackages: !!options.app.subpackages,
  43. ...options.json,
  44. });
  45. nvueCssPathsCache.set(resolvedConfig, nvuePages.map((pagePath) => pagePath + options.style.extname));
  46. (0, uni_cli_shared_1.mergeMiniProgramAppJson)(appJson, manifestJson[platform]);
  47. if (options.json?.formatAppJson) {
  48. options.json.formatAppJson(appJson, manifestJson, pageJsons);
  49. }
  50. // 使用 once 获取的话,可以节省编译时间,但 i18n 内容发生变化时,pages.json 不会自动更新
  51. const i18nOptions = (0, uni_cli_shared_1.initI18nOptionsOnce)(platform, inputDir, false, true);
  52. if (i18nOptions) {
  53. const { locale, locales, delimiters } = i18nOptions;
  54. (0, uni_i18n_1.parseI18nJson)(appJson, locales[locale], delimiters);
  55. (0, uni_i18n_1.parseI18nJson)(pageJsons, locales[locale], delimiters);
  56. }
  57. const { normalize } = options.app;
  58. (0, uni_cli_shared_1.addMiniProgramAppJson)(normalize ? normalize(appJson) : appJson);
  59. Object.keys(pageJsons).forEach((name) => {
  60. if (isNormalPage(name)) {
  61. (0, uni_cli_shared_1.addMiniProgramPageJson)(name, pageJsons[name]);
  62. }
  63. });
  64. return {
  65. code: `import './${uni_cli_shared_1.MANIFEST_JSON_JS}'\n` + importPagesCode(appJson),
  66. map: { mappings: '' },
  67. };
  68. },
  69. generateBundle() {
  70. (0, uni_cli_shared_1.findChangedJsonFiles)(options.app.usingComponents).forEach((value, key) => {
  71. debugPagesJson('json.changed', key);
  72. this.emitFile({
  73. type: 'asset',
  74. fileName: key + '.json',
  75. source: value,
  76. });
  77. });
  78. },
  79. };
  80. });
  81. }
  82. exports.uniPagesJsonPlugin = uniPagesJsonPlugin;
  83. /**
  84. * 字节跳动小程序可以配置 ext:// 开头的插件页面模板,如 ext://microapp-trade-plugin/order-confirm
  85. * @param pagePath
  86. * @returns
  87. */
  88. function isNormalPage(pagePath) {
  89. return !pagePath.startsWith('ext://');
  90. }
  91. function importPagesCode(pagesJson) {
  92. const importPagesCode = [];
  93. function importPageCode(pagePath) {
  94. if (!isNormalPage(pagePath)) {
  95. return;
  96. }
  97. const pagePathWithExtname = (0, uni_cli_shared_1.normalizePagePath)(pagePath, process.env.UNI_PLATFORM);
  98. if (pagePathWithExtname) {
  99. importPagesCode.push(`import('${(0, entry_1.virtualPagePath)(pagePathWithExtname)}')`);
  100. }
  101. }
  102. pagesJson.pages.forEach((pagePath) => importPageCode(pagePath));
  103. if (pagesJson.subPackages) {
  104. pagesJson.subPackages.forEach(({ root, pages }) => {
  105. pages &&
  106. pages.forEach((pagePath) => importPageCode(path_1.default.join(root, pagePath)));
  107. });
  108. }
  109. return `if(!Math){
  110. ${importPagesCode.join('\n')}
  111. }`;
  112. }