xhs-mp-compiler-cli 1.9.5 → 1.9.6

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.
@@ -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
  }
@@ -51,16 +51,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
51
51
  * - 分包A 和 分包B 之间不可以互相引用SJS模块。
52
52
  * - 独立分包内的SJS模块只能在独立分包内使用,不能跨分包引用。
53
53
  */
54
- function formatSJSPath(i) {
55
- if (/\.(sjs|wxs)$/.test(i)) {
56
- return (0, common_1.toUnixPath)(i);
57
- }
58
- const suffix = ['sjs', 'wxs'].find(n => fs_extra_1.default.existsSync(`${i}.${n}`));
59
- if (!suffix) {
60
- throw new packs_1.WebpackError(`[SJS文件编译错误] ${i}.sjs 不存在`);
61
- }
62
- return (0, common_1.toUnixPath)(suffix ? `${i}.${suffix}` : i);
63
- }
64
54
  class SjsEntryPlugin {
65
55
  constructor(options) {
66
56
  this.options = options;
@@ -75,12 +65,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
75
65
  isFileInSubpackage(filePath) {
76
66
  return this.subPackageRoots.some(root => filePath.startsWith(`${root}/`));
77
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
+ }
78
87
  apply(compiler) {
79
88
  compiler.hooks.watchRun.tap('SjsEntryPlugin', () => {
80
89
  this.subPackagePaths.clear();
81
90
  this.mainpkgPaths = [];
82
91
  });
83
92
  compiler.hooks.emit.tapAsync('SjsEntryPlugin', (compilation, callback) => {
93
+ this.compilation = compilation;
84
94
  const { project } = this.options;
85
95
  const { appJSON, miniprogramDir } = project;
86
96
  this.subPackageRoots = appJSON.subPackages.map(pkg => pkg.root);
@@ -130,40 +140,53 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
130
140
  if (sjsPathCache.has(sjsPath)) {
131
141
  continue;
132
142
  }
133
- const sjsAbsPath = formatSJSPath(sjsPath);
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
+ }
134
150
  const content = fs_extra_1.default.readFileSync(sjsAbsPath, 'utf8');
135
- const result = xhs_mp_compiler_sjs_loader_1.default.call(compilation, content, requirePath => {
136
- const realPath = (0, common_1.unixResolve)(path_1.default.dirname(sjsAbsPath), requirePath);
137
- const relativePath = (0, common_1.getShortPath)(project.miniprogramDir, realPath);
138
- /**
139
- * 不是当前分包的 sjs 文件不处理
140
- */
141
- if (pkgRoot && !relativePath.startsWith(pkgRoot)) {
142
- throw new Error(`require(${JSON.stringify(requirePath)}),请检测引入路径是否正确或符合分包规则!`);
143
- }
144
- if (!fs_extra_1.default.existsSync(realPath)) {
145
- throw new Error(`require(${JSON.stringify(requirePath)}) file does not exist`);
151
+ try {
152
+ const result = xhs_mp_compiler_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));
146
174
  }
147
- sjsFiles.push(realPath);
148
- return relativePath;
149
- });
150
- if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
151
- throw new packs_1.WebpackError([
152
- '\n<xhsml!',
153
- `[error:sjs编译错误]: [file:${sjsAbsPath === null || sjsAbsPath === void 0 ? void 0 : sjsAbsPath.trim()}]`,
154
- // swc span 信息不准确
155
- // `${result.errors.map(i => `${i.span.start} ${i.span.end} ${i.message}`).join("\n")}`,
156
- `${result.errors.map(i => i.message).join("\n")}`,
157
- '!xhsml>\n'
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_compiler_sjs_loader_1.SJS_HELPER}){`,
181
+ `${result.code};`,
182
+ `return module;}`
158
183
  ].join('\n'));
184
+ // 处理后的加入缓存
185
+ sjsPathCache.add(sjsPath);
186
+ }
187
+ catch (error) {
188
+ this.emitError(this.generateMessage(error.message, sjsAbsPath));
159
189
  }
160
- const moduleName = (0, common_1.getShortPath)(project.miniprogramDir, sjsAbsPath);
161
- // 老版本兼容代码
162
- sjsModules.push(`"${moduleName}": ${JSON.stringify(result.legacyCode)}`);
163
- // 模块化代码
164
- sjsModules.push(`".sjs#${moduleName}": function(exports,require,module,${xhs_mp_compiler_sjs_loader_1.SJS_HELPER}){${(result.code)};return module;}`);
165
- // 处理后的加入缓存
166
- sjsPathCache.add(sjsPath);
167
190
  }
168
191
  const source = new packs_1.ConcatSource(`globalThis['${sjsModuleName}'] = {`, `${sjsModules.join(',\n')}`, '}');
169
192
  if (packSetting.enableV1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.9.5",
3
+ "version": "1.9.6",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
@@ -94,13 +94,13 @@
94
94
  "webpack-chain": "^6.5.1",
95
95
  "webpack-sources": "^3.2.2",
96
96
  "xhs-mp-workerpool": "^9.1.3",
97
- "xhs-mp-compiler-ml-loader": "3.0.5",
98
- "xhs-mp-compiler-utils": "1.9.5",
99
- "xhs-mp-pack": "1.9.5",
100
- "xhs-mp-project": "1.9.5",
101
- "xhs-mp-utils": "1.9.5",
102
- "xhs-mp-shared-fs": "1.4.5",
103
- "xhs-mp-compiler-sjs-loader": "1.9.5",
97
+ "xhs-mp-compiler-ml-loader": "3.0.6",
98
+ "xhs-mp-compiler-utils": "1.9.6",
99
+ "xhs-mp-pack": "1.9.6",
100
+ "xhs-mp-project": "1.9.6",
101
+ "xhs-mp-utils": "1.9.6",
102
+ "xhs-mp-shared-fs": "1.4.6",
103
+ "xhs-mp-compiler-sjs-loader": "1.9.6",
104
104
  "yauzl": "^2.10.0"
105
105
  },
106
106
  "devDependencies": {