xhs-mp-compiler-cli 1.0.2-beta.0 → 1.0.2

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 (110) hide show
  1. package/dist/bin/xhs-mp-cli-build.d.ts +2 -0
  2. package/dist/bin/xhs-mp-cli-build.js +40 -0
  3. package/dist/bin/xhs-mp-cli-dev.d.ts +2 -0
  4. package/dist/bin/xhs-mp-cli-dev.js +81 -0
  5. package/dist/bin/xhs-mp-cli.d.ts +2 -0
  6. package/dist/bin/xhs-mp-cli.js +16 -0
  7. package/dist/build/index.d.ts +3 -0
  8. package/dist/build/index.js +22 -0
  9. package/dist/dev-server/index.d.ts +3 -0
  10. package/dist/dev-server/index.js +62 -0
  11. package/dist/dev-server/lib/ensurePort.d.ts +2 -0
  12. package/dist/dev-server/lib/ensurePort.js +32 -0
  13. package/dist/dev-server/lib/openBrowser.d.ts +2 -0
  14. package/dist/dev-server/lib/openBrowser.js +23 -0
  15. package/dist/dev-server/lib/openChrome.applescript +78 -0
  16. package/dist/globalData/SjsModule.d.ts +32 -0
  17. package/dist/globalData/SjsModule.js +34 -0
  18. package/dist/index.d.ts +11 -0
  19. package/dist/index.js +114 -0
  20. package/dist/types/index.d.ts +106 -0
  21. package/dist/types/index.js +13 -0
  22. package/dist/utils/asyncWebpack.d.ts +3 -0
  23. package/dist/utils/asyncWebpack.js +38 -0
  24. package/dist/utils/combineExtJson.d.ts +3 -0
  25. package/dist/utils/combineExtJson.js +74 -0
  26. package/dist/utils/common.d.ts +7 -0
  27. package/dist/utils/common.js +35 -0
  28. package/dist/utils/depenedHelper.d.ts +31 -0
  29. package/dist/utils/depenedHelper.js +89 -0
  30. package/dist/utils/getCssSuffixName.d.ts +1 -0
  31. package/dist/utils/getCssSuffixName.js +9 -0
  32. package/dist/utils/getMlSuffixName.d.ts +1 -0
  33. package/dist/utils/getMlSuffixName.js +6 -0
  34. package/dist/utils/getPrefixName.d.ts +1 -0
  35. package/dist/utils/getPrefixName.js +6 -0
  36. package/dist/utils/projectConfig.d.ts +1 -0
  37. package/dist/utils/projectConfig.js +20 -0
  38. package/dist/utils/transformAppJson.d.ts +16 -0
  39. package/dist/utils/transformAppJson.js +199 -0
  40. package/dist/utils/validateAppJson.d.ts +4 -0
  41. package/dist/utils/validateAppJson.js +111 -0
  42. package/dist/webpack-config/index.d.ts +4 -0
  43. package/dist/webpack-config/index.js +41 -0
  44. package/dist/webpack-config/lib/const.d.ts +13 -0
  45. package/dist/webpack-config/lib/const.js +15 -0
  46. package/dist/webpack-config/lib/css.d.ts +2 -0
  47. package/dist/webpack-config/lib/css.js +41 -0
  48. package/dist/webpack-config/lib/postcssRpx2Vw.d.ts +12 -0
  49. package/dist/webpack-config/lib/postcssRpx2Vw.js +35 -0
  50. package/dist/webpack-config/lib/tagTransformMap.d.ts +35 -0
  51. package/dist/webpack-config/lib/tagTransformMap.js +38 -0
  52. package/dist/webpack-config/lib/utils.d.ts +5 -0
  53. package/dist/webpack-config/lib/utils.js +37 -0
  54. package/dist/webpack-config/loaders/component-entry-loader.d.ts +1 -0
  55. package/dist/webpack-config/loaders/component-entry-loader.js +31 -0
  56. package/dist/webpack-config/loaders/component-entry-requirements-loader.d.ts +1 -0
  57. package/dist/webpack-config/loaders/component-entry-requirements-loader.js +24 -0
  58. package/dist/webpack-config/loaders/mini-style-loader.d.ts +1 -0
  59. package/dist/webpack-config/loaders/mini-style-loader.js +69 -0
  60. package/dist/webpack-config/loaders/render-entry-loader.d.ts +1 -0
  61. package/dist/webpack-config/loaders/render-entry-loader.js +50 -0
  62. package/dist/webpack-config/loaders/service-entry-loader.d.ts +1 -0
  63. package/dist/webpack-config/loaders/service-entry-loader.js +51 -0
  64. package/dist/webpack-config/loaders/sjs-loader/index.d.ts +12 -0
  65. package/dist/webpack-config/loaders/sjs-loader/index.js +609 -0
  66. package/dist/webpack-config/loaders/sjs-loader/scope.d.ts +36 -0
  67. package/dist/webpack-config/loaders/sjs-loader/scope.js +122 -0
  68. package/dist/webpack-config/loaders/sjs-loader/tranform.d.ts +2 -0
  69. package/dist/webpack-config/loaders/sjs-loader/tranform.js +31 -0
  70. package/dist/webpack-config/loaders/utils.d.ts +13 -0
  71. package/dist/webpack-config/loaders/utils.js +41 -0
  72. package/dist/webpack-config/loaders/wxss-loader.d.ts +1 -0
  73. package/dist/webpack-config/loaders/wxss-loader.js +76 -0
  74. package/dist/webpack-config/plugins/VueLoaderPlugin.d.ts +19 -0
  75. package/dist/webpack-config/plugins/VueLoaderPlugin.js +205 -0
  76. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.d.ts +8 -0
  77. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.js +40 -0
  78. package/dist/webpack-config/plugins/XhsMpJsonPlugin.d.ts +36 -0
  79. package/dist/webpack-config/plugins/XhsMpJsonPlugin.js +71 -0
  80. package/dist/webpack-config/presets/babel.d.ts +8 -0
  81. package/dist/webpack-config/presets/babel.js +57 -0
  82. package/dist/webpack-config/presets/common.d.ts +4 -0
  83. package/dist/webpack-config/presets/common.js +40 -0
  84. package/dist/webpack-config/presets/mp/MpEntryPlugin.d.ts +23 -0
  85. package/dist/webpack-config/presets/mp/MpEntryPlugin.js +87 -0
  86. package/dist/webpack-config/presets/mp/index.d.ts +4 -0
  87. package/dist/webpack-config/presets/mp/index.js +48 -0
  88. package/dist/webpack-config/presets/prod.d.ts +4 -0
  89. package/dist/webpack-config/presets/prod.js +83 -0
  90. package/dist/webpack-config/presets/render/ComponentEntryPlugin.d.ts +18 -0
  91. package/dist/webpack-config/presets/render/ComponentEntryPlugin.js +77 -0
  92. package/dist/webpack-config/presets/render/RenderChunkPlugin.d.ts +5 -0
  93. package/dist/webpack-config/presets/render/RenderChunkPlugin.js +50 -0
  94. package/dist/webpack-config/presets/render/RenderEntryPlugin.d.ts +14 -0
  95. package/dist/webpack-config/presets/render/RenderEntryPlugin.js +42 -0
  96. package/dist/webpack-config/presets/render/SjsEntryPlugin.d.ts +31 -0
  97. package/dist/webpack-config/presets/render/SjsEntryPlugin.js +160 -0
  98. package/dist/webpack-config/presets/render/index.d.ts +4 -0
  99. package/dist/webpack-config/presets/render/index.js +62 -0
  100. package/dist/webpack-config/presets/service/ServiceEntryPlugin.d.ts +8 -0
  101. package/dist/webpack-config/presets/service/ServiceEntryPlugin.js +36 -0
  102. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.d.ts +6 -0
  103. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.js +46 -0
  104. package/dist/webpack-config/presets/service/XhsResolveLimitPlugin.d.ts +8 -0
  105. package/dist/webpack-config/presets/service/XhsResolveLimitPlugin.js +65 -0
  106. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.d.ts +5 -0
  107. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.js +27 -0
  108. package/dist/webpack-config/presets/service/index.d.ts +4 -0
  109. package/dist/webpack-config/presets/service/index.js +39 -0
  110. package/package.json +2 -2
@@ -0,0 +1,31 @@
1
+ import { Compilation, Compiler } from 'webpack';
2
+ /**
3
+ * @NOTE 分包中使用 SJS 的规则
4
+ *
5
+ * - 整包情况下,可以在所有页面中引用整包内的任意SJS模块。
6
+ * - 分包情况下:
7
+ * - 主包不能引入分包的 SJS 模块。
8
+ * - 分包内部可以引用主分包内的SJS模块。
9
+ * - 分包A 和 分包B 之间不可以互相引用SJS模块。
10
+ * - 独立分包内的SJS模块只能在独立分包内使用,不能跨分包引用。
11
+ */
12
+ interface IRenderEntryPluginOptions {
13
+ enableV1?: boolean;
14
+ enableV2?: boolean;
15
+ miniprogramDir: string;
16
+ }
17
+ declare class SjsEntryPlugin {
18
+ enableV1?: boolean;
19
+ enableV2?: boolean;
20
+ entriesCache: Map<string, boolean>;
21
+ mainpkgPaths: string[];
22
+ subPackagePaths: Map<string, string[]>;
23
+ miniprogramDir: string;
24
+ subPackageRoots: string[];
25
+ constructor(options: IRenderEntryPluginOptions);
26
+ isFileInMainPkg(filePath: any): boolean;
27
+ isFileInSubpackage(filePath: any): boolean;
28
+ apply(compiler: Compiler): void;
29
+ makeSjsFileContent(compilation: Compilation, chunkName: string, sjsFiles: string[] | undefined, sjsModuleName: string, pkgRoot?: string): void;
30
+ }
31
+ export default SjsEntryPlugin;
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const fs_extra_1 = __importDefault(require("fs-extra"));
30
+ const glob_1 = __importDefault(require("glob"));
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");
36
+ function formatSJSPath(i) {
37
+ if (/\.(sjs|wxs)$/.test(i)) {
38
+ return (0, common_1.toUnixPath)(i);
39
+ }
40
+ const suffix = ['sjs', 'wxs'].find(n => fs_extra_1.default.existsSync(`${i}.${n}`));
41
+ if (!suffix) {
42
+ throw new webpack_1.WebpackError(`[SJS文件编译错误] ${i}.sjs 不存在`);
43
+ }
44
+ return (0, common_1.toUnixPath)(suffix ? `${i}.${suffix}` : i);
45
+ }
46
+ class SjsEntryPlugin {
47
+ constructor(options) {
48
+ const { enableV1, enableV2, miniprogramDir } = options;
49
+ this.miniprogramDir = miniprogramDir;
50
+ this.enableV1 = enableV1;
51
+ this.enableV2 = enableV2;
52
+ this.entriesCache = new Map();
53
+ this.mainpkgPaths = [];
54
+ this.subPackagePaths = new Map();
55
+ this.subPackageRoots = [];
56
+ }
57
+ isFileInMainPkg(filePath) {
58
+ return this.subPackageRoots.every(root => !filePath.startsWith(`${root}/`));
59
+ }
60
+ isFileInSubpackage(filePath) {
61
+ return this.subPackageRoots.some(root => filePath.startsWith(`${root}/`));
62
+ }
63
+ apply(compiler) {
64
+ new webpack_1.library.EnableLibraryPlugin('umd').apply(compiler);
65
+ compiler.hooks.watchRun.tap('SjsEntryPlugin', () => {
66
+ this.subPackagePaths.clear();
67
+ this.mainpkgPaths = [];
68
+ });
69
+ compiler.hooks.emit.tapAsync('SjsEntryPlugin', (compilation, callback) => {
70
+ // @ts-ignore
71
+ const { appJSON, miniprogramDir } = (compiler === null || compiler === void 0 ? void 0 : compiler.xhsData) || {};
72
+ this.subPackageRoots = appJSON.subPackages.map(pkg => pkg.root);
73
+ // 默认输出一个空的sjs文件
74
+ this.subPackageRoots.forEach(root => {
75
+ this.subPackagePaths.has(root) || this.subPackagePaths.set(root, []);
76
+ });
77
+ const relativePaths = glob_1.default.sync('**/*.sjs', { cwd: miniprogramDir })
78
+ .concat(glob_1.default.sync('**/*.wxs', { cwd: miniprogramDir }));
79
+ relativePaths.forEach(relativePath => {
80
+ var _a;
81
+ const sjsAbsPath = (0, common_1.unixJoin)(miniprogramDir, relativePath);
82
+ // 如果是主包内的文件
83
+ if (this.isFileInMainPkg(relativePath)) {
84
+ this.mainpkgPaths.push(sjsAbsPath);
85
+ }
86
+ // 如果命中了分包内的文件
87
+ if (this.isFileInSubpackage(relativePath)) {
88
+ const root = this.subPackageRoots.find(root => relativePath.startsWith(`${root}/`));
89
+ if (root) {
90
+ (_a = this.subPackagePaths.get(root)) === null || _a === void 0 ? void 0 : _a.push(sjsAbsPath);
91
+ }
92
+ }
93
+ });
94
+ // 主包
95
+ this.makeSjsFileContent(compilation, 'sjs.js', [...this.mainpkgPaths], 'XHS_SJS');
96
+ // 分包
97
+ Array.from(this.subPackagePaths.keys()).forEach(pkgRoot => {
98
+ // eslint-disable-next-line
99
+ const paths = this.subPackagePaths.get(pkgRoot);
100
+ this.makeSjsFileContent(compilation, `${pkgRoot}/sjs.js`, [...paths], `XHS_SJS_${pkgRoot}`, pkgRoot);
101
+ });
102
+ callback();
103
+ });
104
+ }
105
+ makeSjsFileContent(compilation, chunkName, sjsFiles = [], sjsModuleName, pkgRoot) {
106
+ var _a;
107
+ const sjsModules = [];
108
+ // 用于去除重复引入
109
+ const sjsPathCache = new Set();
110
+ /**
111
+ * @NOTE
112
+ * 在这里校验 sjs 模块引入的路径是否符合规则,分包的规则见上方
113
+ */
114
+ let sjsPath;
115
+ // @ts-ignore
116
+ while ((sjsPath = sjsFiles.shift())) {
117
+ // eslint-disable-line
118
+ if (sjsPathCache.has(sjsPath)) {
119
+ continue;
120
+ }
121
+ const sjsAbsPath = formatSJSPath(sjsPath);
122
+ const content = fs_extra_1.default.readFileSync(sjsAbsPath, 'utf8');
123
+ const result = index_1.default.call(compilation, content, requirePath => {
124
+ const realPath = (0, common_1.unixResolve)(path_1.default.dirname(sjsAbsPath), requirePath);
125
+ const relativePath = (0, common_1.getShortPath)(this.miniprogramDir, realPath);
126
+ /**
127
+ * 不是当前分包的 sjs 文件不处理
128
+ */
129
+ if (pkgRoot && !relativePath.startsWith(pkgRoot)) {
130
+ throw new Error(`require(${JSON.stringify(requirePath)}),请检测引入路径是否正确或符合分包规则!`);
131
+ }
132
+ if (!fs_extra_1.default.existsSync(realPath)) {
133
+ throw new Error(`require(${JSON.stringify(requirePath)}) file does not exist`);
134
+ }
135
+ sjsFiles.push(realPath);
136
+ return relativePath;
137
+ });
138
+ 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")}`);
140
+ }
141
+ const moduleName = (0, common_1.getShortPath)(this.miniprogramDir, sjsAbsPath);
142
+ // 老版本兼容代码
143
+ sjsModules.push(`"${moduleName}": ${JSON.stringify(result.legacyCode)}`);
144
+ // 模块化代码
145
+ sjsModules.push(`".sjs#${moduleName}": function(exports,require,module,${index_1.SJS_HELPER}){${(result.code)};return module;}`);
146
+ // 处理后的加入缓存
147
+ sjsPathCache.add(sjsPath);
148
+ }
149
+ const source = new webpack_sources_1.ConcatSource(`globalThis['${sjsModuleName}'] = {`, `${sjsModules.join(',\n')}`, '}');
150
+ if (this.enableV1) {
151
+ // @ts-ignore
152
+ compilation.assets[`v1/${chunkName}`] = source;
153
+ }
154
+ if (this.enableV2) {
155
+ // @ts-ignore
156
+ compilation.assets[`v2/${chunkName}`] = source;
157
+ }
158
+ }
159
+ }
160
+ exports.default = SjsEntryPlugin;
@@ -0,0 +1,4 @@
1
+ import Config from 'webpack-chain';
2
+ import { WebpackPresetOptions } from '../../../types';
3
+ declare const getBaseConfig: (chain: Config, options: WebpackPresetOptions) => void;
4
+ export default getBaseConfig;
@@ -0,0 +1,62 @@
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 XhsMpJsonPlugin_1 = __importDefault(require("../../plugins/XhsMpJsonPlugin"));
7
+ const RenderEntryPlugin_1 = __importDefault(require("./RenderEntryPlugin"));
8
+ const ComponentEntryPlugin_1 = __importDefault(require("./ComponentEntryPlugin"));
9
+ const css_1 = __importDefault(require("../../lib/css"));
10
+ const types_1 = require("../../../types");
11
+ const getMlSuffixName_1 = __importDefault(require("../../../utils/getMlSuffixName"));
12
+ const getCssSuffixName_1 = __importDefault(require("../../../utils/getCssSuffixName"));
13
+ const SjsEntryPlugin_1 = __importDefault(require("./SjsEntryPlugin"));
14
+ const RenderChunkPlugin_1 = __importDefault(require("./RenderChunkPlugin"));
15
+ const getBaseConfig = (chain, options) => {
16
+ const { extJsonPath, env, enableV1, enableV2, enableVDom, mpDir, miniprogramDir } = options;
17
+ const isProd = env === types_1.ENV.production;
18
+ const mlReg = new RegExp(`.${(0, getMlSuffixName_1.default)()}$`);
19
+ const cssReg = new RegExp(`.${(0, getCssSuffixName_1.default)()}$`);
20
+ chain.merge({
21
+ externals: { vue: 'vue' },
22
+ output: {
23
+ path: options.distDir,
24
+ filename: '[name].render.js',
25
+ globalObject: 'globalThis',
26
+ uniqueName: 'MpRender',
27
+ },
28
+ module: {
29
+ rule: {
30
+ ml: {
31
+ test: mlReg,
32
+ use: {
33
+ 'ml-loader': {
34
+ loader: require.resolve('xhs-mp-compiler-ml-loader'),
35
+ options: {
36
+ removeComentNode: isProd,
37
+ vdom: enableVDom,
38
+ },
39
+ },
40
+ },
41
+ },
42
+ [(0, getCssSuffixName_1.default)()]: {
43
+ test: cssReg,
44
+ use: (0, css_1.default)(options, [
45
+ "mini-style-loader",
46
+ 'postcss-loader',
47
+ 'wxss-loader',
48
+ ].filter(Boolean)),
49
+ }
50
+ },
51
+ },
52
+ });
53
+ chain.plugin('XhsMpJsonPlugin').use(XhsMpJsonPlugin_1.default, [{ mpDir, miniprogramDir, extJsonPath }]);
54
+ // 组件层
55
+ chain.plugin('ComponentEntryPlugin').use(ComponentEntryPlugin_1.default, [{ enableV1, enableV2 }]);
56
+ // 渲染层
57
+ chain.plugin('XhsMpEntryPlugin').use(RenderEntryPlugin_1.default, [{ enableV1, enableV2 }]);
58
+ chain.plugin('SjsEntryPlugin').use(SjsEntryPlugin_1.default, [{ miniprogramDir, enableV1, enableV2 }]);
59
+ // 公共模块
60
+ chain.plugin("RenderChunkPlugin").use(RenderChunkPlugin_1.default);
61
+ };
62
+ exports.default = getBaseConfig;
@@ -0,0 +1,8 @@
1
+ import { Compiler } from 'webpack';
2
+ declare class ServiceEntryPlugin {
3
+ entriesCache: Map<string, boolean>;
4
+ constructor();
5
+ apply(compiler: Compiler): void;
6
+ makeUmdEntry(compiler: Compiler, filename: string, url: string, libraryName: string): void;
7
+ }
8
+ export default ServiceEntryPlugin;
@@ -0,0 +1,36 @@
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 webpack_1 = require("webpack");
7
+ const XhsMpJsonPlugin_1 = __importDefault(require("../../plugins/XhsMpJsonPlugin"));
8
+ const common_1 = require("../../../utils/common");
9
+ class ServiceEntryPlugin {
10
+ constructor() {
11
+ this.entriesCache = new Map();
12
+ }
13
+ apply(compiler) {
14
+ new webpack_1.library.EnableLibraryPlugin('umd').apply(compiler);
15
+ const hooks = XhsMpJsonPlugin_1.default.getHooks();
16
+ hooks.collected.tap('ServiceEntryPlugin', ({ xhsData }) => {
17
+ const { appJSON } = xhsData;
18
+ this.makeUmdEntry(compiler, 'v1/service.js', 'service-entry-loader?pkg=main!', 'XHS_SERVICE');
19
+ for (const { root } of appJSON.subPackages) {
20
+ this.makeUmdEntry(compiler, (0, common_1.unixJoin)('v1', root, 'service.js'), `service-entry-loader?pkg=sub&root=${root}!`, `XHS_SERVICE_${root}`);
21
+ }
22
+ });
23
+ }
24
+ makeUmdEntry(compiler, filename, url, libraryName) {
25
+ if (this.entriesCache.get(filename))
26
+ return;
27
+ const entryOption = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, filename, {
28
+ import: [url],
29
+ filename,
30
+ library: { name: libraryName, type: 'umd', },
31
+ });
32
+ new webpack_1.EntryPlugin(compiler.context, url, entryOption).apply(compiler);
33
+ this.entriesCache.set(filename, true);
34
+ }
35
+ }
36
+ exports.default = ServiceEntryPlugin;
@@ -0,0 +1,6 @@
1
+ import { Compiler } from 'webpack';
2
+ declare class XhsMpServiceChunkPlugin {
3
+ apply(compiler: Compiler): void;
4
+ updateIndependentService(compiler: any, compilation: any, runtimeCode: any): void;
5
+ }
6
+ export default XhsMpServiceChunkPlugin;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const webpack_1 = require("webpack");
4
+ const webpack_sources_1 = require("webpack-sources");
5
+ const common_1 = require("../../../utils/common");
6
+ class XhsMpServiceChunkPlugin {
7
+ apply(compiler) {
8
+ compiler.hooks.compilation.tap('XhsMpServiceChunkPlugin', compilation => {
9
+ compilation.hooks.processAssets.tap({
10
+ name: 'XhsMpServiceChunkPlugin',
11
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
12
+ }, () => {
13
+ var _a, _b, _c;
14
+ const serviceSource = (_a = compilation.getAsset('v1/service.js')) === null || _a === void 0 ? void 0 : _a.source;
15
+ let runtimeCode = (_b = compilation.getAsset('runtime.js')) === null || _b === void 0 ? void 0 : _b.source.source();
16
+ // 当没有引用相同 js 场景时, commonChunk 将是 undefined
17
+ const commonSource = ((_c = compilation.getAsset('service-common.js')) === null || _c === void 0 ? void 0 : _c.source) || '';
18
+ runtimeCode = `
19
+ if (!globalThis.__XHS_SERVICE_RUNTIME_REGISTERED__) {
20
+ globalThis.__XHS_SERVICE_RUNTIME_REGISTERED__ = true;
21
+ ${runtimeCode}
22
+ }`;
23
+ // 独立分包加入 runtimeChunk
24
+ if (serviceSource) {
25
+ // @ts-ignore 主包加入 runtimeChunk、commonChunk
26
+ compilation.updateAsset('v1/service.js', new webpack_sources_1.ConcatSource(runtimeCode, commonSource, serviceSource));
27
+ }
28
+ this.updateIndependentService(compiler, compilation, runtimeCode);
29
+ compilation.deleteAsset('runtime.js');
30
+ compilation.deleteAsset('service-common.js');
31
+ });
32
+ });
33
+ }
34
+ updateIndependentService(compiler, compilation, runtimeCode) {
35
+ var _a;
36
+ (_a = compiler.xhsData.appJSON.subPackages) === null || _a === void 0 ? void 0 : _a.filter(sub => sub.independent).map(sub => (0, common_1.unixJoin)('v1', sub.root, 'service.js')).forEach(servicePath => {
37
+ var _a;
38
+ const independentServiceSource = (_a = compilation.getAsset(servicePath)) === null || _a === void 0 ? void 0 : _a.source;
39
+ if (compilation.getAsset(servicePath)) {
40
+ // @ts-ignore
41
+ compilation.updateAsset(servicePath, new webpack_sources_1.ConcatSource(runtimeCode, independentServiceSource));
42
+ }
43
+ });
44
+ }
45
+ }
46
+ exports.default = XhsMpServiceChunkPlugin;
@@ -0,0 +1,8 @@
1
+ import { Resolver } from 'enhanced-resolve';
2
+ import { CliConfig } from '../../../types';
3
+ declare class XhsResolveLimitPlugin {
4
+ miniprogramDir: string;
5
+ constructor(options: CliConfig);
6
+ apply(resolver: Resolver): void;
7
+ }
8
+ export default XhsResolveLimitPlugin;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const fs_extra_1 = require("fs-extra");
13
+ const common_1 = require("../../../utils/common");
14
+ class XhsResolveLimitPlugin {
15
+ constructor(options) {
16
+ this.miniprogramDir = options.miniprogramDir;
17
+ }
18
+ apply(resolver) {
19
+ resolver
20
+ .getHook('file')
21
+ .tapAsync('XhsResolveLimitPlugin', (request, contextResolver, callback) => __awaiter(this, void 0, void 0, function* () {
22
+ var _a;
23
+ // @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 || '');
26
+ if (!issuer ||
27
+ target.includes('node_modules') ||
28
+ !issuer.startsWith(this.miniprogramDir) ||
29
+ !target.startsWith(this.miniprogramDir)) {
30
+ return callback();
31
+ }
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`);
35
+ appJSON.subPackages = appJSON.subPackages || appJSON.subpackages || [];
36
+ const subPkgs = appJSON.subPackages.map(pkg => pkg.root);
37
+ // 当 issuer 是分包
38
+ const issuerSubPkg = subPkgs.find(pkg => issuer.startsWith(pkg));
39
+ if (issuerSubPkg) {
40
+ // 允许 target 和 issuer 在同一个分包
41
+ if (issuer.startsWith(issuerSubPkg) && target.startsWith(issuerSubPkg)) {
42
+ return callback();
43
+ }
44
+ // issuer 是分包 target 是主包
45
+ if (appJSON.pages.some(page => target.startsWith(page))) {
46
+ // 当 issuer 是 **独立分包** 时,不能引用主包资源
47
+ if (appJSON.subPackages.find(subPkg => subPkg.root === issuerSubPkg).independent) {
48
+ return callback(new Error(`${issuer} 在独立分包内,独立分包不可引用主包 ${target} 资源`), request);
49
+ }
50
+ return callback();
51
+ }
52
+ // issuer 是分包,target 是分包
53
+ if (subPkgs.some(sub => target.startsWith(sub))) {
54
+ return callback(new Error(`${issuer} 不能引用 ${target},因为他们不在同一个分包`), request);
55
+ }
56
+ }
57
+ // issuer 是主包,此时 target 不能在分包
58
+ if (subPkgs.some(pkg => target.startsWith(pkg))) {
59
+ return callback(new Error(`${issuer} 在主包内,主包不可引用分包资源 ${target}`), request);
60
+ }
61
+ return callback();
62
+ }));
63
+ }
64
+ }
65
+ exports.default = XhsResolveLimitPlugin;
@@ -0,0 +1,5 @@
1
+ import { Compiler } from "webpack";
2
+ declare class XhsV2AssetsPlugin {
3
+ apply(compiler: Compiler): void;
4
+ }
5
+ export default XhsV2AssetsPlugin;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const webpack_1 = require("webpack");
4
+ class XhsV2AssetsPlugin {
5
+ apply(compiler) {
6
+ compiler.hooks.compilation.tap(XhsV2AssetsPlugin.name, compilation => {
7
+ // @ts-ignore
8
+ compilation.hooks.processAssets.tap({
9
+ name: XhsV2AssetsPlugin.name,
10
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY, // see below for more stages
11
+ }, (assets) => {
12
+ // @ts-ignore
13
+ const { appJSON } = compiler.xhsData;
14
+ // eslint-disable-next-line no-unneeded-ternary
15
+ if (assets['v1/service.js']) {
16
+ assets['v2/service.js'] = assets['v1/service.js'];
17
+ }
18
+ for (const { root } of appJSON.subPackages) {
19
+ if (assets[`v1/${root}/service.js`]) {
20
+ assets[`v2/${root}/service.js`] = assets[`v1/${root}/service.js`];
21
+ }
22
+ }
23
+ });
24
+ });
25
+ }
26
+ }
27
+ exports.default = XhsV2AssetsPlugin;
@@ -0,0 +1,4 @@
1
+ import Config from 'webpack-chain';
2
+ import { CliConfig } from '../../../types';
3
+ declare const presetService: (chain: Config, options: CliConfig) => void;
4
+ export default presetService;
@@ -0,0 +1,39 @@
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 XhsMpJsonPlugin_1 = __importDefault(require("../../plugins/XhsMpJsonPlugin"));
7
+ const ServiceEntryPlugin_1 = __importDefault(require("./ServiceEntryPlugin"));
8
+ const XhsMpServiceChunkPlugin_1 = __importDefault(require("./XhsMpServiceChunkPlugin"));
9
+ const XhsV2AssetsPlugin_1 = __importDefault(require("./XhsV2AssetsPlugin"));
10
+ const presetService = (chain, options) => {
11
+ const { extJsonPath, enableV2 } = options;
12
+ chain.merge({
13
+ output: {
14
+ path: options.distDir,
15
+ globalObject: 'globalThis',
16
+ uniqueName: 'MpService',
17
+ },
18
+ });
19
+ chain.plugin('ServiceEntryPlugin').use(ServiceEntryPlugin_1.default);
20
+ chain
21
+ .plugin('XhsMpJsonPlugin')
22
+ .use(XhsMpJsonPlugin_1.default, [
23
+ { mpDir: options.mpDir, miniprogramDir: options.miniprogramDir, extJsonPath },
24
+ ]);
25
+ chain.plugin('XhsMpServiceChunkPlugin').use(XhsMpServiceChunkPlugin_1.default);
26
+ if (enableV2) {
27
+ chain.plugin('XhsMpVersionStage2AssetsPlugin').use(XhsV2AssetsPlugin_1.default);
28
+ }
29
+ chain.optimization
30
+ .runtimeChunk({
31
+ name: 'runtime',
32
+ })
33
+ .splitChunks({
34
+ chunks: chunk => !chunk.name.startsWith('v0/'),
35
+ minSize: 0,
36
+ name: 'service-common',
37
+ });
38
+ };
39
+ exports.default = presetService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.0.2-beta.0",
3
+ "version": "1.0.2",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
@@ -34,7 +34,7 @@
34
34
  "@swc/helpers": "^0.5.1",
35
35
  "@vue/babel-preset-jsx": "^1.2.4",
36
36
  "xhs-mp-compiler-core-fork": "1.0.1",
37
- "xhs-mp-compiler-ml-loader": "1.0.2-beta.0",
37
+ "xhs-mp-compiler-ml-loader": "1.0.2",
38
38
  "xhs-mp-compiler-utils": "1.0.1",
39
39
  "babel-loader": "^8.2.2",
40
40
  "babel-plugin-istanbul": "^6.0.0",