@tarojs/webpack5-runner 4.1.12-beta.32 → 4.1.12-beta.33

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.
@@ -0,0 +1,863 @@
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.SUBPACKAGE_STANDALONE_CHILD_TAG = exports.recursiveComponentName = exports.subPackageIndieCustomWrapperRootsKey = void 0;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const helper_1 = require("@tarojs/helper");
9
+ const loader_utils_1 = require("loader-utils");
10
+ const component_1 = require("../utils/component");
11
+ const webpack_1 = require("../utils/webpack");
12
+ const MiniPlugin_1 = require("./MiniPlugin");
13
+ const TaroSingleEntryPlugin_1 = __importDefault(require("./TaroSingleEntryPlugin"));
14
+ exports.subPackageIndieCustomWrapperRootsKey = '__taroSubPackageIndieCustomWrapperRoots';
15
+ exports.recursiveComponentName = 'recursive-component';
16
+ exports.SUBPACKAGE_STANDALONE_CHILD_TAG = 'sub_package_indie_standalone_child';
17
+ const PLUGIN_NAME = 'TaroSubPackageIndiePlugin';
18
+ class SubPackageIndiePlugin {
19
+ constructor(miniPlugin) {
20
+ this.moduleRequestMap = new Map();
21
+ this.miniPlugin = miniPlugin;
22
+ this.options = miniPlugin.options;
23
+ }
24
+ apply() {
25
+ this.miniPlugin.hooks.modifyEntries.tap(PLUGIN_NAME, entries => {
26
+ const shouldSkipRootEntries = this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended;
27
+ if (!shouldSkipRootEntries)
28
+ return entries;
29
+ return Object.assign(Object.assign({}, entries), { skipRootComp: true, skipRootWrapper: true });
30
+ });
31
+ this.miniPlugin.hooks.afterResolveModule.tap(PLUGIN_NAME, (resolveData) => {
32
+ var _a, _b;
33
+ if (!this.options.newBlended)
34
+ return;
35
+ const issuer = (_a = resolveData.contextInfo) === null || _a === void 0 ? void 0 : _a.issuer;
36
+ const request = resolveData.request;
37
+ const resource = ((_b = resolveData.createData) === null || _b === void 0 ? void 0 : _b.resource) || resolveData.resource;
38
+ if (issuer && request && resource) {
39
+ this.setResolvedRequestResource(issuer, request, resource);
40
+ }
41
+ if (resolveData.request !== helper_1.taroJsComponents)
42
+ return;
43
+ if (!issuer || !issuer.startsWith(this.options.sourceDir))
44
+ return;
45
+ const componentName = this.miniPlugin.getComponentName(issuer);
46
+ const root = this.isInSubPackageIndieRoot(componentName);
47
+ if (!root)
48
+ return;
49
+ resolveData.dependencies.forEach((dependency) => {
50
+ var _a;
51
+ if (((_a = dependency.ids) === null || _a === void 0 ? void 0 : _a.length) > 0) {
52
+ dependency.ids.forEach((id) => {
53
+ const dashedName = this.toDashedComponentName(id);
54
+ if (!component_1.componentConfig.scopedIncludes.has(root)) {
55
+ component_1.componentConfig.scopedIncludes.set(root, new Set());
56
+ }
57
+ component_1.componentConfig.scopedIncludes.get(root).add(dashedName);
58
+ });
59
+ }
60
+ });
61
+ });
62
+ this.miniPlugin.hooks.compileExtraEntries.tap(PLUGIN_NAME, (compiler, compilation, promises) => {
63
+ this.compileSubPackageIndieStandaloneEntries(compiler, compilation, promises);
64
+ });
65
+ this.miniPlugin.hooks.modifyChunkRequire.tap(PLUGIN_NAME, (result, chunkId, miniType) => {
66
+ if (!this.options.newBlended)
67
+ return result;
68
+ if (this.miniPlugin.nativeComponents.has(chunkId) || miniType === helper_1.META_TYPE.STATIC) {
69
+ const isSubPackageIndie = this.isInSubPackageIndieRoot(chunkId);
70
+ if (isSubPackageIndie) {
71
+ return {
72
+ source: result.source,
73
+ handled: true,
74
+ };
75
+ }
76
+ }
77
+ else if (miniType === helper_1.META_TYPE.PAGE) {
78
+ const subPackageIndieRoot = this.isInSubPackageIndieRoot(chunkId);
79
+ if (subPackageIndieRoot) {
80
+ return {
81
+ source: (0, webpack_1.addRequireToSource)(chunkId, result.source, [{ name: `${subPackageIndieRoot}/app` }]),
82
+ handled: true,
83
+ };
84
+ }
85
+ }
86
+ return result;
87
+ });
88
+ this.miniPlugin.hooks.modifySkipRootTemplates.tap(PLUGIN_NAME, skip => {
89
+ return skip || (this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended);
90
+ });
91
+ this.miniPlugin.hooks.generateExtraFiles.tap(PLUGIN_NAME, (customWrapperRoots, compilation, compiler) => {
92
+ if (!this.options.newBlended)
93
+ return customWrapperRoots;
94
+ if (!this.getSubPackageIndieConfigs().length)
95
+ return customWrapperRoots;
96
+ const roots = this.generateSubPackageIndieFiles(compilation, compiler, this.options.template, 'base');
97
+ compilation[exports.subPackageIndieCustomWrapperRootsKey] = roots;
98
+ return roots;
99
+ });
100
+ this.miniPlugin.hooks.modifyComponentConfig.tap(PLUGIN_NAME, ({ config, component }, customWrapperRoots) => {
101
+ const indieMatch = this.getSubPackageIndieMatch(component.name);
102
+ if (!indieMatch || component.isNative)
103
+ return;
104
+ const importBaseCompPath = this.getRootRelativePath(component.path, indieMatch.root, this.miniPlugin.getTargetFilePath(MiniPlugin_1.baseCompName, ''));
105
+ const importCustomWrapperPath = this.getRootRelativePath(component.path, indieMatch.root, this.miniPlugin.getTargetFilePath(MiniPlugin_1.customWrapperName, ''));
106
+ config.content.usingComponents = Object.assign({}, config.content.usingComponents);
107
+ const isRootUsingCustomWrapper = customWrapperRoots.has(indieMatch.root);
108
+ const isRootRecursiveDisabled = this.isRecursiveComponentDisabledForRoot(indieMatch.root);
109
+ if (isRootUsingCustomWrapper && !isRootRecursiveDisabled && !config.content.usingComponents[MiniPlugin_1.customWrapperName]) {
110
+ config.content.usingComponents[MiniPlugin_1.customWrapperName] = importCustomWrapperPath;
111
+ }
112
+ if (!this.options.template.isSupportRecursive && !isRootRecursiveDisabled && !config.content.usingComponents[MiniPlugin_1.baseCompName]) {
113
+ config.content.usingComponents[MiniPlugin_1.baseCompName] = importBaseCompPath;
114
+ }
115
+ });
116
+ this.miniPlugin.hooks.modifyPageConfig.tap(PLUGIN_NAME, (context, page, customWrapperRoots) => {
117
+ const subPackageIndieRoot = this.isInSubPackageIndieRoot(page.name);
118
+ if (!subPackageIndieRoot)
119
+ return context;
120
+ const isPageRecursiveDisabled = this.isRecursiveComponentDisabledForRoot(subPackageIndieRoot);
121
+ const shouldUseCustomWrapper = customWrapperRoots.has(subPackageIndieRoot) && !isPageRecursiveDisabled;
122
+ 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,
123
+ isPageRecursiveDisabled });
124
+ });
125
+ this.miniPlugin.hooks.afterGenerateFiles.tap(PLUGIN_NAME, (compilation, compiler) => {
126
+ if (this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended) {
127
+ this.generateMainPackageRuntimeFiles(compilation, compiler);
128
+ }
129
+ });
130
+ this.miniPlugin.hooks.optimizeAssets.tap(PLUGIN_NAME, compilation => {
131
+ if (!this.options.newBlended)
132
+ return;
133
+ const hasSubPackageIndieCustomWrapperRoots = compilation[exports.subPackageIndieCustomWrapperRootsKey] instanceof Set;
134
+ const subPackageIndieCustomWrapperRoots = hasSubPackageIndieCustomWrapperRoots
135
+ ? compilation[exports.subPackageIndieCustomWrapperRootsKey]
136
+ : new Set();
137
+ const isUsingCustomWrapper = component_1.componentConfig.thirdPartyComponents.has(MiniPlugin_1.customWrapperName);
138
+ const shouldKeepRecursiveComponent = !this.options.template.isSupportRecursive || isUsingCustomWrapper;
139
+ this.getAllIndieRoots().forEach(root => {
140
+ if (this.isRecursiveComponentDisabledForRoot(root)) {
141
+ delete compilation.assets[`${root}/${MiniPlugin_1.baseCompName}.js`];
142
+ delete compilation.assets[`${root}/${MiniPlugin_1.baseCompName}${this.options.fileType.config}`];
143
+ delete compilation.assets[`${root}/${MiniPlugin_1.baseCompName}${this.options.fileType.templ}`];
144
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}.js`];
145
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.config}`];
146
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.templ}`];
147
+ delete compilation.assets[`${root}/${exports.recursiveComponentName}.js`];
148
+ return;
149
+ }
150
+ const isRootUsingCustomWrapper = hasSubPackageIndieCustomWrapperRoots
151
+ ? subPackageIndieCustomWrapperRoots.has(root)
152
+ : this.isSubPackageIndieRootUsingCustomWrapper(compilation, root, this.getScopedSubPackageIndieComponentConfig(compilation, root).thirdPartyComponents);
153
+ if (isRootUsingCustomWrapper)
154
+ return;
155
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}.js`];
156
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.config}`];
157
+ delete compilation.assets[`${root}/${MiniPlugin_1.customWrapperName}${this.options.fileType.templ}`];
158
+ });
159
+ if (!shouldKeepRecursiveComponent) {
160
+ this.getAllIndieRoots().forEach(root => {
161
+ delete compilation.assets[`${root}/${exports.recursiveComponentName}.js`];
162
+ });
163
+ }
164
+ if (this.hasSubPackageIndieMainPackageRoot()) {
165
+ const styleExt = this.options.fileType.style;
166
+ const runtimeChunks = ['app', ...this.options.commonChunks];
167
+ runtimeChunks.forEach(chunkName => {
168
+ delete compilation.assets[`${chunkName}.js`];
169
+ delete compilation.assets[`${chunkName}${styleExt}`];
170
+ });
171
+ delete compilation.assets[`app-origin${styleExt}`];
172
+ delete compilation.assets[`${MiniPlugin_1.baseCompName}.js`];
173
+ delete compilation.assets[`${MiniPlugin_1.customWrapperName}.js`];
174
+ }
175
+ });
176
+ this.miniPlugin.hooks.modifyConfig.tap(PLUGIN_NAME, (config, componentName) => {
177
+ const subPackageIndieRoot = this.isInSubPackageIndieRoot(componentName);
178
+ if (subPackageIndieRoot && this.miniPlugin.nativeComponents.has(componentName)) {
179
+ const styleIsolation = this.options.template.isSupportRecursive
180
+ ? MiniPlugin_1.STYLE_ISOLATION_APPLY_SHARED
181
+ : MiniPlugin_1.STYLE_ISOLATION_SHARED;
182
+ config.styleIsolation = config.styleIsolation || styleIsolation;
183
+ config.isNewBlended = true;
184
+ }
185
+ return config;
186
+ });
187
+ this.miniPlugin.hooks.modifyShouldProcessStyles.tap(PLUGIN_NAME, shouldProcess => {
188
+ const hasMainPackageRoot = this.hasSubPackageIndieMainPackageRoot() && this.options.newBlended;
189
+ return shouldProcess || hasMainPackageRoot;
190
+ });
191
+ this.miniPlugin.hooks.modifyStyleImport.tap(PLUGIN_NAME, (context, page) => {
192
+ if (!this.options.newBlended)
193
+ return context;
194
+ const indieMatch = this.getSubPackageIndieMatch(page.name);
195
+ if (!indieMatch)
196
+ return context;
197
+ if (this.miniPlugin.nativeComponents.has(page.name)) {
198
+ if (indieMatch.isMainPackageRoot) {
199
+ return Object.assign(Object.assign({}, context), { importStatement: `@import ${JSON.stringify((0, loader_utils_1.urlToRequest)('./app' + this.options.fileType.style))};\n`, shouldSkip: false, isMainPackageRoot: true });
200
+ }
201
+ return Object.assign(Object.assign({}, context), { importStatement: '', shouldSkip: true, isMainPackageRoot: false });
202
+ }
203
+ if (indieMatch.isMainPackageRoot) {
204
+ return Object.assign(Object.assign({}, context), { importStatement: `@import ${JSON.stringify((0, loader_utils_1.urlToRequest)('./app' + this.options.fileType.style))};\n`, shouldSkip: false, isMainPackageRoot: true });
205
+ }
206
+ return context;
207
+ });
208
+ }
209
+ getRootRelativePath(fromPath, root, targetPath) {
210
+ return (0, helper_1.promoteRelativePath)(node_path_1.default.relative(fromPath, node_path_1.default.join(this.options.sourceDir, root, targetPath)));
211
+ }
212
+ normalizeIndieRoot(root) {
213
+ if (root.endsWith('/index')) {
214
+ return node_path_1.default.dirname(root);
215
+ }
216
+ return root;
217
+ }
218
+ parseIndieRootConfig(rootConfig) {
219
+ if (typeof rootConfig === 'string') {
220
+ return { path: this.normalizeIndieRoot(rootConfig), disableRecursiveComponent: false };
221
+ }
222
+ return {
223
+ path: this.normalizeIndieRoot(rootConfig.path),
224
+ disableRecursiveComponent: !!rootConfig.disableRecursiveComponent,
225
+ };
226
+ }
227
+ getSubPackageIndieConfigs() {
228
+ var _a;
229
+ const subPackageIndie = (_a = this.miniPlugin.appConfig) === null || _a === void 0 ? void 0 : _a.subPackageIndie;
230
+ if (!Array.isArray(subPackageIndie))
231
+ return [];
232
+ return subPackageIndie.map(({ mainPackageRoot, subPackageRoots = [] }) => {
233
+ const mainParsed = this.parseIndieRootConfig(mainPackageRoot);
234
+ const subParsedList = subPackageRoots.map(root => this.parseIndieRootConfig(root));
235
+ const disableRecursiveComponentRoots = new Set();
236
+ if (mainParsed.disableRecursiveComponent) {
237
+ disableRecursiveComponentRoots.add(mainParsed.path);
238
+ }
239
+ subParsedList.forEach(sub => {
240
+ if (sub.disableRecursiveComponent) {
241
+ disableRecursiveComponentRoots.add(sub.path);
242
+ }
243
+ });
244
+ return {
245
+ mainPackageRoot: mainParsed.path,
246
+ subPackageRoots: Array.from(new Set(subParsedList.map(sub => sub.path))),
247
+ disableRecursiveComponentRoots,
248
+ };
249
+ });
250
+ }
251
+ getAllIndieRoots() {
252
+ return Array.from(new Set(this.getSubPackageIndieConfigs().flatMap(({ mainPackageRoot, subPackageRoots }) => [mainPackageRoot, ...subPackageRoots])));
253
+ }
254
+ getAllMainPackageRoots() {
255
+ return Array.from(new Set(this.getSubPackageIndieConfigs().map(item => item.mainPackageRoot)));
256
+ }
257
+ hasSubPackageIndieMainPackageRoot() {
258
+ return this.getAllMainPackageRoots().length > 0;
259
+ }
260
+ isRecursiveComponentDisabledForRoot(root) {
261
+ return this.getSubPackageIndieConfigs().some(config => config.disableRecursiveComponentRoots.has(root));
262
+ }
263
+ getSubPackageIndieMatch(pageName) {
264
+ if (!this.options.newBlended)
265
+ return null;
266
+ const configs = this.getSubPackageIndieConfigs();
267
+ for (const config of configs) {
268
+ if (pageName.startsWith(config.mainPackageRoot + '/') || pageName === config.mainPackageRoot) {
269
+ return {
270
+ config,
271
+ root: config.mainPackageRoot,
272
+ isMainPackageRoot: true,
273
+ };
274
+ }
275
+ for (const root of config.subPackageRoots) {
276
+ if (pageName.startsWith(root + '/') || pageName === root) {
277
+ return {
278
+ config,
279
+ root,
280
+ isMainPackageRoot: false,
281
+ };
282
+ }
283
+ }
284
+ }
285
+ return null;
286
+ }
287
+ isInSubPackageIndieRoot(pageName) {
288
+ var _a;
289
+ return ((_a = this.getSubPackageIndieMatch(pageName)) === null || _a === void 0 ? void 0 : _a.root) || null;
290
+ }
291
+ compileSubPackageIndieStandaloneEntries(compiler, compilation, promises) {
292
+ const indieRoots = this.getAllIndieRoots();
293
+ if (!this.options.newBlended || !indieRoots.length)
294
+ return;
295
+ const JsonpTemplatePlugin = require('webpack/lib/web/JsonpTemplatePlugin');
296
+ const NaturalChunkIdsPlugin = require('webpack/lib/ids/NaturalChunkIdsPlugin');
297
+ indieRoots.forEach(root => {
298
+ if (this.isRecursiveComponentDisabledForRoot(root))
299
+ return;
300
+ const childCompiler = compilation.createChildCompiler(PLUGIN_NAME, {
301
+ path: compiler.options.output.path,
302
+ filename: '[name].js',
303
+ chunkFilename: '[name].js',
304
+ chunkLoadingGlobal: `subpackage_indie_${root.replace(/[\\/]/g, '_')}`,
305
+ });
306
+ childCompiler.inputFileSystem = compiler.inputFileSystem;
307
+ childCompiler.outputFileSystem = compiler.outputFileSystem;
308
+ childCompiler.context = compiler.context;
309
+ new JsonpTemplatePlugin().apply(childCompiler);
310
+ new NaturalChunkIdsPlugin().apply(childCompiler);
311
+ new compiler.webpack.DefinePlugin(this.options.constantsReplaceList).apply(childCompiler);
312
+ childCompiler.hooks.compilation.tap(PLUGIN_NAME, (childCompilation) => {
313
+ childCompilation.__tag = exports.SUBPACKAGE_STANDALONE_CHILD_TAG;
314
+ childCompilation.__name = root;
315
+ });
316
+ childCompiler.options.optimization = Object.assign(Object.assign({}, childCompiler.options.optimization), { splitChunks: false, runtimeChunk: false });
317
+ const outputOptions = childCompiler.options.output || {};
318
+ const libraryOptions = typeof outputOptions.library === 'object' && outputOptions.library !== null
319
+ ? outputOptions.library
320
+ : {};
321
+ childCompiler.options.output = Object.assign(Object.assign({}, outputOptions), { library: Object.assign(Object.assign({}, libraryOptions), { type: 'commonjs2' }) });
322
+ new TaroSingleEntryPlugin_1.default(compiler.context, node_path_1.default.resolve(__dirname, '..', 'template/recursive-component'), `${root}/${exports.recursiveComponentName}`, helper_1.META_TYPE.EXPORTS).apply(childCompiler);
323
+ promises.push(new Promise((resolve, reject) => {
324
+ childCompiler.runAsChild(err => err ? reject(err) : resolve(null));
325
+ }));
326
+ });
327
+ }
328
+ getModuleSourceContent(module) {
329
+ var _a;
330
+ try {
331
+ const source = (_a = module === null || module === void 0 ? void 0 : module.originalSource) === null || _a === void 0 ? void 0 : _a.call(module);
332
+ if (!source || typeof source.source !== 'function')
333
+ return '';
334
+ return String(source.source() || '');
335
+ }
336
+ catch (_err) {
337
+ return '';
338
+ }
339
+ }
340
+ getModuleResource(module) {
341
+ var _a;
342
+ 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);
343
+ return typeof resource === 'string' ? resource : undefined;
344
+ }
345
+ collectFlattenedModules(module, collected, visited = new Set()) {
346
+ if (!module || visited.has(module))
347
+ return;
348
+ visited.add(module);
349
+ collected.add(module);
350
+ const nestedModuleCollections = [module.rootModule, module.modules, module._modules];
351
+ nestedModuleCollections.forEach(item => {
352
+ if (!item)
353
+ return;
354
+ if (item && typeof item[Symbol.iterator] === 'function') {
355
+ for (const nested of item) {
356
+ this.collectFlattenedModules(nested, collected, visited);
357
+ }
358
+ }
359
+ else {
360
+ this.collectFlattenedModules(item, collected, visited);
361
+ }
362
+ });
363
+ }
364
+ getCompilationModuleResourceMap(compilation) {
365
+ const moduleMap = new Map();
366
+ for (const module of compilation.modules) {
367
+ const flattenedModules = new Set();
368
+ this.collectFlattenedModules(module, flattenedModules);
369
+ flattenedModules.forEach(flattenedModule => {
370
+ const resource = this.getModuleResource(flattenedModule);
371
+ if (resource) {
372
+ moduleMap.set(resource, flattenedModule);
373
+ }
374
+ });
375
+ }
376
+ return moduleMap;
377
+ }
378
+ getChunksBySubPackageIndieRoot(compilation, root) {
379
+ const chunks = new Set();
380
+ this.getSubPackageIndieEntriesByRoot(root).forEach(({ name }) => {
381
+ const entryChunk = Array.from(compilation.chunks).find(chunk => chunk.name === name);
382
+ if (!entryChunk)
383
+ return;
384
+ chunks.add(entryChunk);
385
+ entryChunk.groupsIterable.forEach(group => {
386
+ group.chunks.forEach(chunk => chunks.add(chunk));
387
+ });
388
+ });
389
+ return chunks;
390
+ }
391
+ getSubPackageIndieModules(compilation, root) {
392
+ const modules = new Set();
393
+ this.getChunksBySubPackageIndieRoot(compilation, root).forEach(chunk => {
394
+ for (const module of compilation.chunkGraph.getChunkModulesIterable(chunk)) {
395
+ this.collectFlattenedModules(module, modules);
396
+ }
397
+ });
398
+ return modules;
399
+ }
400
+ normalizeModuleRequestMapKey(resource) {
401
+ if (!resource || typeof resource !== 'string')
402
+ return null;
403
+ const resourceWithoutLoader = resource.split('!').pop();
404
+ if (!resourceWithoutLoader)
405
+ return null;
406
+ const resourcePath = resourceWithoutLoader.split('?')[0];
407
+ if (!resourcePath)
408
+ return null;
409
+ return node_path_1.default.normalize(resourcePath);
410
+ }
411
+ getModuleRequestMapKeys(resource) {
412
+ const keys = new Set();
413
+ if (resource && typeof resource === 'string') {
414
+ keys.add(resource);
415
+ }
416
+ const normalizedResource = this.normalizeModuleRequestMapKey(resource);
417
+ if (normalizedResource) {
418
+ keys.add(normalizedResource);
419
+ }
420
+ return Array.from(keys);
421
+ }
422
+ setResolvedRequestResource(issuer, request, resource) {
423
+ this.getModuleRequestMapKeys(issuer).forEach(key => {
424
+ if (!this.moduleRequestMap.has(key)) {
425
+ this.moduleRequestMap.set(key, new Map());
426
+ }
427
+ this.moduleRequestMap.get(key).set(request, resource);
428
+ });
429
+ }
430
+ getResolvedRequestResource(issuerResource, request) {
431
+ var _a;
432
+ for (const key of this.getModuleRequestMapKeys(issuerResource)) {
433
+ const resolvedResource = (_a = this.moduleRequestMap.get(key)) === null || _a === void 0 ? void 0 : _a.get(request);
434
+ if (resolvedResource)
435
+ return resolvedResource;
436
+ }
437
+ }
438
+ getResolvedModuleByResource(moduleResourceMap, resource) {
439
+ const exactModule = moduleResourceMap.get(resource);
440
+ if (exactModule)
441
+ return exactModule;
442
+ const normalizedResource = this.normalizeModuleRequestMapKey(resource);
443
+ if (!normalizedResource)
444
+ return null;
445
+ const normalizedModule = moduleResourceMap.get(normalizedResource);
446
+ if (normalizedModule)
447
+ return normalizedModule;
448
+ for (const [moduleResource, module] of moduleResourceMap.entries()) {
449
+ if (this.normalizeModuleRequestMapKey(moduleResource) === normalizedResource) {
450
+ return module;
451
+ }
452
+ }
453
+ return null;
454
+ }
455
+ resolveRequestedModule(moduleResourceMap, issuerModule, request) {
456
+ const issuerResource = this.getModuleResource(issuerModule);
457
+ const resolvedResource = this.getResolvedRequestResource(issuerResource, request);
458
+ if (resolvedResource) {
459
+ const resolvedModule = this.getResolvedModuleByResource(moduleResourceMap, resolvedResource);
460
+ if (resolvedModule)
461
+ return resolvedModule;
462
+ }
463
+ if (issuerResource && /^[.\\/]/.test(request)) {
464
+ const requestPath = node_path_1.default.resolve(node_path_1.default.dirname(issuerResource), request);
465
+ const resolvedPath = (0, helper_1.resolveMainFilePath)(requestPath);
466
+ const requestModule = this.getResolvedModuleByResource(moduleResourceMap, resolvedPath);
467
+ if (requestModule)
468
+ return requestModule;
469
+ }
470
+ for (const module of moduleResourceMap.values()) {
471
+ if (!module)
472
+ continue;
473
+ if (module.rawRequest === request || module.userRequest === request || module.request === request) {
474
+ return module;
475
+ }
476
+ }
477
+ return null;
478
+ }
479
+ resolveImportBindingToTaroComponentName(moduleResourceMap, issuerModule, binding, propertyName, visited = new Set()) {
480
+ if (!(binding === null || binding === void 0 ? void 0 : binding.source))
481
+ return null;
482
+ if (binding.kind === 'namespace') {
483
+ if (!propertyName)
484
+ return null;
485
+ if (binding.source === helper_1.taroJsComponents) {
486
+ return propertyName;
487
+ }
488
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, issuerModule, binding.source);
489
+ if (!targetModule) {
490
+ if (!/^[.\\/]/.test(binding.source)) {
491
+ return propertyName;
492
+ }
493
+ return null;
494
+ }
495
+ return this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, propertyName, visited);
496
+ }
497
+ const importedName = binding.imported || propertyName;
498
+ if (!importedName || importedName === 'default')
499
+ return null;
500
+ if (binding.source === helper_1.taroJsComponents) {
501
+ return importedName;
502
+ }
503
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, issuerModule, binding.source);
504
+ if (!targetModule) {
505
+ if (!/^[.\\/]/.test(binding.source)) {
506
+ return importedName;
507
+ }
508
+ return null;
509
+ }
510
+ return this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, importedName, visited);
511
+ }
512
+ resolveModuleExportToTaroComponentName(moduleResourceMap, module, exportName, visited = new Set()) {
513
+ var _a, _b;
514
+ const moduleResource = this.getModuleResource(module);
515
+ if (!moduleResource)
516
+ return null;
517
+ const visitKey = `${moduleResource}::${exportName}`;
518
+ if (visited.has(visitKey))
519
+ return null;
520
+ visited.add(visitKey);
521
+ const exportBinding = (_a = module === null || module === void 0 ? void 0 : module.exportBindings) === null || _a === void 0 ? void 0 : _a[exportName];
522
+ if (exportBinding) {
523
+ if (exportBinding.kind === 'reexport' && exportBinding.source) {
524
+ if (exportBinding.source === helper_1.taroJsComponents) {
525
+ return exportBinding.imported || exportName;
526
+ }
527
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, module, exportBinding.source);
528
+ if (targetModule) {
529
+ const resolvedName = this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, exportBinding.imported || exportName, visited);
530
+ if (resolvedName)
531
+ return resolvedName;
532
+ }
533
+ }
534
+ if (exportBinding.kind === 'local' && exportBinding.local) {
535
+ const importedBinding = (_b = module === null || module === void 0 ? void 0 : module.importedBindings) === null || _b === void 0 ? void 0 : _b[exportBinding.local];
536
+ if (importedBinding) {
537
+ const resolvedName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, importedBinding, undefined, visited);
538
+ if (resolvedName)
539
+ return resolvedName;
540
+ }
541
+ }
542
+ }
543
+ for (const source of (module === null || module === void 0 ? void 0 : module.exportAllSources) || []) {
544
+ if (source === helper_1.taroJsComponents) {
545
+ return exportName;
546
+ }
547
+ const targetModule = this.resolveRequestedModule(moduleResourceMap, module, source);
548
+ if (!targetModule)
549
+ continue;
550
+ const resolvedName = this.resolveModuleExportToTaroComponentName(moduleResourceMap, targetModule, exportName, visited);
551
+ if (resolvedName)
552
+ return resolvedName;
553
+ }
554
+ return null;
555
+ }
556
+ resolveUsedComponentRefToTaroComponentName(moduleResourceMap, module, componentRef) {
557
+ var _a, _b;
558
+ if (!componentRef)
559
+ return null;
560
+ if (componentRef.kind === 'identifier') {
561
+ const binding = (_a = module === null || module === void 0 ? void 0 : module.importedBindings) === null || _a === void 0 ? void 0 : _a[componentRef.name];
562
+ if (!binding) {
563
+ return componentRef.name;
564
+ }
565
+ const resolvedName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, binding);
566
+ return resolvedName || componentRef.name;
567
+ }
568
+ if (componentRef.kind === 'member') {
569
+ const binding = (_b = module === null || module === void 0 ? void 0 : module.importedBindings) === null || _b === void 0 ? void 0 : _b[componentRef.object];
570
+ if (!binding) {
571
+ return componentRef.property || null;
572
+ }
573
+ const resolvedName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, binding, componentRef.property);
574
+ return resolvedName || componentRef.property || null;
575
+ }
576
+ return null;
577
+ }
578
+ shouldSkipSubPackageIndieModule(module, root) {
579
+ const resource = this.getModuleResource(module);
580
+ if (typeof resource !== 'string' || !resource.startsWith(this.options.sourceDir)) {
581
+ return false;
582
+ }
583
+ const componentName = this.miniPlugin.getComponentName(resource);
584
+ const targetRoot = this.isInSubPackageIndieRoot(componentName);
585
+ return !!targetRoot && targetRoot !== root;
586
+ }
587
+ collectModuleResolvedComponentIncludes(compilation, root) {
588
+ const includes = new Set();
589
+ const moduleResourceMap = this.getCompilationModuleResourceMap(compilation);
590
+ for (const module of this.getSubPackageIndieModules(compilation, root)) {
591
+ if (this.shouldSkipSubPackageIndieModule(module, root))
592
+ continue;
593
+ for (const elementName of (module === null || module === void 0 ? void 0 : module.elementNameSet) || []) {
594
+ if (!component_1.componentConfig.thirdPartyComponents.has(elementName)) {
595
+ includes.add(elementName);
596
+ }
597
+ }
598
+ for (const binding of Object.values((module === null || module === void 0 ? void 0 : module.importedBindings) || {})) {
599
+ const resolvedComponentName = this.resolveImportBindingToTaroComponentName(moduleResourceMap, module, binding);
600
+ if (!resolvedComponentName)
601
+ continue;
602
+ const dashedName = this.toDashedComponentName(resolvedComponentName);
603
+ if (!component_1.componentConfig.thirdPartyComponents.has(dashedName)) {
604
+ includes.add(dashedName);
605
+ }
606
+ }
607
+ for (const componentRef of (module === null || module === void 0 ? void 0 : module.usedComponentRefs) || []) {
608
+ const resolvedComponentName = this.resolveUsedComponentRefToTaroComponentName(moduleResourceMap, module, componentRef);
609
+ if (!resolvedComponentName)
610
+ continue;
611
+ const dashedName = this.toDashedComponentName(resolvedComponentName);
612
+ if (!component_1.componentConfig.thirdPartyComponents.has(dashedName)) {
613
+ includes.add(dashedName);
614
+ }
615
+ }
616
+ }
617
+ return includes;
618
+ }
619
+ expandScopedIncludes(includes) {
620
+ const scopedIncludes = new Set(includes);
621
+ if (scopedIncludes.has('view')) {
622
+ scopedIncludes.add('catch-view');
623
+ scopedIncludes.add('static-view');
624
+ scopedIncludes.add('pure-view');
625
+ scopedIncludes.add('click-view');
626
+ }
627
+ if (scopedIncludes.has('image')) {
628
+ scopedIncludes.add('static-image');
629
+ }
630
+ if (scopedIncludes.has('text')) {
631
+ scopedIncludes.add('static-text');
632
+ }
633
+ return scopedIncludes;
634
+ }
635
+ toDashedComponentName(componentName) {
636
+ return componentName.replace(/[A-Z]/g, (match, offset) => `${offset === 0 ? '' : '-'}${match.toLowerCase()}`);
637
+ }
638
+ collectScopedInnerComponents(compilation, root) {
639
+ const includes = new Set();
640
+ const scopedIncludes = component_1.componentConfig.scopedIncludes.get(root);
641
+ if (scopedIncludes && scopedIncludes.size > 0) {
642
+ scopedIncludes.forEach(name => includes.add(name));
643
+ }
644
+ this.collectModuleResolvedComponentIncludes(compilation, root).forEach(name => includes.add(name));
645
+ const finalIncludes = includes.size > 0
646
+ ? includes
647
+ : new Set(component_1.componentConfig.includes);
648
+ return this.expandScopedIncludes(finalIncludes);
649
+ }
650
+ isSubPackageIndieRootUsingCustomWrapperByModules(compilation, root) {
651
+ const moduleResourceMap = this.getCompilationModuleResourceMap(compilation);
652
+ for (const module of this.getSubPackageIndieModules(compilation, root)) {
653
+ if (this.shouldSkipSubPackageIndieModule(module, root))
654
+ continue;
655
+ for (const componentRef of (module === null || module === void 0 ? void 0 : module.usedComponentRefs) || []) {
656
+ const resolvedComponentName = this.resolveUsedComponentRefToTaroComponentName(moduleResourceMap, module, componentRef);
657
+ if (resolvedComponentName === 'CustomWrapper') {
658
+ return true;
659
+ }
660
+ }
661
+ }
662
+ return false;
663
+ }
664
+ isSubPackageIndieRootUsingCustomWrapper(compilation, root, scopedThirdPartyComponents) {
665
+ if (scopedThirdPartyComponents === null || scopedThirdPartyComponents === void 0 ? void 0 : scopedThirdPartyComponents.has(MiniPlugin_1.customWrapperName)) {
666
+ return true;
667
+ }
668
+ if (this.isSubPackageIndieRootUsingCustomWrapperByModules(compilation, root)) {
669
+ return true;
670
+ }
671
+ return false;
672
+ }
673
+ resolveUsingComponentTarget(filePath, componentPath) {
674
+ if (componentPath.startsWith('plugin://'))
675
+ return null;
676
+ let sourcePath = componentPath;
677
+ if (node_path_1.default.isAbsolute(componentPath)) {
678
+ sourcePath = helper_1.fs.existsSync(componentPath)
679
+ ? componentPath
680
+ : node_path_1.default.resolve(this.options.sourceDir, `.${componentPath}`);
681
+ }
682
+ else {
683
+ const isRelativePath = /^[.\\/]/;
684
+ if (!isRelativePath.test(componentPath))
685
+ return null;
686
+ sourcePath = node_path_1.default.resolve(node_path_1.default.dirname(filePath), componentPath);
687
+ }
688
+ const targetPath = (0, helper_1.resolveMainFilePath)(sourcePath);
689
+ const targetName = this.miniPlugin.getComponentName(targetPath);
690
+ return {
691
+ path: targetPath,
692
+ name: targetName,
693
+ root: this.isInSubPackageIndieRoot(targetName),
694
+ };
695
+ }
696
+ cloneThirdPartyComponent(thirdPartyComponents, componentName) {
697
+ const attrs = component_1.componentConfig.thirdPartyComponents.get(componentName);
698
+ if (!attrs || thirdPartyComponents.has(componentName))
699
+ return;
700
+ thirdPartyComponents.set(componentName, new Set(attrs));
701
+ }
702
+ getSubPackageIndieEntriesByRoot(root) {
703
+ const entries = new Map();
704
+ [...this.miniPlugin.pages, ...this.miniPlugin.components].forEach(item => {
705
+ if (this.isInSubPackageIndieRoot(item.name) === root) {
706
+ entries.set(item.name, item);
707
+ }
708
+ });
709
+ return Array.from(entries.values());
710
+ }
711
+ getComponentByName(componentName) {
712
+ return [...this.miniPlugin.pages, ...this.miniPlugin.components].find(item => item.name === componentName);
713
+ }
714
+ getScopedSubPackageIndieComponentConfig(compilation, root) {
715
+ var _a;
716
+ const thirdPartyComponents = new Map();
717
+ const queue = this.getSubPackageIndieEntriesByRoot(root);
718
+ const visited = new Set();
719
+ while (queue.length) {
720
+ const current = queue.shift();
721
+ if (!current || visited.has(current.name))
722
+ continue;
723
+ visited.add(current.name);
724
+ const fileConfig = (_a = this.miniPlugin.filesConfig[this.miniPlugin.getConfigFilePath(current.name)]) === null || _a === void 0 ? void 0 : _a.content;
725
+ const usingComponents = fileConfig === null || fileConfig === void 0 ? void 0 : fileConfig.usingComponents;
726
+ if (!usingComponents)
727
+ continue;
728
+ for (const [componentName, componentValue] of Object.entries(usingComponents)) {
729
+ if (componentName === MiniPlugin_1.customWrapperName) {
730
+ this.cloneThirdPartyComponent(thirdPartyComponents, componentName);
731
+ continue;
732
+ }
733
+ if (!component_1.componentConfig.thirdPartyComponents.has(componentName))
734
+ continue;
735
+ const componentPath = componentValue instanceof Array ? componentValue[0] : componentValue;
736
+ const target = this.resolveUsingComponentTarget(current.path, componentPath);
737
+ this.cloneThirdPartyComponent(thirdPartyComponents, componentName);
738
+ if (!target)
739
+ continue;
740
+ const targetComponent = this.getComponentByName(target.name);
741
+ if (targetComponent && (!target.root || target.root === root) && !visited.has(targetComponent.name)) {
742
+ queue.push(targetComponent);
743
+ }
744
+ }
745
+ }
746
+ return {
747
+ includes: this.collectScopedInnerComponents(compilation, root),
748
+ exclude: new Set(component_1.componentConfig.exclude),
749
+ thirdPartyComponents,
750
+ includeAll: false,
751
+ skipRecursiveComponent: false,
752
+ };
753
+ }
754
+ generateSubPackageIndieScriptFile(compilation, compiler, filePath, content) {
755
+ const { RawSource } = compiler.webpack.sources;
756
+ compilation.assets[this.miniPlugin.getTargetFilePath(filePath, '.js')] = new RawSource(content);
757
+ }
758
+ createRecursiveComponentWrapperSource(componentName) {
759
+ const args = componentName ? JSON.stringify(componentName) : '';
760
+ return `const registerRecursiveComponent = globalThis.__taroRegisterRecursiveComponent
761
+
762
+ if (typeof registerRecursiveComponent !== 'function') {
763
+ throw new Error('globalThis.__taroRegisterRecursiveComponent is not a function')
764
+ }
765
+
766
+ registerRecursiveComponent(${args})
767
+ `;
768
+ }
769
+ generateSubPackageIndieFiles(compilation, compiler, template, baseTemplateName) {
770
+ const customWrapperRoots = new Set();
771
+ if (!this.getSubPackageIndieConfigs().length)
772
+ return customWrapperRoots;
773
+ const allIndieRoots = this.getAllIndieRoots();
774
+ allIndieRoots.forEach(root => {
775
+ const scopedComponentConfig = this.getScopedSubPackageIndieComponentConfig(compilation, root);
776
+ const isRecursiveDisabled = this.isRecursiveComponentDisabledForRoot(root);
777
+ if (isRecursiveDisabled) {
778
+ scopedComponentConfig.skipRecursiveComponent = true;
779
+ }
780
+ const isRootUsingCustomWrapper = isRecursiveDisabled
781
+ ? false
782
+ : this.isSubPackageIndieRootUsingCustomWrapper(compilation, root, scopedComponentConfig.thirdPartyComponents);
783
+ if (isRootUsingCustomWrapper) {
784
+ customWrapperRoots.add(root);
785
+ if (!scopedComponentConfig.thirdPartyComponents.has(MiniPlugin_1.customWrapperName)) {
786
+ const attrs = component_1.componentConfig.thirdPartyComponents.get(MiniPlugin_1.customWrapperName);
787
+ scopedComponentConfig.thirdPartyComponents.set(MiniPlugin_1.customWrapperName, new Set(attrs || []));
788
+ }
789
+ }
790
+ this.miniPlugin.generateTemplateFile(compilation, compiler, `${root}/${baseTemplateName}`, template.buildTemplate, scopedComponentConfig);
791
+ this.miniPlugin.generateXSFile(compilation, compiler, `${root}/utils`);
792
+ if (!template.isSupportRecursive && !isRecursiveDisabled) {
793
+ this.generateSubPackageIndieScriptFile(compilation, compiler, `${root}/${MiniPlugin_1.baseCompName}`, this.createRecursiveComponentWrapperSource());
794
+ this.miniPlugin.generateTemplateFile(compilation, compiler, `${root}/${MiniPlugin_1.baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
795
+ const compConfig = {
796
+ component: true,
797
+ styleIsolation: MiniPlugin_1.STYLE_ISOLATION_APPLY_SHARED,
798
+ usingComponents: {
799
+ [MiniPlugin_1.baseCompName]: `./${MiniPlugin_1.baseCompName}`,
800
+ },
801
+ };
802
+ if (isRootUsingCustomWrapper) {
803
+ compConfig.usingComponents[MiniPlugin_1.customWrapperName] = `./${MiniPlugin_1.customWrapperName}`;
804
+ }
805
+ this.miniPlugin.generateConfigFile(compilation, compiler, `${root}/${MiniPlugin_1.baseCompName}`, compConfig);
806
+ }
807
+ if (isRootUsingCustomWrapper) {
808
+ this.generateSubPackageIndieScriptFile(compilation, compiler, `${root}/${MiniPlugin_1.customWrapperName}`, this.createRecursiveComponentWrapperSource(MiniPlugin_1.customWrapperName));
809
+ this.miniPlugin.generateTemplateFile(compilation, compiler, `${root}/${MiniPlugin_1.customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
810
+ this.miniPlugin.generateConfigFile(compilation, compiler, `${root}/${MiniPlugin_1.customWrapperName}`, {
811
+ component: true,
812
+ styleIsolation: MiniPlugin_1.STYLE_ISOLATION_APPLY_SHARED,
813
+ usingComponents: {
814
+ [MiniPlugin_1.baseCompName]: `./${MiniPlugin_1.baseCompName}`,
815
+ [MiniPlugin_1.customWrapperName]: `./${MiniPlugin_1.customWrapperName}`,
816
+ },
817
+ });
818
+ }
819
+ });
820
+ return customWrapperRoots;
821
+ }
822
+ generateMainPackageRuntimeFiles(compilation, _compiler) {
823
+ const { commonChunks, fileType } = this.options;
824
+ const mainPackageRoots = this.getAllMainPackageRoots();
825
+ if (!mainPackageRoots.length)
826
+ return;
827
+ const styleExt = fileType.style;
828
+ const runtimeChunks = ['app', ...commonChunks];
829
+ mainPackageRoots.forEach(mainPackageRoot => {
830
+ runtimeChunks.forEach(chunkName => {
831
+ var _a, _b, _c, _d;
832
+ const jsFile = `${chunkName}.js`;
833
+ if (compilation.assets[jsFile]) {
834
+ let jsContent = compilation.assets[jsFile];
835
+ if (chunkName === 'app') {
836
+ const { RawSource } = ((_b = (_a = compilation.compiler) === null || _a === void 0 ? void 0 : _a.webpack) === null || _b === void 0 ? void 0 : _b.sources) || require('webpack').sources;
837
+ const originalSource = String(((_d = (_c = jsContent).source) === null || _d === void 0 ? void 0 : _d.call(_c)) || String(jsContent));
838
+ const registrarExpr = `(typeof globalThis.__taroRegisterRecursiveComponent==="function"||(globalThis.__taroRegisterRecursiveComponent=function(componentName){const cache=__webpack_require__.c||{};let createRecursiveComponentConfig;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));}))`;
839
+ let patchedSource = originalSource;
840
+ if (/,\s*exports\.taroApp\s*=/.test(patchedSource)) {
841
+ patchedSource = patchedSource.replace(/,\s*exports\.taroApp\s*=/, `,${registrarExpr},exports.taroApp=`);
842
+ }
843
+ else {
844
+ patchedSource = patchedSource.replace(/exports\.taroApp\s*=/, `;${registrarExpr};exports.taroApp=`);
845
+ }
846
+ jsContent = new RawSource(patchedSource);
847
+ }
848
+ compilation.assets[`${mainPackageRoot}/${jsFile}`] = jsContent;
849
+ }
850
+ const cssFile = `${chunkName}${styleExt}`;
851
+ if (compilation.assets[cssFile]) {
852
+ compilation.assets[`${mainPackageRoot}/${cssFile}`] = compilation.assets[cssFile];
853
+ }
854
+ });
855
+ const appOriginCss = `app-origin${styleExt}`;
856
+ if (compilation.assets[appOriginCss]) {
857
+ compilation.assets[`${mainPackageRoot}/${appOriginCss}`] = compilation.assets[appOriginCss];
858
+ }
859
+ });
860
+ }
861
+ }
862
+ exports.default = SubPackageIndiePlugin;
863
+ //# sourceMappingURL=SubPackageIndiePlugin.js.map