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.
- package/dist/bin/xhs-mp-cli-build.js +48 -34
- package/dist/bin/xhs-mp-cli-dev.js +44 -44
- package/dist/bin/xhs-mp-cli.js +22 -12
- package/dist/compiler.d.ts +21 -28
- package/dist/compiler.js +327 -327
- package/dist/compilerImpl.d.ts +19 -0
- package/dist/compilerImpl.js +173 -0
- package/dist/config/constant.config.d.ts +24 -0
- package/dist/config/constant.config.js +82 -30
- package/dist/config/dir.config.js +23 -13
- package/dist/index.d.ts +9 -9
- package/dist/index.js +454 -408
- package/dist/packs/index.d.ts +4 -4
- package/dist/packs/index.js +38 -30
- package/dist/packs/mp-pack/index.js +38 -30
- package/dist/packs/webpack/dev-server/index.js +61 -50
- package/dist/packs/webpack/dev-server/lib/ensurePort.js +33 -23
- package/dist/packs/webpack/dev-server/lib/openBrowser.js +28 -18
- package/dist/packs/webpack/index.d.ts +1 -1
- package/dist/packs/webpack/index.js +99 -90
- package/dist/packs/webpack/webpack.d.ts +2 -1
- package/dist/packs/webpack/webpack.js +81 -63
- package/dist/presets/babel.d.ts +0 -1
- package/dist/presets/babel.js +64 -58
- package/dist/presets/common.js +42 -45
- package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +50 -40
- package/dist/presets/configs/minigame/assets/index.js +62 -52
- package/dist/presets/configs/minigame/service/index.js +49 -30
- package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +33 -23
- package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +2 -2
- package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -60
- package/dist/presets/configs/minigame/service/serviceEntryPlugin.d.ts +3 -3
- package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +39 -29
- package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +120 -113
- package/dist/presets/configs/miniprogram/assets/index.js +45 -43
- package/dist/presets/configs/miniprogram/render/component.js +51 -41
- package/dist/presets/configs/miniprogram/render/index.d.ts +2 -3
- package/dist/presets/configs/miniprogram/render/index.js +85 -68
- package/dist/presets/configs/miniprogram/render/render.js +53 -43
- package/dist/presets/configs/miniprogram/render/renderChunkPlugin.js +84 -49
- package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
- package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.js +164 -126
- package/dist/presets/configs/miniprogram/service/index.js +82 -65
- package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.js +60 -50
- package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +93 -55
- package/dist/presets/index.js +100 -90
- package/dist/presets/loaders/mg-entry-loader.js +32 -22
- package/dist/presets/loaders/mini-style-loader.js +79 -55
- package/dist/presets/loaders/mp-entry-loader.js +149 -140
- package/dist/presets/loaders/wxss-loader.js +73 -63
- package/dist/presets/plugins/InjectorPlugin.js +23 -13
- package/dist/presets/plugins/SourceMapDevToolPlugin.d.ts +1 -0
- package/dist/presets/plugins/SourceMapDevToolPlugin.js +23 -0
- package/dist/presets/prod.js +88 -78
- package/dist/sharedFs.d.ts +2 -0
- package/dist/sharedFs.js +24 -0
- package/dist/types/index.d.ts +11 -37
- package/dist/types/index.js +23 -13
- package/dist/utils/{apm/index.d.ts → apm.d.ts} +5 -3
- package/dist/utils/apm.js +245 -0
- package/dist/utils/cache.d.ts +28 -0
- package/dist/utils/cache.js +49 -0
- package/dist/utils/common.js +42 -32
- package/dist/utils/css.d.ts +1 -1
- package/dist/utils/css.js +43 -35
- package/dist/utils/depenedHelper.js +95 -85
- package/dist/utils/file.js +39 -29
- package/dist/utils/loader-utils.js +63 -53
- package/dist/utils/postcssRpx2Vw.js +43 -33
- package/dist/utils/project.d.ts +3 -2
- package/dist/utils/project.js +129 -119
- package/dist/utils/projectConfig.js +24 -15
- package/dist/utils/utils.js +51 -42
- package/dist/utils/workerPool.d.ts +29 -0
- package/dist/utils/workerPool.js +125 -0
- package/dist/utils/zip.js +239 -229
- package/package.json +33 -14
- package/dist/compilerCP.d.ts +0 -1
- package/dist/compilerCP.js +0 -175
- package/dist/presets/loaders/sjs-loader/index.d.ts +0 -12
- package/dist/presets/loaders/sjs-loader/index.js +0 -605
- package/dist/presets/loaders/sjs-loader/scope.d.ts +0 -36
- package/dist/presets/loaders/sjs-loader/scope.js +0 -122
- package/dist/presets/loaders/sjs-loader/tranform.d.ts +0 -2
- package/dist/presets/loaders/sjs-loader/tranform.js +0 -31
- package/dist/utils/apm/index.js +0 -198
- package/dist/utils/asyncWebpack.d.ts +0 -3
- package/dist/utils/asyncWebpack.js +0 -38
- package/dist/utils/const.d.ts +0 -27
- package/dist/utils/const.js +0 -47
- package/dist/utils/getSuffixName.d.ts +0 -3
- package/dist/utils/getSuffixName.js +0 -18
- package/dist/utils/tagTransformMap.d.ts +0 -36
- package/dist/utils/tagTransformMap.js +0 -39
|
@@ -1,49 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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);
|
|
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;
|
|
11
5
|
}
|
|
12
|
-
if (
|
|
13
|
-
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "xhs-mp-compiler-utils", "xhs-mp-pack"], factory);
|
|
14
8
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
exports
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getRenderEntry = void 0;
|
|
13
|
+
const xhs_mp_compiler_utils_1 = require("xhs-mp-compiler-utils");
|
|
14
|
+
const xhs_mp_pack_1 = require("xhs-mp-pack");
|
|
15
|
+
const getRenderEntry = (options) => {
|
|
16
|
+
const entries = {};
|
|
17
|
+
const { packSetting } = options;
|
|
18
|
+
if (packSetting.enableV2) {
|
|
19
|
+
appendV2Entry(options, entries);
|
|
26
20
|
}
|
|
27
|
-
|
|
28
|
-
|
|
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)()}`;
|
|
21
|
+
if (packSetting.enableV1) {
|
|
22
|
+
appendV1Entry(options, entries);
|
|
31
23
|
}
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
24
|
+
return entries;
|
|
25
|
+
};
|
|
26
|
+
exports.getRenderEntry = getRenderEntry;
|
|
27
|
+
const appendV2Entry = (options, entries) => {
|
|
28
|
+
const { project, packSetting } = options;
|
|
29
|
+
const projectMiniprogramPath = project.projectMiniprogramPath;
|
|
30
|
+
const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
|
|
31
|
+
if (mainPkg) {
|
|
32
|
+
const pages = project.getPages();
|
|
33
|
+
for (const { path } of pages) {
|
|
34
|
+
entries[`v2/${path}.render`] = `mp-entry-loader?type=render&mode=v2&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!${projectMiniprogramPath}/${path}.${(0, xhs_mp_compiler_utils_1.getMlSuffixName)()}`;
|
|
35
|
+
}
|
|
42
36
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
for (const { pages, root } of subPkgs) {
|
|
38
|
+
for (const { path } of pages) {
|
|
39
|
+
entries[`v2/${path}.render`] = `mp-entry-loader?type=render&mode=v2&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!${projectMiniprogramPath}/${path}.${(0, xhs_mp_compiler_utils_1.getMlSuffixName)()}`;
|
|
40
|
+
}
|
|
47
41
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
42
|
+
};
|
|
43
|
+
const appendV1Entry = (options, entries) => {
|
|
44
|
+
const { project, packSetting } = options;
|
|
45
|
+
const projectMiniprogramPath = project.projectMiniprogramPath;
|
|
46
|
+
const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries();
|
|
47
|
+
if (mainPkg) {
|
|
48
|
+
const pages = project.getPages();
|
|
49
|
+
for (const { path } of pages) {
|
|
50
|
+
entries[`v1/${path}.render`] = `mp-entry-loader?type=render&mode=v1&pkg=${xhs_mp_pack_1.CONSTANTS.MAIN_PKG}!${projectMiniprogramPath}/${path}.${(0, xhs_mp_compiler_utils_1.getMlSuffixName)()}`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const { pages, root } of subPkgs) {
|
|
54
|
+
for (const { path } of pages) {
|
|
55
|
+
entries[`v1/${path}.render`] = `mp-entry-loader?type=render&mode=v1&pkg=${xhs_mp_pack_1.CONSTANTS.SUB_PKG}&root=${root}!${projectMiniprogramPath}/${path}.${(0, xhs_mp_compiler_utils_1.getMlSuffixName)()}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
});
|
|
@@ -1,55 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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", "../../../../packs"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const packs_1 = require("../../../../packs");
|
|
13
|
+
/**
|
|
14
|
+
* 注入变量不可以使用 let const var 变量形式
|
|
15
|
+
* 因为2.0 架构下 render.js 和 component.js 运行在逻辑层(只有一个环境) 会导致变量冲突
|
|
16
|
+
*/
|
|
17
|
+
const common = `
|
|
18
|
+
globalThis.__XHS_WEBVIEW = (globalThis.localStorage && globalThis.document && globalThis.document.querySelectorAll);
|
|
19
|
+
`;
|
|
20
|
+
const injectLink = `
|
|
21
|
+
${common}
|
|
22
|
+
globalThis.__XHS_ADD_LINK = function(src){
|
|
23
|
+
if (!globalThis.__XHS_WEBVIEW) return;
|
|
24
|
+
const host = globalThis.SmallApp && globalThis.SmallApp.__BOOTSTRAP_HOST__
|
|
25
|
+
const link = document.createElement('link')
|
|
26
|
+
link.href = host + "/" + src
|
|
27
|
+
link.type = 'text/css'
|
|
28
|
+
link.rel = 'stylesheet'
|
|
29
|
+
link.setAttribute('xhs-p', src)
|
|
30
|
+
document.head.appendChild(link);
|
|
31
|
+
};
|
|
32
|
+
`;
|
|
33
|
+
/**
|
|
34
|
+
* 2.0模式下 render.js 和 component.js 运行在逻辑层
|
|
35
|
+
* 目前 2.0 各项功能已经稳定 降级可能性不大
|
|
36
|
+
* 2.0中原先css打包了两份 一份是 .css 文件 一份是 在render.js中注入的style标签
|
|
37
|
+
* 现在2.0 打包 render.js 中改为使用link标签注入,这样降级到 1.0 时仍然正常运行
|
|
38
|
+
* 且不会使得包体积变大
|
|
39
|
+
* 2.0 后面量起来后,可全部采用加载link方式打包,不必区分1.0 2.0
|
|
40
|
+
*/
|
|
41
|
+
const injectStyle = `
|
|
42
|
+
${common}
|
|
43
|
+
globalThis.__XHS_ADD_STYLE = function(style, src){
|
|
44
|
+
if (!globalThis.__XHS_WEBVIEW) return;
|
|
45
|
+
const cssTextNode = document.createTextNode(String(style))
|
|
46
|
+
const styleNode = document.createElement('style')
|
|
47
|
+
styleNode.setAttribute('xhs-p', src)
|
|
12
48
|
styleNode.appendChild(cssTextNode)
|
|
13
|
-
head.appendChild(styleNode)
|
|
49
|
+
document.head.appendChild(styleNode)
|
|
14
50
|
};
|
|
15
51
|
`;
|
|
16
|
-
// 注入css跟webpack runtime
|
|
17
|
-
class RenderChunkPlugin {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
compilation.updateAsset(key, new packs_1.ConcatSource(injectCode, assets[key]));
|
|
52
|
+
// 注入css跟webpack runtime
|
|
53
|
+
class RenderChunkPlugin {
|
|
54
|
+
constructor(options) {
|
|
55
|
+
this.options = options;
|
|
56
|
+
}
|
|
57
|
+
apply(compiler) {
|
|
58
|
+
compiler.hooks.compilation.tap(RenderChunkPlugin.name, compilation => {
|
|
59
|
+
compilation.hooks.processAssets.tap({
|
|
60
|
+
name: RenderChunkPlugin.name,
|
|
61
|
+
stage: packs_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
|
|
62
|
+
}, assets => {
|
|
63
|
+
var _a, _b;
|
|
64
|
+
const { project, packSetting } = this.options;
|
|
65
|
+
// 注入css逻辑
|
|
66
|
+
const injectCode = packSetting.enableVDom ? injectLink : injectStyle;
|
|
67
|
+
const appJSON = project.appJSON;
|
|
68
|
+
const pathMap = [
|
|
69
|
+
(_a = appJSON.pages) === null || _a === void 0 ? void 0 : _a.map(({ path }) => path),
|
|
70
|
+
(_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); })
|
|
71
|
+
]
|
|
72
|
+
.flat(4)
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.reduce((ac, path) => {
|
|
75
|
+
ac[`v1/${path}.render.js`] = true;
|
|
76
|
+
ac[`v2/${path}.render.js`] = true;
|
|
77
|
+
return ac;
|
|
78
|
+
}, {});
|
|
79
|
+
// 将runtime 塞到render.js中去
|
|
80
|
+
Object.keys(assets).forEach(key => {
|
|
81
|
+
if (!pathMap[key])
|
|
82
|
+
return;
|
|
83
|
+
compilation.updateAsset(key, new packs_1.ConcatSource(injectCode, assets[key]));
|
|
84
|
+
});
|
|
50
85
|
});
|
|
51
86
|
});
|
|
52
|
-
}
|
|
87
|
+
}
|
|
53
88
|
}
|
|
54
|
-
|
|
55
|
-
|
|
89
|
+
exports.default = RenderChunkPlugin;
|
|
90
|
+
});
|
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
import { Compilation, ICompiler } from '../../../../packs';
|
|
2
2
|
import { IPresetOptions } from '../../../../types';
|
|
3
|
+
/**
|
|
4
|
+
* @NOTE 分包中使用 SJS 的规则
|
|
5
|
+
*
|
|
6
|
+
* - 整包情况下,可以在所有页面中引用整包内的任意SJS模块。
|
|
7
|
+
* - 分包情况下:
|
|
8
|
+
* - 主包不能引入分包的 SJS 模块。
|
|
9
|
+
* - 分包内部可以引用主分包内的SJS模块。
|
|
10
|
+
* - 分包A 和 分包B 之间不可以互相引用SJS模块。
|
|
11
|
+
* - 独立分包内的SJS模块只能在独立分包内使用,不能跨分包引用。
|
|
12
|
+
*/
|
|
3
13
|
declare class SjsEntryPlugin {
|
|
4
14
|
options: IPresetOptions;
|
|
5
15
|
entriesCache: Map<string, boolean>;
|
|
6
16
|
mainpkgPaths: string[];
|
|
7
17
|
subPackagePaths: Map<string, string[]>;
|
|
8
18
|
subPackageRoots: string[];
|
|
19
|
+
_compilation: Compilation;
|
|
9
20
|
constructor(options: IPresetOptions);
|
|
10
21
|
isFileInMainPkg(filePath: any): boolean;
|
|
11
22
|
isFileInSubpackage(filePath: any): boolean;
|
|
23
|
+
get compilation(): Compilation;
|
|
24
|
+
set compilation(value: Compilation);
|
|
25
|
+
emitError(details: string): void;
|
|
26
|
+
generateMessage(_errs: string | string[], fileName?: string): string;
|
|
12
27
|
apply(compiler: ICompiler): void;
|
|
13
28
|
makeSjsFileContent(compilation: Compilation, chunkName: string, sjsFiles: string[] | undefined, sjsModuleName: string, pkgRoot?: string): void;
|
|
14
29
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
2
|
if (k2 === undefined) k2 = k;
|
|
4
3
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -25,139 +24,178 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
24
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
25
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
26
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
*/
|
|
45
|
-
function formatSJSPath(i) {
|
|
46
|
-
if (/\.(sjs|wxs)$/.test(i)) {
|
|
47
|
-
return (0, common_1.toUnixPath)(i);
|
|
27
|
+
(function (factory) {
|
|
28
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
29
|
+
var v = factory(require, exports);
|
|
30
|
+
if (v !== undefined) module.exports = v;
|
|
48
31
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
throw new packs_1.WebpackError(`[SJS文件编译错误] ${i}.sjs 不存在`);
|
|
32
|
+
else if (typeof define === "function" && define.amd) {
|
|
33
|
+
define(["require", "exports", "fs-extra", "glob", "path", "../../../../packs", "xhs-mp-sjs-loader", "../../../../utils/common"], factory);
|
|
52
34
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
35
|
+
})(function (require, exports) {
|
|
36
|
+
"use strict";
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
39
|
+
const glob_1 = __importDefault(require("glob"));
|
|
40
|
+
const path_1 = __importDefault(require("path"));
|
|
41
|
+
const packs_1 = require("../../../../packs");
|
|
42
|
+
const xhs_mp_sjs_loader_1 = __importStar(require("xhs-mp-sjs-loader"));
|
|
43
|
+
const common_1 = require("../../../../utils/common");
|
|
44
|
+
/**
|
|
45
|
+
* @NOTE 分包中使用 SJS 的规则
|
|
46
|
+
*
|
|
47
|
+
* - 整包情况下,可以在所有页面中引用整包内的任意SJS模块。
|
|
48
|
+
* - 分包情况下:
|
|
49
|
+
* - 主包不能引入分包的 SJS 模块。
|
|
50
|
+
* - 分包内部可以引用主分包内的SJS模块。
|
|
51
|
+
* - 分包A 和 分包B 之间不可以互相引用SJS模块。
|
|
52
|
+
* - 独立分包内的SJS模块只能在独立分包内使用,不能跨分包引用。
|
|
53
|
+
*/
|
|
54
|
+
class SjsEntryPlugin {
|
|
55
|
+
constructor(options) {
|
|
56
|
+
this.options = options;
|
|
57
|
+
this.entriesCache = new Map();
|
|
72
58
|
this.mainpkgPaths = [];
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.subPackageRoots
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
59
|
+
this.subPackagePaths = new Map();
|
|
60
|
+
this.subPackageRoots = [];
|
|
61
|
+
}
|
|
62
|
+
isFileInMainPkg(filePath) {
|
|
63
|
+
return this.subPackageRoots.every(root => !filePath.startsWith(`${root}/`));
|
|
64
|
+
}
|
|
65
|
+
isFileInSubpackage(filePath) {
|
|
66
|
+
return this.subPackageRoots.some(root => filePath.startsWith(`${root}/`));
|
|
67
|
+
}
|
|
68
|
+
get compilation() {
|
|
69
|
+
return this._compilation;
|
|
70
|
+
}
|
|
71
|
+
set compilation(value) {
|
|
72
|
+
this._compilation = value;
|
|
73
|
+
}
|
|
74
|
+
emitError(details) {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
(_b = (_a = this.compilation) === null || _a === void 0 ? void 0 : _a.errors) === null || _b === void 0 ? void 0 : _b.push(new packs_1.WebpackError(details));
|
|
77
|
+
}
|
|
78
|
+
generateMessage(_errs, fileName = '') {
|
|
79
|
+
const errs = Array.isArray(_errs) ? _errs : [_errs];
|
|
80
|
+
return [
|
|
81
|
+
'\n<xhsml!',
|
|
82
|
+
(fileName === null || fileName === void 0 ? void 0 : fileName.trim()) ? `[error:sjs编译错误]: [file:${fileName === null || fileName === void 0 ? void 0 : fileName.trim()}]` : '[error:sjs编译错误]',
|
|
83
|
+
...errs,
|
|
84
|
+
'!xhsml>\n'
|
|
85
|
+
].join('\n');
|
|
86
|
+
}
|
|
87
|
+
apply(compiler) {
|
|
88
|
+
compiler.hooks.watchRun.tap('SjsEntryPlugin', () => {
|
|
89
|
+
this.subPackagePaths.clear();
|
|
90
|
+
this.mainpkgPaths = [];
|
|
81
91
|
});
|
|
82
|
-
|
|
83
|
-
.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
this.
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
compiler.hooks.emit.tapAsync('SjsEntryPlugin', (compilation, callback) => {
|
|
93
|
+
this.compilation = compilation;
|
|
94
|
+
const { project } = this.options;
|
|
95
|
+
const { appJSON, miniprogramDir } = project;
|
|
96
|
+
this.subPackageRoots = appJSON.subPackages.map(pkg => pkg.root);
|
|
97
|
+
// 默认输出一个空的sjs文件
|
|
98
|
+
this.subPackageRoots.forEach(root => {
|
|
99
|
+
this.subPackagePaths.has(root) || this.subPackagePaths.set(root, []);
|
|
100
|
+
});
|
|
101
|
+
const relativePaths = glob_1.default.sync('**/*.sjs', { cwd: miniprogramDir })
|
|
102
|
+
.concat(glob_1.default.sync('**/*.wxs', { cwd: miniprogramDir }));
|
|
103
|
+
relativePaths.forEach(relativePath => {
|
|
104
|
+
var _a;
|
|
105
|
+
const sjsAbsPath = (0, common_1.unixJoin)(miniprogramDir, relativePath);
|
|
106
|
+
// 如果是主包内的文件
|
|
107
|
+
if (this.isFileInMainPkg(relativePath)) {
|
|
108
|
+
this.mainpkgPaths.push(sjsAbsPath);
|
|
96
109
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
110
|
+
// 如果命中了分包内的文件
|
|
111
|
+
if (this.isFileInSubpackage(relativePath)) {
|
|
112
|
+
const root = this.subPackageRoots.find(root => relativePath.startsWith(`${root}/`));
|
|
113
|
+
if (root) {
|
|
114
|
+
(_a = this.subPackagePaths.get(root)) === null || _a === void 0 ? void 0 : _a.push(sjsAbsPath);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
// 主包
|
|
119
|
+
this.makeSjsFileContent(compilation, 'sjs.js', [...this.mainpkgPaths], 'XHS_SJS');
|
|
120
|
+
// 分包
|
|
121
|
+
Array.from(this.subPackagePaths.keys()).forEach(pkgRoot => {
|
|
122
|
+
const paths = this.subPackagePaths.get(pkgRoot);
|
|
123
|
+
this.makeSjsFileContent(compilation, `${pkgRoot}/sjs.js`, [...paths], `XHS_SJS_${pkgRoot}`, pkgRoot);
|
|
124
|
+
});
|
|
125
|
+
callback();
|
|
105
126
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (sjsPathCache.has(sjsPath)) {
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
const sjsAbsPath = formatSJSPath(sjsPath);
|
|
125
|
-
const content = fs_extra_1.default.readFileSync(sjsAbsPath, 'utf8');
|
|
126
|
-
const result = index_1.default.call(compilation, content, requirePath => {
|
|
127
|
-
const realPath = (0, common_1.unixResolve)(path_1.default.dirname(sjsAbsPath), requirePath);
|
|
128
|
-
const relativePath = (0, common_1.getShortPath)(project.miniprogramDir, realPath);
|
|
129
|
-
/**
|
|
130
|
-
* 不是当前分包的 sjs 文件不处理
|
|
131
|
-
*/
|
|
132
|
-
if (pkgRoot && !relativePath.startsWith(pkgRoot)) {
|
|
133
|
-
throw new Error(`require(${JSON.stringify(requirePath)}),请检测引入路径是否正确或符合分包规则!`);
|
|
127
|
+
}
|
|
128
|
+
makeSjsFileContent(compilation, chunkName, sjsFiles = [], sjsModuleName, pkgRoot) {
|
|
129
|
+
var _a;
|
|
130
|
+
const { project, packSetting } = this.options;
|
|
131
|
+
const sjsModules = [];
|
|
132
|
+
// 用于去除重复引入
|
|
133
|
+
const sjsPathCache = new Set();
|
|
134
|
+
/**
|
|
135
|
+
* @NOTE
|
|
136
|
+
* 在这里校验 sjs 模块引入的路径是否符合规则,分包的规则见上方
|
|
137
|
+
*/
|
|
138
|
+
let sjsPath;
|
|
139
|
+
while ((sjsPath = sjsFiles.shift())) {
|
|
140
|
+
if (sjsPathCache.has(sjsPath)) {
|
|
141
|
+
continue;
|
|
134
142
|
}
|
|
135
|
-
|
|
136
|
-
|
|
143
|
+
let sjsAbsPath = '';
|
|
144
|
+
if (/\.(sjs|wxs)$/.test(sjsPath)) {
|
|
145
|
+
sjsAbsPath = (0, common_1.toUnixPath)(sjsPath);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
this.emitError(this.generateMessage(`文件后缀不正确 ${sjsPath}`));
|
|
149
|
+
}
|
|
150
|
+
const content = fs_extra_1.default.readFileSync(sjsAbsPath, 'utf8');
|
|
151
|
+
try {
|
|
152
|
+
const result = xhs_mp_sjs_loader_1.default.call(compilation, content, requirePath => {
|
|
153
|
+
const realPath = (0, common_1.unixResolve)(path_1.default.dirname(sjsAbsPath), requirePath);
|
|
154
|
+
const relativePath = (0, common_1.getShortPath)(project.miniprogramDir, realPath);
|
|
155
|
+
/**
|
|
156
|
+
* 不是当前分包的 sjs 文件不处理
|
|
157
|
+
*/
|
|
158
|
+
if (pkgRoot && !relativePath.startsWith(pkgRoot)) {
|
|
159
|
+
this.emitError(this.generateMessage(`require(${JSON.stringify(requirePath)}),请检测引入路径是否正确或符合分包规则!`, sjsAbsPath));
|
|
160
|
+
}
|
|
161
|
+
else if (!fs_extra_1.default.existsSync(realPath)) {
|
|
162
|
+
this.emitError(this.generateMessage(`require(${JSON.stringify(requirePath)}),请检测引入路径是否正确!`, sjsAbsPath));
|
|
163
|
+
}
|
|
164
|
+
else if (!/\.(sjs|wxs)$/.test(requirePath)) {
|
|
165
|
+
this.emitError(this.generateMessage(`require(${JSON.stringify(requirePath)}),仅支持引入 .js 后缀结尾的文件!`, sjsAbsPath));
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
sjsFiles.push(realPath);
|
|
169
|
+
}
|
|
170
|
+
return relativePath;
|
|
171
|
+
});
|
|
172
|
+
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
|
|
173
|
+
this.emitError(this.generateMessage(result.errors.map(i => i.message).join("\n"), sjsAbsPath));
|
|
174
|
+
}
|
|
175
|
+
const moduleName = (0, common_1.getShortPath)(project.miniprogramDir, sjsAbsPath);
|
|
176
|
+
// 老版本兼容代码
|
|
177
|
+
sjsModules.push(`"${moduleName}": ${JSON.stringify(result.legacyCode)}`);
|
|
178
|
+
// 模块化代码
|
|
179
|
+
sjsModules.push([
|
|
180
|
+
`".sjs#${moduleName}": function(exports,require,module,${xhs_mp_sjs_loader_1.SJS_HELPER}){`,
|
|
181
|
+
`${result.code};`,
|
|
182
|
+
`return module;}`
|
|
183
|
+
].join('\n'));
|
|
184
|
+
// 处理后的加入缓存
|
|
185
|
+
sjsPathCache.add(sjsPath);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
this.emitError(this.generateMessage(error.message, sjsAbsPath));
|
|
137
189
|
}
|
|
138
|
-
sjsFiles.push(realPath);
|
|
139
|
-
return relativePath;
|
|
140
|
-
});
|
|
141
|
-
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
|
|
142
|
-
throw new packs_1.WebpackError(`[SJS 文件编译错误] ${sjsAbsPath}\n${result.errors.map(i => i.message).join("\n")}`);
|
|
143
190
|
}
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
const source = new packs_1.ConcatSource(`globalThis['${sjsModuleName}'] = {`, `${sjsModules.join(',\n')}`, '}');
|
|
153
|
-
if (packSetting.enableV1) {
|
|
154
|
-
// @ts-ignore
|
|
155
|
-
compilation.assets[`v1/${chunkName}`] = source;
|
|
156
|
-
}
|
|
157
|
-
if (packSetting.enableV2) {
|
|
158
|
-
// @ts-ignore
|
|
159
|
-
compilation.assets[`v2/${chunkName}`] = source;
|
|
191
|
+
const source = new packs_1.ConcatSource(`globalThis['${sjsModuleName}'] = {`, `${sjsModules.join(',\n')}`, '}');
|
|
192
|
+
if (packSetting.enableV1) {
|
|
193
|
+
compilation.assets[`v1/${chunkName}`] = source;
|
|
194
|
+
}
|
|
195
|
+
if (packSetting.enableV2) {
|
|
196
|
+
compilation.assets[`v2/${chunkName}`] = source;
|
|
197
|
+
}
|
|
160
198
|
}
|
|
161
199
|
}
|
|
162
|
-
|
|
163
|
-
|
|
200
|
+
exports.default = SjsEntryPlugin;
|
|
201
|
+
});
|