@tarojs/webpack5-runner 4.1.12-beta.5 → 4.1.12-beta.51

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,27 @@ 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");
47
+ const forceCustomWrapper_1 = require("../utils/forceCustomWrapper");
22
48
  const webpack_1 = require("../utils/webpack");
49
+ const SubPackageIndiePlugin_1 = __importStar(require("./SubPackageIndiePlugin"));
23
50
  const TaroLoadChunksPlugin_1 = __importDefault(require("./TaroLoadChunksPlugin"));
24
51
  const TaroNormalModulesPlugin_1 = __importDefault(require("./TaroNormalModulesPlugin"));
25
52
  const TaroSingleEntryPlugin_1 = __importDefault(require("./TaroSingleEntryPlugin"));
26
- const baseCompName = 'comp';
27
- const customWrapperName = 'custom-wrapper';
53
+ exports.baseCompName = 'comp';
54
+ exports.customWrapperName = 'custom-wrapper';
28
55
  const PLUGIN_NAME = 'TaroMiniPlugin';
29
56
  const CHILD_COMPILER_TAG = 'child';
57
+ exports.STYLE_ISOLATION_APPLY_SHARED = 'apply-shared';
58
+ exports.STYLE_ISOLATION_SHARED = 'shared';
30
59
  function isLoaderExist(loaders, loaderName) {
31
60
  return loaders.some(item => item.loader === loaderName);
32
61
  }
@@ -46,6 +75,8 @@ class TaroMiniPlugin {
46
75
  this.dependencies = new Map();
47
76
  this.pageLoaderName = '@tarojs/taro-loader/lib/page';
48
77
  this.independentPackages = new Map();
78
+ this.subPackageIndiePlugin = null;
79
+ this.forceCustomWrapperDefineDefinitions = null;
49
80
  const { combination } = options;
50
81
  const miniBuildConfig = combination.config;
51
82
  const { template, baseLevel = 16, experimental } = miniBuildConfig;
@@ -69,6 +100,22 @@ class TaroMiniPlugin {
69
100
  hot: options.hot,
70
101
  loaderMeta: options.loaderMeta || {},
71
102
  };
103
+ // 初始化扩展钩子
104
+ this.hooks = {
105
+ modifyEntries: new tapable_1.SyncWaterfallHook(['entries']),
106
+ modifyChunkRequire: new tapable_1.SyncWaterfallHook(['result', 'chunkId', 'miniType']),
107
+ afterResolveModule: new tapable_1.SyncHook(['resolveData']),
108
+ compileExtraEntries: new tapable_1.SyncHook(['compiler', 'compilation', 'promises']),
109
+ modifySkipRootTemplates: new tapable_1.SyncWaterfallHook(['skip']),
110
+ generateExtraFiles: new tapable_1.SyncWaterfallHook(['customWrapperRoots', 'compilation', 'compiler']),
111
+ modifyPageConfig: new tapable_1.SyncWaterfallHook(['context', 'page', 'customWrapperRoots']),
112
+ modifyComponentConfig: new tapable_1.SyncHook(['context', 'customWrapperRoots']),
113
+ afterGenerateFiles: new tapable_1.SyncHook(['compilation', 'compiler']),
114
+ optimizeAssets: new tapable_1.SyncHook(['compilation']),
115
+ modifyConfig: new tapable_1.SyncWaterfallHook(['config', 'componentName']),
116
+ modifyStyleImport: new tapable_1.SyncWaterfallHook(['context', 'page']),
117
+ modifyShouldProcessStyles: new tapable_1.SyncWaterfallHook(['shouldProcess']),
118
+ };
72
119
  if (template.isSupportRecursive === false && baseLevel > 0) {
73
120
  template.baseLevel = baseLevel;
74
121
  }
@@ -99,11 +146,16 @@ class TaroMiniPlugin {
99
146
  apply(compiler) {
100
147
  this.context = compiler.context;
101
148
  this.appEntry = this.getAppEntry(compiler);
149
+ if (this.options.newBlended) {
150
+ this.subPackageIndiePlugin = new SubPackageIndiePlugin_1.default(this);
151
+ this.subPackageIndiePlugin.apply();
152
+ }
102
153
  const { commonChunks, combination, framework, isBuildPlugin, newBlended, } = this.options;
103
154
  const { addChunkPages, onCompilerMake, modifyBuildAssets, onParseCreateElement, } = combination.config;
104
155
  /** build mode */
105
156
  compiler.hooks.run.tapAsync(PLUGIN_NAME, this.tryAsync((compiler) => __awaiter(this, void 0, void 0, function* () {
106
157
  yield this.run(compiler);
158
+ this.applyForceCustomWrapperDefine(compiler);
107
159
  new TaroLoadChunksPlugin_1.default({
108
160
  commonChunks: commonChunks,
109
161
  isBuildPlugin,
@@ -119,6 +171,7 @@ class TaroMiniPlugin {
119
171
  this.isWatch = true;
120
172
  }
121
173
  yield this.run(compiler);
174
+ this.applyForceCustomWrapperDefine(compiler);
122
175
  if (!this.loadChunksPlugin) {
123
176
  this.loadChunksPlugin = new TaroLoadChunksPlugin_1.default({
124
177
  commonChunks: commonChunks,
@@ -135,6 +188,7 @@ class TaroMiniPlugin {
135
188
  const dependencies = this.dependencies;
136
189
  const promises = [];
137
190
  this.compileIndependentPages(compiler, compilation, dependencies, promises);
191
+ this.hooks.compileExtraEntries.call(compiler, compilation, promises);
138
192
  dependencies.forEach(dep => {
139
193
  promises.push(new Promise((resolve, reject) => {
140
194
  compilation.addEntry(this.options.sourceDir, dep, Object.assign({ name: dep.name }, dep.options), err => err ? reject(err) : resolve(null));
@@ -147,6 +201,11 @@ class TaroMiniPlugin {
147
201
  /** For Webpack compilation get factory from compilation.dependencyFactories by denpendence's constructor */
148
202
  compilation.dependencyFactories.set(EntryDependency_1.default, normalModuleFactory);
149
203
  compilation.dependencyFactories.set(TaroSingleEntryDependency_1.default, normalModuleFactory);
204
+ if (this.options.newBlended) {
205
+ normalModuleFactory.hooks.afterResolve.tap(PLUGIN_NAME, (resolveData) => {
206
+ this.hooks.afterResolveModule.call(resolveData);
207
+ });
208
+ }
150
209
  /**
151
210
  * webpack NormalModule 在 runLoaders 真正解析资源的前一刻,
152
211
  * 往 NormalModule.loaders 中插入对应的 Taro Loader
@@ -290,6 +349,10 @@ class TaroMiniPlugin {
290
349
  let source;
291
350
  const id = (0, webpack_1.getChunkIdOrName)(chunk);
292
351
  const { miniType } = entryModule;
352
+ const modifyResult = this.hooks.modifyChunkRequire.call({ source: modules, handled: false }, id, miniType);
353
+ if (modifyResult === null || modifyResult === void 0 ? void 0 : modifyResult.handled) {
354
+ return modifyResult.source;
355
+ }
293
356
  const entryChunk = [{ name: 'app' }];
294
357
  // 所有模块都依赖app.js,确保@tarojs\plugin-platform-xxx\dist\runtime.js先于@tarojs/runtime执行,避免Taro API未被初始化
295
358
  if (this.nativeComponents.has(id) || miniType === helper_1.META_TYPE.STATIC) {
@@ -298,7 +361,8 @@ class TaroMiniPlugin {
298
361
  source = (0, webpack_1.addRequireToSource)(id, modules, v);
299
362
  }
300
363
  });
301
- return source;
364
+ // 如果没有依赖需要注入,返回原始模块
365
+ return source || modules;
302
366
  }
303
367
  else if (miniType === helper_1.META_TYPE.PAGE) {
304
368
  return (0, webpack_1.addRequireToSource)(id, modules, entryChunk);
@@ -352,6 +416,8 @@ class TaroMiniPlugin {
352
416
  */
353
417
  run(compiler) {
354
418
  return __awaiter(this, void 0, void 0, function* () {
419
+ var _a;
420
+ (_a = this.subPackageIndiePlugin) === null || _a === void 0 ? void 0 : _a.invalidateRunCache();
355
421
  if (this.options.isBuildPlugin) {
356
422
  this.getPluginFiles();
357
423
  this.getConfigFiles(compiler);
@@ -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 });
@@ -664,6 +740,39 @@ class TaroMiniPlugin {
664
740
  }
665
741
  });
666
742
  }
743
+ computeForceCustomWrapperForWebpackMainComp() {
744
+ var _a;
745
+ if (this.options.template.isSupportRecursive)
746
+ return false;
747
+ const independentPaths = new Set();
748
+ this.independentPackages.forEach(({ pages }) => {
749
+ pages.forEach(p => independentPaths.add(p));
750
+ });
751
+ for (const page of this.pages) {
752
+ if (page.isNative || independentPaths.has(page.path))
753
+ continue;
754
+ const content = (_a = this.filesConfig[this.getConfigFilePath(page.name)]) === null || _a === void 0 ? void 0 : _a.content;
755
+ if (content === null || content === void 0 ? void 0 : content.forceCustomWrapper)
756
+ return true;
757
+ }
758
+ return false;
759
+ }
760
+ computeForceCustomWrapperForIndieRoot(pages) {
761
+ return (0, forceCustomWrapper_1.computeForceCustomWrapperForIndependentPackage)(pages, this.pages, this.filesConfig, (p) => this.getConfigFilePath(p));
762
+ }
763
+ applyForceCustomWrapperDefine(compiler) {
764
+ if (this.options.template.isSupportRecursive)
765
+ return;
766
+ const value = JSON.stringify(this.computeForceCustomWrapperForWebpackMainComp());
767
+ if (!this.forceCustomWrapperDefineDefinitions) {
768
+ this.forceCustomWrapperDefineDefinitions = {
769
+ TARO_FORCE_CUSTOM_WRAPPER: value
770
+ };
771
+ new compiler.webpack.DefinePlugin(this.forceCustomWrapperDefineDefinitions).apply(compiler);
772
+ return;
773
+ }
774
+ this.forceCustomWrapperDefineDefinitions.TARO_FORCE_CUSTOM_WRAPPER = value;
775
+ }
667
776
  replaceExt(file, ext) {
668
777
  return node_path_1.default.join(node_path_1.default.dirname(file), node_path_1.default.basename(file, node_path_1.default.extname(file)) + `${ext}`);
669
778
  }
@@ -823,7 +932,7 @@ class TaroMiniPlugin {
823
932
  filename: `[name]${this.options.fileType.style}`,
824
933
  chunkFilename: `[name]${this.options.fileType.style}`
825
934
  }).apply(childCompiler);
826
- new compiler.webpack.DefinePlugin(this.options.constantsReplaceList).apply(childCompiler);
935
+ new compiler.webpack.DefinePlugin(Object.assign(Object.assign({}, this.options.constantsReplaceList), { TARO_FORCE_CUSTOM_WRAPPER: JSON.stringify(this.computeForceCustomWrapperForIndieRoot(pages)) })).apply(childCompiler);
827
936
  if (compiler.options.optimization) {
828
937
  new SplitChunksPlugin({
829
938
  chunks: 'all',
@@ -962,6 +1071,12 @@ class TaroMiniPlugin {
962
1071
  usingComponents[key] = match ? `${node_path_1.default.sep}${match[0]}` : compPath;
963
1072
  }
964
1073
  }
1074
+ cloneThirdPartyComponent(thirdPartyComponents, componentName) {
1075
+ const attrs = component_1.componentConfig.thirdPartyComponents.get(componentName);
1076
+ if (!attrs || thirdPartyComponents.has(componentName))
1077
+ return;
1078
+ thirdPartyComponents.set(componentName, new Set(attrs));
1079
+ }
965
1080
  /** 生成小程序独立分包的相关文件 */
966
1081
  generateIndependentMiniFiles(compilation, compiler) {
967
1082
  return __awaiter(this, void 0, void 0, function* () {
@@ -978,24 +1093,27 @@ class TaroMiniPlugin {
978
1093
  this.generateTemplateFile(compilation, compiler, `${name}/${baseTemplateName}`, template.buildTemplate, component_1.componentConfig);
979
1094
  if (!template.isSupportRecursive) {
980
1095
  // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
981
- this.generateConfigFile(compilation, compiler, `${name}/${baseCompName}`, {
1096
+ const compConfig = {
982
1097
  component: true,
983
- styleIsolation: 'apply-shared',
1098
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
984
1099
  usingComponents: {
985
- [baseCompName]: `./${baseCompName}`,
986
- [customWrapperName]: `./${customWrapperName}`
1100
+ [exports.baseCompName]: `./${exports.baseCompName}`
987
1101
  }
988
- });
989
- this.generateTemplateFile(compilation, compiler, `${name}/${baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
1102
+ };
1103
+ if (isUsingCustomWrapper) {
1104
+ compConfig.usingComponents[exports.customWrapperName] = `./${exports.customWrapperName}`;
1105
+ }
1106
+ this.generateConfigFile(compilation, compiler, `${name}/${exports.baseCompName}`, compConfig);
1107
+ this.generateTemplateFile(compilation, compiler, `${name}/${exports.baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
990
1108
  }
991
- this.generateConfigFile(compilation, compiler, `${name}/${customWrapperName}`, {
1109
+ this.generateConfigFile(compilation, compiler, `${name}/${exports.customWrapperName}`, {
992
1110
  component: true,
993
- styleIsolation: 'apply-shared',
1111
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
994
1112
  usingComponents: {
995
- [customWrapperName]: `./${customWrapperName}`
1113
+ [exports.customWrapperName]: `./${exports.customWrapperName}`
996
1114
  }
997
1115
  });
998
- this.generateTemplateFile(compilation, compiler, `${name}/${customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
1116
+ this.generateTemplateFile(compilation, compiler, `${name}/${exports.customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
999
1117
  this.generateXSFile(compilation, compiler, `${name}/utils`);
1000
1118
  });
1001
1119
  this.pages.forEach(page => {
@@ -1018,14 +1136,14 @@ class TaroMiniPlugin {
1018
1136
  this.generateTemplateFile(compilation, compiler, page.path, template.buildPageTemplate, importBaseTemplatePath, config);
1019
1137
  }
1020
1138
  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, ''))));
1139
+ const importBaseCompPath = (0, helper_1.promoteRelativePath)(node_path_1.default.relative(page.path, node_path_1.default.join(sourceDir, independentName, this.getTargetFilePath(exports.baseCompName, ''))));
1140
+ 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
1141
  config.content.usingComponents = Object.assign({}, config.content.usingComponents);
1024
1142
  if (isUsingCustomWrapper) {
1025
- config.content.usingComponents[customWrapperName] = importCustomWrapperPath;
1143
+ config.content.usingComponents[exports.customWrapperName] = importCustomWrapperPath;
1026
1144
  }
1027
1145
  if (!template.isSupportRecursive && !page.isNative) {
1028
- config.content.usingComponents[baseCompName] = importBaseCompPath;
1146
+ config.content.usingComponents[exports.baseCompName] = importBaseCompPath;
1029
1147
  }
1030
1148
  this.generateConfigFile(compilation, compiler, page.path, config.content);
1031
1149
  }
@@ -1040,6 +1158,8 @@ class TaroMiniPlugin {
1040
1158
  const { modifyMiniConfigs } = combination.config;
1041
1159
  const baseTemplateName = 'base';
1042
1160
  const isUsingCustomWrapper = component_1.componentConfig.thirdPartyComponents.has('custom-wrapper');
1161
+ let subPackageIndieCustomWrapperRoots = new Set();
1162
+ compilation[SubPackageIndiePlugin_1.subPackageIndieCustomWrapperRootsKey] = subPackageIndieCustomWrapperRoots;
1043
1163
  /**
1044
1164
  * 与原生小程序混写时解析模板与样式
1045
1165
  */
@@ -1059,47 +1179,55 @@ class TaroMiniPlugin {
1059
1179
  const appConfigName = node_path_1.default.basename(appConfigPath).replace(node_path_1.default.extname(appConfigPath), '');
1060
1180
  this.generateConfigFile(compilation, compiler, this.appEntry, this.filesConfig[appConfigName].content);
1061
1181
  }
1062
- if (!template.isSupportRecursive) {
1182
+ let skipRootTemplates = false;
1183
+ skipRootTemplates = this.hooks.modifySkipRootTemplates.call(skipRootTemplates);
1184
+ if (!template.isSupportRecursive && !skipRootTemplates) {
1063
1185
  // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
1064
- this.generateTemplateFile(compilation, compiler, baseCompName, template.buildBaseComponentTemplate, this.options.fileType.templ);
1186
+ this.generateTemplateFile(compilation, compiler, exports.baseCompName, template.buildBaseComponentTemplate, this.options.fileType.templ);
1065
1187
  const baseCompConfig = {
1066
1188
  component: true,
1067
- styleIsolation: 'apply-shared',
1189
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
1068
1190
  usingComponents: {
1069
- [baseCompName]: `./${baseCompName}`
1191
+ [exports.baseCompName]: `./${exports.baseCompName}`
1070
1192
  }
1071
1193
  };
1072
1194
  if (isUsingCustomWrapper) {
1073
- baseCompConfig.usingComponents[customWrapperName] = `./${customWrapperName}`;
1074
- this.generateConfigFile(compilation, compiler, customWrapperName, {
1195
+ baseCompConfig.usingComponents[exports.customWrapperName] = `./${exports.customWrapperName}`;
1196
+ this.generateConfigFile(compilation, compiler, exports.customWrapperName, {
1075
1197
  component: true,
1076
- styleIsolation: 'apply-shared',
1198
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
1077
1199
  usingComponents: {
1078
- [baseCompName]: `./${baseCompName}`,
1079
- [customWrapperName]: `./${customWrapperName}`
1200
+ [exports.baseCompName]: `./${exports.baseCompName}`,
1201
+ [exports.customWrapperName]: `./${exports.customWrapperName}`
1080
1202
  }
1081
1203
  });
1082
1204
  }
1083
- this.generateConfigFile(compilation, compiler, baseCompName, baseCompConfig);
1205
+ this.generateConfigFile(compilation, compiler, exports.baseCompName, baseCompConfig);
1084
1206
  }
1085
- else {
1207
+ else if (!skipRootTemplates) {
1086
1208
  if (isUsingCustomWrapper) {
1087
- this.generateConfigFile(compilation, compiler, customWrapperName, {
1209
+ this.generateConfigFile(compilation, compiler, exports.customWrapperName, {
1088
1210
  component: true,
1089
- styleIsolation: 'apply-shared',
1211
+ styleIsolation: exports.STYLE_ISOLATION_APPLY_SHARED,
1090
1212
  usingComponents: {
1091
- [customWrapperName]: `./${customWrapperName}`
1213
+ [exports.customWrapperName]: `./${exports.customWrapperName}`
1092
1214
  }
1093
1215
  });
1094
1216
  }
1095
1217
  }
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');
1218
+ // 当配置了 mainPackageRoot 时,跳过根目录 base.wxml 和 utils.wxs 的生成
1219
+ if (!skipRootTemplates) {
1220
+ this.generateTemplateFile(compilation, compiler, baseTemplateName, template.buildTemplate, component_1.componentConfig);
1221
+ isUsingCustomWrapper && this.generateTemplateFile(compilation, compiler, exports.customWrapperName, template.buildCustomComponentTemplate, this.options.fileType.templ);
1222
+ this.generateXSFile(compilation, compiler, 'utils');
1223
+ }
1224
+ subPackageIndieCustomWrapperRoots = this.hooks.generateExtraFiles.call(subPackageIndieCustomWrapperRoots, compilation, compiler);
1225
+ compilation[SubPackageIndiePlugin_1.subPackageIndieCustomWrapperRootsKey] = subPackageIndieCustomWrapperRoots;
1099
1226
  this.components.forEach(component => {
1100
1227
  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
1228
  const config = this.filesConfig[this.getConfigFilePath(component.name)];
1102
1229
  if (config) {
1230
+ this.hooks.modifyComponentConfig.call({ config, component }, subPackageIndieCustomWrapperRoots);
1103
1231
  this.generateConfigFile(compilation, compiler, component.path, config.content);
1104
1232
  }
1105
1233
  if (!component.isNative) {
@@ -1107,31 +1235,46 @@ class TaroMiniPlugin {
1107
1235
  }
1108
1236
  });
1109
1237
  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))));
1238
+ 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
1239
  const config = this.filesConfig[this.getConfigFilePath(page.name)];
1112
1240
  // pages 里面会混合独立分包的,在这里需要过滤一下,避免重复生成 assets
1113
1241
  const isIndependent = !!this.getIndependentPackage(page.path);
1114
1242
  if (isIndependent)
1115
1243
  return;
1116
1244
  // 生成页面模板需要在生成页面配置之前,因为会依赖到页面配置的部分内容
1245
+ 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, ''))));
1246
+ 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, ''))));
1247
+ let isPageRecursiveDisabled = false;
1248
+ let shouldUseCustomWrapper = isUsingCustomWrapper;
1249
+ const pageConfigContext = this.hooks.modifyPageConfig.call({
1250
+ importBaseTemplatePath,
1251
+ importBaseCompPath,
1252
+ importCustomWrapperPath,
1253
+ shouldUseCustomWrapper,
1254
+ isPageRecursiveDisabled,
1255
+ }, page, subPackageIndieCustomWrapperRoots);
1256
+ importBaseTemplatePath = pageConfigContext.importBaseTemplatePath;
1257
+ importBaseCompPath = pageConfigContext.importBaseCompPath;
1258
+ importCustomWrapperPath = pageConfigContext.importCustomWrapperPath;
1259
+ shouldUseCustomWrapper = pageConfigContext.shouldUseCustomWrapper;
1260
+ isPageRecursiveDisabled = pageConfigContext.isPageRecursiveDisabled;
1117
1261
  if (!page.isNative) {
1118
1262
  this.generateTemplateFile(compilation, compiler, page.path, template.buildPageTemplate, importBaseTemplatePath, config);
1119
1263
  }
1120
1264
  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
1265
  config.content.usingComponents = Object.assign({}, config.content.usingComponents);
1124
- if (isUsingCustomWrapper) {
1125
- config.content.usingComponents[customWrapperName] = importCustomWrapperPath;
1266
+ if (shouldUseCustomWrapper) {
1267
+ config.content.usingComponents[exports.customWrapperName] = importCustomWrapperPath;
1126
1268
  }
1127
- if (!template.isSupportRecursive && !page.isNative) {
1128
- config.content.usingComponents[baseCompName] = importBaseCompPath;
1269
+ if (!template.isSupportRecursive && !page.isNative && !isPageRecursiveDisabled) {
1270
+ config.content.usingComponents[exports.baseCompName] = importBaseCompPath;
1129
1271
  }
1130
1272
  this.generateConfigFile(compilation, compiler, page.path, config.content);
1131
1273
  }
1132
1274
  });
1133
1275
  this.generateTabBarFiles(compilation, compiler);
1134
1276
  this.injectCommonStyles(compilation, compiler);
1277
+ this.hooks.afterGenerateFiles.call(compilation, compiler);
1135
1278
  if (this.themeLocation) {
1136
1279
  this.generateDarkModeFile(compilation, compiler);
1137
1280
  }
@@ -1164,12 +1307,15 @@ class TaroMiniPlugin {
1164
1307
  delete compilation.assets[assetPath];
1165
1308
  }
1166
1309
  });
1310
+ this.hooks.optimizeAssets.call(compilation);
1167
1311
  });
1168
1312
  }
1169
1313
  generateConfigFile(compilation, compiler, filePath, config) {
1170
1314
  const { RawSource } = compiler.webpack.sources;
1171
1315
  const fileConfigName = this.getConfigPath(this.getComponentName(filePath));
1172
- const unofficialConfigs = ['enableShareAppMessage', 'enableShareTimeline', 'enablePageMeta', 'components'];
1316
+ const componentName = this.getComponentName(filePath);
1317
+ config = this.hooks.modifyConfig.call(config, componentName);
1318
+ const unofficialConfigs = ['enableShareAppMessage', 'enableShareTimeline', 'enablePageMeta', 'components', 'forceCustomWrapper'];
1173
1319
  unofficialConfigs.forEach(item => {
1174
1320
  delete config[item];
1175
1321
  });
@@ -1315,12 +1461,21 @@ class TaroMiniPlugin {
1315
1461
  });
1316
1462
  });
1317
1463
  }
1318
- if (commons.size() > 0) {
1464
+ // 判断是否需要处理样式:有 common chunks 或者有 app.wxss(可被扩展插件修改)
1465
+ const hasAppStyle = !!assets[appStyle];
1466
+ let shouldProcessStyles = commons.size() > 0;
1467
+ if (!hasAppStyle) {
1468
+ shouldProcessStyles = false;
1469
+ }
1470
+ shouldProcessStyles = this.hooks.modifyShouldProcessStyles.call(shouldProcessStyles);
1471
+ if (shouldProcessStyles) {
1319
1472
  const APP_STYLE_NAME = 'app-origin' + styleExt;
1320
1473
  assets[APP_STYLE_NAME] = new ConcatSource(originSource);
1321
1474
  const source = new ConcatSource('');
1322
1475
  source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(APP_STYLE_NAME))};`);
1323
- source.add(commons);
1476
+ if (commons.size() > 0) {
1477
+ source.add(commons);
1478
+ }
1324
1479
  source.add('\n');
1325
1480
  assets[appStyle] = source;
1326
1481
  if (newBlended) {
@@ -1336,14 +1491,43 @@ class TaroMiniPlugin {
1336
1491
  }
1337
1492
  const source = new ConcatSource('');
1338
1493
  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
- });
1494
+ const styleImportContext = this.hooks.modifyStyleImport.call({
1495
+ importStatement: '',
1496
+ shouldSkip: false,
1497
+ isMainPackageRoot: false,
1498
+ }, page);
1499
+ if (styleImportContext.shouldSkip) {
1500
+ // no-op
1501
+ }
1502
+ else if (styleImportContext.importStatement) {
1503
+ source.add(styleImportContext.importStatement);
1504
+ }
1505
+ else {
1506
+ componentCommons.forEach(item => {
1507
+ source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(node_path_1.default.posix.relative(node_path_1.default.dirname(pageStyle), item)))};\n`);
1508
+ });
1509
+ }
1342
1510
  source.add(originSource);
1343
1511
  assets[pageStyle] = source;
1344
1512
  }
1345
1513
  else {
1346
- if (pageStyle in assets) {
1514
+ // 普通页面(非本地化组件)
1515
+ const styleImportContext = this.hooks.modifyStyleImport.call({
1516
+ importStatement: '',
1517
+ shouldSkip: false,
1518
+ isMainPackageRoot: false,
1519
+ }, page);
1520
+ if (styleImportContext.importStatement) {
1521
+ const source = new ConcatSource('');
1522
+ if (pageStyle in assets) {
1523
+ source.add(assets[pageStyle]);
1524
+ }
1525
+ const importSource = new ConcatSource('');
1526
+ importSource.add(styleImportContext.importStatement);
1527
+ importSource.add(source);
1528
+ assets[pageStyle] = importSource;
1529
+ }
1530
+ else if (pageStyle in assets) {
1347
1531
  const source = new ConcatSource('');
1348
1532
  const originSource = assets[pageStyle];
1349
1533
  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`);