@tarojs/webpack5-runner 4.1.12-beta.3 → 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.
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -12,21 +35,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
36
  };
14
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.STYLE_ISOLATION_SHARED = exports.STYLE_ISOLATION_APPLY_SHARED = exports.customWrapperName = exports.baseCompName = void 0;
15
39
  const node_path_1 = __importDefault(require("node:path"));
16
40
  const helper_1 = require("@tarojs/helper");
17
41
  const loader_utils_1 = require("loader-utils");
42
+ const tapable_1 = require("tapable");
18
43
  const EntryDependency_1 = __importDefault(require("webpack/lib/dependencies/EntryDependency"));
19
44
  const TaroSingleEntryDependency_1 = __importDefault(require("../dependencies/TaroSingleEntryDependency"));
20
45
  const prerender_1 = require("../prerender/prerender");
21
46
  const component_1 = require("../utils/component");
22
47
  const webpack_1 = require("../utils/webpack");
48
+ const SubPackageIndiePlugin_1 = __importStar(require("./SubPackageIndiePlugin"));
23
49
  const TaroLoadChunksPlugin_1 = __importDefault(require("./TaroLoadChunksPlugin"));
24
50
  const TaroNormalModulesPlugin_1 = __importDefault(require("./TaroNormalModulesPlugin"));
25
51
  const TaroSingleEntryPlugin_1 = __importDefault(require("./TaroSingleEntryPlugin"));
26
- const baseCompName = 'comp';
27
- const customWrapperName = 'custom-wrapper';
52
+ exports.baseCompName = 'comp';
53
+ exports.customWrapperName = 'custom-wrapper';
28
54
  const PLUGIN_NAME = 'TaroMiniPlugin';
29
55
  const CHILD_COMPILER_TAG = 'child';
56
+ exports.STYLE_ISOLATION_APPLY_SHARED = 'apply-shared';
57
+ exports.STYLE_ISOLATION_SHARED = 'shared';
30
58
  function isLoaderExist(loaders, loaderName) {
31
59
  return loaders.some(item => item.loader === loaderName);
32
60
  }
@@ -46,6 +74,7 @@ class TaroMiniPlugin {
46
74
  this.dependencies = new Map();
47
75
  this.pageLoaderName = '@tarojs/taro-loader/lib/page';
48
76
  this.independentPackages = new Map();
77
+ this.subPackageIndiePlugin = null;
49
78
  const { combination } = options;
50
79
  const miniBuildConfig = combination.config;
51
80
  const { template, baseLevel = 16, experimental } = miniBuildConfig;
@@ -69,6 +98,22 @@ class TaroMiniPlugin {
69
98
  hot: options.hot,
70
99
  loaderMeta: options.loaderMeta || {},
71
100
  };
101
+ // 初始化扩展钩子
102
+ this.hooks = {
103
+ modifyEntries: new tapable_1.SyncWaterfallHook(['entries']),
104
+ modifyChunkRequire: new tapable_1.SyncWaterfallHook(['result', 'chunkId', 'miniType']),
105
+ afterResolveModule: new tapable_1.SyncHook(['resolveData']),
106
+ compileExtraEntries: new tapable_1.SyncHook(['compiler', 'compilation', 'promises']),
107
+ modifySkipRootTemplates: new tapable_1.SyncWaterfallHook(['skip']),
108
+ generateExtraFiles: new tapable_1.SyncWaterfallHook(['customWrapperRoots', 'compilation', 'compiler']),
109
+ modifyPageConfig: new tapable_1.SyncWaterfallHook(['context', 'page', 'customWrapperRoots']),
110
+ modifyComponentConfig: new tapable_1.SyncHook(['context', 'customWrapperRoots']),
111
+ afterGenerateFiles: new tapable_1.SyncHook(['compilation', 'compiler']),
112
+ optimizeAssets: new tapable_1.SyncHook(['compilation']),
113
+ modifyConfig: new tapable_1.SyncWaterfallHook(['config', 'componentName']),
114
+ modifyStyleImport: new tapable_1.SyncWaterfallHook(['context', 'page']),
115
+ modifyShouldProcessStyles: new tapable_1.SyncWaterfallHook(['shouldProcess']),
116
+ };
72
117
  if (template.isSupportRecursive === false && baseLevel > 0) {
73
118
  template.baseLevel = baseLevel;
74
119
  }
@@ -99,6 +144,10 @@ class TaroMiniPlugin {
99
144
  apply(compiler) {
100
145
  this.context = compiler.context;
101
146
  this.appEntry = this.getAppEntry(compiler);
147
+ if (this.options.newBlended) {
148
+ this.subPackageIndiePlugin = new SubPackageIndiePlugin_1.default(this);
149
+ this.subPackageIndiePlugin.apply();
150
+ }
102
151
  const { commonChunks, combination, framework, isBuildPlugin, newBlended, } = this.options;
103
152
  const { addChunkPages, onCompilerMake, modifyBuildAssets, onParseCreateElement, } = combination.config;
104
153
  /** build mode */
@@ -135,6 +184,7 @@ class TaroMiniPlugin {
135
184
  const dependencies = this.dependencies;
136
185
  const promises = [];
137
186
  this.compileIndependentPages(compiler, compilation, dependencies, promises);
187
+ this.hooks.compileExtraEntries.call(compiler, compilation, promises);
138
188
  dependencies.forEach(dep => {
139
189
  promises.push(new Promise((resolve, reject) => {
140
190
  compilation.addEntry(this.options.sourceDir, dep, Object.assign({ name: dep.name }, dep.options), err => err ? reject(err) : resolve(null));
@@ -147,6 +197,11 @@ class TaroMiniPlugin {
147
197
  /** For Webpack compilation get factory from compilation.dependencyFactories by denpendence's constructor */
148
198
  compilation.dependencyFactories.set(EntryDependency_1.default, normalModuleFactory);
149
199
  compilation.dependencyFactories.set(TaroSingleEntryDependency_1.default, normalModuleFactory);
200
+ if (this.options.newBlended) {
201
+ normalModuleFactory.hooks.afterResolve.tap(PLUGIN_NAME, (resolveData) => {
202
+ this.hooks.afterResolveModule.call(resolveData);
203
+ });
204
+ }
150
205
  /**
151
206
  * webpack NormalModule 在 runLoaders 真正解析资源的前一刻,
152
207
  * 往 NormalModule.loaders 中插入对应的 Taro Loader
@@ -225,6 +280,8 @@ class TaroMiniPlugin {
225
280
  name: PLUGIN_NAME,
226
281
  stage: PROCESS_ASSETS_STAGE_ADDITIONAL
227
282
  }, this.tryAsync(() => __awaiter(this, void 0, void 0, function* () {
283
+ if (compilation.__tag === SubPackageIndiePlugin_1.SUBPACKAGE_STANDALONE_CHILD_TAG)
284
+ return;
228
285
  // 如果是子编译器,证明是编译独立分包,进行单独的处理
229
286
  if (compilation.__tag === CHILD_COMPILER_TAG) {
230
287
  yield this.generateIndependentMiniFiles(compilation, compiler);
@@ -239,6 +296,8 @@ class TaroMiniPlugin {
239
296
  // Stage 触发顺序:https://webpack.js.org/api/compilation-hooks/#list-of-asset-processing-stages
240
297
  stage: PROCESS_ASSETS_STAGE_OPTIMIZE
241
298
  }, this.tryAsync(() => __awaiter(this, void 0, void 0, function* () {
299
+ if (compilation.__tag === SubPackageIndiePlugin_1.SUBPACKAGE_STANDALONE_CHILD_TAG)
300
+ return;
242
301
  yield this.optimizeMiniFiles(compilation, compiler);
243
302
  })));
244
303
  compilation.hooks.processAssets.tapAsync({
@@ -246,6 +305,8 @@ class TaroMiniPlugin {
246
305
  // 该 stage 是最后执行的,确保 taro 暴露给用户的钩子 modifyBuildAssets 在内部处理完 assets 之后再调用
247
306
  stage: PROCESS_ASSETS_STAGE_REPORT
248
307
  }, this.tryAsync(() => __awaiter(this, void 0, void 0, function* () {
308
+ if (compilation.__tag === SubPackageIndiePlugin_1.SUBPACKAGE_STANDALONE_CHILD_TAG)
309
+ return;
249
310
  if (typeof modifyBuildAssets === 'function') {
250
311
  yield modifyBuildAssets(compilation.assets, this);
251
312
  }
@@ -290,6 +351,10 @@ class TaroMiniPlugin {
290
351
  let source;
291
352
  const id = (0, webpack_1.getChunkIdOrName)(chunk);
292
353
  const { miniType } = entryModule;
354
+ const modifyResult = this.hooks.modifyChunkRequire.call({ source: modules, handled: false }, id, miniType);
355
+ if (modifyResult === null || modifyResult === void 0 ? void 0 : modifyResult.handled) {
356
+ return modifyResult.source;
357
+ }
293
358
  const entryChunk = [{ name: 'app' }];
294
359
  // 所有模块都依赖app.js,确保@tarojs\plugin-platform-xxx\dist\runtime.js先于@tarojs/runtime执行,避免Taro API未被初始化
295
360
  if (this.nativeComponents.has(id) || miniType === helper_1.META_TYPE.STATIC) {
@@ -298,7 +363,8 @@ class TaroMiniPlugin {
298
363
  source = (0, webpack_1.addRequireToSource)(id, modules, v);
299
364
  }
300
365
  });
301
- return source;
366
+ // 如果没有依赖需要注入,返回原始模块
367
+ return source || modules;
302
368
  }
303
369
  else if (miniType === helper_1.META_TYPE.PAGE) {
304
370
  return (0, webpack_1.addRequireToSource)(id, modules, entryChunk);
@@ -477,12 +543,12 @@ class TaroMiniPlugin {
477
543
  }
478
544
  if (!this.options.template.isSupportRecursive) {
479
545
  pluginJSON.publicComponents = Object.assign({}, publicComponents, {
480
- [baseCompName]: baseCompName
546
+ [exports.baseCompName]: exports.baseCompName
481
547
  });
482
548
  }
483
549
  if (isUsingCustomWrapper) {
484
550
  pluginJSON.publicComponents = Object.assign({}, publicComponents, {
485
- [customWrapperName]: customWrapperName
551
+ [exports.customWrapperName]: exports.customWrapperName
486
552
  });
487
553
  }
488
554
  }
@@ -611,8 +677,11 @@ class TaroMiniPlugin {
611
677
  */
612
678
  addEntry(entryPath, entryName, entryType, options = {}) {
613
679
  let dep;
614
- if (this.dependencies.has(entryPath)) {
615
- dep = this.dependencies.get(entryPath);
680
+ // 对于 STATIC 类型(如 comp/custom-wrapper),使用 entryName 作为 key 的一部分
681
+ // 这允许同一个模板文件被多次添加为不同名称的入口
682
+ const depKey = entryType === helper_1.META_TYPE.STATIC ? `${entryPath}#${entryName}` : entryPath;
683
+ if (this.dependencies.has(depKey)) {
684
+ dep = this.dependencies.get(depKey);
616
685
  dep.name = entryName;
617
686
  dep.loc = { name: entryName };
618
687
  dep.request = entryPath;
@@ -623,7 +692,7 @@ class TaroMiniPlugin {
623
692
  else {
624
693
  dep = new TaroSingleEntryDependency_1.default(entryPath, entryName, { name: entryName }, entryType, options);
625
694
  }
626
- this.dependencies.set(entryPath, dep);
695
+ this.dependencies.set(depKey, dep);
627
696
  }
628
697
  /**
629
698
  * 在 this.dependencies 中新增或修改 app、模板组件、页面、组件等资源模块
@@ -631,10 +700,17 @@ class TaroMiniPlugin {
631
700
  addEntries() {
632
701
  const { template } = this.options;
633
702
  this.addEntry(this.appEntry, 'app', helper_1.META_TYPE.ENTRY);
634
- if (!template.isSupportRecursive) {
703
+ const entryFlags = this.hooks.modifyEntries.call({
704
+ skipRootComp: false,
705
+ skipRootWrapper: false,
706
+ });
707
+ if (!template.isSupportRecursive && !entryFlags.skipRootComp) {
635
708
  this.addEntry(node_path_1.default.resolve(__dirname, '..', 'template/comp'), 'comp', helper_1.META_TYPE.STATIC);
636
709
  }
637
- this.addEntry(node_path_1.default.resolve(__dirname, '..', 'template/custom-wrapper'), 'custom-wrapper', helper_1.META_TYPE.STATIC);
710
+ if (!entryFlags.skipRootWrapper) {
711
+ this.addEntry(node_path_1.default.resolve(__dirname, '..', 'template/custom-wrapper'), 'custom-wrapper', helper_1.META_TYPE.STATIC);
712
+ }
713
+ // subPackageIndie 下的 comp/custom-wrapper 统一走独立子编译器,避免被主编译的公共 chunk 共享
638
714
  this.pages.forEach(item => {
639
715
  if (item.isNative) {
640
716
  this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL, { isNativePage: true });
@@ -962,6 +1038,12 @@ class TaroMiniPlugin {
962
1038
  usingComponents[key] = match ? `${node_path_1.default.sep}${match[0]}` : compPath;
963
1039
  }
964
1040
  }
1041
+ cloneThirdPartyComponent(thirdPartyComponents, componentName) {
1042
+ const attrs = component_1.componentConfig.thirdPartyComponents.get(componentName);
1043
+ if (!attrs || thirdPartyComponents.has(componentName))
1044
+ return;
1045
+ thirdPartyComponents.set(componentName, new Set(attrs));
1046
+ }
965
1047
  /** 生成小程序独立分包的相关文件 */
966
1048
  generateIndependentMiniFiles(compilation, compiler) {
967
1049
  return __awaiter(this, void 0, void 0, function* () {
@@ -978,24 +1060,27 @@ class TaroMiniPlugin {
978
1060
  this.generateTemplateFile(compilation, compiler, `${name}/${baseTemplateName}`, template.buildTemplate, component_1.componentConfig);
979
1061
  if (!template.isSupportRecursive) {
980
1062
  // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
981
- this.generateConfigFile(compilation, compiler, `${name}/${baseCompName}`, {
1063
+ const compConfig = {
982
1064
  component: true,
983
- styleIsolation: 'apply-shared',
1065
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
984
1066
  usingComponents: {
985
- [baseCompName]: `./${baseCompName}`,
986
- [customWrapperName]: `./${customWrapperName}`
1067
+ [exports.baseCompName]: `./${exports.baseCompName}`
987
1068
  }
988
- });
989
- this.generateTemplateFile(compilation, compiler, `${name}/${baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
1069
+ };
1070
+ if (isUsingCustomWrapper) {
1071
+ compConfig.usingComponents[exports.customWrapperName] = `./${exports.customWrapperName}`;
1072
+ }
1073
+ this.generateConfigFile(compilation, compiler, `${name}/${exports.baseCompName}`, compConfig);
1074
+ this.generateTemplateFile(compilation, compiler, `${name}/${exports.baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
990
1075
  }
991
- this.generateConfigFile(compilation, compiler, `${name}/${customWrapperName}`, {
1076
+ this.generateConfigFile(compilation, compiler, `${name}/${exports.customWrapperName}`, {
992
1077
  component: true,
993
- styleIsolation: 'apply-shared',
1078
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
994
1079
  usingComponents: {
995
- [customWrapperName]: `./${customWrapperName}`
1080
+ [exports.customWrapperName]: `./${exports.customWrapperName}`
996
1081
  }
997
1082
  });
998
- this.generateTemplateFile(compilation, compiler, `${name}/${customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
1083
+ this.generateTemplateFile(compilation, compiler, `${name}/${exports.customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
999
1084
  this.generateXSFile(compilation, compiler, `${name}/utils`);
1000
1085
  });
1001
1086
  this.pages.forEach(page => {
@@ -1018,14 +1103,14 @@ class TaroMiniPlugin {
1018
1103
  this.generateTemplateFile(compilation, compiler, page.path, template.buildPageTemplate, importBaseTemplatePath, config);
1019
1104
  }
1020
1105
  if (config) {
1021
- const importBaseCompPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, independentName, this.getTargetFilePath(baseCompName, ''))));
1022
- const importCustomWrapperPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, independentName, this.getTargetFilePath(customWrapperName, ''))));
1106
+ const importBaseCompPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, independentName, this.getTargetFilePath(exports.baseCompName, ''))));
1107
+ const importCustomWrapperPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, independentName, this.getTargetFilePath(exports.customWrapperName, ''))));
1023
1108
  config.content.usingComponents = Object.assign({}, config.content.usingComponents);
1024
1109
  if (isUsingCustomWrapper) {
1025
- config.content.usingComponents[customWrapperName] = importCustomWrapperPath;
1110
+ config.content.usingComponents[exports.customWrapperName] = importCustomWrapperPath;
1026
1111
  }
1027
1112
  if (!template.isSupportRecursive && !page.isNative) {
1028
- config.content.usingComponents[baseCompName] = importBaseCompPath;
1113
+ config.content.usingComponents[exports.baseCompName] = importBaseCompPath;
1029
1114
  }
1030
1115
  this.generateConfigFile(compilation, compiler, page.path, config.content);
1031
1116
  }
@@ -1040,6 +1125,8 @@ class TaroMiniPlugin {
1040
1125
  const { modifyMiniConfigs } = combination.config;
1041
1126
  const baseTemplateName = 'base';
1042
1127
  const isUsingCustomWrapper = component_1.componentConfig.thirdPartyComponents.has('custom-wrapper');
1128
+ let subPackageIndieCustomWrapperRoots = new Set();
1129
+ compilation[SubPackageIndiePlugin_1.subPackageIndieCustomWrapperRootsKey] = subPackageIndieCustomWrapperRoots;
1043
1130
  /**
1044
1131
  * 与原生小程序混写时解析模板与样式
1045
1132
  */
@@ -1059,47 +1146,55 @@ class TaroMiniPlugin {
1059
1146
  const appConfigName = node_path_1.default.basename(appConfigPath).replace(node_path_1.default.extname(appConfigPath), '');
1060
1147
  this.generateConfigFile(compilation, compiler, this.appEntry, this.filesConfig[appConfigName].content);
1061
1148
  }
1062
- if (!template.isSupportRecursive) {
1149
+ let skipRootTemplates = false;
1150
+ skipRootTemplates = this.hooks.modifySkipRootTemplates.call(skipRootTemplates);
1151
+ if (!template.isSupportRecursive && !skipRootTemplates) {
1063
1152
  // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
1064
- this.generateTemplateFile(compilation, compiler, baseCompName, template.buildBaseComponentTemplate, this.options.fileType.templ);
1153
+ this.generateTemplateFile(compilation, compiler, exports.baseCompName, template.buildBaseComponentTemplate, this.options.fileType.templ);
1065
1154
  const baseCompConfig = {
1066
1155
  component: true,
1067
- styleIsolation: 'apply-shared',
1156
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
1068
1157
  usingComponents: {
1069
- [baseCompName]: `./${baseCompName}`
1158
+ [exports.baseCompName]: `./${exports.baseCompName}`
1070
1159
  }
1071
1160
  };
1072
1161
  if (isUsingCustomWrapper) {
1073
- baseCompConfig.usingComponents[customWrapperName] = `./${customWrapperName}`;
1074
- this.generateConfigFile(compilation, compiler, customWrapperName, {
1162
+ baseCompConfig.usingComponents[exports.customWrapperName] = `./${exports.customWrapperName}`;
1163
+ this.generateConfigFile(compilation, compiler, exports.customWrapperName, {
1075
1164
  component: true,
1076
- styleIsolation: 'apply-shared',
1165
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
1077
1166
  usingComponents: {
1078
- [baseCompName]: `./${baseCompName}`,
1079
- [customWrapperName]: `./${customWrapperName}`
1167
+ [exports.baseCompName]: `./${exports.baseCompName}`,
1168
+ [exports.customWrapperName]: `./${exports.customWrapperName}`
1080
1169
  }
1081
1170
  });
1082
1171
  }
1083
- this.generateConfigFile(compilation, compiler, baseCompName, baseCompConfig);
1172
+ this.generateConfigFile(compilation, compiler, exports.baseCompName, baseCompConfig);
1084
1173
  }
1085
- else {
1174
+ else if (!skipRootTemplates) {
1086
1175
  if (isUsingCustomWrapper) {
1087
- this.generateConfigFile(compilation, compiler, customWrapperName, {
1176
+ this.generateConfigFile(compilation, compiler, exports.customWrapperName, {
1088
1177
  component: true,
1089
- styleIsolation: 'apply-shared',
1178
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
1090
1179
  usingComponents: {
1091
- [customWrapperName]: `./${customWrapperName}`
1180
+ [exports.customWrapperName]: `./${exports.customWrapperName}`
1092
1181
  }
1093
1182
  });
1094
1183
  }
1095
1184
  }
1096
- this.generateTemplateFile(compilation, compiler, baseTemplateName, template.buildTemplate, component_1.componentConfig);
1097
- isUsingCustomWrapper && this.generateTemplateFile(compilation, compiler, customWrapperName, template.buildCustomComponentTemplate, this.options.fileType.templ);
1098
- this.generateXSFile(compilation, compiler, 'utils');
1185
+ // 当配置了 mainPackageRoot 时,跳过根目录 base.wxml 和 utils.wxs 的生成
1186
+ if (!skipRootTemplates) {
1187
+ this.generateTemplateFile(compilation, compiler, baseTemplateName, template.buildTemplate, component_1.componentConfig);
1188
+ isUsingCustomWrapper && this.generateTemplateFile(compilation, compiler, exports.customWrapperName, template.buildCustomComponentTemplate, this.options.fileType.templ);
1189
+ this.generateXSFile(compilation, compiler, 'utils');
1190
+ }
1191
+ subPackageIndieCustomWrapperRoots = this.hooks.generateExtraFiles.call(subPackageIndieCustomWrapperRoots, compilation, compiler);
1192
+ compilation[SubPackageIndiePlugin_1.subPackageIndieCustomWrapperRootsKey] = subPackageIndieCustomWrapperRoots;
1099
1193
  this.components.forEach(component => {
1100
1194
  const importBaseTemplatePath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(component.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTemplatePath(baseTemplateName))));
1101
1195
  const config = this.filesConfig[this.getConfigFilePath(component.name)];
1102
1196
  if (config) {
1197
+ this.hooks.modifyComponentConfig.call({ config, component }, subPackageIndieCustomWrapperRoots);
1103
1198
  this.generateConfigFile(compilation, compiler, component.path, config.content);
1104
1199
  }
1105
1200
  if (!component.isNative) {
@@ -1107,31 +1202,46 @@ class TaroMiniPlugin {
1107
1202
  }
1108
1203
  });
1109
1204
  this.pages.forEach(page => {
1110
- const importBaseTemplatePath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTemplatePath(baseTemplateName))));
1205
+ let importBaseTemplatePath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTemplatePath(baseTemplateName))));
1111
1206
  const config = this.filesConfig[this.getConfigFilePath(page.name)];
1112
1207
  // pages 里面会混合独立分包的,在这里需要过滤一下,避免重复生成 assets
1113
1208
  const isIndependent = !!this.getIndependentPackage(page.path);
1114
1209
  if (isIndependent)
1115
1210
  return;
1116
1211
  // 生成页面模板需要在生成页面配置之前,因为会依赖到页面配置的部分内容
1212
+ let importBaseCompPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTargetFilePath(exports.baseCompName, ''))));
1213
+ let importCustomWrapperPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTargetFilePath(exports.customWrapperName, ''))));
1214
+ let isPageRecursiveDisabled = false;
1215
+ let shouldUseCustomWrapper = isUsingCustomWrapper;
1216
+ const pageConfigContext = this.hooks.modifyPageConfig.call({
1217
+ importBaseTemplatePath,
1218
+ importBaseCompPath,
1219
+ importCustomWrapperPath,
1220
+ shouldUseCustomWrapper,
1221
+ isPageRecursiveDisabled,
1222
+ }, page, subPackageIndieCustomWrapperRoots);
1223
+ importBaseTemplatePath = pageConfigContext.importBaseTemplatePath;
1224
+ importBaseCompPath = pageConfigContext.importBaseCompPath;
1225
+ importCustomWrapperPath = pageConfigContext.importCustomWrapperPath;
1226
+ shouldUseCustomWrapper = pageConfigContext.shouldUseCustomWrapper;
1227
+ isPageRecursiveDisabled = pageConfigContext.isPageRecursiveDisabled;
1117
1228
  if (!page.isNative) {
1118
1229
  this.generateTemplateFile(compilation, compiler, page.path, template.buildPageTemplate, importBaseTemplatePath, config);
1119
1230
  }
1120
1231
  if (config) {
1121
- const importBaseCompPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTargetFilePath(baseCompName, ''))));
1122
- const importCustomWrapperPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, isBuildPlugin ? 'plugin' : '', this.getTargetFilePath(customWrapperName, ''))));
1123
1232
  config.content.usingComponents = Object.assign({}, config.content.usingComponents);
1124
- if (isUsingCustomWrapper) {
1125
- config.content.usingComponents[customWrapperName] = importCustomWrapperPath;
1233
+ if (shouldUseCustomWrapper) {
1234
+ config.content.usingComponents[exports.customWrapperName] = importCustomWrapperPath;
1126
1235
  }
1127
- if (!template.isSupportRecursive && !page.isNative) {
1128
- config.content.usingComponents[baseCompName] = importBaseCompPath;
1236
+ if (!template.isSupportRecursive && !page.isNative && !isPageRecursiveDisabled) {
1237
+ config.content.usingComponents[exports.baseCompName] = importBaseCompPath;
1129
1238
  }
1130
1239
  this.generateConfigFile(compilation, compiler, page.path, config.content);
1131
1240
  }
1132
1241
  });
1133
1242
  this.generateTabBarFiles(compilation, compiler);
1134
1243
  this.injectCommonStyles(compilation, compiler);
1244
+ this.hooks.afterGenerateFiles.call(compilation, compiler);
1135
1245
  if (this.themeLocation) {
1136
1246
  this.generateDarkModeFile(compilation, compiler);
1137
1247
  }
@@ -1164,11 +1274,14 @@ class TaroMiniPlugin {
1164
1274
  delete compilation.assets[assetPath];
1165
1275
  }
1166
1276
  });
1277
+ this.hooks.optimizeAssets.call(compilation);
1167
1278
  });
1168
1279
  }
1169
1280
  generateConfigFile(compilation, compiler, filePath, config) {
1170
1281
  const { RawSource } = compiler.webpack.sources;
1171
1282
  const fileConfigName = this.getConfigPath(this.getComponentName(filePath));
1283
+ const componentName = this.getComponentName(filePath);
1284
+ config = this.hooks.modifyConfig.call(config, componentName);
1172
1285
  const unofficialConfigs = ['enableShareAppMessage', 'enableShareTimeline', 'enablePageMeta', 'components'];
1173
1286
  unofficialConfigs.forEach(item => {
1174
1287
  delete config[item];
@@ -1177,6 +1290,21 @@ class TaroMiniPlugin {
1177
1290
  const fileConfigStr = JSON.stringify(config);
1178
1291
  compilation.assets[fileConfigName] = new RawSource(fileConfigStr);
1179
1292
  }
1293
+ generateSubPackageIndieScriptFile(compilation, compiler, filePath, content) {
1294
+ const { RawSource } = compiler.webpack.sources;
1295
+ compilation.assets[this.getTargetFilePath(filePath, '.js')] = new RawSource(content);
1296
+ }
1297
+ createRecursiveComponentWrapperSource(componentName) {
1298
+ const args = componentName ? JSON.stringify(componentName) : '';
1299
+ return `const registerRecursiveComponent = globalThis.__taroRegisterRecursiveComponent
1300
+
1301
+ if (typeof registerRecursiveComponent !== 'function') {
1302
+ throw new Error('globalThis.__taroRegisterRecursiveComponent is not a function')
1303
+ }
1304
+
1305
+ registerRecursiveComponent(${args})
1306
+ `;
1307
+ }
1180
1308
  generateTemplateFile(compilation, compiler, filePath, templateFn, ...options) {
1181
1309
  var _a;
1182
1310
  const { RawSource } = compiler.webpack.sources;
@@ -1315,12 +1443,21 @@ class TaroMiniPlugin {
1315
1443
  });
1316
1444
  });
1317
1445
  }
1318
- if (commons.size() > 0) {
1446
+ // 判断是否需要处理样式:有 common chunks 或者有 app.wxss(可被扩展插件修改)
1447
+ const hasAppStyle = !!assets[appStyle];
1448
+ let shouldProcessStyles = commons.size() > 0;
1449
+ if (!hasAppStyle) {
1450
+ shouldProcessStyles = false;
1451
+ }
1452
+ shouldProcessStyles = this.hooks.modifyShouldProcessStyles.call(shouldProcessStyles);
1453
+ if (shouldProcessStyles) {
1319
1454
  const APP_STYLE_NAME = 'app-origin' + styleExt;
1320
1455
  assets[APP_STYLE_NAME] = new ConcatSource(originSource);
1321
1456
  const source = new ConcatSource('');
1322
1457
  source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(APP_STYLE_NAME))};`);
1323
- source.add(commons);
1458
+ if (commons.size() > 0) {
1459
+ source.add(commons);
1460
+ }
1324
1461
  source.add('\n');
1325
1462
  assets[appStyle] = source;
1326
1463
  if (newBlended) {
@@ -1336,14 +1473,43 @@ class TaroMiniPlugin {
1336
1473
  }
1337
1474
  const source = new ConcatSource('');
1338
1475
  const originSource = assets[pageStyle];
1339
- componentCommons.forEach(item => {
1340
- source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(node_path_1.default.posix.relative(node_path_1.default.dirname(pageStyle), item)))};\n`);
1341
- });
1476
+ const styleImportContext = this.hooks.modifyStyleImport.call({
1477
+ importStatement: '',
1478
+ shouldSkip: false,
1479
+ isMainPackageRoot: false,
1480
+ }, page);
1481
+ if (styleImportContext.shouldSkip) {
1482
+ // no-op
1483
+ }
1484
+ else if (styleImportContext.importStatement) {
1485
+ source.add(styleImportContext.importStatement);
1486
+ }
1487
+ else {
1488
+ componentCommons.forEach(item => {
1489
+ source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(node_path_1.default.posix.relative(node_path_1.default.dirname(pageStyle), item)))};\n`);
1490
+ });
1491
+ }
1342
1492
  source.add(originSource);
1343
1493
  assets[pageStyle] = source;
1344
1494
  }
1345
1495
  else {
1346
- if (pageStyle in assets) {
1496
+ // 普通页面(非本地化组件)
1497
+ const styleImportContext = this.hooks.modifyStyleImport.call({
1498
+ importStatement: '',
1499
+ shouldSkip: false,
1500
+ isMainPackageRoot: false,
1501
+ }, page);
1502
+ if (styleImportContext.importStatement) {
1503
+ const source = new ConcatSource('');
1504
+ if (pageStyle in assets) {
1505
+ source.add(assets[pageStyle]);
1506
+ }
1507
+ const importSource = new ConcatSource('');
1508
+ importSource.add(styleImportContext.importStatement);
1509
+ importSource.add(source);
1510
+ assets[pageStyle] = importSource;
1511
+ }
1512
+ else if (pageStyle in assets) {
1347
1513
  const source = new ConcatSource('');
1348
1514
  const originSource = assets[pageStyle];
1349
1515
  source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(node_path_1.default.posix.relative(node_path_1.default.dirname(pageStyle), 'app' + styleExt)))};\n`);