@tarojs/webpack5-runner 4.2.1 → 4.2.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/index.mini.js +15 -4
  2. package/dist/index.mini.js.map +1 -1
  3. package/dist/plugins/AsyncSubPackagePlugin.js +489 -0
  4. package/dist/plugins/AsyncSubPackagePlugin.js.map +1 -0
  5. package/dist/plugins/BuildNativePlugin.js +22 -11
  6. package/dist/plugins/BuildNativePlugin.js.map +1 -1
  7. package/dist/plugins/HarmonyPlugin.js +1 -1
  8. package/dist/plugins/HarmonyPlugin.js.map +1 -1
  9. package/dist/plugins/MiniCompileModePlugin.js +49 -4
  10. package/dist/plugins/MiniCompileModePlugin.js.map +1 -1
  11. package/dist/plugins/MiniPlugin.js +282 -58
  12. package/dist/plugins/MiniPlugin.js.map +1 -1
  13. package/dist/plugins/SubPackageIndiePlugin.js +1060 -0
  14. package/dist/plugins/SubPackageIndiePlugin.js.map +1 -0
  15. package/dist/plugins/TaroInjectSyncCorePlugin.js +39 -0
  16. package/dist/plugins/TaroInjectSyncCorePlugin.js.map +1 -0
  17. package/dist/plugins/TaroNormalModule.js +16 -0
  18. package/dist/plugins/TaroNormalModule.js.map +1 -1
  19. package/dist/plugins/TaroNormalModulesPlugin.js +127 -6
  20. package/dist/plugins/TaroNormalModulesPlugin.js.map +1 -1
  21. package/dist/plugins/TaroSingleEntryPlugin.js +1 -1
  22. package/dist/plugins/TaroSingleEntryPlugin.js.map +1 -1
  23. package/dist/shared-runtime/app-shim.js +305 -0
  24. package/dist/shared-runtime/async-provider.js +144 -0
  25. package/dist/shared-runtime/build-shared-runtime.js +377 -0
  26. package/dist/shared-runtime/build-shared-runtime.js.map +1 -0
  27. package/dist/shared-runtime/constants.js +50 -0
  28. package/dist/shared-runtime/constants.js.map +1 -0
  29. package/dist/shared-runtime/entry.sync.js +102 -0
  30. package/dist/shared-runtime/externals.js +96 -0
  31. package/dist/shared-runtime/externals.js.map +1 -0
  32. package/dist/shared-runtime/scan-imperative-api.js +267 -0
  33. package/dist/shared-runtime/scan-imperative-api.js.map +1 -0
  34. package/dist/template/comp.js +2 -1
  35. package/dist/utils/asyncSubPackage.js +106 -0
  36. package/dist/utils/asyncSubPackage.js.map +1 -0
  37. package/dist/utils/component.js +2 -1
  38. package/dist/utils/component.js.map +1 -1
  39. package/dist/utils/forceCustomWrapper.js +17 -0
  40. package/dist/utils/forceCustomWrapper.js.map +1 -0
  41. package/dist/utils/webpack.js +40 -1
  42. package/dist/utils/webpack.js.map +1 -1
  43. package/dist/webpack/HarmonyWebpackPlugin.js +3 -1
  44. package/dist/webpack/HarmonyWebpackPlugin.js.map +1 -1
  45. package/dist/webpack/MiniBaseConfig.js +2 -0
  46. package/dist/webpack/MiniBaseConfig.js.map +1 -1
  47. package/dist/webpack/MiniCombination.js +51 -26
  48. package/dist/webpack/MiniCombination.js.map +1 -1
  49. package/dist/webpack/MiniWebpackPlugin.js +17 -1
  50. package/dist/webpack/MiniWebpackPlugin.js.map +1 -1
  51. package/package.json +14 -13
@@ -0,0 +1,1060 @@
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.subPackageIndieCustomWrapperRootsKey = void 0;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const helper_1 = require("@tarojs/helper");
9
+ const constants_1 = require("../shared-runtime/constants");
10
+ const component_1 = require("../utils/component");
11
+ const webpack_1 = require("../utils/webpack");
12
+ const MiniPlugin_1 = require("./MiniPlugin");
13
+ exports.subPackageIndieCustomWrapperRootsKey = '__taroSubPackageIndieCustomWrapperRoots';
14
+ const PLUGIN_NAME = 'TaroSubPackageIndiePlugin';
15
+ class SubPackageIndiePlugin {
16
+ constructor(miniPlugin) {
17
+ this.moduleRequestMap = new Map();
18
+ this.subPackageIndieConfigsCache = null;
19
+ this.allIndieRootsCache = null;
20
+ this.allMainPackageRootsCache = null;
21
+ this.subPackageIndieMatchCache = new Map();
22
+ this.entriesByRootCache = null;
23
+ this.componentByNameCache = null;
24
+ this.rootConfigContentCache = new Map();
25
+ this.forceCustomWrapperByRootCache = new Map();
26
+ this.scopedComponentConfigByCompilation = new WeakMap();
27
+ this.rootUsingCustomWrapperByCompilation = new WeakMap();
28
+ this.miniPlugin = miniPlugin;
29
+ this.options = miniPlugin.options;
30
+ }
31
+ invalidateRunCache() {
32
+ this.subPackageIndieConfigsCache = null;
33
+ this.allIndieRootsCache = null;
34
+ this.allMainPackageRootsCache = null;
35
+ this.subPackageIndieMatchCache.clear();
36
+ this.entriesByRootCache = null;
37
+ this.componentByNameCache = null;
38
+ this.rootConfigContentCache.clear();
39
+ this.forceCustomWrapperByRootCache.clear();
40
+ }
41
+ apply() {
42
+ this.miniPlugin.hooks.modifyEntries.tap(PLUGIN_NAME, entries => {
43
+ const shouldSkipRootEntries = this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended;
44
+ if (!shouldSkipRootEntries)
45
+ return entries;
46
+ return Object.assign(Object.assign({}, entries), { skipRootComp: true, skipRootWrapper: true });
47
+ });
48
+ this.miniPlugin.hooks.afterResolveModule.tap(PLUGIN_NAME, (resolveData) => {
49
+ var _a, _b;
50
+ if (!this.options.newBlended)
51
+ return;
52
+ const issuer = (_a = resolveData.contextInfo) === null || _a === void 0 ? void 0 : _a.issuer;
53
+ const request = resolveData.request;
54
+ const resource = ((_b = resolveData.createData) === null || _b === void 0 ? void 0 : _b.resource) || resolveData.resource;
55
+ if (issuer && request && resource) {
56
+ this.setResolvedRequestResource(issuer, request, resource);
57
+ }
58
+ if (resolveData.request !== helper_1.taroJsComponents)
59
+ return;
60
+ if (!issuer || !issuer.startsWith(this.options.sourceDir))
61
+ return;
62
+ const componentName = this.miniPlugin.getComponentName(issuer);
63
+ const root = this.isInSubPackageIndieRoot(componentName);
64
+ if (!root)
65
+ return;
66
+ resolveData.dependencies.forEach((dependency) => {
67
+ var _a;
68
+ if (((_a = dependency.ids) === null || _a === void 0 ? void 0 : _a.length) > 0) {
69
+ dependency.ids.forEach((id) => {
70
+ const dashedName = this.toDashedComponentName(id);
71
+ if (!component_1.componentConfig.scopedIncludes.has(root)) {
72
+ component_1.componentConfig.scopedIncludes.set(root, new Set());
73
+ }
74
+ component_1.componentConfig.scopedIncludes.get(root).add(dashedName);
75
+ });
76
+ }
77
+ });
78
+ });
79
+ this.miniPlugin.hooks.modifyChunkRequire.tap(PLUGIN_NAME, (result, chunkId, miniType) => {
80
+ if (!this.options.newBlended)
81
+ return result;
82
+ if (this.miniPlugin.nativeComponents.has(chunkId) || miniType === helper_1.META_TYPE.STATIC) {
83
+ const isSubPackageIndie = this.isInSubPackageIndieRoot(chunkId);
84
+ if (isSubPackageIndie) {
85
+ return {
86
+ source: result.source,
87
+ handled: true,
88
+ };
89
+ }
90
+ }
91
+ else if (miniType === helper_1.META_TYPE.PAGE) {
92
+ const subPackageIndieRoot = this.isInSubPackageIndieRoot(chunkId);
93
+ if (subPackageIndieRoot) {
94
+ return {
95
+ source: (0, webpack_1.addRequireToSource)(chunkId, result.source, [{ name: `${subPackageIndieRoot}/app` }]),
96
+ handled: true,
97
+ };
98
+ }
99
+ }
100
+ return result;
101
+ });
102
+ this.miniPlugin.hooks.modifySkipRootTemplates.tap(PLUGIN_NAME, skip => {
103
+ return skip || (this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended);
104
+ });
105
+ this.miniPlugin.hooks.generateExtraFiles.tap(PLUGIN_NAME, (customWrapperRoots, compilation, compiler) => {
106
+ if (!this.options.newBlended)
107
+ return customWrapperRoots;
108
+ if (!this.getSubPackageIndieConfigs().length)
109
+ return customWrapperRoots;
110
+ const roots = this.generateSubPackageIndieFiles(compilation, compiler, this.options.template, 'base');
111
+ compilation[exports.subPackageIndieCustomWrapperRootsKey] = roots;
112
+ return roots;
113
+ });
114
+ this.miniPlugin.hooks.modifyComponentConfig.tap(PLUGIN_NAME, ({ config, component }, customWrapperRoots) => {
115
+ const indieMatch = this.getSubPackageIndieMatch(component.name);
116
+ if (!indieMatch || component.isNative)
117
+ return;
118
+ const importBaseCompPath = this.getRootRelativePath(component.path, indieMatch.root, this.miniPlugin.getTargetFilePath(MiniPlugin_1.baseCompName, ''));
119
+ const importCustomWrapperPath = this.getRootRelativePath(component.path, indieMatch.root, this.miniPlugin.getTargetFilePath(MiniPlugin_1.customWrapperName, ''));
120
+ config.content.usingComponents = Object.assign({}, config.content.usingComponents);
121
+ const isRootUsingCustomWrapper = customWrapperRoots.has(indieMatch.root);
122
+ const isRootRecursiveDisabled = this.isRecursiveComponentDisabledForRoot(indieMatch.root);
123
+ if (isRootUsingCustomWrapper && !isRootRecursiveDisabled && !config.content.usingComponents[MiniPlugin_1.customWrapperName]) {
124
+ config.content.usingComponents[MiniPlugin_1.customWrapperName] = importCustomWrapperPath;
125
+ }
126
+ if (!this.options.template.isSupportRecursive && !isRootRecursiveDisabled && !config.content.usingComponents[MiniPlugin_1.baseCompName]) {
127
+ config.content.usingComponents[MiniPlugin_1.baseCompName] = importBaseCompPath;
128
+ }
129
+ });
130
+ this.miniPlugin.hooks.modifyPageConfig.tap(PLUGIN_NAME, (context, page, customWrapperRoots) => {
131
+ const subPackageIndieRoot = this.isInSubPackageIndieRoot(page.name);
132
+ if (!subPackageIndieRoot)
133
+ return context;
134
+ const isPageRecursiveDisabled = this.isRecursiveComponentDisabledForRoot(subPackageIndieRoot);
135
+ const shouldUseCustomWrapper = customWrapperRoots.has(subPackageIndieRoot) && !isPageRecursiveDisabled;
136
+ return Object.assign(Object.assign({}, context), { importBaseTemplatePath: this.getRootRelativePath(page.path, subPackageIndieRoot, this.miniPlugin.getTemplatePath('base')), importBaseCompPath: this.getRootRelativePath(page.path, subPackageIndieRoot, this.miniPlugin.getTargetFilePath(MiniPlugin_1.baseCompName, '')), importCustomWrapperPath: this.getRootRelativePath(page.path, subPackageIndieRoot, this.miniPlugin.getTargetFilePath(MiniPlugin_1.customWrapperName, '')), shouldUseCustomWrapper,
137
+ isPageRecursiveDisabled });
138
+ });
139
+ this.miniPlugin.hooks.afterGenerateFiles.tap(PLUGIN_NAME, (compilation, compiler) => {
140
+ if (this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended) {
141
+ this.generateMainPackageRuntimeFiles(compilation, compiler);
142
+ }
143
+ if (this.getSubPackageIndieConfigs().length) {
144
+ this.cleanSubPackageIndieAppConfig(compilation, compiler);
145
+ }
146
+ });
147
+ this.miniPlugin.hooks.optimizeAssets.tap(PLUGIN_NAME, compilation => {
148
+ if (!this.options.newBlended)
149
+ return;
150
+ const hasSubPackageIndieCustomWrapperRoots = compilation[exports.subPackageIndieCustomWrapperRootsKey] instanceof Set;
151
+ const subPackageIndieCustomWrapperRoots = hasSubPackageIndieCustomWrapperRoots
152
+ ? compilation[exports.subPackageIndieCustomWrapperRootsKey]
153
+ : new Set();
154
+ this.getAllIndieRoots().forEach(root => {
155
+ if (this.isRecursiveComponentDisabledForRoot(root)) {
156
+ delete compilation.assets[`${root}/${MiniPlugin_1.baseCompName}.js`];
157
+ delete compilation.assets[`${root}/${MiniPlugin_1.baseCompName}${this.options.fileType.config}`];
158
+ delete compilation.assets[`${root}/${MiniPlugin_1.baseCompName}${this.options.fileType.templ}`];
159
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}.js`];
160
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.config}`];
161
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.templ}`];
162
+ return;
163
+ }
164
+ const isRootUsingCustomWrapper = hasSubPackageIndieCustomWrapperRoots
165
+ ? subPackageIndieCustomWrapperRoots.has(root)
166
+ : this.isSubPackageIndieRootUsingCustomWrapper(compilation, root, this.getScopedSubPackageIndieComponentConfig(compilation, root).thirdPartyComponents);
167
+ if (isRootUsingCustomWrapper)
168
+ return;
169
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}.js`];
170
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.config}`];
171
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.templ}`];
172
+ });
173
+ if (this.hasSubPackageIndieMainPackageRoot()) {
174
+ const styleExt = this.options.fileType.style;
175
+ const runtimeChunks = ['app', ...this.options.commonChunks];
176
+ runtimeChunks.forEach(chunkName => {
177
+ delete compilation.assets[`${chunkName}.js`];
178
+ delete compilation.assets[`${chunkName}${styleExt}`];
179
+ });
180
+ delete compilation.assets[`app-origin${styleExt}`];
181
+ delete compilation.assets[`${MiniPlugin_1.baseCompName}.js`];
182
+ delete compilation.assets[`${MiniPlugin_1.customWrapperName}.js`];
183
+ delete compilation.assets[`common-templates${this.options.fileType.templ}`];
184
+ }
185
+ });
186
+ this.miniPlugin.hooks.modifyConfig.tap(PLUGIN_NAME, (config, componentName) => {
187
+ const subPackageIndieRoot = this.isInSubPackageIndieRoot(componentName);
188
+ if (subPackageIndieRoot && this.miniPlugin.nativeComponents.has(componentName)) {
189
+ const styleIsolation = this.options.template.isSupportRecursive
190
+ ? MiniPlugin_1.STYLE_ISOLATION_APPLY_SHARED
191
+ : MiniPlugin_1.STYLE_ISOLATION_SHARED;
192
+ config.styleIsolation = config.styleIsolation || styleIsolation;
193
+ config.isNewBlended = true;
194
+ }
195
+ return config;
196
+ });
197
+ this.miniPlugin.hooks.modifyShouldProcessStyles.tap(PLUGIN_NAME, shouldProcess => {
198
+ const hasMainPackageRoot = this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended;
199
+ return shouldProcess || hasMainPackageRoot;
200
+ });
201
+ this.miniPlugin.hooks.modifyStyleImport.tap(PLUGIN_NAME, (context, page) => {
202
+ if (!this.options.newBlended)
203
+ return context;
204
+ const indieMatch = this.getSubPackageIndieMatch(page.name);
205
+ if (!indieMatch)
206
+ return context;
207
+ if (this.miniPlugin.nativeComponents.has(page.name)) {
208
+ if (indieMatch.isMainPackageRoot) {
209
+ return Object.assign(Object.assign({}, context), { importStatement: this.buildMainRootAppStyleImport(page.name, indieMatch.root), shouldSkip: false, isMainPackageRoot: true });
210
+ }
211
+ return Object.assign(Object.assign({}, context), { importStatement: '', shouldSkip: true, isMainPackageRoot: false });
212
+ }
213
+ if (indieMatch.isMainPackageRoot) {
214
+ return Object.assign(Object.assign({}, context), { importStatement: this.buildMainRootAppStyleImport(page.name, indieMatch.root), shouldSkip: false, isMainPackageRoot: true });
215
+ }
216
+ return context;
217
+ });
218
+ }
219
+ /**
220
+ * mainPackageRoot 页面/组件 wxss 里 @import app 样式的语句。
221
+ * app 样式(app.wxss)随 runtime chunks 落在**归一化后**的 mainPackageRoot(normalizeIndieRoot 会砍掉
222
+ * 末尾 /index),而页面 wxss 落在 `${page.name}/<basename>.wxss`——两者深度可能不同(如 page
223
+ * pages/order/index/index 与 root pages/order,页面深一级)。故不能硬编码 './app',要按页面 wxss 目录
224
+ * 到 `${root}/app` 重算,算法与 MiniPlugin 默认样式注入(path.dirname(pageStyle) → app 样式)一致。
225
+ */
226
+ buildMainRootAppStyleImport(pageName, root) {
227
+ const styleExt = this.options.fileType.style;
228
+ return (0, webpack_1.buildRootStyleImport)(`${pageName}${styleExt}`, `${root}/app${styleExt}`);
229
+ }
230
+ /**
231
+ * 生成注入 app.js 的 __taroRegisterRecursiveComponent(供各 root 的 comp.js 调用注册递归基础组件)。
232
+ * 递归组件配置由 @tarojs/runtime 的 createRecursiveComponentConfig 提供。
233
+ * - vanilla:@tarojs/runtime 打进本包 webpack 模块,registrar 遍历 __webpack_require__.c/.m 找到它。
234
+ * - 共享运行时:@tarojs/runtime 被 external 到共享全局(wx.__TARO_RT_ASYNC_V*__),不在 webpack 模块里,
235
+ * 原遍历必然失败 → throw,故 sharedRuntime 下在遍历前先查共享全局。
236
+ * 严格 gate:非 sharedRuntime 产出与原字符串逐字节一致,vanilla 零回归。
237
+ */
238
+ buildRecursiveComponentRegistrarExpr() {
239
+ var _a;
240
+ const sharedRuntime = !!this.options.combination.config.sharedRuntime;
241
+ // 共享全局查找片段:sharedRuntime 时在 webpack 模块遍历前先试共享全局的 @tarojs/runtime。
242
+ // globalObject(wx/my/swan…)与全局 key 均取自单一来源,拼进运行时字符串。
243
+ let sharedLookup = '';
244
+ if (sharedRuntime) {
245
+ const globalObject = ((_a = this.options.combination.config.output) === null || _a === void 0 ? void 0 : _a.globalObject) || 'wx';
246
+ const g = `${globalObject}[${JSON.stringify(constants_1.SHARED_GLOBAL_ASYNC)}]`;
247
+ sharedLookup =
248
+ `var g=typeof ${globalObject}!=="undefined"?${g}:undefined;` +
249
+ `if(g&&g["@tarojs/runtime"]&&typeof g["@tarojs/runtime"].createRecursiveComponentConfig==="function"){createRecursiveComponentConfig=g["@tarojs/runtime"].createRecursiveComponentConfig;}`;
250
+ }
251
+ return `(typeof globalThis.__taroRegisterRecursiveComponent==="function"||(globalThis.__taroRegisterRecursiveComponent=function(componentName,forceCustomWrapper){let createRecursiveComponentConfig;${sharedLookup}if(typeof createRecursiveComponentConfig!=="function"){const cache=__webpack_require__.c||{};for(const key in cache){const exports=cache[key]&&cache[key].exports;if(exports&&typeof exports.createRecursiveComponentConfig==="function"){createRecursiveComponentConfig=exports.createRecursiveComponentConfig;break;}}}if(typeof createRecursiveComponentConfig!=="function"){const modules=__webpack_require__.m||{};for(const moduleId in modules){const moduleFactory=modules[moduleId];if(!moduleFactory||typeof moduleFactory!=="function")continue;const source=String(moduleFactory);if(source.indexOf("createRecursiveComponentConfig")===-1)continue;const exports=__webpack_require__(moduleId);if(exports&&typeof exports.createRecursiveComponentConfig==="function"){createRecursiveComponentConfig=exports.createRecursiveComponentConfig;break;}}}if(typeof createRecursiveComponentConfig!=="function"){throw new Error("Cannot find createRecursiveComponentConfig in webpack modules");}Component(createRecursiveComponentConfig(componentName,forceCustomWrapper));}))`;
252
+ }
253
+ getRootRelativePath(fromPath, root, targetPath) {
254
+ return (0, helper_1.promoteRelativePath)(node_path_1.default.relative(fromPath, node_path_1.default.join(this.options.sourceDir, root, targetPath)));
255
+ }
256
+ normalizeIndieRoot(root) {
257
+ if (root.endsWith('/index')) {
258
+ return node_path_1.default.dirname(root);
259
+ }
260
+ return root;
261
+ }
262
+ parseIndieRootConfig(rootConfig) {
263
+ var _a;
264
+ if (typeof rootConfig === 'string') {
265
+ return { path: this.normalizeIndieRoot(rootConfig), originalPath: rootConfig, disableRecursiveComponent: false };
266
+ }
267
+ return {
268
+ path: this.normalizeIndieRoot(rootConfig.path),
269
+ originalPath: rootConfig.path,
270
+ disableRecursiveComponent: !!rootConfig.disableRecursiveComponent,
271
+ asyncRoot: (_a = rootConfig.asyncSubPackage) === null || _a === void 0 ? void 0 : _a.asyncRoot,
272
+ };
273
+ }
274
+ getSubPackageIndieConfigs() {
275
+ if (this.subPackageIndieConfigsCache)
276
+ return this.subPackageIndieConfigsCache;
277
+ const appConfig = this.miniPlugin.appConfig;
278
+ const subPackageIndie = appConfig === null || appConfig === void 0 ? void 0 : appConfig.subPackageIndie;
279
+ if (!Array.isArray(subPackageIndie)) {
280
+ this.subPackageIndieConfigsCache = [];
281
+ return this.subPackageIndieConfigsCache;
282
+ }
283
+ const appPages = new Set((appConfig === null || appConfig === void 0 ? void 0 : appConfig.pages) || []);
284
+ this.subPackageIndieConfigsCache = subPackageIndie.map(({ mainPackageRoot, subPackageRoots = [] }) => {
285
+ var _a;
286
+ const mainParsed = this.parseIndieRootConfig(mainPackageRoot);
287
+ if (!appPages.has(mainParsed.originalPath) && ((_a = this.options.logger) === null || _a === void 0 ? void 0 : _a.quiet) === false) {
288
+ (0, helper_1.printLog)("warning" /* processTypeEnum.WARNING */, 'subPackageIndie', `mainPackageRoot.path "${mainParsed.originalPath}" 未配置在 app.config 的 pages 中,请补充 pages: [..., "${mainParsed.originalPath}"],否则主包入口页面文件不会生成。`);
289
+ }
290
+ const subParsedList = subPackageRoots.map(root => this.parseIndieRootConfig(root));
291
+ const disableRecursiveComponentRoots = new Set();
292
+ const asyncSubPackageRootMap = new Map();
293
+ if (mainParsed.disableRecursiveComponent) {
294
+ disableRecursiveComponentRoots.add(mainParsed.path);
295
+ }
296
+ if (mainParsed.asyncRoot) {
297
+ asyncSubPackageRootMap.set(mainParsed.path, mainParsed.asyncRoot);
298
+ }
299
+ subParsedList.forEach(sub => {
300
+ if (sub.disableRecursiveComponent) {
301
+ disableRecursiveComponentRoots.add(sub.path);
302
+ }
303
+ if (sub.asyncRoot) {
304
+ asyncSubPackageRootMap.set(sub.path, sub.asyncRoot);
305
+ }
306
+ });
307
+ return {
308
+ mainPackageRoot: mainParsed.path,
309
+ subPackageRoots: Array.from(new Set(subParsedList.map(sub => sub.path))),
310
+ disableRecursiveComponentRoots,
311
+ asyncSubPackageRootMap,
312
+ };
313
+ });
314
+ return this.subPackageIndieConfigsCache;
315
+ }
316
+ getAllIndieRoots() {
317
+ if (!this.allIndieRootsCache) {
318
+ this.allIndieRootsCache = Array.from(new Set(this.getSubPackageIndieConfigs().flatMap(({ mainPackageRoot, subPackageRoots }) => [mainPackageRoot, ...subPackageRoots])));
319
+ }
320
+ return this.allIndieRootsCache;
321
+ }
322
+ /**
323
+ * 获取所有配置了 asyncSubPackage 的 root -> asyncRoot 映射
324
+ * key: source root 路径, value: asyncRoot 路径
325
+ */
326
+ getAsyncSubPackageRootMap() {
327
+ const result = new Map();
328
+ for (const config of this.getSubPackageIndieConfigs()) {
329
+ for (const [sourceRoot, asyncRoot] of config.asyncSubPackageRootMap) {
330
+ result.set(sourceRoot, asyncRoot);
331
+ }
332
+ }
333
+ return result;
334
+ }
335
+ /**
336
+ * 获取 asyncSubPackage 运行时路径解析锚点。
337
+ * blended 场景下动态组件实际渲染在 mainPackageRoot 页面上下文中,
338
+ * require.async 也应始终基于当前 mainRoot route 计算到 asyncRoot/index 的相对路径。
339
+ * sourceRoot -> asyncRoot 映射仍用于 chunk/style/template 归属;运行时只需要 mainPackageRoot -> asyncRoot。
340
+ */
341
+ getAsyncSubPackageRuntimeRoots() {
342
+ const result = [];
343
+ const seen = new Set();
344
+ const push = (sourceRoot, asyncRoot) => {
345
+ const key = `${sourceRoot}${asyncRoot}`;
346
+ if (seen.has(key))
347
+ return;
348
+ seen.add(key);
349
+ result.push({ sourceRoot, asyncRoot });
350
+ };
351
+ for (const config of this.getSubPackageIndieConfigs()) {
352
+ const asyncRoots = new Set(config.asyncSubPackageRootMap.values());
353
+ for (const asyncRoot of asyncRoots) {
354
+ push(config.mainPackageRoot, asyncRoot);
355
+ }
356
+ }
357
+ return result;
358
+ }
359
+ getAllMainPackageRoots() {
360
+ if (!this.allMainPackageRootsCache) {
361
+ this.allMainPackageRootsCache = Array.from(new Set(this.getSubPackageIndieConfigs().map(item => item.mainPackageRoot)));
362
+ }
363
+ return this.allMainPackageRootsCache;
364
+ }
365
+ hasSubPackageIndieMainPackageRoot() {
366
+ return this.getAllMainPackageRoots().length > 0;
367
+ }
368
+ isRecursiveComponentDisabledForRoot(root) {
369
+ return this.getSubPackageIndieConfigs().some(config => config.disableRecursiveComponentRoots.has(root));
370
+ }
371
+ getSubPackageIndieMatch(pageName) {
372
+ if (!this.options.newBlended)
373
+ return null;
374
+ if (this.subPackageIndieMatchCache.has(pageName)) {
375
+ return this.subPackageIndieMatchCache.get(pageName) || null;
376
+ }
377
+ const configs = this.getSubPackageIndieConfigs();
378
+ for (const config of configs) {
379
+ if (pageName.startsWith(config.mainPackageRoot + '/') || pageName === config.mainPackageRoot) {
380
+ const match = {
381
+ config,
382
+ root: config.mainPackageRoot,
383
+ isMainPackageRoot: true,
384
+ };
385
+ this.subPackageIndieMatchCache.set(pageName, match);
386
+ return match;
387
+ }
388
+ for (const root of config.subPackageRoots) {
389
+ if (pageName.startsWith(root + '/') || pageName === root) {
390
+ const match = {
391
+ config,
392
+ root,
393
+ isMainPackageRoot: false,
394
+ };
395
+ this.subPackageIndieMatchCache.set(pageName, match);
396
+ return match;
397
+ }
398
+ }
399
+ }
400
+ this.subPackageIndieMatchCache.set(pageName, null);
401
+ return null;
402
+ }
403
+ isInSubPackageIndieRoot(pageName) {
404
+ var _a;
405
+ return ((_a = this.getSubPackageIndieMatch(pageName)) === null || _a === void 0 ? void 0 : _a.root) || null;
406
+ }
407
+ getModuleSourceContent(module) {
408
+ var _a;
409
+ try {
410
+ const source = (_a = module === null || module === void 0 ? void 0 : module.originalSource) === null || _a === void 0 ? void 0 : _a.call(module);
411
+ if (!source || typeof source.source !== 'function')
412
+ return '';
413
+ return String(source.source() || '');
414
+ }
415
+ catch (_err) {
416
+ return '';
417
+ }
418
+ }
419
+ getModuleResource(module) {
420
+ var _a;
421
+ const resource = (module === null || module === void 0 ? void 0 : module.resource) || ((_a = module === null || module === void 0 ? void 0 : module.rootModule) === null || _a === void 0 ? void 0 : _a.resource);
422
+ return typeof resource === 'string' ? resource : undefined;
423
+ }
424
+ collectFlattenedModules(module, collected, visited = new Set()) {
425
+ if (!module || visited.has(module))
426
+ return;
427
+ visited.add(module);
428
+ collected.add(module);
429
+ const nestedModuleCollections = [module.rootModule, module.modules, module._modules];
430
+ nestedModuleCollections.forEach(item => {
431
+ if (!item)
432
+ return;
433
+ if (item && typeof item[Symbol.iterator] === 'function') {
434
+ for (const nested of item) {
435
+ this.collectFlattenedModules(nested, collected, visited);
436
+ }
437
+ }
438
+ else {
439
+ this.collectFlattenedModules(item, collected, visited);
440
+ }
441
+ });
442
+ }
443
+ getCompilationModuleResourceMap(compilation) {
444
+ const moduleMap = new Map();
445
+ for (const module of compilation.modules) {
446
+ const flattenedModules = new Set();
447
+ this.collectFlattenedModules(module, flattenedModules);
448
+ flattenedModules.forEach(flattenedModule => {
449
+ const resource = this.getModuleResource(flattenedModule);
450
+ if (resource) {
451
+ moduleMap.set(resource, flattenedModule);
452
+ }
453
+ });
454
+ }
455
+ return moduleMap;
456
+ }
457
+ getChunksBySubPackageIndieRoot(compilation, root) {
458
+ const chunks = new Set();
459
+ this.getSubPackageIndieEntriesByRoot(root).forEach(({ name }) => {
460
+ const entryChunk = Array.from(compilation.chunks).find(chunk => chunk.name === name);
461
+ if (!entryChunk)
462
+ return;
463
+ chunks.add(entryChunk);
464
+ entryChunk.groupsIterable.forEach(group => {
465
+ group.chunks.forEach(chunk => chunks.add(chunk));
466
+ });
467
+ });
468
+ return chunks;
469
+ }
470
+ getSubPackageIndieModules(compilation, root) {
471
+ const modules = new Set();
472
+ this.getChunksBySubPackageIndieRoot(compilation, root).forEach(chunk => {
473
+ for (const module of compilation.chunkGraph.getChunkModulesIterable(chunk)) {
474
+ this.collectFlattenedModules(module, modules);
475
+ }
476
+ });
477
+ return modules;
478
+ }
479
+ normalizeModuleRequestMapKey(resource) {
480
+ if (!resource || typeof resource !== 'string')
481
+ return null;
482
+ const resourceWithoutLoader = resource.split('!').pop();
483
+ if (!resourceWithoutLoader)
484
+ return null;
485
+ const resourcePath = resourceWithoutLoader.split('?')[0];
486
+ if (!resourcePath)
487
+ return null;
488
+ return node_path_1.default.normalize(resourcePath);
489
+ }
490
+ getModuleRequestMapKeys(resource) {
491
+ const keys = new Set();
492
+ if (resource && typeof resource === 'string') {
493
+ keys.add(resource);
494
+ }
495
+ const normalizedResource = this.normalizeModuleRequestMapKey(resource);
496
+ if (normalizedResource) {
497
+ keys.add(normalizedResource);
498
+ }
499
+ return Array.from(keys);
500
+ }
501
+ setResolvedRequestResource(issuer, request, resource) {
502
+ this.getModuleRequestMapKeys(issuer).forEach(key => {
503
+ if (!this.moduleRequestMap.has(key)) {
504
+ this.moduleRequestMap.set(key, new Map());
505
+ }
506
+ this.moduleRequestMap.get(key).set(request, resource);
507
+ });
508
+ }
509
+ getResolvedRequestResource(issuerResource, request) {
510
+ var _a;
511
+ for (const key of this.getModuleRequestMapKeys(issuerResource)) {
512
+ const resolvedResource = (_a = this.moduleRequestMap.get(key)) === null || _a === void 0 ? void 0 : _a.get(request);
513
+ if (resolvedResource)
514
+ return resolvedResource;
515
+ }
516
+ }
517
+ getResolvedModuleByResource(moduleResourceMap, resource) {
518
+ const exactModule = moduleResourceMap.get(resource);
519
+ if (exactModule)
520
+ return exactModule;
521
+ const normalizedResource = this.normalizeModuleRequestMapKey(resource);
522
+ if (!normalizedResource)
523
+ return null;
524
+ const normalizedModule = moduleResourceMap.get(normalizedResource);
525
+ if (normalizedModule)
526
+ return normalizedModule;
527
+ for (const [moduleResource, module] of moduleResourceMap.entries()) {
528
+ if (this.normalizeModuleRequestMapKey(moduleResource) === normalizedResource) {
529
+ return module;
530
+ }
531
+ }
532
+ return null;
533
+ }
534
+ resolveRequestedModule(moduleResourceMap, issuerModule, request) {
535
+ const issuerResource = this.getModuleResource(issuerModule);
536
+ const resolvedResource = this.getResolvedRequestResource(issuerResource, request);
537
+ if (resolvedResource) {
538
+ const resolvedModule = this.getResolvedModuleByResource(moduleResourceMap, resolvedResource);
539
+ if (resolvedModule)
540
+ return resolvedModule;
541
+ }
542
+ if (issuerResource && /^[.\\/]/.test(request)) {
543
+ const requestPath = node_path_1.default.resolve(node_path_1.default.dirname(issuerResource), request);
544
+ const resolvedPath = (0, helper_1.resolveMainFilePath)(requestPath);
545
+ const requestModule = this.getResolvedModuleByResource(moduleResourceMap, resolvedPath);
546
+ if (requestModule)
547
+ return requestModule;
548
+ }
549
+ for (const module of moduleResourceMap.values()) {
550
+ if (!module)
551
+ continue;
552
+ if (module.rawRequest === request || module.userRequest === request || module.request === request) {
553
+ return module;
554
+ }
555
+ }
556
+ return null;
557
+ }
558
+ resolveImportBindingToTaroComponentName(moduleResourceMap, issuerModule, binding, propertyName, visited = new Set()) {
559
+ if (!(binding === null || binding === void 0 ? void 0 : binding.source))
560
+ return null;
561
+ if (binding.kind === 'namespace') {
562
+ if (!propertyName)
563
+ return null;
564
+ if (binding.source === helper_1.taroJsComponents) {
565
+ return propertyName;
566
+ }
567
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, issuerModule, binding.source);
568
+ if (!targetModule) {
569
+ if (!/^[.\\/]/.test(binding.source)) {
570
+ return propertyName;
571
+ }
572
+ return null;
573
+ }
574
+ return this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, propertyName, visited);
575
+ }
576
+ const importedName = binding.imported || propertyName;
577
+ if (!importedName || importedName === 'default')
578
+ return null;
579
+ if (binding.source === helper_1.taroJsComponents) {
580
+ return importedName;
581
+ }
582
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, issuerModule, binding.source);
583
+ if (!targetModule) {
584
+ if (!/^[.\\/]/.test(binding.source)) {
585
+ return importedName;
586
+ }
587
+ return null;
588
+ }
589
+ return this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, importedName, visited);
590
+ }
591
+ resolveModuleExportToTaroComponentName(moduleResourceMap, module, exportName, visited = new Set()) {
592
+ var _a, _b;
593
+ const moduleResource = this.getModuleResource(module);
594
+ if (!moduleResource)
595
+ return null;
596
+ const visitKey = `${moduleResource}::${exportName}`;
597
+ if (visited.has(visitKey))
598
+ return null;
599
+ visited.add(visitKey);
600
+ const exportBinding = (_a = module === null || module === void 0 ? void 0 : module.exportBindings) === null || _a === void 0 ? void 0 : _a[exportName];
601
+ if (exportBinding) {
602
+ if (exportBinding.kind === 'reexport' && exportBinding.source) {
603
+ if (exportBinding.source === helper_1.taroJsComponents) {
604
+ return exportBinding.imported || exportName;
605
+ }
606
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, module, exportBinding.source);
607
+ if (targetModule) {
608
+ const resolvedName = this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, exportBinding.imported || exportName, visited);
609
+ if (resolvedName)
610
+ return resolvedName;
611
+ }
612
+ }
613
+ if (exportBinding.kind === 'local' && exportBinding.local) {
614
+ const importedBinding = (_b = module === null || module === void 0 ? void 0 : module.importedBindings) === null || _b === void 0 ? void 0 : _b[exportBinding.local];
615
+ if (importedBinding) {
616
+ const resolvedName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, importedBinding, undefined, visited);
617
+ if (resolvedName)
618
+ return resolvedName;
619
+ }
620
+ }
621
+ }
622
+ for (const source of (module === null || module === void 0 ? void 0 : module.exportAllSources) || []) {
623
+ if (source === helper_1.taroJsComponents) {
624
+ return exportName;
625
+ }
626
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, module, source);
627
+ if (!targetModule)
628
+ continue;
629
+ const resolvedName = this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, exportName, visited);
630
+ if (resolvedName)
631
+ return resolvedName;
632
+ }
633
+ return null;
634
+ }
635
+ resolveUsedComponentRefToTaroComponentName(moduleResourceMap, module, componentRef) {
636
+ var _a, _b;
637
+ if (!componentRef)
638
+ return null;
639
+ if (componentRef.kind === 'identifier') {
640
+ const binding = (_a = module === null || module === void 0 ? void 0 : module.importedBindings) === null || _a === void 0 ? void 0 : _a[componentRef.name];
641
+ if (!binding) {
642
+ return componentRef.name;
643
+ }
644
+ const resolvedName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, binding);
645
+ return resolvedName || componentRef.name;
646
+ }
647
+ if (componentRef.kind === 'member') {
648
+ const binding = (_b = module === null || module === void 0 ? void 0 : module.importedBindings) === null || _b === void 0 ? void 0 : _b[componentRef.object];
649
+ if (!binding) {
650
+ return componentRef.property || null;
651
+ }
652
+ const resolvedName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, binding, componentRef.property);
653
+ return resolvedName || componentRef.property || null;
654
+ }
655
+ return null;
656
+ }
657
+ shouldSkipSubPackageIndieModule(module, root) {
658
+ const resource = this.getModuleResource(module);
659
+ if (typeof resource !== 'string' || !resource.startsWith(this.options.sourceDir)) {
660
+ return false;
661
+ }
662
+ const componentName = this.miniPlugin.getComponentName(resource);
663
+ const targetRoot = this.isInSubPackageIndieRoot(componentName);
664
+ return !!targetRoot && targetRoot !== root;
665
+ }
666
+ collectModuleResolvedComponentIncludes(compilation, root) {
667
+ const includes = new Set();
668
+ const moduleResourceMap = this.getCompilationModuleResourceMap(compilation);
669
+ for (const module of this.getSubPackageIndieModules(compilation, root)) {
670
+ if (this.shouldSkipSubPackageIndieModule(module, root))
671
+ continue;
672
+ for (const elementName of (module === null || module === void 0 ? void 0 : module.elementNameSet) || []) {
673
+ if (!component_1.componentConfig.thirdPartyComponents.has(elementName)) {
674
+ includes.add(elementName);
675
+ }
676
+ }
677
+ for (const binding of Object.values((module === null || module === void 0 ? void 0 : module.importedBindings) || {})) {
678
+ const resolvedComponentName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, binding);
679
+ if (!resolvedComponentName)
680
+ continue;
681
+ const dashedName = this.toDashedComponentName(resolvedComponentName);
682
+ if (!component_1.componentConfig.thirdPartyComponents.has(dashedName)) {
683
+ includes.add(dashedName);
684
+ }
685
+ }
686
+ for (const componentRef of (module === null || module === void 0 ? void 0 : module.usedComponentRefs) || []) {
687
+ const resolvedComponentName = this.resolveUsedComponentRefToTaroComponentName(moduleResourceMap, module, componentRef);
688
+ if (!resolvedComponentName)
689
+ continue;
690
+ const dashedName = this.toDashedComponentName(resolvedComponentName);
691
+ if (!component_1.componentConfig.thirdPartyComponents.has(dashedName)) {
692
+ includes.add(dashedName);
693
+ }
694
+ }
695
+ }
696
+ return includes;
697
+ }
698
+ expandScopedIncludes(includes) {
699
+ const scopedIncludes = new Set(includes);
700
+ if (scopedIncludes.has('view')) {
701
+ scopedIncludes.add('catch-view');
702
+ scopedIncludes.add('static-view');
703
+ scopedIncludes.add('pure-view');
704
+ scopedIncludes.add('click-view');
705
+ }
706
+ if (scopedIncludes.has('image')) {
707
+ scopedIncludes.add('static-image');
708
+ }
709
+ if (scopedIncludes.has('text')) {
710
+ scopedIncludes.add('static-text');
711
+ }
712
+ return scopedIncludes;
713
+ }
714
+ toDashedComponentName(componentName) {
715
+ return componentName.replace(/[A-Z]/g, (match, offset) => `${offset === 0 ? '' : '-'}${match.toLowerCase()}`);
716
+ }
717
+ collectScopedInnerComponents(compilation, root) {
718
+ const includes = new Set();
719
+ const scopedIncludes = component_1.componentConfig.scopedIncludes.get(root);
720
+ if (scopedIncludes && scopedIncludes.size > 0) {
721
+ scopedIncludes.forEach(name => includes.add(name));
722
+ }
723
+ this.collectModuleResolvedComponentIncludes(compilation, root).forEach(name => includes.add(name));
724
+ const finalIncludes = includes.size > 0
725
+ ? includes
726
+ : new Set(component_1.componentConfig.includes);
727
+ return this.expandScopedIncludes(finalIncludes);
728
+ }
729
+ isSubPackageIndieRootUsingCustomWrapperByModules(compilation, root) {
730
+ const moduleResourceMap = this.getCompilationModuleResourceMap(compilation);
731
+ for (const module of this.getSubPackageIndieModules(compilation, root)) {
732
+ if (this.shouldSkipSubPackageIndieModule(module, root))
733
+ continue;
734
+ for (const componentRef of (module === null || module === void 0 ? void 0 : module.usedComponentRefs) || []) {
735
+ const resolvedComponentName = this.resolveUsedComponentRefToTaroComponentName(moduleResourceMap, module, componentRef);
736
+ if (resolvedComponentName === 'CustomWrapper') {
737
+ return true;
738
+ }
739
+ }
740
+ }
741
+ return false;
742
+ }
743
+ isSubPackageIndieRootUsingCustomWrapper(compilation, root, scopedThirdPartyComponents) {
744
+ if (scopedThirdPartyComponents === null || scopedThirdPartyComponents === void 0 ? void 0 : scopedThirdPartyComponents.has(MiniPlugin_1.customWrapperName)) {
745
+ return true;
746
+ }
747
+ let cache = this.rootUsingCustomWrapperByCompilation.get(compilation);
748
+ if (!cache) {
749
+ cache = new Map();
750
+ this.rootUsingCustomWrapperByCompilation.set(compilation, cache);
751
+ }
752
+ if (cache.has(root)) {
753
+ return cache.get(root) || false;
754
+ }
755
+ const usingCustomWrapper = this.isSubPackageIndieRootUsingCustomWrapperByModules(compilation, root);
756
+ cache.set(root, usingCustomWrapper);
757
+ return usingCustomWrapper;
758
+ }
759
+ resolveUsingComponentTarget(filePath, componentPath) {
760
+ if (componentPath.startsWith('plugin://'))
761
+ return null;
762
+ let sourcePath = componentPath;
763
+ if (node_path_1.default.isAbsolute(componentPath)) {
764
+ sourcePath = helper_1.fs.existsSync(componentPath)
765
+ ? componentPath
766
+ : node_path_1.default.resolve(this.options.sourceDir, `.${componentPath}`);
767
+ }
768
+ else {
769
+ const isRelativePath = /^[.\\/]/;
770
+ if (!isRelativePath.test(componentPath))
771
+ return null;
772
+ sourcePath = node_path_1.default.resolve(node_path_1.default.dirname(filePath), componentPath);
773
+ }
774
+ const targetPath = (0, helper_1.resolveMainFilePath)(sourcePath);
775
+ const targetName = this.miniPlugin.getComponentName(targetPath);
776
+ return {
777
+ path: targetPath,
778
+ name: targetName,
779
+ root: this.isInSubPackageIndieRoot(targetName),
780
+ };
781
+ }
782
+ cloneThirdPartyComponent(thirdPartyComponents, componentName) {
783
+ const attrs = component_1.componentConfig.thirdPartyComponents.get(componentName);
784
+ if (!attrs || thirdPartyComponents.has(componentName))
785
+ return;
786
+ thirdPartyComponents.set(componentName, new Set(attrs));
787
+ }
788
+ getSubPackageIndieEntriesByRoot(root) {
789
+ if (!this.entriesByRootCache) {
790
+ const entriesByRoot = new Map();
791
+ [...this.miniPlugin.pages, ...this.miniPlugin.components].forEach(item => {
792
+ const indieRoot = this.isInSubPackageIndieRoot(item.name);
793
+ if (!indieRoot)
794
+ return;
795
+ if (!entriesByRoot.has(indieRoot)) {
796
+ entriesByRoot.set(indieRoot, new Map());
797
+ }
798
+ entriesByRoot.get(indieRoot).set(item.name, item);
799
+ });
800
+ this.entriesByRootCache = new Map();
801
+ entriesByRoot.forEach((entries, indieRoot) => {
802
+ this.entriesByRootCache.set(indieRoot, Array.from(entries.values()));
803
+ });
804
+ }
805
+ return this.entriesByRootCache.get(root) || [];
806
+ }
807
+ getSubPackageIndieRootEntry(root) {
808
+ const entries = this.getSubPackageIndieEntriesByRoot(root);
809
+ return entries.find(entry => entry.name === `${root}/index`) || entries.find(entry => entry.name === root) || entries[0];
810
+ }
811
+ getSubPackageIndieRootConfigContent(root) {
812
+ var _a;
813
+ if (this.rootConfigContentCache.has(root)) {
814
+ return this.rootConfigContentCache.get(root);
815
+ }
816
+ const entry = this.getSubPackageIndieRootEntry(root);
817
+ const content = entry
818
+ ? (_a = this.miniPlugin.filesConfig[this.miniPlugin.getConfigFilePath(entry.name)]) === null || _a === void 0 ? void 0 : _a.content
819
+ : undefined;
820
+ this.rootConfigContentCache.set(root, content);
821
+ return content;
822
+ }
823
+ getSubPackageIndieRootUsingComponents(root) {
824
+ const content = this.getSubPackageIndieRootConfigContent(root);
825
+ return Object.assign({}, content === null || content === void 0 ? void 0 : content.usingComponents);
826
+ }
827
+ getSubPackageIndieRootComponentPlaceholder(root) {
828
+ const content = this.getSubPackageIndieRootConfigContent(root);
829
+ const componentPlaceholder = content === null || content === void 0 ? void 0 : content.componentPlaceholder;
830
+ if (!componentPlaceholder || Object.keys(componentPlaceholder).length === 0)
831
+ return undefined;
832
+ return Object.assign({}, componentPlaceholder);
833
+ }
834
+ isForceCustomWrapperEnabledForRoot(root) {
835
+ var _a;
836
+ if (this.options.template.isSupportRecursive)
837
+ return false;
838
+ if (this.forceCustomWrapperByRootCache.has(root)) {
839
+ return this.forceCustomWrapperByRootCache.get(root) || false;
840
+ }
841
+ const entries = this.getSubPackageIndieEntriesByRoot(root);
842
+ for (const entry of entries) {
843
+ if (entry === null || entry === void 0 ? void 0 : entry.isNative)
844
+ continue;
845
+ const content = (_a = this.miniPlugin.filesConfig[this.miniPlugin.getConfigFilePath(entry.name)]) === null || _a === void 0 ? void 0 : _a.content;
846
+ if (content === null || content === void 0 ? void 0 : content.forceCustomWrapper) {
847
+ this.forceCustomWrapperByRootCache.set(root, true);
848
+ return true;
849
+ }
850
+ }
851
+ this.forceCustomWrapperByRootCache.set(root, false);
852
+ return false;
853
+ }
854
+ getComponentByName(componentName) {
855
+ if (!this.componentByNameCache) {
856
+ this.componentByNameCache = new Map();
857
+ [...this.miniPlugin.pages, ...this.miniPlugin.components].forEach(item => {
858
+ this.componentByNameCache.set(item.name, item);
859
+ });
860
+ }
861
+ return this.componentByNameCache.get(componentName);
862
+ }
863
+ getScopedSubPackageIndieComponentConfig(compilation, root) {
864
+ var _a;
865
+ let cache = this.scopedComponentConfigByCompilation.get(compilation);
866
+ if (!cache) {
867
+ cache = new Map();
868
+ this.scopedComponentConfigByCompilation.set(compilation, cache);
869
+ }
870
+ if (cache.has(root)) {
871
+ return cache.get(root);
872
+ }
873
+ const thirdPartyComponents = new Map();
874
+ const queue = [...this.getSubPackageIndieEntriesByRoot(root)];
875
+ const visited = new Set();
876
+ while (queue.length) {
877
+ const current = queue.shift();
878
+ if (!current || visited.has(current.name))
879
+ continue;
880
+ visited.add(current.name);
881
+ const fileConfig = (_a = this.miniPlugin.filesConfig[this.miniPlugin.getConfigFilePath(current.name)]) === null || _a === void 0 ? void 0 : _a.content;
882
+ const usingComponents = fileConfig === null || fileConfig === void 0 ? void 0 : fileConfig.usingComponents;
883
+ if (!usingComponents)
884
+ continue;
885
+ for (const [componentName, componentValue] of Object.entries(usingComponents)) {
886
+ if (componentName === MiniPlugin_1.customWrapperName) {
887
+ this.cloneThirdPartyComponent(thirdPartyComponents, componentName);
888
+ continue;
889
+ }
890
+ if (!component_1.componentConfig.thirdPartyComponents.has(componentName))
891
+ continue;
892
+ const componentPath = componentValue instanceof Array ? componentValue[0] : componentValue;
893
+ const target = this.resolveUsingComponentTarget(current.path, componentPath);
894
+ this.cloneThirdPartyComponent(thirdPartyComponents, componentName);
895
+ if (!target)
896
+ continue;
897
+ const targetComponent = this.getComponentByName(target.name);
898
+ if (targetComponent && (!target.root || target.root === root) && !visited.has(targetComponent.name)) {
899
+ queue.push(targetComponent);
900
+ }
901
+ }
902
+ }
903
+ const scopedComponentConfig = {
904
+ includes: this.collectScopedInnerComponents(compilation, root),
905
+ exclude: new Set(component_1.componentConfig.exclude),
906
+ thirdPartyComponents,
907
+ includeAll: false,
908
+ skipRecursiveComponent: false,
909
+ };
910
+ cache.set(root, scopedComponentConfig);
911
+ return scopedComponentConfig;
912
+ }
913
+ generateSubPackageIndieScriptFile(compilation, compiler, filePath, content) {
914
+ const { RawSource } = compiler.webpack.sources;
915
+ compilation.assets[this.miniPlugin.getTargetFilePath(filePath, '.js')] = new RawSource(content);
916
+ }
917
+ createRecursiveComponentWrapperSource(componentName, forceCustomWrapper = false) {
918
+ const args = [];
919
+ if (componentName) {
920
+ args.push(JSON.stringify(componentName));
921
+ }
922
+ else if (forceCustomWrapper) {
923
+ args.push('undefined');
924
+ }
925
+ if (forceCustomWrapper) {
926
+ args.push('true');
927
+ }
928
+ return `const registerRecursiveComponent = globalThis.__taroRegisterRecursiveComponent
929
+
930
+ if (typeof registerRecursiveComponent !== 'function') {
931
+ throw new Error('globalThis.__taroRegisterRecursiveComponent is not a function')
932
+ }
933
+
934
+ registerRecursiveComponent(${args.join(', ')})
935
+ `;
936
+ }
937
+ generateSubPackageIndieFiles(compilation, compiler, template, baseTemplateName) {
938
+ const customWrapperRoots = new Set();
939
+ if (!this.getSubPackageIndieConfigs().length)
940
+ return customWrapperRoots;
941
+ const allIndieRoots = this.getAllIndieRoots();
942
+ allIndieRoots.forEach(root => {
943
+ const scopedComponentConfig = this.getScopedSubPackageIndieComponentConfig(compilation, root);
944
+ const isRecursiveDisabled = this.isRecursiveComponentDisabledForRoot(root);
945
+ if (isRecursiveDisabled) {
946
+ scopedComponentConfig.skipRecursiveComponent = true;
947
+ }
948
+ const forceCustomWrapperForRoot = isRecursiveDisabled
949
+ ? false
950
+ : this.isForceCustomWrapperEnabledForRoot(root);
951
+ const isRootUsingCustomWrapper = isRecursiveDisabled
952
+ ? false
953
+ : this.isSubPackageIndieRootUsingCustomWrapper(compilation, root, scopedComponentConfig.thirdPartyComponents);
954
+ if (isRootUsingCustomWrapper) {
955
+ customWrapperRoots.add(root);
956
+ if (!scopedComponentConfig.thirdPartyComponents.has(MiniPlugin_1.customWrapperName)) {
957
+ const attrs = component_1.componentConfig.thirdPartyComponents.get(MiniPlugin_1.customWrapperName);
958
+ scopedComponentConfig.thirdPartyComponents.set(MiniPlugin_1.customWrapperName, new Set(attrs || []));
959
+ }
960
+ }
961
+ this.miniPlugin.generateTemplateFile(compilation, compiler, `${root}/${baseTemplateName}`, template.buildTemplate, scopedComponentConfig);
962
+ this.miniPlugin.generateXSFile(compilation, compiler, `${root}/utils`);
963
+ if (!template.isSupportRecursive && !isRecursiveDisabled) {
964
+ this.generateSubPackageIndieScriptFile(compilation, compiler, `${root}/${MiniPlugin_1.baseCompName}`, this.createRecursiveComponentWrapperSource(undefined, forceCustomWrapperForRoot));
965
+ this.miniPlugin.generateTemplateFile(compilation, compiler, `${root}/${MiniPlugin_1.baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
966
+ const compConfig = {
967
+ component: true,
968
+ styleIsolation: MiniPlugin_1.STYLE_ISOLATION_APPLY_SHARED,
969
+ usingComponents: {
970
+ [MiniPlugin_1.baseCompName]: `./${MiniPlugin_1.baseCompName}`,
971
+ },
972
+ };
973
+ if (isRootUsingCustomWrapper) {
974
+ compConfig.usingComponents[MiniPlugin_1.customWrapperName] = `./${MiniPlugin_1.customWrapperName}`;
975
+ }
976
+ this.miniPlugin.generateConfigFile(compilation, compiler, `${root}/${MiniPlugin_1.baseCompName}`, compConfig);
977
+ }
978
+ if (isRootUsingCustomWrapper) {
979
+ this.generateSubPackageIndieScriptFile(compilation, compiler, `${root}/${MiniPlugin_1.customWrapperName}`, this.createRecursiveComponentWrapperSource(MiniPlugin_1.customWrapperName, forceCustomWrapperForRoot));
980
+ this.miniPlugin.generateTemplateFile(compilation, compiler, `${root}/${MiniPlugin_1.customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
981
+ const customWrapperConfig = {
982
+ component: true,
983
+ styleIsolation: MiniPlugin_1.STYLE_ISOLATION_APPLY_SHARED,
984
+ usingComponents: Object.assign(Object.assign({}, this.getSubPackageIndieRootUsingComponents(root)), { [MiniPlugin_1.baseCompName]: `./${MiniPlugin_1.baseCompName}`, [MiniPlugin_1.customWrapperName]: `./${MiniPlugin_1.customWrapperName}` }),
985
+ };
986
+ const componentPlaceholder = this.getSubPackageIndieRootComponentPlaceholder(root);
987
+ if (componentPlaceholder) {
988
+ customWrapperConfig.componentPlaceholder = componentPlaceholder;
989
+ }
990
+ this.miniPlugin.generateConfigFile(compilation, compiler, `${root}/${MiniPlugin_1.customWrapperName}`, customWrapperConfig);
991
+ }
992
+ });
993
+ return customWrapperRoots;
994
+ }
995
+ cleanSubPackageIndieAppConfig(compilation, compiler) {
996
+ var _a;
997
+ const { RawSource } = compiler.webpack.sources;
998
+ const appCfgPath = this.miniPlugin.getConfigFilePath(this.miniPlugin.appEntry);
999
+ const appCfgName = node_path_1.default.basename(appCfgPath).replace(node_path_1.default.extname(appCfgPath), '');
1000
+ const fc = (_a = this.miniPlugin.filesConfig) === null || _a === void 0 ? void 0 : _a[appCfgName];
1001
+ if (!(fc === null || fc === void 0 ? void 0 : fc.content))
1002
+ return;
1003
+ delete fc.content.subPackageIndie;
1004
+ compilation.assets[this.miniPlugin.getConfigPath(appCfgName)] = new RawSource(JSON.stringify(fc.content, null, 2));
1005
+ }
1006
+ generateMainPackageRuntimeFiles(compilation, compiler) {
1007
+ var _a, _b;
1008
+ const { commonChunks, fileType } = this.options;
1009
+ const mainPackageRoots = this.getAllMainPackageRoots();
1010
+ if (!mainPackageRoots.length)
1011
+ return;
1012
+ const styleExt = fileType.style;
1013
+ const templateExt = fileType.templ;
1014
+ const runtimeChunks = ['app', ...commonChunks];
1015
+ const commonTemplates = `common-templates${templateExt}`;
1016
+ const appJsContent = compilation.assets['app.js'];
1017
+ let patchedAppJsContent = appJsContent;
1018
+ if (appJsContent) {
1019
+ const { RawSource } = compiler.webpack.sources;
1020
+ const originalSource = String(((_b = (_a = appJsContent).source) === null || _b === void 0 ? void 0 : _b.call(_a)) || String(appJsContent));
1021
+ const registrarExpr = this.buildRecursiveComponentRegistrarExpr();
1022
+ let patchedSource = originalSource;
1023
+ if (/,\s*exports\.taroApp\s*=/.test(patchedSource)) {
1024
+ patchedSource = patchedSource.replace(/,\s*exports\.taroApp\s*=/, `,${registrarExpr},exports.taroApp=`);
1025
+ }
1026
+ else {
1027
+ patchedSource = patchedSource.replace(/exports\.taroApp\s*=/, `;${registrarExpr};exports.taroApp=`);
1028
+ }
1029
+ patchedAppJsContent = new RawSource(patchedSource);
1030
+ }
1031
+ mainPackageRoots.forEach(mainPackageRoot => {
1032
+ runtimeChunks.forEach(chunkName => {
1033
+ const jsFile = `${chunkName}.js`;
1034
+ if (compilation.assets[jsFile]) {
1035
+ const jsContent = chunkName === 'app'
1036
+ ? patchedAppJsContent
1037
+ : compilation.assets[jsFile];
1038
+ // 共享运行时:app.js 头部的 require("./taro-shared-sync")(TaroInjectSyncCorePlugin 按根级
1039
+ // chunk-id 'app' 注入,天然是"同级"形态)保持不动。同步核文件由 buildSharedRuntime 在子构建后
1040
+ // 拷进 ${mainPackageRoot}/,与此处 app.js 同级(subPackageIndie"每个 root 自包含"原则),
1041
+ // 故这个同级 require 直接正确,无需重算。
1042
+ compilation.assets[`${mainPackageRoot}/${jsFile}`] = jsContent;
1043
+ }
1044
+ const cssFile = `${chunkName}${styleExt}`;
1045
+ if (compilation.assets[cssFile]) {
1046
+ compilation.assets[`${mainPackageRoot}/${cssFile}`] = compilation.assets[cssFile];
1047
+ }
1048
+ });
1049
+ const appOriginCss = `app-origin${styleExt}`;
1050
+ if (compilation.assets[appOriginCss]) {
1051
+ compilation.assets[`${mainPackageRoot}/${appOriginCss}`] = compilation.assets[appOriginCss];
1052
+ }
1053
+ if (compilation.assets[commonTemplates]) {
1054
+ compilation.assets[`${mainPackageRoot}/${commonTemplates}`] = compilation.assets[commonTemplates];
1055
+ }
1056
+ });
1057
+ }
1058
+ }
1059
+ exports.default = SubPackageIndiePlugin;
1060
+ //# sourceMappingURL=SubPackageIndiePlugin.js.map