@tarojs/webpack5-runner 3.7.0-alpha.7 → 3.7.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/index.h5.js +4 -1
  2. package/dist/index.h5.js.map +1 -1
  3. package/dist/plugins/H5Plugin.js +1 -6
  4. package/dist/plugins/H5Plugin.js.map +1 -1
  5. package/dist/plugins/MiniPlugin.js +283 -54
  6. package/dist/plugins/MiniPlugin.js.map +1 -1
  7. package/dist/plugins/MiniSplitChunksPlugin.js +96 -90
  8. package/dist/plugins/MiniSplitChunksPlugin.js.map +1 -1
  9. package/dist/plugins/TaroComponentsExportsPlugin.js +27 -10
  10. package/dist/plugins/TaroComponentsExportsPlugin.js.map +1 -1
  11. package/dist/plugins/TaroLoadChunksPlugin.js +2 -2
  12. package/dist/plugins/TaroLoadChunksPlugin.js.map +1 -1
  13. package/dist/plugins/TaroNormalModule.js +79 -1
  14. package/dist/plugins/TaroNormalModule.js.map +1 -1
  15. package/dist/plugins/TaroNormalModulesPlugin.js +69 -31
  16. package/dist/plugins/TaroNormalModulesPlugin.js.map +1 -1
  17. package/dist/plugins/TaroSingleEntryPlugin.js +6 -5
  18. package/dist/plugins/TaroSingleEntryPlugin.js.map +1 -1
  19. package/dist/utils/component.js +4 -1
  20. package/dist/utils/component.js.map +1 -1
  21. package/dist/webpack/Combination.js +10 -7
  22. package/dist/webpack/Combination.js.map +1 -1
  23. package/dist/webpack/H5Combination.js +0 -1
  24. package/dist/webpack/H5Combination.js.map +1 -1
  25. package/dist/webpack/MiniCombination.js +0 -1
  26. package/dist/webpack/MiniCombination.js.map +1 -1
  27. package/dist/webpack/MiniWebpackModule.js +2 -2
  28. package/dist/webpack/MiniWebpackModule.js.map +1 -1
  29. package/dist/webpack/MiniWebpackPlugin.js +3 -1
  30. package/dist/webpack/MiniWebpackPlugin.js.map +1 -1
  31. package/dist/webpack/WebpackModule.js +3 -0
  32. package/dist/webpack/WebpackModule.js.map +1 -1
  33. package/package.json +19 -19
@@ -19,10 +19,14 @@ const EntryDependency_1 = __importDefault(require("webpack/lib/dependencies/Entr
19
19
  const TaroSingleEntryDependency_1 = __importDefault(require("../dependencies/TaroSingleEntryDependency"));
20
20
  const prerender_1 = require("../prerender/prerender");
21
21
  const component_1 = require("../utils/component");
22
+ const webpack_1 = require("../utils/webpack");
22
23
  const TaroLoadChunksPlugin_1 = __importDefault(require("./TaroLoadChunksPlugin"));
23
24
  const TaroNormalModulesPlugin_1 = __importDefault(require("./TaroNormalModulesPlugin"));
24
25
  const TaroSingleEntryPlugin_1 = __importDefault(require("./TaroSingleEntryPlugin"));
26
+ const baseCompName = 'comp';
27
+ const customWrapperName = 'custom-wrapper';
25
28
  const PLUGIN_NAME = 'TaroMiniPlugin';
29
+ const CHILD_COMPILER_TAG = 'child';
26
30
  function isLoaderExist(loaders, loaderName) {
27
31
  return loaders.some(item => item.loader === loaderName);
28
32
  }
@@ -34,6 +38,8 @@ class TaroMiniPlugin {
34
38
  /** 页面列表 */
35
39
  this.pages = new Set();
36
40
  this.components = new Set();
41
+ /** 新的混合原生编译模式 newBlended 模式下,需要单独编译成原生代码的 component 的Map */
42
+ this.nativeComponents = new Map();
37
43
  /** tabbar icon 图片路径列表 */
38
44
  this.tabBarIcons = new Set();
39
45
  this.dependencies = new Map();
@@ -80,7 +86,7 @@ class TaroMiniPlugin {
80
86
  apply(compiler) {
81
87
  this.context = compiler.context;
82
88
  this.appEntry = this.getAppEntry(compiler);
83
- const { commonChunks, addChunkPages, framework, isBuildPlugin } = this.options;
89
+ const { commonChunks, addChunkPages, framework, isBuildPlugin, newBlended, } = this.options;
84
90
  /** build mode */
85
91
  compiler.hooks.run.tapAsync(PLUGIN_NAME, this.tryAsync((compiler) => __awaiter(this, void 0, void 0, function* () {
86
92
  yield this.run(compiler);
@@ -146,6 +152,7 @@ class TaroMiniPlugin {
146
152
  config: this.appConfig,
147
153
  runtimePath: this.options.runtimePath,
148
154
  blended: this.options.blended,
155
+ newBlended: this.options.newBlended,
149
156
  pxTransformConfig
150
157
  }
151
158
  });
@@ -158,13 +165,19 @@ class TaroMiniPlugin {
158
165
  isIndependent = true;
159
166
  }
160
167
  });
161
- const loaderName = isBuildPlugin ? '@tarojs/taro-loader/lib/native-component' : (isIndependent ? '@tarojs/taro-loader/lib/independentPage' : this.pageLoaderName);
168
+ const isNewBlended = this.nativeComponents.has(module.name);
169
+ const loaderName = (isNewBlended || isBuildPlugin)
170
+ ? '@tarojs/taro-loader/lib/native-component'
171
+ : (isIndependent
172
+ ? '@tarojs/taro-loader/lib/independentPage'
173
+ : this.pageLoaderName);
162
174
  if (!isLoaderExist(module.loaders, loaderName)) {
163
175
  module.loaders.unshift({
164
176
  loader: loaderName,
165
177
  options: {
166
178
  framework,
167
179
  loaderMeta,
180
+ isNewBlended,
168
181
  name: module.name,
169
182
  prerender: this.prerenderPages.has(module.name),
170
183
  config: this.filesConfig,
@@ -191,12 +204,18 @@ class TaroMiniPlugin {
191
204
  }
192
205
  }
193
206
  });
194
- const { PROCESS_ASSETS_STAGE_ADDITIONAL, PROCESS_ASSETS_STAGE_OPTIMIZE } = compiler.webpack.Compilation;
207
+ const { PROCESS_ASSETS_STAGE_ADDITIONAL, PROCESS_ASSETS_STAGE_OPTIMIZE, PROCESS_ASSETS_STAGE_REPORT } = compiler.webpack.Compilation;
195
208
  compilation.hooks.processAssets.tapAsync({
196
209
  name: PLUGIN_NAME,
197
210
  stage: PROCESS_ASSETS_STAGE_ADDITIONAL
198
211
  }, this.tryAsync(() => __awaiter(this, void 0, void 0, function* () {
199
- yield this.generateMiniFiles(compilation, compiler);
212
+ // 如果是子编译器,证明是编译独立分包,进行单独的处理
213
+ if (compilation.__tag === CHILD_COMPILER_TAG) {
214
+ yield this.generateIndependentMiniFiles(compilation, compiler);
215
+ }
216
+ else {
217
+ yield this.generateMiniFiles(compilation, compiler);
218
+ }
200
219
  })));
201
220
  compilation.hooks.processAssets.tapAsync({
202
221
  name: PLUGIN_NAME,
@@ -206,11 +225,72 @@ class TaroMiniPlugin {
206
225
  }, this.tryAsync(() => __awaiter(this, void 0, void 0, function* () {
207
226
  yield this.optimizeMiniFiles(compilation, compiler);
208
227
  })));
228
+ compilation.hooks.processAssets.tapAsync({
229
+ name: PLUGIN_NAME,
230
+ // 该 stage 是最后执行的,确保 taro 暴露给用户的钩子 modifyBuildAssets 在内部处理完 assets 之后再调用
231
+ stage: PROCESS_ASSETS_STAGE_REPORT
232
+ }, this.tryAsync(() => __awaiter(this, void 0, void 0, function* () {
233
+ const { modifyBuildAssets } = this.options;
234
+ if (typeof modifyBuildAssets === 'function') {
235
+ yield modifyBuildAssets(compilation.assets, this);
236
+ }
237
+ })));
209
238
  });
210
239
  compiler.hooks.afterEmit.tapAsync(PLUGIN_NAME, this.tryAsync((compilation) => __awaiter(this, void 0, void 0, function* () {
211
240
  yield this.addTarBarFilesToDependencies(compilation);
212
241
  })));
213
242
  new TaroNormalModulesPlugin_1.default(this.options.onParseCreateElement).apply(compiler);
243
+ newBlended && this.addLoadChunksPlugin(compiler);
244
+ }
245
+ addLoadChunksPlugin(compiler) {
246
+ const fileChunks = new Map();
247
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
248
+ compilation.hooks.afterOptimizeChunks.tap(PLUGIN_NAME, chunks => {
249
+ for (const chunk of chunks) {
250
+ const id = (0, webpack_1.getChunkIdOrName)(chunk);
251
+ if (this.options.commonChunks.includes(id))
252
+ return;
253
+ const deps = [];
254
+ for (const group of chunk.groupsIterable) {
255
+ group.chunks.forEach(chunk => {
256
+ const currentChunkId = (0, webpack_1.getChunkIdOrName)(chunk);
257
+ if (id === currentChunkId)
258
+ return;
259
+ deps.push({
260
+ name: currentChunkId
261
+ });
262
+ });
263
+ }
264
+ fileChunks.set(id, deps);
265
+ }
266
+ });
267
+ compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation).render.tap(PLUGIN_NAME, (modules, { chunk }) => {
268
+ var _a;
269
+ const chunkEntryModule = (0, webpack_1.getChunkEntryModule)(compilation, chunk);
270
+ if (!chunkEntryModule)
271
+ return;
272
+ const entryModule = (_a = chunkEntryModule.rootModule) !== null && _a !== void 0 ? _a : chunkEntryModule;
273
+ // addChunkPages
274
+ if (fileChunks.size) {
275
+ let source;
276
+ const id = (0, webpack_1.getChunkIdOrName)(chunk);
277
+ const { miniType } = entryModule;
278
+ const entryChunk = [{ name: 'app' }];
279
+ // 所有模块都依赖app.js,确保@tarojs\plugin-platform-xxx\dist\runtime.js先于@tarojs/runtime执行,避免Taro API未被初始化
280
+ if (this.nativeComponents.has(id) || miniType === helper_1.META_TYPE.STATIC) {
281
+ fileChunks.forEach((v, k) => {
282
+ if (k === id) {
283
+ source = (0, webpack_1.addRequireToSource)(id, modules, v);
284
+ }
285
+ });
286
+ return source;
287
+ }
288
+ else if (miniType === helper_1.META_TYPE.PAGE) {
289
+ return (0, webpack_1.addRequireToSource)(id, modules, entryChunk);
290
+ }
291
+ }
292
+ });
293
+ });
214
294
  }
215
295
  /**
216
296
  * 根据 webpack entry 配置获取入口文件路径
@@ -339,7 +419,7 @@ class TaroMiniPlugin {
339
419
  }
340
420
  this.compileFile(item);
341
421
  if (item.isNative) {
342
- this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL);
422
+ this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL, { isNativePage: true });
343
423
  if (item.stylePath && helper_1.fs.existsSync(item.stylePath)) {
344
424
  this.addEntry(item.stylePath, this.getStylePath(item.name), helper_1.META_TYPE.NORMAL);
345
425
  }
@@ -354,7 +434,7 @@ class TaroMiniPlugin {
354
434
  this.components.forEach(item => {
355
435
  this.compileFile(item);
356
436
  if (item.isNative) {
357
- this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL);
437
+ this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL, { isNativePage: true });
358
438
  if (item.stylePath && helper_1.fs.existsSync(item.stylePath)) {
359
439
  this.addEntry(item.stylePath, this.getStylePath(item.name), helper_1.META_TYPE.NORMAL);
360
440
  }
@@ -373,9 +453,14 @@ class TaroMiniPlugin {
373
453
  if (main) {
374
454
  pluginJSON.main = this.getTargetFilePath(main, '.js');
375
455
  }
376
- if (publicComponents && isUsingCustomWrapper) {
456
+ if (!this.options.template.isSupportRecursive) {
377
457
  pluginJSON.publicComponents = Object.assign({}, publicComponents, {
378
- 'custom-wrapper': 'custom-wrapper'
458
+ [baseCompName]: baseCompName
459
+ });
460
+ }
461
+ if (isUsingCustomWrapper) {
462
+ pluginJSON.publicComponents = Object.assign({}, publicComponents, {
463
+ [customWrapperName]: customWrapperName
379
464
  });
380
465
  }
381
466
  }
@@ -403,6 +488,7 @@ class TaroMiniPlugin {
403
488
  */
404
489
  getPages() {
405
490
  var _a;
491
+ const { newBlended } = this.options;
406
492
  if ((0, helper_1.isEmptyObject)(this.appConfig)) {
407
493
  throw new Error('缺少 app 全局配置文件,请检查!');
408
494
  }
@@ -431,6 +517,30 @@ class TaroMiniPlugin {
431
517
  })
432
518
  ]);
433
519
  this.getSubPackages(this.appConfig);
520
+ // 新的混合原生编译模式 newBlended 下,需要收集独立编译为原生自定义组件
521
+ newBlended && this.getNativeComponent();
522
+ }
523
+ /**
524
+ * 收集需要转换为本地化组件的内容
525
+ */
526
+ getNativeComponent() {
527
+ const { frameworkExts } = this.options;
528
+ const components = this.appConfig.components || [];
529
+ components.forEach(item => {
530
+ var _a;
531
+ const pagePath = (0, helper_1.resolveMainFilePath)(path_1.default.join(this.options.sourceDir, item), frameworkExts);
532
+ const componentObj = {
533
+ name: item,
534
+ path: pagePath,
535
+ isNative: false,
536
+ };
537
+ if (!this.isWatch && ((_a = this.options.logger) === null || _a === void 0 ? void 0 : _a.quiet) === false) {
538
+ (0, helper_1.printLog)("compile" /* processTypeEnum.COMPILE */, `发现[${item}]Native组件`);
539
+ }
540
+ this.pages.add(componentObj);
541
+ // 登记需要编译成原生版本的组件
542
+ this.nativeComponents.set(item, componentObj);
543
+ });
434
544
  }
435
545
  /**
436
546
  * 读取页面及其依赖的组件的配置
@@ -497,7 +607,7 @@ class TaroMiniPlugin {
497
607
  this.addEntry(path_1.default.resolve(__dirname, '..', 'template/custom-wrapper'), 'custom-wrapper', helper_1.META_TYPE.STATIC);
498
608
  this.pages.forEach(item => {
499
609
  if (item.isNative) {
500
- this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL);
610
+ this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL, { isNativePage: true });
501
611
  if (item.stylePath && helper_1.fs.existsSync(item.stylePath)) {
502
612
  this.addEntry(item.stylePath, this.getStylePath(item.name), helper_1.META_TYPE.NORMAL);
503
613
  }
@@ -511,7 +621,7 @@ class TaroMiniPlugin {
511
621
  });
512
622
  this.components.forEach(item => {
513
623
  if (item.isNative) {
514
- this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL);
624
+ this.addEntry(item.path, item.name, helper_1.META_TYPE.NORMAL, { isNativePage: true });
515
625
  if (item.stylePath && helper_1.fs.existsSync(item.stylePath)) {
516
626
  this.addEntry(item.stylePath, this.getStylePath(item.name), helper_1.META_TYPE.NORMAL);
517
627
  }
@@ -534,7 +644,17 @@ class TaroMiniPlugin {
534
644
  const filePath = file.path;
535
645
  const fileConfigPath = file.isNative ? this.replaceExt(filePath, '.json') : this.getConfigFilePath(filePath);
536
646
  const fileConfig = (0, helper_1.readConfig)(fileConfigPath);
647
+ const { componentGenerics } = fileConfig;
537
648
  const usingComponents = fileConfig.usingComponents;
649
+ if (this.options.isBuildPlugin && componentGenerics) {
650
+ Object.keys(componentGenerics).forEach(component => {
651
+ if (componentGenerics[component]) {
652
+ if (!component_1.componentConfig.thirdPartyComponents.has(component)) {
653
+ component_1.componentConfig.thirdPartyComponents.set(component, new Set());
654
+ }
655
+ }
656
+ });
657
+ }
538
658
  // 递归收集依赖的第三方组件
539
659
  if (usingComponents) {
540
660
  const componentNames = Object.keys(usingComponents);
@@ -546,6 +666,17 @@ class TaroMiniPlugin {
546
666
  compPath = (0, helper_1.replaceAliasPath)(filePath, compPath, alias);
547
667
  fileConfig.usingComponents[compName] = compPath;
548
668
  }
669
+ // 判断是否为第三方依赖的正则,如果 test 为 false 则为第三方依赖
670
+ const notNpmPkgReg = /^[.\\/]/;
671
+ if (!this.options.skipProcessUsingComponents
672
+ && !compPath.startsWith('plugin://')
673
+ && !notNpmPkgReg.test(compPath)) {
674
+ const tempCompPath = (0, helper_1.getNpmPackageAbsolutePath)(compPath);
675
+ if (tempCompPath) {
676
+ compPath = tempCompPath;
677
+ fileConfig.usingComponents[compName] = compPath;
678
+ }
679
+ }
549
680
  depComponents.push({
550
681
  name: compName,
551
682
  path: compPath
@@ -558,6 +689,9 @@ class TaroMiniPlugin {
558
689
  const componentPath = (0, helper_1.resolveMainFilePath)(path_1.default.resolve(path_1.default.dirname(file.path), item.path));
559
690
  if (helper_1.fs.existsSync(componentPath) && !Array.from(this.components).some(item => item.path === componentPath)) {
560
691
  const componentName = this.getComponentName(componentPath);
692
+ // newBlended 模式下,本地化组件使用Page进行处理,此处直接跳过
693
+ if (this.nativeComponents.has(componentName))
694
+ return;
561
695
  const componentTempPath = this.getTemplatePath(componentPath);
562
696
  const isNative = this.isNativePageORComponent(componentTempPath);
563
697
  const componentObj = {
@@ -684,7 +818,7 @@ class TaroMiniPlugin {
684
818
  pages.forEach(pagePath => {
685
819
  if (dependencies.has(pagePath)) {
686
820
  const dep = dependencies.get(pagePath);
687
- new TaroSingleEntryPlugin_1.default(compiler.context, dep === null || dep === void 0 ? void 0 : dep.request, dep === null || dep === void 0 ? void 0 : dep.name, dep === null || dep === void 0 ? void 0 : dep.miniType).apply(childCompiler);
821
+ new TaroSingleEntryPlugin_1.default(compiler.context, dep === null || dep === void 0 ? void 0 : dep.request, dep === null || dep === void 0 ? void 0 : dep.name, dep === null || dep === void 0 ? void 0 : dep.miniType, dep === null || dep === void 0 ? void 0 : dep.options).apply(childCompiler);
688
822
  }
689
823
  this.pages.forEach(item => {
690
824
  if (item.path === pagePath) {
@@ -705,6 +839,13 @@ class TaroMiniPlugin {
705
839
  // 添加 comp 和 custom-wrapper 组件
706
840
  new TaroSingleEntryPlugin_1.default(compiler.context, path_1.default.resolve(__dirname, '..', 'template/comp'), `${name}/comp`, helper_1.META_TYPE.STATIC).apply(childCompiler);
707
841
  new TaroSingleEntryPlugin_1.default(compiler.context, path_1.default.resolve(__dirname, '..', 'template/custom-wrapper'), `${name}/custom-wrapper`, helper_1.META_TYPE.STATIC).apply(childCompiler);
842
+ // 给每个子编译器标记上名称和 tag
843
+ // tag 用于生成模板和 config 时区别于主编译器走不同的方法
844
+ // 名称用于在生成资源时判断是否为当前子编译器的资源
845
+ childCompiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
846
+ compilation.__name = name;
847
+ compilation.__tag = CHILD_COMPILER_TAG;
848
+ });
708
849
  promises.push(new Promise((resolve, reject) => {
709
850
  childCompiler.runAsChild(err => {
710
851
  if (err) {
@@ -763,14 +904,93 @@ class TaroMiniPlugin {
763
904
  getShowPath(filePath) {
764
905
  return filePath.replace(this.context, '').replace(/\\/g, '/').replace(/^\//, '');
765
906
  }
907
+ // 调整 config 文件中 usingComponents 的路径
908
+ // 1. 将 node_modules 调整为 npm
909
+ // 2. 将 ../../../node_modules/xxx 调整为 /npm/xxx
910
+ adjustConfigContent(config) {
911
+ const { usingComponents } = config;
912
+ if (!usingComponents || this.options.skipProcessUsingComponents)
913
+ return;
914
+ for (const [key, value] of Object.entries(usingComponents)) {
915
+ if (!value.includes(helper_1.NODE_MODULES))
916
+ return;
917
+ const match = value.replace(helper_1.NODE_MODULES, 'npm').match(/npm.*/);
918
+ usingComponents[key] = match ? `${path_1.default.sep}${match[0]}` : value;
919
+ }
920
+ }
921
+ /** 生成小程序独立分包的相关文件 */
922
+ generateIndependentMiniFiles(compilation, compiler) {
923
+ return __awaiter(this, void 0, void 0, function* () {
924
+ const { template, isBuildPlugin, sourceDir } = this.options;
925
+ const baseTemplateName = this.getIsBuildPluginPath('base', isBuildPlugin);
926
+ const isUsingCustomWrapper = component_1.componentConfig.thirdPartyComponents.has('custom-wrapper');
927
+ // @ts-ignore
928
+ const childName = compilation.__name;
929
+ // 为独立分包生成 base/comp/custom-wrapper
930
+ this.independentPackages.forEach((_pages, name) => {
931
+ // independentPackages 是包含了所有 ChildCompiler 的资源,如果不是当前 ChildCompiler 的资源不做处理
932
+ if (name !== childName)
933
+ return;
934
+ this.generateTemplateFile(compilation, compiler, `${name}/${baseTemplateName}`, template.buildTemplate, component_1.componentConfig);
935
+ if (!template.isSupportRecursive) {
936
+ // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
937
+ this.generateConfigFile(compilation, compiler, `${name}/${baseCompName}`, {
938
+ component: true,
939
+ usingComponents: {
940
+ [baseCompName]: `./${baseCompName}`,
941
+ [customWrapperName]: `./${customWrapperName}`
942
+ }
943
+ });
944
+ this.generateTemplateFile(compilation, compiler, `${name}/${baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
945
+ }
946
+ this.generateConfigFile(compilation, compiler, `${name}/${customWrapperName}`, {
947
+ component: true,
948
+ usingComponents: {
949
+ [customWrapperName]: `./${customWrapperName}`
950
+ }
951
+ });
952
+ this.generateTemplateFile(compilation, compiler, `${name}/${customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
953
+ this.generateXSFile(compilation, compiler, `${name}/utils`, isBuildPlugin);
954
+ });
955
+ this.pages.forEach(page => {
956
+ let importBaseTemplatePath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTemplatePath(baseTemplateName))));
957
+ const config = this.filesConfig[this.getConfigFilePath(page.name)];
958
+ let isIndependent = false;
959
+ let independentName = '';
960
+ this.independentPackages.forEach((pages, name) => {
961
+ // independentPackages 是包含了所有 ChildCompiler 的资源,如果不是当前 ChildCompiler 的资源不做处理
962
+ if (pages.includes(page.path) && name === childName) {
963
+ isIndependent = true;
964
+ independentName = name;
965
+ importBaseTemplatePath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, name, this.getTemplatePath(baseTemplateName))));
966
+ }
967
+ });
968
+ if (!isIndependent)
969
+ return;
970
+ if (config) {
971
+ const importBaseCompPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, independentName, this.getTargetFilePath(this.getIsBuildPluginPath(baseCompName, isBuildPlugin), ''))));
972
+ const importCustomWrapperPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, independentName, this.getTargetFilePath(this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), ''))));
973
+ config.content.usingComponents = Object.assign({}, config.content.usingComponents);
974
+ if (isUsingCustomWrapper) {
975
+ config.content.usingComponents[customWrapperName] = importCustomWrapperPath;
976
+ }
977
+ if (!template.isSupportRecursive && !page.isNative) {
978
+ config.content.usingComponents[baseCompName] = importBaseCompPath;
979
+ }
980
+ this.generateConfigFile(compilation, compiler, page.path, config.content);
981
+ }
982
+ if (!page.isNative) {
983
+ this.generateTemplateFile(compilation, compiler, page.path, template.buildPageTemplate, importBaseTemplatePath);
984
+ }
985
+ });
986
+ });
987
+ }
766
988
  /** 生成小程序相关文件 */
767
989
  generateMiniFiles(compilation, compiler) {
768
990
  return __awaiter(this, void 0, void 0, function* () {
769
991
  const { RawSource } = compiler.webpack.sources;
770
- const { template, modifyBuildAssets, modifyMiniConfigs, isBuildPlugin, sourceDir } = this.options;
992
+ const { template, modifyMiniConfigs, isBuildPlugin, sourceDir } = this.options;
771
993
  const baseTemplateName = this.getIsBuildPluginPath('base', isBuildPlugin);
772
- const baseCompName = 'comp';
773
- const customWrapperName = 'custom-wrapper';
774
994
  const isUsingCustomWrapper = component_1.componentConfig.thirdPartyComponents.has('custom-wrapper');
775
995
  /**
776
996
  * 与原生小程序混写时解析模板与样式
@@ -786,7 +1006,7 @@ class TaroMiniPlugin {
786
1006
  if (typeof modifyMiniConfigs === 'function') {
787
1007
  yield modifyMiniConfigs(this.filesConfig);
788
1008
  }
789
- if (!this.options.blended && !isBuildPlugin) {
1009
+ if ((!this.options.blended || !this.options.newBlended) && !isBuildPlugin) {
790
1010
  const appConfigPath = this.getConfigFilePath(this.appEntry);
791
1011
  const appConfigName = path_1.default.basename(appConfigPath).replace(path_1.default.extname(appConfigPath), '');
792
1012
  this.generateConfigFile(compilation, compiler, this.appEntry, this.filesConfig[appConfigName].content);
@@ -801,7 +1021,7 @@ class TaroMiniPlugin {
801
1021
  }
802
1022
  };
803
1023
  if (isUsingCustomWrapper) {
804
- baseCompConfig[customWrapperName] = `./${customWrapperName}`;
1024
+ baseCompConfig.usingComponents[customWrapperName] = `./${customWrapperName}`;
805
1025
  this.generateConfigFile(compilation, compiler, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
806
1026
  component: true,
807
1027
  usingComponents: {
@@ -825,29 +1045,6 @@ class TaroMiniPlugin {
825
1045
  this.generateTemplateFile(compilation, compiler, baseTemplateName, template.buildTemplate, component_1.componentConfig);
826
1046
  isUsingCustomWrapper && this.generateTemplateFile(compilation, compiler, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), template.buildCustomComponentTemplate, this.options.fileType.templ);
827
1047
  this.generateXSFile(compilation, compiler, 'utils', isBuildPlugin);
828
- // 为独立分包生成 base/comp/custom-wrapper
829
- this.independentPackages.forEach((_pages, name) => {
830
- this.generateTemplateFile(compilation, compiler, `${name}/${baseTemplateName}`, template.buildTemplate, component_1.componentConfig);
831
- if (!template.isSupportRecursive) {
832
- // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
833
- this.generateConfigFile(compilation, compiler, `${name}/${baseCompName}`, {
834
- component: true,
835
- usingComponents: {
836
- [baseCompName]: `./${baseCompName}`,
837
- [customWrapperName]: `./${customWrapperName}`
838
- }
839
- });
840
- this.generateTemplateFile(compilation, compiler, `${name}/${baseCompName}`, template.buildBaseComponentTemplate, this.options.fileType.templ);
841
- }
842
- this.generateConfigFile(compilation, compiler, `${name}/${customWrapperName}`, {
843
- component: true,
844
- usingComponents: {
845
- [customWrapperName]: `./${customWrapperName}`
846
- }
847
- });
848
- this.generateTemplateFile(compilation, compiler, `${name}/${customWrapperName}`, template.buildCustomComponentTemplate, this.options.fileType.templ);
849
- this.generateXSFile(compilation, compiler, `${name}/utils`, isBuildPlugin);
850
- });
851
1048
  this.components.forEach(component => {
852
1049
  const importBaseTemplatePath = (0, helper_1.promoteRelativePath)(path_1.default.relative(component.path, path_1.default.join(sourceDir, this.getTemplatePath(baseTemplateName))));
853
1050
  const config = this.filesConfig[this.getConfigFilePath(component.name)];
@@ -859,24 +1056,20 @@ class TaroMiniPlugin {
859
1056
  }
860
1057
  });
861
1058
  this.pages.forEach(page => {
862
- let importBaseTemplatePath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTemplatePath(baseTemplateName))));
1059
+ const importBaseTemplatePath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTemplatePath(baseTemplateName))));
863
1060
  const config = this.filesConfig[this.getConfigFilePath(page.name)];
864
1061
  let isIndependent = false;
865
- let independentName = '';
866
- this.independentPackages.forEach((pages, name) => {
1062
+ // pages 里面会混合独立分包的,在这里需要过滤一下,避免重复生成 assets
1063
+ this.independentPackages.forEach(pages => {
867
1064
  if (pages.includes(page.path)) {
868
1065
  isIndependent = true;
869
- independentName = name;
870
- importBaseTemplatePath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, name, this.getTemplatePath(baseTemplateName))));
871
1066
  }
872
1067
  });
1068
+ if (isIndependent)
1069
+ return;
873
1070
  if (config) {
874
- let importBaseCompPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTargetFilePath(this.getIsBuildPluginPath(baseCompName, isBuildPlugin), ''))));
875
- let importCustomWrapperPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTargetFilePath(this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), ''))));
876
- if (isIndependent) {
877
- importBaseCompPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, independentName, this.getTargetFilePath(this.getIsBuildPluginPath(baseCompName, isBuildPlugin), ''))));
878
- importCustomWrapperPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, independentName, this.getTargetFilePath(this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), ''))));
879
- }
1071
+ const importBaseCompPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTargetFilePath(this.getIsBuildPluginPath(baseCompName, isBuildPlugin), ''))));
1072
+ const importCustomWrapperPath = (0, helper_1.promoteRelativePath)(path_1.default.relative(page.path, path_1.default.join(sourceDir, this.getTargetFilePath(this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), ''))));
880
1073
  config.content.usingComponents = Object.assign({}, config.content.usingComponents);
881
1074
  if (isUsingCustomWrapper) {
882
1075
  config.content.usingComponents[customWrapperName] = importCustomWrapperPath;
@@ -904,9 +1097,6 @@ class TaroMiniPlugin {
904
1097
  compilation.assets[relativePath] = new RawSource(JSON.stringify(pluginJSON));
905
1098
  }
906
1099
  }
907
- if (typeof modifyBuildAssets === 'function') {
908
- yield modifyBuildAssets(compilation.assets, this);
909
- }
910
1100
  });
911
1101
  }
912
1102
  optimizeMiniFiles(compilation, _compiler) {
@@ -937,6 +1127,7 @@ class TaroMiniPlugin {
937
1127
  unofficialConfigs.forEach(item => {
938
1128
  delete config[item];
939
1129
  });
1130
+ this.adjustConfigContent(config);
940
1131
  const fileConfigStr = JSON.stringify(config);
941
1132
  compilation.assets[fileConfigName] = new RawSource(fileConfigStr);
942
1133
  }
@@ -1036,17 +1227,25 @@ class TaroMiniPlugin {
1036
1227
  * 小程序全局样式文件中引入 common chunks 中的公共样式文件
1037
1228
  */
1038
1229
  injectCommonStyles({ assets }, { webpack }) {
1230
+ const { newBlended } = this.options;
1039
1231
  const { ConcatSource, RawSource } = webpack.sources;
1040
1232
  const styleExt = this.options.fileType.style;
1041
1233
  const appStyle = `app${styleExt}`;
1042
1234
  const REG_STYLE_EXT = new RegExp(`\\.(${styleExt.replace('.', '')})(\\?.*)?$`);
1043
1235
  const originSource = assets[appStyle] || new RawSource('');
1044
1236
  const commons = new ConcatSource('');
1237
+ const componentCommons = [];
1238
+ const independentPackageNames = [];
1239
+ this.independentPackages.forEach((_, name) => { independentPackageNames.push(name); });
1045
1240
  Object.keys(assets).forEach(assetName => {
1046
1241
  const fileName = path_1.default.basename(assetName, path_1.default.extname(assetName));
1047
- if ((helper_1.REG_STYLE.test(assetName) || REG_STYLE_EXT.test(assetName)) && this.options.commonChunks.includes(fileName)) {
1242
+ if ((helper_1.REG_STYLE.test(assetName) || REG_STYLE_EXT.test(assetName))
1243
+ && this.options.commonChunks.includes(fileName)
1244
+ // app.wxss 不能引入独立分包中的 common 样式文件
1245
+ && independentPackageNames.every(name => !assetName.includes(name))) {
1048
1246
  commons.add('\n');
1049
1247
  commons.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(assetName))};`);
1248
+ componentCommons.push(assetName);
1050
1249
  }
1051
1250
  });
1052
1251
  if (commons.size() > 0) {
@@ -1057,6 +1256,36 @@ class TaroMiniPlugin {
1057
1256
  source.add(commons);
1058
1257
  source.add('\n');
1059
1258
  assets[appStyle] = source;
1259
+ if (newBlended) {
1260
+ // 本地化组件引入common公共样式文件
1261
+ this.pages.forEach(page => {
1262
+ if (page.isNative)
1263
+ return;
1264
+ const pageStyle = `${page.name}${styleExt}`;
1265
+ if (this.nativeComponents.has(page.name)) {
1266
+ // 本地化组件如果没有wxss则直接写入一个空的
1267
+ if (!(pageStyle in assets)) {
1268
+ assets[pageStyle] = new ConcatSource('');
1269
+ }
1270
+ const source = new ConcatSource('');
1271
+ const originSource = assets[pageStyle];
1272
+ componentCommons.forEach(item => {
1273
+ source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(path_1.default.relative(path_1.default.dirname(pageStyle), item)))};\n`);
1274
+ });
1275
+ source.add(originSource);
1276
+ assets[pageStyle] = source;
1277
+ }
1278
+ else {
1279
+ if (pageStyle in assets) {
1280
+ const source = new ConcatSource('');
1281
+ const originSource = assets[pageStyle];
1282
+ source.add(`@import ${JSON.stringify((0, loader_utils_1.urlToRequest)(path_1.default.relative(path_1.default.dirname(pageStyle), 'app.wxss')))};\n`);
1283
+ source.add(originSource);
1284
+ assets[pageStyle] = source;
1285
+ }
1286
+ }
1287
+ });
1288
+ }
1060
1289
  }
1061
1290
  }
1062
1291
  addTarBarFilesToDependencies(compilation) {