xhs-mp-compiler-cli 2.0.32 → 2.0.33-beta.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.
- package/dist/compilerManager.js +1 -2
- package/dist/packs/webpack/index.js +1 -1
- package/dist/presets/configs/miniprogram/service/sanbox.d.ts +7 -0
- package/dist/presets/configs/miniprogram/service/sanbox.js +168 -0
- package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.d.ts +2 -1
- package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +180 -34
- package/package.json +11 -11
package/dist/compilerManager.js
CHANGED
|
@@ -346,7 +346,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
346
346
|
let compressCss = false;
|
|
347
347
|
let enableSourcemap = true;
|
|
348
348
|
let appendSourcemapComment = true;
|
|
349
|
-
|
|
349
|
+
const runInServiceSandbox = true;
|
|
350
350
|
if (entryType === constant_config_1.COMPILE_ENTRY.preview) {
|
|
351
351
|
compressCss = true;
|
|
352
352
|
compressJs = true;
|
|
@@ -361,7 +361,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
361
361
|
else {
|
|
362
362
|
compressCss = false;
|
|
363
363
|
compressJs = (_a = settings.minified) !== null && _a !== void 0 ? _a : false;
|
|
364
|
-
runInServiceSandbox = true;
|
|
365
364
|
}
|
|
366
365
|
const buildConfig = Object.assign({ entryType,
|
|
367
366
|
compressCss,
|
|
@@ -42,7 +42,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
42
42
|
};
|
|
43
43
|
};
|
|
44
44
|
const formatCompilerConfig = startConfig => {
|
|
45
|
-
const { projectPath, action, tsConfigPath, platform, compressCss = true, compressJs = true, enableSourcemap = true, appendSourcemapComment = true, runInServiceSandbox =
|
|
45
|
+
const { projectPath, action, tsConfigPath, platform, compressCss = true, compressJs = true, enableSourcemap = true, appendSourcemapComment = true, runInServiceSandbox = true, mpUploadOptions = false } = startConfig;
|
|
46
46
|
const { enableV1, enableV2, enableVDom } = handleVersion(startConfig);
|
|
47
47
|
const distDir = startConfig.distDir || path_1.default.join(process.cwd(), 'dist');
|
|
48
48
|
process.env.__platform = platform;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getXHSSandBox = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* 用户代码沙盒,用于隔离用户代码和框架代码
|
|
15
|
+
* 防止用户利用漏洞获进行相关框架代码的调用
|
|
16
|
+
* 小程序在编译时会利用 with() {} 包裹用户代码
|
|
17
|
+
* 本函数最后要toString() 获得函数体 所以不能依赖外部变量,所有实现均在内部实现
|
|
18
|
+
*/
|
|
19
|
+
const getXHSSandBox = function () {
|
|
20
|
+
if (!globalThis.xhsLazyAppJs) {
|
|
21
|
+
return globalThis;
|
|
22
|
+
}
|
|
23
|
+
const FrameworkGlobalThis = globalThis;
|
|
24
|
+
const BussinessGlobalThis = Object.create(FrameworkGlobalThis.constructor.prototype);
|
|
25
|
+
/**
|
|
26
|
+
* 框架提供的能力, 都是可写可读的
|
|
27
|
+
*/
|
|
28
|
+
const mpVars = [
|
|
29
|
+
"Page", "getCurrentPages", "Component",
|
|
30
|
+
"Behavior",
|
|
31
|
+
"App", "getApp",
|
|
32
|
+
"setTimeout", "clearTimeout", "setInterval", "clearInterval",
|
|
33
|
+
"xhs", "wx",
|
|
34
|
+
];
|
|
35
|
+
const proxyDynamicVars = ["__xhsRoute", "__pageId"];
|
|
36
|
+
/**
|
|
37
|
+
* 这里是获取的微信8.0.62版本中全局环境中的值
|
|
38
|
+
*/
|
|
39
|
+
const baseJSValue = [
|
|
40
|
+
"Object", "Function", "Array", "Number", "Boolean", "String", "Symbol", "Date", "Promise", "RegExp",
|
|
41
|
+
"Infinity", "NaN", "undefined",
|
|
42
|
+
"parseFloat", "parseInt", "isFinite", "isNaN",
|
|
43
|
+
"Error", "AggregateError", "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError", "SuppressedError",
|
|
44
|
+
"JSON", "Math", "console", "Atomics",
|
|
45
|
+
"SharedArrayBuffer", "ArrayBuffer", "DataView", "Uint8Array", "Int8Array", "Uint16Array", "Int16Array", "Uint32Array", "Int32Array", "BigUint64Array", "BigInt64Array", "Uint8ClampedArray", "Float32Array", "Float64Array",
|
|
46
|
+
"Map", "BigInt", "Set", "WeakMap", "WeakSet", "Proxy", "Reflect", "FinalizationRegistry", "WeakRef",
|
|
47
|
+
"decodeURI", "decodeURIComponent", "encodeURI", "encodeURIComponent", "escape", "unescape", "eval",
|
|
48
|
+
"Iterator", "AsyncIterator", "DisposableStack",
|
|
49
|
+
];
|
|
50
|
+
// 获取 globalThis 上的 Symbol 属性并分类
|
|
51
|
+
const symbolProperties = Object.getOwnPropertySymbols(FrameworkGlobalThis);
|
|
52
|
+
/**
|
|
53
|
+
* 拷贝描述符号
|
|
54
|
+
* @param items Symbol[] string[]
|
|
55
|
+
*/
|
|
56
|
+
const copyDescriptors = (items) => {
|
|
57
|
+
for (const item of items) {
|
|
58
|
+
try {
|
|
59
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(FrameworkGlobalThis, item);
|
|
60
|
+
if (!originalDescriptor)
|
|
61
|
+
continue;
|
|
62
|
+
Object.defineProperty(BussinessGlobalThis, item, originalDescriptor);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.error(`Error defining ${item}:`, error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* 拷贝值
|
|
71
|
+
* @param items string[]
|
|
72
|
+
*/
|
|
73
|
+
const copyValue = (items) => {
|
|
74
|
+
const syncMap = new Map();
|
|
75
|
+
for (const item of items) {
|
|
76
|
+
Object.defineProperty(BussinessGlobalThis, item, {
|
|
77
|
+
get() {
|
|
78
|
+
if (syncMap.has(item)) {
|
|
79
|
+
return syncMap.get(item);
|
|
80
|
+
}
|
|
81
|
+
return FrameworkGlobalThis[item];
|
|
82
|
+
},
|
|
83
|
+
set(value) {
|
|
84
|
+
syncMap.set(item, value);
|
|
85
|
+
},
|
|
86
|
+
configurable: true,
|
|
87
|
+
enumerable: true,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* 拷贝不可修改的属性
|
|
93
|
+
* @param items string[]
|
|
94
|
+
*/
|
|
95
|
+
const copyUnModifiable = (items) => {
|
|
96
|
+
for (const item of items) {
|
|
97
|
+
try {
|
|
98
|
+
Object.defineProperty(BussinessGlobalThis, item, {
|
|
99
|
+
get() {
|
|
100
|
+
return FrameworkGlobalThis[item];
|
|
101
|
+
},
|
|
102
|
+
set(value) {
|
|
103
|
+
// noop
|
|
104
|
+
},
|
|
105
|
+
configurable: false,
|
|
106
|
+
enumerable: true
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
console.error("Error defining property:", item, error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
copyDescriptors(baseJSValue);
|
|
115
|
+
copyDescriptors(symbolProperties);
|
|
116
|
+
// 特殊标记
|
|
117
|
+
copyDescriptors(['xhsLazyAppJs']);
|
|
118
|
+
copyValue(mpVars);
|
|
119
|
+
copyUnModifiable(proxyDynamicVars);
|
|
120
|
+
try {
|
|
121
|
+
Object.defineProperty(BussinessGlobalThis, 'globalThis', {
|
|
122
|
+
get() { return BussinessGlobalThis; },
|
|
123
|
+
set(value) {
|
|
124
|
+
// 忽略修改
|
|
125
|
+
},
|
|
126
|
+
configurable: false,
|
|
127
|
+
enumerable: true,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.error("Error defining property: globalThis", error);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 业务代码层 globalThis 的修改,需要同步到框架层的 globalThis 上,供框架层消费
|
|
135
|
+
*/
|
|
136
|
+
const copySync = (items) => {
|
|
137
|
+
for (const item of items) {
|
|
138
|
+
try {
|
|
139
|
+
Object.defineProperty(BussinessGlobalThis, item, {
|
|
140
|
+
get() {
|
|
141
|
+
return FrameworkGlobalThis[item];
|
|
142
|
+
},
|
|
143
|
+
set(value) {
|
|
144
|
+
FrameworkGlobalThis[item] = value;
|
|
145
|
+
},
|
|
146
|
+
configurable: false,
|
|
147
|
+
enumerable: false,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
console.error("Error defining property:", item, error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const getSubService = (listString) => {
|
|
156
|
+
return listString.split(';')
|
|
157
|
+
.map(item => `XHS_SERVICE_${item.trim()}`);
|
|
158
|
+
};
|
|
159
|
+
const syncList = [
|
|
160
|
+
'__MP_APP_JSON__',
|
|
161
|
+
'XHS_SERVICE',
|
|
162
|
+
...getSubService('${PKG_SLOT}'), // 替换为真实分包路径 这里要把所有的分包枚举出来
|
|
163
|
+
];
|
|
164
|
+
copySync(syncList);
|
|
165
|
+
return BussinessGlobalThis;
|
|
166
|
+
};
|
|
167
|
+
exports.getXHSSandBox = getXHSSandBox;
|
|
168
|
+
});
|
|
@@ -4,6 +4,7 @@ declare class ServiceChunkPlugin {
|
|
|
4
4
|
options: IPresetOptions;
|
|
5
5
|
constructor(options: IPresetOptions);
|
|
6
6
|
apply(compiler: IXhsCompiler): void;
|
|
7
|
-
updateIndependentService(compilation: any,
|
|
7
|
+
updateIndependentService(compilation: any, wrappedRuntimeCode: any): void;
|
|
8
|
+
updateNonIndependentService(compilation: any): void;
|
|
8
9
|
}
|
|
9
10
|
export default ServiceChunkPlugin;
|
|
@@ -7,28 +7,125 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
7
7
|
if (v !== undefined) module.exports = v;
|
|
8
8
|
}
|
|
9
9
|
else if (typeof define === "function" && define.amd) {
|
|
10
|
-
define(["require", "exports", "path", "
|
|
10
|
+
define(["require", "exports", "path", "webpack", "./sanbox"], factory);
|
|
11
11
|
}
|
|
12
12
|
})(function (require, exports) {
|
|
13
13
|
"use strict";
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const path_1 = __importDefault(require("path"));
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
16
|
+
const webpack_1 = require("webpack");
|
|
17
|
+
const sanbox_1 = require("./sanbox");
|
|
18
|
+
// 使用 webpack 内置的 sources 类型
|
|
19
|
+
const { SourceMapSource, RawSource } = webpack_1.sources;
|
|
20
|
+
// 统一处理 sourceAndMap() 返回结果的函数
|
|
21
|
+
function extractSourceAndMap(source) {
|
|
22
|
+
if (!source) {
|
|
23
|
+
return { sourceString: '', sourceMap: null };
|
|
24
|
+
}
|
|
25
|
+
// 如果是字符串,直接返回
|
|
26
|
+
if (typeof source === 'string') {
|
|
27
|
+
return { sourceString: source, sourceMap: null };
|
|
28
|
+
}
|
|
29
|
+
// 如果是 Source 对象,调用 sourceAndMap 方法
|
|
30
|
+
const result = source.sourceAndMap();
|
|
31
|
+
const sourceString = typeof result.source === 'string' ? result.source : result.source.toString();
|
|
32
|
+
return { sourceString, sourceMap: result.map };
|
|
33
|
+
}
|
|
34
|
+
// 类型安全的 sourcemap 转换函数
|
|
35
|
+
function createSourceMapSource(sourceCode, filename, sourceMap, originalSource) {
|
|
36
|
+
if (!sourceMap) {
|
|
37
|
+
return new RawSource(sourceCode);
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
return new SourceMapSource(sourceCode, filename, sourceMap, originalSource);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.warn(`Failed to create SourceMapSource for ${filename}, falling back to RawSource:`, error);
|
|
44
|
+
return new RawSource(sourceCode);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// 通用的代码包装函数
|
|
48
|
+
function wrapCodeWithGlobalCheck(code, globalVarName, filename, sourceMap, originalSource) {
|
|
49
|
+
const wrappedCode = [
|
|
50
|
+
`if (!globalThis.${globalVarName}) {`,
|
|
51
|
+
`globalThis.${globalVarName} = true;`,
|
|
52
|
+
code,
|
|
53
|
+
`}`
|
|
54
|
+
].join('\n');
|
|
55
|
+
if (sourceMap && originalSource) {
|
|
56
|
+
return createSourceMapSource(wrappedCode, filename, sourceMap, originalSource);
|
|
57
|
+
}
|
|
58
|
+
return new RawSource(wrappedCode);
|
|
59
|
+
}
|
|
60
|
+
// 为 common 代码添加注入保护包装
|
|
61
|
+
function wrapCommonCodeWithProtection(commonSource) {
|
|
62
|
+
// 使用统一的 extractSourceAndMap 函数处理所有情况
|
|
63
|
+
const { sourceString, sourceMap } = extractSourceAndMap(commonSource);
|
|
64
|
+
// 如果没有内容,返回空的源码信息
|
|
65
|
+
if (!sourceString) {
|
|
66
|
+
return { sourceString: '', sourceMap: null };
|
|
67
|
+
}
|
|
68
|
+
const wrappedSource = wrapCodeWithGlobalCheck(sourceString, '__XHS_COMMON_SERVICE_REGISTERED__', 'service-common.js', sourceMap, sourceString);
|
|
69
|
+
const wrappedSourceAndMap = extractSourceAndMap(wrappedSource);
|
|
70
|
+
return wrappedSourceAndMap;
|
|
71
|
+
}
|
|
72
|
+
// 处理业务代码的 sourcemap(支持不同的代码组合)
|
|
73
|
+
function createBusinessCodeSource(runtimeCode, serviceSource, filename, commonSource) {
|
|
74
|
+
const codeParts = [runtimeCode];
|
|
75
|
+
// 如果提供了 common 代码,则添加到代码组合中
|
|
76
|
+
if (commonSource && commonSource.sourceString) {
|
|
77
|
+
codeParts.push(commonSource.sourceString);
|
|
78
|
+
}
|
|
79
|
+
codeParts.push(serviceSource.sourceString);
|
|
80
|
+
const finalCode = codeParts.filter(Boolean).join('\n');
|
|
81
|
+
// 优先使用 service 的 sourcemap,如果没有则使用 common 的 sourcemap
|
|
82
|
+
if (serviceSource.sourceMap) {
|
|
83
|
+
return createSourceMapSource(finalCode, filename, serviceSource.sourceMap, serviceSource.sourceString);
|
|
84
|
+
}
|
|
85
|
+
if (commonSource && commonSource.sourceMap) {
|
|
86
|
+
return createSourceMapSource(finalCode, filename, commonSource.sourceMap, commonSource.sourceString);
|
|
87
|
+
}
|
|
88
|
+
return new RawSource(finalCode);
|
|
89
|
+
}
|
|
90
|
+
// bom环境变量 与微信产物对齐
|
|
91
|
+
const bomVars = [
|
|
92
|
+
'window',
|
|
93
|
+
'document',
|
|
94
|
+
'frames',
|
|
95
|
+
'self',
|
|
96
|
+
'location',
|
|
97
|
+
'navigator',
|
|
98
|
+
'localStorage',
|
|
99
|
+
'history',
|
|
100
|
+
'screen',
|
|
101
|
+
'alert',
|
|
102
|
+
'confirm',
|
|
103
|
+
'prompt',
|
|
104
|
+
];
|
|
105
|
+
function adapteServiceCode(assetName, serviceSource, packSetting, project, isIndependent) {
|
|
106
|
+
var _a;
|
|
107
|
+
const subPackages = project.getSubPackages();
|
|
19
108
|
let result;
|
|
20
109
|
if (packSetting.runInServiceSandbox) {
|
|
21
|
-
const {
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
110
|
+
const { sourceString, sourceMap } = extractSourceAndMap(serviceSource);
|
|
111
|
+
const subPackagesStr = subPackages.map(sub => sub.root).join(';');
|
|
112
|
+
const codeParts = [];
|
|
113
|
+
// 只有独立分包或主包才需要注入沙盒函数定义
|
|
114
|
+
// 非独立分包会使用主包已经定义的沙盒函数
|
|
115
|
+
if (isIndependent) {
|
|
116
|
+
// __PKG_SLOT__ 替换为真实分包路径
|
|
117
|
+
codeParts.push(`globalThis.__XHS_CACHED_SANDBOX__ = globalThis.__XHS_CACHED_SANDBOX__ || (${(_a = sanbox_1.getXHSSandBox.toString()) === null || _a === void 0 ? void 0 : _a.replace('${PKG_SLOT}', subPackagesStr)})();`);
|
|
118
|
+
}
|
|
119
|
+
// 如果在沙盒直接定义这些属性会导致 document in 等操作符返回true 所以bom变量通过函数参数来定义
|
|
120
|
+
codeParts.push(`(function(${bomVars.join(',')}) {`);
|
|
121
|
+
// 所有分包都需要 with 包装来使用沙盒环境
|
|
122
|
+
codeParts.push('with (this) {');
|
|
123
|
+
codeParts.push(sourceString);
|
|
124
|
+
codeParts.push('}'); // with 结束
|
|
125
|
+
codeParts.push('}).call(globalThis.__XHS_CACHED_SANDBOX__);');
|
|
126
|
+
const newSource = codeParts.join('\n');
|
|
127
|
+
// 使用类型安全的 sourcemap 创建函数
|
|
128
|
+
result = createSourceMapSource(newSource, assetName, sourceMap, sourceString);
|
|
32
129
|
}
|
|
33
130
|
else {
|
|
34
131
|
result = serviceSource;
|
|
@@ -48,52 +145,101 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
48
145
|
const { project, packSetting } = this.options;
|
|
49
146
|
let serviceSourceV2 = (_a = compilation.getAsset('v2/service.js')) === null || _a === void 0 ? void 0 : _a.source;
|
|
50
147
|
let serviceSource = (_b = compilation.getAsset('v1/service.js')) === null || _b === void 0 ? void 0 : _b.source;
|
|
51
|
-
|
|
148
|
+
const runtimeSourceOriginal = (_c = compilation.getAsset('runtime.js')) === null || _c === void 0 ? void 0 : _c.source;
|
|
52
149
|
// 当没有引用相同 js 场景时, commonChunk 将是 undefined
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
150
|
+
const commonSourceOriginal = (_d = compilation.getAsset('service-common.js')) === null || _d === void 0 ? void 0 : _d.source;
|
|
151
|
+
// 包装 runtime code,统一使用 extractSourceAndMap 处理
|
|
152
|
+
const { sourceString: runtimeCodeString } = extractSourceAndMap(runtimeSourceOriginal);
|
|
153
|
+
const wrappedRuntimeCodeSource = wrapCodeWithGlobalCheck(runtimeCodeString, '__XHS_WEBPACK_RUNTIME_REGISTERED__', 'runtime.js');
|
|
154
|
+
const wrappedRuntimeCode = wrappedRuntimeCodeSource.source().toString();
|
|
155
|
+
// 为 common 代码添加注入保护包装,保持 sourcemap
|
|
156
|
+
const wrappedCommonSource = wrapCommonCodeWithProtection(commonSourceOriginal);
|
|
58
157
|
if (packSetting.enableV2 && serviceSourceV2) {
|
|
59
158
|
// 注入 v2 service.js
|
|
60
159
|
// 主包加入 runtimeChunk、commonChunk
|
|
61
160
|
const assetName = 'v2/service.js';
|
|
62
|
-
|
|
161
|
+
// 先获取原始的 source 和 sourcemap
|
|
162
|
+
const serviceSource = extractSourceAndMap(serviceSourceV2);
|
|
163
|
+
// 创建业务代码的 SourceMapSource(保留 common + service 的 sourcemap)
|
|
164
|
+
const concatenatedSource = createBusinessCodeSource(wrappedRuntimeCode, serviceSource, assetName, wrappedCommonSource);
|
|
165
|
+
// 在写入之前进行 adapteServiceCode 处理(主包,需要沙盒)
|
|
166
|
+
const finalSourceWithMap = adapteServiceCode(assetName, concatenatedSource, packSetting, project, true);
|
|
63
167
|
// @ts-ignore
|
|
64
|
-
compilation.updateAsset(assetName,
|
|
168
|
+
compilation.updateAsset(assetName, finalSourceWithMap);
|
|
65
169
|
}
|
|
66
170
|
// 独立分包加入 runtimeChunk
|
|
67
171
|
if (packSetting.enableV1 && serviceSource) {
|
|
68
172
|
// // 主包加入 runtimeChunk、commonChunk
|
|
69
173
|
const assetName = 'v1/service.js';
|
|
70
|
-
|
|
174
|
+
// 先获取原始的 source 和 sourcemap
|
|
175
|
+
const v1ServiceSource = extractSourceAndMap(serviceSource);
|
|
176
|
+
// 创建业务代码的 SourceMapSource(保留 common + service 的 sourcemap)
|
|
177
|
+
const concatenatedV1Source = createBusinessCodeSource(wrappedRuntimeCode, v1ServiceSource, assetName, wrappedCommonSource);
|
|
178
|
+
// 在写入之前进行 adapteServiceCode 处理(主包,需要沙盒)
|
|
179
|
+
const finalV1SourceWithMap = adapteServiceCode(assetName, concatenatedV1Source, packSetting, project, true);
|
|
71
180
|
// @ts-ignore
|
|
72
|
-
compilation.updateAsset(assetName,
|
|
181
|
+
compilation.updateAsset(assetName, finalV1SourceWithMap);
|
|
73
182
|
}
|
|
74
|
-
|
|
183
|
+
// 处理独立分包(只需要 runtime,不需要 common)
|
|
184
|
+
this.updateIndependentService(compilation, wrappedRuntimeCode);
|
|
185
|
+
// 处理非独立分包(不需要 runtime 和 common)
|
|
186
|
+
this.updateNonIndependentService(compilation);
|
|
75
187
|
compilation.deleteAsset('runtime.js');
|
|
76
188
|
compilation.deleteAsset('service-common.js');
|
|
77
189
|
});
|
|
78
190
|
});
|
|
79
191
|
}
|
|
80
|
-
updateIndependentService(compilation,
|
|
192
|
+
updateIndependentService(compilation, wrappedRuntimeCode) {
|
|
81
193
|
const { project, packSetting } = this.options;
|
|
82
194
|
const usingPackageType = packSetting.usingPackageType;
|
|
83
195
|
const subPackages = project.getSubPackages();
|
|
84
196
|
subPackages
|
|
85
197
|
.filter(sub => sub.independent)
|
|
86
|
-
.
|
|
87
|
-
|
|
198
|
+
.forEach(sub => {
|
|
199
|
+
var _a;
|
|
200
|
+
const servicePath = path_1.default.join(usingPackageType, sub.root, 'service.js');
|
|
201
|
+
if (compilation.getAsset(servicePath)) {
|
|
202
|
+
const independentServiceSource = (_a = compilation.getAsset(servicePath)) === null || _a === void 0 ? void 0 : _a.source;
|
|
203
|
+
// 先获取原始的 source 和 sourcemap
|
|
204
|
+
const indepServiceSource = extractSourceAndMap(independentServiceSource);
|
|
205
|
+
// 创建独立分包的业务代码(只包含 runtime + service,不包含 common)
|
|
206
|
+
const concatenatedIndepSource = createBusinessCodeSource(wrappedRuntimeCode, indepServiceSource, servicePath
|
|
207
|
+
// 不传入 commonSource,所以不包含 common 代码
|
|
208
|
+
);
|
|
209
|
+
// 在写入之前进行 adapteServiceCode 处理(独立分包,需要沙盒)
|
|
210
|
+
const finalIndepSourceWithMap = adapteServiceCode(servicePath, concatenatedIndepSource, packSetting, project, true);
|
|
211
|
+
// 写入
|
|
212
|
+
compilation.updateAsset(servicePath, finalIndepSourceWithMap);
|
|
213
|
+
if (packSetting.enableV2 && packSetting.enableV1) {
|
|
214
|
+
compilation.updateAsset(servicePath.replace(usingPackageType, 'v1'), finalIndepSourceWithMap);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
updateNonIndependentService(compilation) {
|
|
220
|
+
const { project, packSetting } = this.options;
|
|
221
|
+
const usingPackageType = packSetting.usingPackageType;
|
|
222
|
+
const subPackages = project.getSubPackages();
|
|
223
|
+
subPackages
|
|
224
|
+
.filter(sub => !sub.independent) // 非独立分包
|
|
225
|
+
.forEach(sub => {
|
|
88
226
|
var _a;
|
|
227
|
+
const servicePath = path_1.default.join(usingPackageType, sub.root, 'service.js');
|
|
89
228
|
if (compilation.getAsset(servicePath)) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
229
|
+
const nonIndependentServiceSource = (_a = compilation.getAsset(servicePath)) === null || _a === void 0 ? void 0 : _a.source;
|
|
230
|
+
// 先获取原始的 source 和 sourcemap
|
|
231
|
+
const serviceSource = extractSourceAndMap(nonIndependentServiceSource);
|
|
232
|
+
// 非独立分包只需要处理自己的 service 代码,不需要 runtime 和 common
|
|
233
|
+
const processedServiceSource = createBusinessCodeSource('', // 不需要 runtime
|
|
234
|
+
serviceSource, servicePath
|
|
235
|
+
// 不传入 commonSource,所以不包含 common 代码
|
|
236
|
+
);
|
|
237
|
+
// 在写入之前进行 adapteServiceCode 处理(非独立分包,不需要沙盒)
|
|
238
|
+
const finalServiceWithMap = adapteServiceCode(servicePath, processedServiceSource, packSetting, project, false);
|
|
93
239
|
// 写入
|
|
94
|
-
compilation.updateAsset(servicePath,
|
|
240
|
+
compilation.updateAsset(servicePath, finalServiceWithMap);
|
|
95
241
|
if (packSetting.enableV2 && packSetting.enableV1) {
|
|
96
|
-
compilation.updateAsset(servicePath.replace(usingPackageType, 'v1'),
|
|
242
|
+
compilation.updateAsset(servicePath.replace(usingPackageType, 'v1'), finalServiceWithMap);
|
|
97
243
|
}
|
|
98
244
|
}
|
|
99
245
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xhs-mp-compiler-cli",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.33-beta.0",
|
|
4
4
|
"description": "xhs mp command tool.",
|
|
5
5
|
"preferGlobal": true,
|
|
6
6
|
"category": "esm",
|
|
@@ -88,18 +88,18 @@
|
|
|
88
88
|
"webpack-chain": "^6.5.1",
|
|
89
89
|
"webpack-sources": "^3.2.2",
|
|
90
90
|
"xhs-mp-workerpool": "^9.2.0",
|
|
91
|
-
"xhs-mp-ml-loader": "2.0.
|
|
92
|
-
"xhs-mp-compiler-utils": "2.0.
|
|
93
|
-
"xhs-mp-pack": "2.0.
|
|
94
|
-
"xhs-mp-project": "2.0.
|
|
95
|
-
"xhs-mp-shared": "2.0.
|
|
96
|
-
"xhs-mp-shared-fs": "2.0.
|
|
97
|
-
"xhs-mp-sjs-loader": "2.0.
|
|
98
|
-
"xhs-mp-sketch-loader": "2.0.
|
|
91
|
+
"xhs-mp-ml-loader": "2.0.33-beta.0",
|
|
92
|
+
"xhs-mp-compiler-utils": "2.0.33-beta.0",
|
|
93
|
+
"xhs-mp-pack": "2.0.33-beta.0",
|
|
94
|
+
"xhs-mp-project": "2.0.33-beta.0",
|
|
95
|
+
"xhs-mp-shared": "2.0.33-beta.0",
|
|
96
|
+
"xhs-mp-shared-fs": "2.0.33-beta.0",
|
|
97
|
+
"xhs-mp-sjs-loader": "2.0.33-beta.0",
|
|
98
|
+
"xhs-mp-sketch-loader": "2.0.33-beta.0",
|
|
99
99
|
"yauzl": "^2.10.0"
|
|
100
100
|
},
|
|
101
101
|
"peerDependencies": {
|
|
102
|
-
"xhs-mp-ml-parser": "2.0.
|
|
102
|
+
"xhs-mp-ml-parser": "2.0.33-beta.0"
|
|
103
103
|
},
|
|
104
104
|
"devDependencies": {
|
|
105
105
|
"@types/babel__generator": "7.6.3",
|
|
@@ -122,7 +122,7 @@
|
|
|
122
122
|
"@types/node": "14",
|
|
123
123
|
"typescript": "5.1.6",
|
|
124
124
|
"webpack-dev-server": "4.0.0-beta.3",
|
|
125
|
-
"xhs-mp-ml-parser": "2.0.
|
|
125
|
+
"xhs-mp-ml-parser": "2.0.33-beta.0"
|
|
126
126
|
},
|
|
127
127
|
"scripts": {
|
|
128
128
|
"version": "formula changelog && git add .",
|