xhs-mp-compiler-cli 2.0.0-beta.3 → 2.0.0

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 (94) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +48 -34
  2. package/dist/bin/xhs-mp-cli-dev.js +44 -44
  3. package/dist/bin/xhs-mp-cli.js +22 -12
  4. package/dist/compiler.d.ts +21 -28
  5. package/dist/compiler.js +327 -327
  6. package/dist/compilerImpl.d.ts +19 -0
  7. package/dist/compilerImpl.js +173 -0
  8. package/dist/config/constant.config.d.ts +24 -0
  9. package/dist/config/constant.config.js +82 -30
  10. package/dist/config/dir.config.js +23 -13
  11. package/dist/index.d.ts +9 -9
  12. package/dist/index.js +454 -408
  13. package/dist/packs/index.d.ts +4 -4
  14. package/dist/packs/index.js +38 -30
  15. package/dist/packs/mp-pack/index.js +38 -30
  16. package/dist/packs/webpack/dev-server/index.js +61 -50
  17. package/dist/packs/webpack/dev-server/lib/ensurePort.js +33 -23
  18. package/dist/packs/webpack/dev-server/lib/openBrowser.js +28 -18
  19. package/dist/packs/webpack/index.d.ts +1 -1
  20. package/dist/packs/webpack/index.js +99 -90
  21. package/dist/packs/webpack/webpack.d.ts +2 -1
  22. package/dist/packs/webpack/webpack.js +81 -63
  23. package/dist/presets/babel.d.ts +0 -1
  24. package/dist/presets/babel.js +64 -58
  25. package/dist/presets/common.js +42 -45
  26. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +50 -40
  27. package/dist/presets/configs/minigame/assets/index.js +62 -52
  28. package/dist/presets/configs/minigame/service/index.js +49 -30
  29. package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +33 -23
  30. package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +2 -2
  31. package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -60
  32. package/dist/presets/configs/minigame/service/serviceEntryPlugin.d.ts +3 -3
  33. package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +39 -29
  34. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +120 -113
  35. package/dist/presets/configs/miniprogram/assets/index.js +45 -43
  36. package/dist/presets/configs/miniprogram/render/component.js +51 -41
  37. package/dist/presets/configs/miniprogram/render/index.d.ts +2 -3
  38. package/dist/presets/configs/miniprogram/render/index.js +85 -68
  39. package/dist/presets/configs/miniprogram/render/render.js +53 -43
  40. package/dist/presets/configs/miniprogram/render/renderChunkPlugin.js +84 -49
  41. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
  42. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.js +164 -126
  43. package/dist/presets/configs/miniprogram/service/index.js +82 -65
  44. package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.js +60 -50
  45. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +93 -55
  46. package/dist/presets/index.js +100 -90
  47. package/dist/presets/loaders/mg-entry-loader.js +32 -22
  48. package/dist/presets/loaders/mini-style-loader.js +79 -55
  49. package/dist/presets/loaders/mp-entry-loader.js +149 -140
  50. package/dist/presets/loaders/wxss-loader.js +73 -63
  51. package/dist/presets/plugins/InjectorPlugin.js +23 -13
  52. package/dist/presets/plugins/SourceMapDevToolPlugin.d.ts +1 -0
  53. package/dist/presets/plugins/SourceMapDevToolPlugin.js +23 -0
  54. package/dist/presets/prod.js +88 -78
  55. package/dist/sharedFs.d.ts +2 -0
  56. package/dist/sharedFs.js +24 -0
  57. package/dist/types/index.d.ts +11 -37
  58. package/dist/types/index.js +23 -13
  59. package/dist/utils/{apm/index.d.ts → apm.d.ts} +5 -3
  60. package/dist/utils/apm.js +245 -0
  61. package/dist/utils/cache.d.ts +28 -0
  62. package/dist/utils/cache.js +49 -0
  63. package/dist/utils/common.js +42 -32
  64. package/dist/utils/css.d.ts +1 -1
  65. package/dist/utils/css.js +43 -35
  66. package/dist/utils/depenedHelper.js +95 -85
  67. package/dist/utils/file.js +39 -29
  68. package/dist/utils/loader-utils.js +63 -53
  69. package/dist/utils/postcssRpx2Vw.js +43 -33
  70. package/dist/utils/project.d.ts +3 -2
  71. package/dist/utils/project.js +129 -119
  72. package/dist/utils/projectConfig.js +24 -15
  73. package/dist/utils/utils.js +51 -42
  74. package/dist/utils/workerPool.d.ts +29 -0
  75. package/dist/utils/workerPool.js +125 -0
  76. package/dist/utils/zip.js +239 -229
  77. package/package.json +33 -14
  78. package/dist/compilerCP.d.ts +0 -1
  79. package/dist/compilerCP.js +0 -175
  80. package/dist/presets/loaders/sjs-loader/index.d.ts +0 -12
  81. package/dist/presets/loaders/sjs-loader/index.js +0 -605
  82. package/dist/presets/loaders/sjs-loader/scope.d.ts +0 -36
  83. package/dist/presets/loaders/sjs-loader/scope.js +0 -122
  84. package/dist/presets/loaders/sjs-loader/tranform.d.ts +0 -2
  85. package/dist/presets/loaders/sjs-loader/tranform.js +0 -31
  86. package/dist/utils/apm/index.js +0 -198
  87. package/dist/utils/asyncWebpack.d.ts +0 -3
  88. package/dist/utils/asyncWebpack.js +0 -38
  89. package/dist/utils/const.d.ts +0 -27
  90. package/dist/utils/const.js +0 -47
  91. package/dist/utils/getSuffixName.d.ts +0 -3
  92. package/dist/utils/getSuffixName.js +0 -18
  93. package/dist/utils/tagTransformMap.d.ts +0 -36
  94. package/dist/utils/tagTransformMap.js +0 -39
@@ -1,71 +1,88 @@
1
- "use strict";
2
1
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
2
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
3
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.presetService = exports.getServiceEntry = void 0;
7
- const xhs_mp_pack_1 = require("xhs-mp-pack");
8
- const serviceChunkPlugin_1 = __importDefault(require("./serviceChunkPlugin"));
9
- const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
10
- const getServiceEntry = options => () => {
11
- const entries = {};
12
- const { packSetting } = options;
13
- if (packSetting.enableV2) {
14
- appendV2Entry(options, entries);
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
15
8
  }
16
- if (packSetting.enableV1) {
17
- appendV1Entry(options, entries);
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "path", "xhs-mp-pack", "./serviceChunkPlugin", "../../../plugins/InjectorPlugin", "../../../plugins/SourceMapDevToolPlugin", "../../../../utils/cache"], factory);
18
11
  }
19
- return entries;
20
- };
21
- exports.getServiceEntry = getServiceEntry;
22
- const appendV2Entry = (options, entries) => {
23
- const { packSetting } = options;
24
- const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
25
- if (mainPkg) {
26
- entries['v2/service'] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!`;
27
- }
28
- for (const { root } of subPkgs) {
29
- entries[`v2/${root}/service`] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!`;
30
- }
31
- };
32
- const appendV1Entry = (options, entries) => {
33
- const { packSetting } = options;
34
- const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
35
- if (mainPkg) {
36
- entries['v1/service'] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!`;
37
- }
38
- for (const { root } of subPkgs) {
39
- entries[`v1/${root}/service`] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!`;
40
- }
41
- };
42
- const presetService = (chain, options) => {
43
- const { packSetting } = options;
44
- const { env, distDir, cacheDirectory } = packSetting;
45
- chain.merge({
46
- output: {
47
- path: distDir,
48
- globalObject: 'globalThis',
49
- libraryTarget: 'umd',
50
- uniqueName: 'MpService',
51
- },
52
- // cache: {
53
- // type: 'filesystem',
54
- // cacheDirectory: path.join(cacheDirectory!, 'service'),
55
- // },
56
- });
57
- chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
58
- chain.plugin('ServiceChunkPlugin').use(serviceChunkPlugin_1.default, [options]);
59
- chain.optimization.runtimeChunk({
60
- name: 'runtime'
61
- });
62
- // dev模式下开启懒编译,不能抽离common chunk
63
- if (env !== xhs_mp_pack_1.ENV.development) {
64
- chain.optimization.splitChunks({
65
- chunks: chunk => !chunk.name.startsWith('v0/'),
66
- minSize: 0,
67
- name: 'service-common'
12
+ })(function (require, exports) {
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.presetService = exports.getServiceEntry = void 0;
16
+ const path_1 = __importDefault(require("path"));
17
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
18
+ const serviceChunkPlugin_1 = __importDefault(require("./serviceChunkPlugin"));
19
+ const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
20
+ const SourceMapDevToolPlugin_1 = require("../../../plugins/SourceMapDevToolPlugin");
21
+ const cache_1 = require("../../../../utils/cache");
22
+ const getServiceEntry = options => () => {
23
+ const entries = {};
24
+ const { packSetting } = options;
25
+ if (packSetting.enableV2) {
26
+ appendV2Entry(options, entries);
27
+ }
28
+ if (packSetting.enableV1) {
29
+ appendV1Entry(options, entries);
30
+ }
31
+ return entries;
32
+ };
33
+ exports.getServiceEntry = getServiceEntry;
34
+ const appendV2Entry = (options, entries) => {
35
+ const { packSetting } = options;
36
+ const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
37
+ if (mainPkg) {
38
+ entries['v2/service'] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!`;
39
+ }
40
+ for (const { root } of subPkgs) {
41
+ entries[`v2/${root}/service`] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!`;
42
+ }
43
+ };
44
+ const appendV1Entry = (options, entries) => {
45
+ const { packSetting } = options;
46
+ const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
47
+ if (mainPkg) {
48
+ entries['v1/service'] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!`;
49
+ }
50
+ for (const { root } of subPkgs) {
51
+ entries[`v1/${root}/service`] = `mp-entry-loader?type=service&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!`;
52
+ }
53
+ };
54
+ const presetService = (chain, options) => {
55
+ const { packSetting } = options;
56
+ const { env, distDir, cacheDirectory, enableSourcemap, appendSourcemapComment } = packSetting;
57
+ const absCacheDirectory = path_1.default.join(cacheDirectory, 'service');
58
+ chain.merge(Object.assign({
59
+ // target: 'webworker',
60
+ output: {
61
+ path: distDir,
62
+ globalObject: 'globalThis',
63
+ libraryTarget: 'umd',
64
+ uniqueName: 'MpService',
65
+ } }, (0, cache_1.getCacheConfig)(absCacheDirectory, options)));
66
+ chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
67
+ chain.plugin('ServiceChunkPlugin').use(serviceChunkPlugin_1.default, [options]);
68
+ if (enableSourcemap && !appendSourcemapComment) {
69
+ const SourceMapDevToolPlugin = (0, SourceMapDevToolPlugin_1.getSourceMapDevtoolPlugin)(options.packMode);
70
+ chain.plugin('SourceMapDevToolPlugin').use(SourceMapDevToolPlugin, [{
71
+ filename: '[file].map',
72
+ append: false
73
+ }]);
74
+ }
75
+ chain.optimization.runtimeChunk({
76
+ name: 'runtime'
68
77
  });
69
- }
70
- };
71
- exports.presetService = presetService;
78
+ // dev模式下开启懒编译,不能抽离common chunk
79
+ if (env !== xhs_mp_pack_1.ENV.development) {
80
+ chain.optimization.splitChunks({
81
+ chunks: chunk => !chunk.name.startsWith('v0/'),
82
+ minSize: 0,
83
+ name: 'service-common'
84
+ });
85
+ }
86
+ };
87
+ exports.presetService = presetService;
88
+ });
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,59 +7,70 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const fs_extra_1 = require("fs-extra");
13
- class XhsResolveLimitPlugin {
14
- constructor(options) {
15
- this.options = options;
10
+ (function (factory) {
11
+ if (typeof module === "object" && typeof module.exports === "object") {
12
+ var v = factory(require, exports);
13
+ if (v !== undefined) module.exports = v;
16
14
  }
17
- apply(compiler, resolver) {
18
- resolver
19
- .getHook('file')
20
- .tapAsync('XhsResolveLimitPlugin', (request, contextResolver, callback) => __awaiter(this, void 0, void 0, function* () {
21
- var _a;
22
- const { project } = this.options;
23
- const projectMiniprogramPath = project.projectMiniprogramPath;
24
- // @ts-ignore
25
- let issuer = ((_a = request.context) === null || _a === void 0 ? void 0 : _a.issuer) || '';
26
- let target = request.path || '';
27
- if (!issuer ||
28
- target.includes('node_modules') ||
29
- !issuer.startsWith(projectMiniprogramPath) ||
30
- !target.startsWith(projectMiniprogramPath)) {
31
- return callback();
32
- }
33
- issuer = issuer.replace(`${projectMiniprogramPath}/`, '');
34
- target = target.replace(new RegExp(projectMiniprogramPath, 'g'), '').replace(/^\/+/g, '');
35
- const appJSON = yield (0, fs_extra_1.readJson)(`${projectMiniprogramPath}/app.json`);
36
- appJSON.subPackages = appJSON.subPackages || appJSON.subpackages || [];
37
- const subPkgs = appJSON.subPackages.map(pkg => pkg.root);
38
- // issuer 是分包
39
- const issuerSubPkg = subPkgs.find(pkg => issuer.startsWith(pkg));
40
- if (issuerSubPkg) {
41
- // 允许 target 和 issuer 在同一个分包
42
- if (issuer.startsWith(issuerSubPkg) && target.startsWith(issuerSubPkg)) {
15
+ else if (typeof define === "function" && define.amd) {
16
+ define(["require", "exports", "fs-extra"], factory);
17
+ }
18
+ })(function (require, exports) {
19
+ "use strict";
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ const fs_extra_1 = require("fs-extra");
22
+ class XhsResolveLimitPlugin {
23
+ constructor(options) {
24
+ this.options = options;
25
+ }
26
+ apply(compiler, resolver) {
27
+ resolver
28
+ .getHook('file')
29
+ .tapAsync('XhsResolveLimitPlugin', (request, contextResolver, callback) => __awaiter(this, void 0, void 0, function* () {
30
+ var _a;
31
+ const { project } = this.options;
32
+ const projectMiniprogramPath = project.projectMiniprogramPath;
33
+ // @ts-ignore
34
+ let issuer = ((_a = request.context) === null || _a === void 0 ? void 0 : _a.issuer) || '';
35
+ let target = request.path || '';
36
+ if (!issuer ||
37
+ target.includes('node_modules') ||
38
+ !issuer.startsWith(projectMiniprogramPath) ||
39
+ !target.startsWith(projectMiniprogramPath)) {
43
40
  return callback();
44
41
  }
45
- // issuer 是分包 target 是主包
46
- if (appJSON.pages.some(page => target.startsWith(page))) {
47
- // issuer **独立分包** 时,不能引用主包资源
48
- if (appJSON.subPackages.find(subPkg => subPkg.root === issuerSubPkg).independent) {
49
- return callback(new Error(`${issuer} 在独立分包内,独立分包不可引用主包 ${target} 资源`), request);
42
+ issuer = issuer.replace(`${projectMiniprogramPath}/`, '');
43
+ target = target.replace(new RegExp(projectMiniprogramPath, 'g'), '').replace(/^\/+/g, '');
44
+ const appJSON = yield (0, fs_extra_1.readJson)(`${projectMiniprogramPath}/app.json`);
45
+ appJSON.subPackages = appJSON.subPackages || appJSON.subpackages || [];
46
+ const subPkgs = appJSON.subPackages.map(pkg => pkg.root);
47
+ // 当 issuer 是分包
48
+ const issuerSubPkg = subPkgs.find(pkg => issuer.startsWith(pkg));
49
+ if (issuerSubPkg) {
50
+ // 允许 target 和 issuer 在同一个分包
51
+ if (issuer.startsWith(issuerSubPkg) && target.startsWith(issuerSubPkg)) {
52
+ return callback();
53
+ }
54
+ // issuer 是分包 target 是主包
55
+ if (appJSON.pages.some(page => target.startsWith(page))) {
56
+ // 当 issuer 是 **独立分包** 时,不能引用主包资源
57
+ if (appJSON.subPackages.find(subPkg => subPkg.root === issuerSubPkg).independent) {
58
+ return callback(new Error(`${issuer} 在独立分包内,独立分包不可引用主包 ${target} 资源`), request);
59
+ }
60
+ return callback();
61
+ }
62
+ // issuer 是分包,target 是分包
63
+ if (subPkgs.some(sub => target.startsWith(sub))) {
64
+ return callback(new Error(`${issuer} 不能引用 ${target},因为他们不在同一个分包`), request);
50
65
  }
51
- return callback();
52
66
  }
53
- // issuer 是分包,target 是分包
54
- if (subPkgs.some(sub => target.startsWith(sub))) {
55
- return callback(new Error(`${issuer} 不能引用 ${target},因为他们不在同一个分包`), request);
67
+ // issuer 是主包,此时 target 不能在分包
68
+ if (subPkgs.some(pkg => target.startsWith(pkg))) {
69
+ return callback(new Error(`${issuer} 在主包内,主包不可引用分包资源 ${target}`), request);
56
70
  }
57
- }
58
- // issuer 是主包,此时 target 不能在分包
59
- if (subPkgs.some(pkg => target.startsWith(pkg))) {
60
- return callback(new Error(`${issuer} 在主包内,主包不可引用分包资源 ${target}`), request);
61
- }
62
- return callback();
63
- }));
71
+ return callback();
72
+ }));
73
+ }
64
74
  }
65
- }
66
- exports.default = XhsResolveLimitPlugin;
75
+ exports.default = XhsResolveLimitPlugin;
76
+ });
@@ -1,65 +1,103 @@
1
- "use strict";
2
1
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
2
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
3
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const path_1 = __importDefault(require("path"));
7
- const xhs_mp_pack_1 = require("xhs-mp-pack");
8
- class ServiceChunkPlugin {
9
- constructor(options) {
10
- this.options = options;
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
11
8
  }
12
- apply(compiler) {
13
- compiler.hooks.compilation.tap('ServiceChunkPlugin', compilation => {
14
- compilation.hooks.processAssets.tap({
15
- name: 'ServiceChunkPlugin',
16
- }, () => {
17
- var _a, _b, _c, _d;
18
- const { project, packSetting } = this.options;
19
- const serviceSourceV2 = (_a = compilation.getAsset('v2/service.js')) === null || _a === void 0 ? void 0 : _a.source;
20
- const serviceSource = (_b = compilation.getAsset('v1/service.js')) === null || _b === void 0 ? void 0 : _b.source;
21
- let runtimeCode = (_c = compilation.getAsset('runtime.js')) === null || _c === void 0 ? void 0 : _c.source.source();
22
- // 当没有引用相同 js 场景时, commonChunk 将是 undefined
23
- const commonSource = ((_d = compilation.getAsset('service-common.js')) === null || _d === void 0 ? void 0 : _d.source) || '';
24
- runtimeCode = `
25
- if (!globalThis.__XHS_RUNTIME_REGISTERED__) {
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "path", "xhs-mp-pack", "webpack-sources"], factory);
11
+ }
12
+ })(function (require, exports) {
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const path_1 = __importDefault(require("path"));
16
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
17
+ const webpack_sources_1 = require("webpack-sources");
18
+ function adapteServiceCode(assetName, serviceSource, packSetting) {
19
+ let result;
20
+ if (packSetting.runInServiceSandbox) {
21
+ const { source, map } = serviceSource === null || serviceSource === void 0 ? void 0 : serviceSource.sourceAndMap();
22
+ const beforeContent = '(function(window,document){';
23
+ const afterContent = '\n})(undefined,undefined)';
24
+ const newSource = beforeContent + source + afterContent;
25
+ result = new webpack_sources_1.SourceMapSource(newSource, // 新的资源内容
26
+ assetName, // 资源文件名
27
+ // @ts-ignore
28
+ map, // 旧的 source map
29
+ source // 旧的源代码
30
+ );
31
+ // @ts-ignore
32
+ }
33
+ else {
34
+ result = serviceSource;
35
+ }
36
+ return result;
37
+ }
38
+ class ServiceChunkPlugin {
39
+ constructor(options) {
40
+ this.options = options;
41
+ }
42
+ apply(compiler) {
43
+ compiler.hooks.compilation.tap('ServiceChunkPlugin', (compilation) => {
44
+ compilation.hooks.processAssets.tap({
45
+ name: 'ServiceChunkPlugin'
46
+ }, () => {
47
+ var _a, _b, _c, _d;
48
+ const { project, packSetting } = this.options;
49
+ let serviceSourceV2 = (_a = compilation.getAsset('v2/service.js')) === null || _a === void 0 ? void 0 : _a.source;
50
+ let serviceSource = (_b = compilation.getAsset('v1/service.js')) === null || _b === void 0 ? void 0 : _b.source;
51
+ let runtimeCode = (_c = compilation.getAsset('runtime.js')) === null || _c === void 0 ? void 0 : _c.source.source();
52
+ // 当没有引用相同 js 场景时, commonChunk 将是 undefined
53
+ const commonSource = ((_d = compilation.getAsset('service-common.js')) === null || _d === void 0 ? void 0 : _d.source) || '';
54
+ runtimeCode = `if (!globalThis.__XHS_RUNTIME_REGISTERED__) {
26
55
  globalThis.__XHS_RUNTIME_REGISTERED__ = true;
27
56
  ${runtimeCode}
28
57
  }`;
29
- if (packSetting.enableV2 && serviceSourceV2) {
30
- // 注入 v2 service.js
31
- // 主包加入 runtimeChunk、commonChunk
32
- compilation.updateAsset('v2/service.js', new xhs_mp_pack_1.ConcatSource(runtimeCode, commonSource, serviceSourceV2));
33
- }
34
- // 独立分包加入 runtimeChunk
35
- if (packSetting.enableV1 && serviceSource) {
36
- // 主包加入 runtimeChunk、commonChunk
37
- compilation.updateAsset('v1/service.js', new xhs_mp_pack_1.ConcatSource(runtimeCode, commonSource, serviceSource));
38
- }
39
- this.updateIndependentService(compilation, runtimeCode);
40
- compilation.deleteAsset('runtime.js');
41
- compilation.deleteAsset('service-common.js');
58
+ if (packSetting.enableV2 && serviceSourceV2) {
59
+ // 注入 v2 service.js
60
+ // 主包加入 runtimeChunk、commonChunk
61
+ const assetName = 'v2/service.js';
62
+ serviceSourceV2 = adapteServiceCode(assetName, serviceSourceV2, packSetting);
63
+ // @ts-ignore
64
+ compilation.updateAsset(assetName, new xhs_mp_pack_1.ConcatSource(runtimeCode, commonSource, serviceSourceV2));
65
+ }
66
+ // 独立分包加入 runtimeChunk
67
+ if (packSetting.enableV1 && serviceSource) {
68
+ // // 主包加入 runtimeChunk、commonChunk
69
+ const assetName = 'v1/service.js';
70
+ serviceSource = adapteServiceCode(assetName, serviceSource, packSetting);
71
+ // @ts-ignore
72
+ compilation.updateAsset(assetName, new xhs_mp_pack_1.ConcatSource(runtimeCode, commonSource, serviceSource));
73
+ }
74
+ this.updateIndependentService(compilation, runtimeCode);
75
+ compilation.deleteAsset('runtime.js');
76
+ compilation.deleteAsset('service-common.js');
77
+ });
42
78
  });
43
- });
44
- }
45
- updateIndependentService(compilation, runtimeCode) {
46
- const { project, packSetting } = this.options;
47
- const usingPackageType = packSetting.usingPackageType;
48
- const subPackages = project.getSubPackages();
49
- subPackages
50
- .filter(sub => sub.independent)
51
- .map(sub => path_1.default.join(usingPackageType, sub.root, 'service.js'))
52
- .forEach(servicePath => {
53
- var _a;
54
- const independentServiceSource = (_a = compilation.getAsset(servicePath)) === null || _a === void 0 ? void 0 : _a.source;
55
- if (compilation.getAsset(servicePath)) {
56
- const subServiceSource = new xhs_mp_pack_1.ConcatSource(runtimeCode, independentServiceSource);
57
- compilation.updateAsset(servicePath, subServiceSource);
58
- if (packSetting.enableV2 && packSetting.enableV1) {
59
- compilation.updateAsset(servicePath.replace(usingPackageType, 'v1'), subServiceSource);
79
+ }
80
+ updateIndependentService(compilation, runtimeCode) {
81
+ const { project, packSetting } = this.options;
82
+ const usingPackageType = packSetting.usingPackageType;
83
+ const subPackages = project.getSubPackages();
84
+ subPackages
85
+ .filter(sub => sub.independent)
86
+ .map(sub => path_1.default.join(usingPackageType, sub.root, 'service.js'))
87
+ .forEach(servicePath => {
88
+ var _a;
89
+ if (compilation.getAsset(servicePath)) {
90
+ let independentServiceSource = (_a = compilation.getAsset(servicePath)) === null || _a === void 0 ? void 0 : _a.source;
91
+ independentServiceSource = adapteServiceCode(servicePath, independentServiceSource, packSetting);
92
+ const subServiceSource = new xhs_mp_pack_1.ConcatSource(runtimeCode, independentServiceSource);
93
+ // 写入
94
+ compilation.updateAsset(servicePath, subServiceSource);
95
+ if (packSetting.enableV2 && packSetting.enableV1) {
96
+ compilation.updateAsset(servicePath.replace(usingPackageType, 'v1'), subServiceSource);
97
+ }
60
98
  }
61
- }
62
- });
99
+ });
100
+ }
63
101
  }
64
- }
65
- exports.default = ServiceChunkPlugin;
102
+ exports.default = ServiceChunkPlugin;
103
+ });