xhs-mp-compiler-cli 1.0.2 → 1.1.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 (150) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +3 -3
  2. package/dist/bin/xhs-mp-cli-dev.js +9 -31
  3. package/dist/compiler.d.ts +76 -0
  4. package/dist/compiler.js +361 -0
  5. package/dist/compilerCP.js +175 -0
  6. package/dist/config/constant.config.d.ts +7 -0
  7. package/dist/config/constant.config.js +10 -0
  8. package/dist/dev-server/index.d.ts +2 -2
  9. package/dist/dev-server/index.js +7 -5
  10. package/dist/index.d.ts +1 -11
  11. package/dist/index.js +3 -112
  12. package/dist/packs/index.d.ts +14 -0
  13. package/dist/packs/index.js +31 -0
  14. package/dist/packs/mp-pack/index.d.ts +3 -0
  15. package/dist/packs/mp-pack/index.js +32 -0
  16. package/dist/packs/webpack/index.d.ts +3 -0
  17. package/dist/packs/webpack/index.js +92 -0
  18. package/dist/packs/webpack/webpack.d.ts +21 -0
  19. package/dist/packs/webpack/webpack.js +79 -0
  20. package/dist/{webpack-config/presets → presets}/babel.d.ts +3 -3
  21. package/dist/{webpack-config/presets → presets}/babel.js +13 -11
  22. package/dist/presets/common.d.ts +4 -0
  23. package/dist/presets/common.js +46 -0
  24. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.d.ts +10 -0
  25. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +46 -0
  26. package/dist/presets/configs/minigame/assets/index.d.ts +3 -0
  27. package/dist/presets/configs/minigame/assets/index.js +65 -0
  28. package/dist/presets/configs/minigame/service/index.d.ts +3 -0
  29. package/dist/presets/configs/minigame/service/index.js +34 -0
  30. package/dist/{webpack-config/presets/service/XhsResolveLimitPlugin.d.ts → presets/configs/minigame/service/resolveLimitPlugin.d.ts} +3 -3
  31. package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +35 -0
  32. package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +9 -0
  33. package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -0
  34. package/dist/{webpack-config/presets/service/ServiceEntryPlugin.d.ts → presets/configs/minigame/service/serviceEntryPlugin.d.ts} +3 -1
  35. package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +32 -0
  36. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.d.ts +12 -0
  37. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +132 -0
  38. package/dist/presets/configs/miniprogram/assets/index.d.ts +4 -0
  39. package/dist/presets/configs/miniprogram/assets/index.js +47 -0
  40. package/dist/presets/configs/miniprogram/render/component.d.ts +3 -0
  41. package/dist/presets/configs/miniprogram/render/component.js +47 -0
  42. package/dist/presets/configs/miniprogram/render/index.d.ts +5 -0
  43. package/dist/presets/configs/miniprogram/render/index.js +70 -0
  44. package/dist/presets/configs/miniprogram/render/render.d.ts +3 -0
  45. package/dist/presets/configs/miniprogram/render/render.js +49 -0
  46. package/dist/presets/configs/miniprogram/render/renderChunkPlugin.d.ts +8 -0
  47. package/dist/{webpack-config/presets/render/RenderChunkPlugin.js → presets/configs/miniprogram/render/renderChunkPlugin.js} +13 -8
  48. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
  49. package/dist/{webpack-config/presets/render/SjsEntryPlugin.js → presets/configs/miniprogram/render/sjsEntryPlugin.js} +24 -21
  50. package/dist/presets/configs/miniprogram/service/index.d.ts +5 -0
  51. package/dist/presets/configs/miniprogram/service/index.js +72 -0
  52. package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.d.ts +8 -0
  53. package/dist/{webpack-config/presets/service/XhsResolveLimitPlugin.js → presets/configs/miniprogram/service/resolveLimitPlugin.js} +11 -10
  54. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.d.ts +9 -0
  55. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +65 -0
  56. package/dist/presets/index.d.ts +2 -0
  57. package/dist/presets/index.js +95 -0
  58. package/dist/presets/loaders/mg-entry-loader.js +27 -0
  59. package/dist/{webpack-config → presets}/loaders/mini-style-loader.js +13 -10
  60. package/dist/presets/loaders/mp-entry-loader.js +196 -0
  61. package/dist/{webpack-config → presets}/loaders/sjs-loader/index.js +0 -4
  62. package/dist/{webpack-config → presets}/loaders/wxss-loader.js +7 -6
  63. package/dist/presets/plugins/InjectorPlugin.d.ts +7 -0
  64. package/dist/presets/plugins/InjectorPlugin.js +14 -0
  65. package/dist/presets/prod.d.ts +4 -0
  66. package/dist/presets/prod.js +80 -0
  67. package/dist/types/index.d.ts +18 -6
  68. package/dist/utils/common.d.ts +2 -1
  69. package/dist/utils/common.js +7 -6
  70. package/dist/utils/const.d.ts +27 -0
  71. package/dist/utils/const.js +47 -0
  72. package/dist/{webpack-config/lib → utils}/css.js +6 -6
  73. package/dist/utils/getSuffixName.d.ts +3 -0
  74. package/dist/utils/getSuffixName.js +18 -0
  75. package/dist/{webpack-config/loaders/utils.d.ts → utils/loader-utils.d.ts} +2 -0
  76. package/dist/{webpack-config/loaders/utils.js → utils/loader-utils.js} +19 -1
  77. package/dist/utils/projectConfig.d.ts +1 -1
  78. package/dist/utils/projectConfig.js +2 -2
  79. package/dist/{webpack-config/lib → utils}/utils.js +2 -2
  80. package/package.json +10 -9
  81. package/dist/build/index.d.ts +0 -3
  82. package/dist/build/index.js +0 -22
  83. package/dist/globalData/SjsModule.d.ts +0 -32
  84. package/dist/globalData/SjsModule.js +0 -34
  85. package/dist/utils/combineExtJson.d.ts +0 -3
  86. package/dist/utils/combineExtJson.js +0 -74
  87. package/dist/utils/getCssSuffixName.d.ts +0 -1
  88. package/dist/utils/getCssSuffixName.js +0 -9
  89. package/dist/utils/getMlSuffixName.d.ts +0 -1
  90. package/dist/utils/getMlSuffixName.js +0 -6
  91. package/dist/utils/getPrefixName.d.ts +0 -1
  92. package/dist/utils/getPrefixName.js +0 -6
  93. package/dist/utils/transformAppJson.d.ts +0 -16
  94. package/dist/utils/transformAppJson.js +0 -199
  95. package/dist/utils/validateAppJson.d.ts +0 -4
  96. package/dist/utils/validateAppJson.js +0 -111
  97. package/dist/webpack-config/index.d.ts +0 -4
  98. package/dist/webpack-config/index.js +0 -41
  99. package/dist/webpack-config/lib/const.d.ts +0 -13
  100. package/dist/webpack-config/lib/const.js +0 -15
  101. package/dist/webpack-config/loaders/component-entry-loader.js +0 -31
  102. package/dist/webpack-config/loaders/component-entry-requirements-loader.js +0 -24
  103. package/dist/webpack-config/loaders/render-entry-loader.js +0 -50
  104. package/dist/webpack-config/loaders/service-entry-loader.d.ts +0 -1
  105. package/dist/webpack-config/loaders/service-entry-loader.js +0 -51
  106. package/dist/webpack-config/plugins/VueLoaderPlugin.d.ts +0 -19
  107. package/dist/webpack-config/plugins/VueLoaderPlugin.js +0 -205
  108. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.d.ts +0 -8
  109. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.js +0 -40
  110. package/dist/webpack-config/plugins/XhsMpJsonPlugin.d.ts +0 -36
  111. package/dist/webpack-config/plugins/XhsMpJsonPlugin.js +0 -71
  112. package/dist/webpack-config/presets/common.d.ts +0 -4
  113. package/dist/webpack-config/presets/common.js +0 -40
  114. package/dist/webpack-config/presets/mp/MpEntryPlugin.d.ts +0 -23
  115. package/dist/webpack-config/presets/mp/MpEntryPlugin.js +0 -87
  116. package/dist/webpack-config/presets/mp/index.d.ts +0 -4
  117. package/dist/webpack-config/presets/mp/index.js +0 -48
  118. package/dist/webpack-config/presets/prod.d.ts +0 -4
  119. package/dist/webpack-config/presets/prod.js +0 -83
  120. package/dist/webpack-config/presets/render/ComponentEntryPlugin.d.ts +0 -18
  121. package/dist/webpack-config/presets/render/ComponentEntryPlugin.js +0 -77
  122. package/dist/webpack-config/presets/render/RenderChunkPlugin.d.ts +0 -5
  123. package/dist/webpack-config/presets/render/RenderEntryPlugin.d.ts +0 -14
  124. package/dist/webpack-config/presets/render/RenderEntryPlugin.js +0 -42
  125. package/dist/webpack-config/presets/render/SjsEntryPlugin.d.ts +0 -31
  126. package/dist/webpack-config/presets/render/index.d.ts +0 -4
  127. package/dist/webpack-config/presets/render/index.js +0 -62
  128. package/dist/webpack-config/presets/service/ServiceEntryPlugin.js +0 -36
  129. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.d.ts +0 -6
  130. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.js +0 -46
  131. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.d.ts +0 -5
  132. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.js +0 -27
  133. package/dist/webpack-config/presets/service/index.d.ts +0 -4
  134. package/dist/webpack-config/presets/service/index.js +0 -39
  135. /package/dist/{webpack-config/loaders/component-entry-loader.d.ts → compilerCP.d.ts} +0 -0
  136. /package/dist/{webpack-config/loaders/component-entry-requirements-loader.d.ts → presets/loaders/mg-entry-loader.d.ts} +0 -0
  137. /package/dist/{webpack-config → presets}/loaders/mini-style-loader.d.ts +0 -0
  138. /package/dist/{webpack-config/loaders/render-entry-loader.d.ts → presets/loaders/mp-entry-loader.d.ts} +0 -0
  139. /package/dist/{webpack-config → presets}/loaders/sjs-loader/index.d.ts +0 -0
  140. /package/dist/{webpack-config → presets}/loaders/sjs-loader/scope.d.ts +0 -0
  141. /package/dist/{webpack-config → presets}/loaders/sjs-loader/scope.js +0 -0
  142. /package/dist/{webpack-config → presets}/loaders/sjs-loader/tranform.d.ts +0 -0
  143. /package/dist/{webpack-config → presets}/loaders/sjs-loader/tranform.js +0 -0
  144. /package/dist/{webpack-config → presets}/loaders/wxss-loader.d.ts +0 -0
  145. /package/dist/{webpack-config/lib → utils}/css.d.ts +0 -0
  146. /package/dist/{webpack-config/lib → utils}/postcssRpx2Vw.d.ts +0 -0
  147. /package/dist/{webpack-config/lib → utils}/postcssRpx2Vw.js +0 -0
  148. /package/dist/{webpack-config/lib → utils}/tagTransformMap.d.ts +0 -0
  149. /package/dist/{webpack-config/lib → utils}/tagTransformMap.js +0 -0
  150. /package/dist/{webpack-config/lib → utils}/utils.d.ts +0 -0
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getComponentEntry = void 0;
4
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
5
+ const getComponentEntry = (options) => {
6
+ const entries = {};
7
+ const { packSetting } = options;
8
+ // 编译v2
9
+ if (packSetting.enableV2) {
10
+ appendV2Entry(options, entries);
11
+ }
12
+ // 构建 v1 的包
13
+ if (packSetting.enableV1) {
14
+ appendV1Entry(options, entries);
15
+ }
16
+ return entries;
17
+ };
18
+ exports.getComponentEntry = getComponentEntry;
19
+ const appendV2Entry = (options, entries) => {
20
+ const { project, packSetting } = options;
21
+ const subPackages = project.getSubPackages();
22
+ const subPackageRoots = subPackages.map(pkg => pkg.root);
23
+ const componentsMap = project.getComponentsMap();
24
+ const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
25
+ Object.keys(componentsMap).forEach(compPath => {
26
+ if (mainPkg && project.isFileInMainPkg(compPath)) {
27
+ entries[`v2/${compPath}`] = `mp-entry-loader?type=component&mode=v2&compPath=${compPath}&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!`;
28
+ }
29
+ for (const subPkg of subPkgs) {
30
+ if (compPath.startsWith(`${subPkg.root}/`)) {
31
+ const root = subPackageRoots.find(root => compPath.startsWith(`${root}/`));
32
+ entries[`v2/${root}/${compPath}`] = `mp-entry-loader?type=component&mode=v2&compPath=${compPath}&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!`;
33
+ }
34
+ }
35
+ });
36
+ };
37
+ function appendV1Entry(options, entries) {
38
+ const { packSetting } = options;
39
+ const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
40
+ if (mainPkg) {
41
+ entries['v1/components'] = `mp-entry-loader?type=component&mode=v1&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!`;
42
+ }
43
+ for (const subPkg of subPkgs) {
44
+ const { root } = subPkg;
45
+ entries[`v1/${root}/components`] = `mp-entry-loader?type=component&mode=v1&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!`;
46
+ }
47
+ }
@@ -0,0 +1,5 @@
1
+ import { Config } from '../../../../packs';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare const getRenderEntry: (options: IPresetOptions) => () => {};
4
+ declare const presetRender: (chain: Config, options: IPresetOptions) => void;
5
+ export { getRenderEntry, presetRender };
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.presetRender = exports.getRenderEntry = void 0;
7
+ const css_1 = __importDefault(require("../../../../utils/css"));
8
+ const getSuffixName_1 = require("../../../../utils/getSuffixName");
9
+ const sjsEntryPlugin_1 = __importDefault(require("./sjsEntryPlugin"));
10
+ const render_1 = require("./render");
11
+ const component_1 = require("./component");
12
+ const renderChunkPlugin_1 = __importDefault(require("./renderChunkPlugin"));
13
+ const types_1 = require("../../../../types");
14
+ const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
15
+ const getRenderEntry = (options) => () => (Object.assign(Object.assign({}, (0, render_1.getRenderEntry)(options)), (0, component_1.getComponentEntry)(options)));
16
+ exports.getRenderEntry = getRenderEntry;
17
+ const presetRender = (chain, options) => {
18
+ const { packSetting } = options;
19
+ const { enableVDom, env, distDir } = packSetting;
20
+ const mlReg = new RegExp(`.${(0, getSuffixName_1.getMlSuffixName)()}$`);
21
+ const cssReg = new RegExp(`.${(0, getSuffixName_1.getCssSuffixName)()}$`);
22
+ const isProd = env === types_1.ENV.production;
23
+ chain.merge({
24
+ externals: { vue: 'vue' },
25
+ output: {
26
+ path: distDir,
27
+ globalObject: 'globalThis',
28
+ libraryTarget: 'umd',
29
+ uniqueName: 'MpRender',
30
+ },
31
+ // cache: {
32
+ // type: 'filesystem',
33
+ // cacheDirectory: packSetting.cacheDirectory,
34
+ // },
35
+ module: {
36
+ rule: {
37
+ ml: {
38
+ test: mlReg,
39
+ use: {
40
+ // 'thread-loader': {
41
+ // loader: require.resolve('thread-loader'),
42
+ // options: {
43
+ // workers: require('os').cpus().length,
44
+ // },
45
+ // },
46
+ 'ml-loader': {
47
+ loader: require.resolve('xhs-mp-compiler-ml-loader'),
48
+ options: {
49
+ removeComentNode: isProd,
50
+ vdom: enableVDom,
51
+ },
52
+ },
53
+ },
54
+ },
55
+ [(0, getSuffixName_1.getCssSuffixName)()]: {
56
+ test: cssReg,
57
+ use: (0, css_1.default)(options, [
58
+ "mini-style-loader",
59
+ 'postcss-loader',
60
+ 'wxss-loader',
61
+ ]),
62
+ },
63
+ },
64
+ },
65
+ });
66
+ chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
67
+ chain.plugin('SjsEntryPlugin').use(sjsEntryPlugin_1.default, [options]);
68
+ chain.plugin("RenderChunkPlugin").use(renderChunkPlugin_1.default, [options]);
69
+ };
70
+ exports.presetRender = presetRender;
@@ -0,0 +1,3 @@
1
+ import { IPresetOptions } from '../../../../types';
2
+ declare const getRenderEntry: (options: IPresetOptions) => {};
3
+ export { getRenderEntry };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRenderEntry = void 0;
4
+ const getSuffixName_1 = require("../../../../utils/getSuffixName");
5
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
6
+ const getRenderEntry = (options) => {
7
+ const entries = {};
8
+ const { packSetting } = options;
9
+ if (packSetting.enableV2) {
10
+ appendV2Entry(options, entries);
11
+ }
12
+ if (packSetting.enableV1) {
13
+ appendV1Entry(options, entries);
14
+ }
15
+ return entries;
16
+ };
17
+ exports.getRenderEntry = getRenderEntry;
18
+ const appendV2Entry = (options, entries) => {
19
+ const { project, packSetting } = options;
20
+ const projectMiniprogramPath = project.projectMiniprogramPath;
21
+ const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
22
+ if (mainPkg) {
23
+ const pages = project.getPages();
24
+ for (const { path } of pages) {
25
+ entries[`v2/${path}.render`] = `mp-entry-loader?type=render&mode=v2&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!${projectMiniprogramPath}/${path}.${(0, getSuffixName_1.getMlSuffixName)()}`;
26
+ }
27
+ }
28
+ for (const { pages, root } of subPkgs) {
29
+ for (const { path } of pages) {
30
+ entries[`v2/${path}.render`] = `mp-entry-loader?type=render&mode=v2&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!${projectMiniprogramPath}/${path}.${(0, getSuffixName_1.getMlSuffixName)()}`;
31
+ }
32
+ }
33
+ };
34
+ const appendV1Entry = (options, entries) => {
35
+ const { project, packSetting } = options;
36
+ const projectMiniprogramPath = project.projectMiniprogramPath;
37
+ const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
38
+ if (mainPkg) {
39
+ const pages = project.getPages();
40
+ for (const { path } of pages) {
41
+ entries[`v1/${path}.render`] = `mp-entry-loader?type=render&mode=v1&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!${projectMiniprogramPath}/${path}.${(0, getSuffixName_1.getMlSuffixName)()}`;
42
+ }
43
+ }
44
+ for (const { pages, root } of subPkgs) {
45
+ for (const { path } of pages) {
46
+ entries[`v1/${path}.render`] = `mp-entry-loader?type=render&mode=v1&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!${projectMiniprogramPath}/${path}.${(0, getSuffixName_1.getMlSuffixName)()}`;
47
+ }
48
+ }
49
+ };
@@ -0,0 +1,8 @@
1
+ import { ICompiler } from '../../../../packs';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare class RenderChunkPlugin {
4
+ options: IPresetOptions;
5
+ constructor(options: IPresetOptions);
6
+ apply(compiler: ICompiler): void;
7
+ }
8
+ export default RenderChunkPlugin;
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const webpack_1 = require("webpack");
4
- const webpack_sources_1 = require("webpack-sources");
3
+ const packs_1 = require("../../../../packs");
5
4
  const injectCSS = `
6
- globalThis.__XHS_IS_WEBVIEW = (globalThis.localStorage && document && document.getElementsByTagName)
5
+ globalThis.__XHS_IS_WEBVIEW = (globalThis.localStorage && document && document.querySelectorAll)
7
6
  globalThis.__XHS_APPEND_CSS = function(cssText){
8
7
  if (!globalThis.__XHS_IS_WEBVIEW) return;
9
8
  var headTags = document.getElementsByTagName('head')
@@ -16,21 +15,27 @@ globalThis.__XHS_APPEND_CSS = function(cssText){
16
15
  `;
17
16
  // 注入css跟webpack runtime
18
17
  class RenderChunkPlugin {
18
+ constructor(options) {
19
+ this.options = options;
20
+ }
19
21
  apply(compiler) {
20
22
  compiler.hooks.compilation.tap(RenderChunkPlugin.name, compilation => {
21
23
  compilation.hooks.processAssets.tap({
22
24
  name: RenderChunkPlugin.name,
23
- stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
24
- }, (assets) => {
25
+ stage: packs_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
26
+ }, assets => {
25
27
  var _a, _b;
28
+ const { project } = this.options;
26
29
  // 注入css逻辑
27
30
  const injectCode = injectCSS;
28
31
  // @ts-ignore
29
- const appJSON = compiler.xhsData.appJSON;
32
+ const appJSON = project.appJSON;
30
33
  const pathMap = [
31
34
  (_a = appJSON.pages) === null || _a === void 0 ? void 0 : _a.map(({ path }) => path),
32
35
  (_b = appJSON.subPackages) === null || _b === void 0 ? void 0 : _b.map(sub => { var _a; return (_a = sub.pages) === null || _a === void 0 ? void 0 : _a.map(({ path }) => path); })
33
- ].flat(4).filter(Boolean)
36
+ ]
37
+ .flat(4)
38
+ .filter(Boolean)
34
39
  .reduce((ac, path) => {
35
40
  ac[`v1/${path}.render.js`] = true;
36
41
  ac[`v2/${path}.render.js`] = true;
@@ -41,7 +46,7 @@ class RenderChunkPlugin {
41
46
  if (!pathMap[key])
42
47
  return;
43
48
  // @ts-ignore
44
- compilation.updateAsset(key, new webpack_sources_1.ConcatSource(injectCode, assets[key]));
49
+ compilation.updateAsset(key, new packs_1.ConcatSource(injectCode, assets[key]));
45
50
  });
46
51
  });
47
52
  });
@@ -0,0 +1,15 @@
1
+ import { Compilation, ICompiler } from '../../../../packs';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare class SjsEntryPlugin {
4
+ options: IPresetOptions;
5
+ entriesCache: Map<string, boolean>;
6
+ mainpkgPaths: string[];
7
+ subPackagePaths: Map<string, string[]>;
8
+ subPackageRoots: string[];
9
+ constructor(options: IPresetOptions);
10
+ isFileInMainPkg(filePath: any): boolean;
11
+ isFileInSubpackage(filePath: any): boolean;
12
+ apply(compiler: ICompiler): void;
13
+ makeSjsFileContent(compilation: Compilation, chunkName: string, sjsFiles: string[] | undefined, sjsModuleName: string, pkgRoot?: string): void;
14
+ }
15
+ export default SjsEntryPlugin;
@@ -29,26 +29,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const fs_extra_1 = __importDefault(require("fs-extra"));
30
30
  const glob_1 = __importDefault(require("glob"));
31
31
  const path_1 = __importDefault(require("path"));
32
- const webpack_sources_1 = require("webpack-sources");
33
- const webpack_1 = require("webpack");
34
- const index_1 = __importStar(require("../../loaders/sjs-loader/index"));
35
- const common_1 = require("../../../utils/common");
32
+ const packs_1 = require("../../../../packs");
33
+ const index_1 = __importStar(require("../../../loaders/sjs-loader/index"));
34
+ const common_1 = require("../../../../utils/common");
35
+ /**
36
+ * @NOTE 分包中使用 SJS 的规则
37
+ *
38
+ * - 整包情况下,可以在所有页面中引用整包内的任意SJS模块。
39
+ * - 分包情况下:
40
+ * - 主包不能引入分包的 SJS 模块。
41
+ * - 分包内部可以引用主分包内的SJS模块。
42
+ * - 分包A 和 分包B 之间不可以互相引用SJS模块。
43
+ * - 独立分包内的SJS模块只能在独立分包内使用,不能跨分包引用。
44
+ */
36
45
  function formatSJSPath(i) {
37
46
  if (/\.(sjs|wxs)$/.test(i)) {
38
47
  return (0, common_1.toUnixPath)(i);
39
48
  }
40
49
  const suffix = ['sjs', 'wxs'].find(n => fs_extra_1.default.existsSync(`${i}.${n}`));
41
50
  if (!suffix) {
42
- throw new webpack_1.WebpackError(`[SJS文件编译错误] ${i}.sjs 不存在`);
51
+ throw new packs_1.WebpackError(`[SJS文件编译错误] ${i}.sjs 不存在`);
43
52
  }
44
53
  return (0, common_1.toUnixPath)(suffix ? `${i}.${suffix}` : i);
45
54
  }
46
55
  class SjsEntryPlugin {
47
56
  constructor(options) {
48
- const { enableV1, enableV2, miniprogramDir } = options;
49
- this.miniprogramDir = miniprogramDir;
50
- this.enableV1 = enableV1;
51
- this.enableV2 = enableV2;
57
+ this.options = options;
52
58
  this.entriesCache = new Map();
53
59
  this.mainpkgPaths = [];
54
60
  this.subPackagePaths = new Map();
@@ -61,14 +67,13 @@ class SjsEntryPlugin {
61
67
  return this.subPackageRoots.some(root => filePath.startsWith(`${root}/`));
62
68
  }
63
69
  apply(compiler) {
64
- new webpack_1.library.EnableLibraryPlugin('umd').apply(compiler);
65
70
  compiler.hooks.watchRun.tap('SjsEntryPlugin', () => {
66
71
  this.subPackagePaths.clear();
67
72
  this.mainpkgPaths = [];
68
73
  });
69
74
  compiler.hooks.emit.tapAsync('SjsEntryPlugin', (compilation, callback) => {
70
- // @ts-ignore
71
- const { appJSON, miniprogramDir } = (compiler === null || compiler === void 0 ? void 0 : compiler.xhsData) || {};
75
+ const { project } = this.options;
76
+ const { appJSON, miniprogramDir } = project;
72
77
  this.subPackageRoots = appJSON.subPackages.map(pkg => pkg.root);
73
78
  // 默认输出一个空的sjs文件
74
79
  this.subPackageRoots.forEach(root => {
@@ -95,7 +100,6 @@ class SjsEntryPlugin {
95
100
  this.makeSjsFileContent(compilation, 'sjs.js', [...this.mainpkgPaths], 'XHS_SJS');
96
101
  // 分包
97
102
  Array.from(this.subPackagePaths.keys()).forEach(pkgRoot => {
98
- // eslint-disable-next-line
99
103
  const paths = this.subPackagePaths.get(pkgRoot);
100
104
  this.makeSjsFileContent(compilation, `${pkgRoot}/sjs.js`, [...paths], `XHS_SJS_${pkgRoot}`, pkgRoot);
101
105
  });
@@ -104,6 +108,7 @@ class SjsEntryPlugin {
104
108
  }
105
109
  makeSjsFileContent(compilation, chunkName, sjsFiles = [], sjsModuleName, pkgRoot) {
106
110
  var _a;
111
+ const { project, packSetting } = this.options;
107
112
  const sjsModules = [];
108
113
  // 用于去除重复引入
109
114
  const sjsPathCache = new Set();
@@ -112,9 +117,7 @@ class SjsEntryPlugin {
112
117
  * 在这里校验 sjs 模块引入的路径是否符合规则,分包的规则见上方
113
118
  */
114
119
  let sjsPath;
115
- // @ts-ignore
116
120
  while ((sjsPath = sjsFiles.shift())) {
117
- // eslint-disable-line
118
121
  if (sjsPathCache.has(sjsPath)) {
119
122
  continue;
120
123
  }
@@ -122,7 +125,7 @@ class SjsEntryPlugin {
122
125
  const content = fs_extra_1.default.readFileSync(sjsAbsPath, 'utf8');
123
126
  const result = index_1.default.call(compilation, content, requirePath => {
124
127
  const realPath = (0, common_1.unixResolve)(path_1.default.dirname(sjsAbsPath), requirePath);
125
- const relativePath = (0, common_1.getShortPath)(this.miniprogramDir, realPath);
128
+ const relativePath = (0, common_1.getShortPath)(project.miniprogramDir, realPath);
126
129
  /**
127
130
  * 不是当前分包的 sjs 文件不处理
128
131
  */
@@ -136,9 +139,9 @@ class SjsEntryPlugin {
136
139
  return relativePath;
137
140
  });
138
141
  if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
139
- throw new webpack_1.WebpackError(`[SJS 文件编译错误] ${sjsAbsPath}\n${result.errors.map(i => i.message).join("\n")}`);
142
+ throw new packs_1.WebpackError(`[SJS 文件编译错误] ${sjsAbsPath}\n${result.errors.map(i => i.message).join("\n")}`);
140
143
  }
141
- const moduleName = (0, common_1.getShortPath)(this.miniprogramDir, sjsAbsPath);
144
+ const moduleName = (0, common_1.getShortPath)(project.miniprogramDir, sjsAbsPath);
142
145
  // 老版本兼容代码
143
146
  sjsModules.push(`"${moduleName}": ${JSON.stringify(result.legacyCode)}`);
144
147
  // 模块化代码
@@ -146,12 +149,12 @@ class SjsEntryPlugin {
146
149
  // 处理后的加入缓存
147
150
  sjsPathCache.add(sjsPath);
148
151
  }
149
- const source = new webpack_sources_1.ConcatSource(`globalThis['${sjsModuleName}'] = {`, `${sjsModules.join(',\n')}`, '}');
150
- if (this.enableV1) {
152
+ const source = new packs_1.ConcatSource(`globalThis['${sjsModuleName}'] = {`, `${sjsModules.join(',\n')}`, '}');
153
+ if (packSetting.enableV1) {
151
154
  // @ts-ignore
152
155
  compilation.assets[`v1/${chunkName}`] = source;
153
156
  }
154
- if (this.enableV2) {
157
+ if (packSetting.enableV2) {
155
158
  // @ts-ignore
156
159
  compilation.assets[`v2/${chunkName}`] = source;
157
160
  }
@@ -0,0 +1,5 @@
1
+ import { Config } from 'xhs-mp-pack';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare const getServiceEntry: (options: any) => () => {};
4
+ declare const presetService: (chain: Config, options: IPresetOptions) => void;
5
+ export { getServiceEntry, presetService };
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.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);
15
+ }
16
+ if (packSetting.enableV1) {
17
+ appendV1Entry(options, entries);
18
+ }
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 } = packSetting;
45
+ const isProd = env === xhs_mp_pack_1.ENV.production;
46
+ chain.merge({
47
+ output: {
48
+ path: distDir,
49
+ globalObject: 'globalThis',
50
+ libraryTarget: 'umd',
51
+ uniqueName: 'MpService',
52
+ }
53
+ // cache: {
54
+ // type: 'filesystem',
55
+ // cacheDirectory: cacheDirectory,
56
+ // },
57
+ });
58
+ chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
59
+ chain.plugin('ServiceChunkPlugin').use(serviceChunkPlugin_1.default, [options]);
60
+ chain.optimization.runtimeChunk({
61
+ name: 'runtime'
62
+ });
63
+ // dev模式下开启懒编译,不能抽离common chunk
64
+ if (env !== xhs_mp_pack_1.ENV.development) {
65
+ chain.optimization.splitChunks({
66
+ chunks: chunk => !chunk.name.startsWith('v0/'),
67
+ minSize: 0,
68
+ name: 'service-common'
69
+ });
70
+ }
71
+ };
72
+ exports.presetService = presetService;
@@ -0,0 +1,8 @@
1
+ import { Resolver } from 'xhs-mp-pack';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare class XhsResolveLimitPlugin {
4
+ options: IPresetOptions;
5
+ constructor(options: IPresetOptions);
6
+ apply(compiler: any, resolver: Resolver): void;
7
+ }
8
+ export default XhsResolveLimitPlugin;
@@ -10,28 +10,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const fs_extra_1 = require("fs-extra");
13
- const common_1 = require("../../../utils/common");
14
13
  class XhsResolveLimitPlugin {
15
14
  constructor(options) {
16
- this.miniprogramDir = options.miniprogramDir;
15
+ this.options = options;
17
16
  }
18
- apply(resolver) {
17
+ apply(compiler, resolver) {
19
18
  resolver
20
19
  .getHook('file')
21
20
  .tapAsync('XhsResolveLimitPlugin', (request, contextResolver, callback) => __awaiter(this, void 0, void 0, function* () {
22
21
  var _a;
22
+ const { project } = this.options;
23
+ const projectMiniprogramPath = project.projectMiniprogramPath;
23
24
  // @ts-ignore
24
- let issuer = (0, common_1.toUnixPath)(((_a = request.context) === null || _a === void 0 ? void 0 : _a.issuer) || '');
25
- let target = (0, common_1.toUnixPath)(request.path || '');
25
+ let issuer = ((_a = request.context) === null || _a === void 0 ? void 0 : _a.issuer) || '';
26
+ let target = request.path || '';
26
27
  if (!issuer ||
27
28
  target.includes('node_modules') ||
28
- !issuer.startsWith(this.miniprogramDir) ||
29
- !target.startsWith(this.miniprogramDir)) {
29
+ !issuer.startsWith(projectMiniprogramPath) ||
30
+ !target.startsWith(projectMiniprogramPath)) {
30
31
  return callback();
31
32
  }
32
- issuer = issuer.replace(`${this.miniprogramDir}/`, '');
33
- target = target.replace(new RegExp(this.miniprogramDir, 'g'), '').replace(/^\/+/g, '');
34
- const appJSON = yield (0, fs_extra_1.readJson)(`${this.miniprogramDir}/app.json`);
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`);
35
36
  appJSON.subPackages = appJSON.subPackages || appJSON.subpackages || [];
36
37
  const subPkgs = appJSON.subPackages.map(pkg => pkg.root);
37
38
  // 当 issuer 是分包
@@ -0,0 +1,9 @@
1
+ import { IXhsCompiler } from 'xhs-mp-pack';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare class ServiceChunkPlugin {
4
+ options: IPresetOptions;
5
+ constructor(options: IPresetOptions);
6
+ apply(compiler: IXhsCompiler): void;
7
+ updateIndependentService(compilation: any, runtimeCode: any): void;
8
+ }
9
+ export default ServiceChunkPlugin;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ 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;
11
+ }
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__) {
26
+ globalThis.__XHS_RUNTIME_REGISTERED__ = true;
27
+ ${runtimeCode}
28
+ }`;
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');
42
+ });
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);
60
+ }
61
+ }
62
+ });
63
+ }
64
+ }
65
+ exports.default = ServiceChunkPlugin;
@@ -0,0 +1,2 @@
1
+ import { IPresetOptions } from '../types';
2
+ export declare const createConfigs: (options: IPresetOptions) => import("webpack").Configuration[];