xhs-mp-compiler-cli 1.9.6-beta.0 → 1.9.7-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/index.d.ts CHANGED
@@ -23,7 +23,6 @@ export type ICompilerProps = ({
23
23
  interface ICompileBaseConfig {
24
24
  compressJs: boolean;
25
25
  enableSourcemap: boolean;
26
- appendSourcemapComment: boolean;
27
26
  enableV2: boolean;
28
27
  enableV1: boolean;
29
28
  enableVDom: boolean;
package/dist/index.js CHANGED
@@ -246,14 +246,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
246
246
  return __awaiter(this, void 0, void 0, function* () {
247
247
  var _a, _b, _c;
248
248
  try {
249
- const { entryType, compressJs, enableSourcemap, appendSourcemapComment, enableV2, enableV1, enableVDom, mpUploadOptions } = config;
249
+ const { entryType, compressJs, enableSourcemap, enableV2, enableV1, enableVDom, mpUploadOptions } = config;
250
250
  this.emit('compile-project-start');
251
251
  (_a = this.logger) === null || _a === void 0 ? void 0 : _a.log('Compile project...');
252
252
  const compilerImpl = this.getComilerImpl(entryType);
253
253
  yield compilerImpl.build({
254
254
  compressJs,
255
255
  enableSourcemap,
256
- appendSourcemapComment,
257
256
  enableV2,
258
257
  enableV1,
259
258
  enableVDom,
@@ -279,7 +278,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
279
278
  }
280
279
  compileAndZip(opts) {
281
280
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
282
- var _a;
283
281
  const { can_upload_ext_json, upload_app_id, entryType } = opts;
284
282
  try {
285
283
  const appJsonContent = this.project.appJsonContent;
@@ -291,25 +289,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
291
289
  const { enableV2, enableV1, enableVDom } = yield this.getUsingPackageType(entryType);
292
290
  let compressJs = false;
293
291
  let enableSourcemap = true;
294
- let appendSourcemapComment = true;
295
- if (entryType === constant_config_1.COMPILE_ENTRY.preview) {
292
+ if (entryType === constant_config_1.COMPILE_ENTRY.upload || entryType === constant_config_1.COMPILE_ENTRY.preview) {
296
293
  compressJs = true;
294
+ enableSourcemap = false;
297
295
  }
298
- else if (entryType === constant_config_1.COMPILE_ENTRY.upload) {
299
- compressJs = true;
300
- enableSourcemap = true;
301
- appendSourcemapComment = false;
302
- }
303
- else {
304
- compressJs = (_a = settings.minified) !== null && _a !== void 0 ? _a : false;
305
- enableSourcemap = true;
306
- appendSourcemapComment = true;
296
+ else if (settings.hasOwnProperty('minified')) {
297
+ compressJs = settings.minified;
307
298
  }
308
299
  const buildConfig = {
309
300
  entryType,
310
301
  compressJs,
311
302
  enableSourcemap,
312
- appendSourcemapComment,
313
303
  enableV2,
314
304
  enableV1,
315
305
  enableVDom,
@@ -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, extJsonPath, compressJs = false, enableSourcemap = false, appendSourcemapComment = false, mpUploadOptions = false } = startConfig;
45
+ const { projectPath, action, tsConfigPath, platform, extJsonPath, compressJs = false, enableSourcemap = false, 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;
@@ -69,7 +69,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
69
69
  cacheDirectory: startConfig.cacheDirectory,
70
70
  compressJs,
71
71
  enableSourcemap,
72
- appendSourcemapComment,
73
72
  enableV1,
74
73
  enableV2,
75
74
  enableVDom,
@@ -7,7 +7,7 @@ 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", "./serviceEntryPlugin", "./serviceChunkPlugin", "./resolveLimitPlugin", "../../../plugins/InjectorPlugin", "../../../../utils/cache", "webpack"], factory);
10
+ define(["require", "exports", "path", "./serviceEntryPlugin", "./serviceChunkPlugin", "./resolveLimitPlugin", "../../../plugins/InjectorPlugin", "../../../../utils/cache"], factory);
11
11
  }
12
12
  })(function (require, exports) {
13
13
  "use strict";
@@ -19,10 +19,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
19
19
  const resolveLimitPlugin_1 = __importDefault(require("./resolveLimitPlugin"));
20
20
  const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
21
21
  const cache_1 = require("../../../../utils/cache");
22
- const webpack_1 = require("webpack");
23
22
  const presetService = (chain, options) => {
24
23
  const { packSetting } = options;
25
- const { distDir, cacheDirectory, enableSourcemap, appendSourcemapComment } = packSetting;
24
+ const { distDir, cacheDirectory } = packSetting;
26
25
  const absCacheDirectory = path_1.default.join(cacheDirectory, 'service');
27
26
  chain.merge(Object.assign(Object.assign({}, (0, cache_1.getCacheConfig)(absCacheDirectory, options)), { output: {
28
27
  path: distDir,
@@ -31,12 +30,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
30
  chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
32
31
  chain.plugin('ServiceEntryPlugin').use(serviceEntryPlugin_1.default, [options]);
33
32
  chain.plugin('ServiceChunkPlugin').use(serviceChunkPlugin_1.default, [options]);
34
- if (enableSourcemap && !appendSourcemapComment) {
35
- chain.plugin('SourceMapDevToolPlugin').use(webpack_1.SourceMapDevToolPlugin, [{
36
- filename: '[file].map',
37
- append: false
38
- }]);
39
- }
40
33
  chain.resolve.plugin('ResolveLimitPlugin').use(resolveLimitPlugin_1.default, [options]);
41
34
  chain.optimization
42
35
  .runtimeChunk({
@@ -7,7 +7,7 @@ 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", "../../../../utils/css", "../../../../utils/getSuffixName", "./sjsEntryPlugin", "./render", "./component", "./renderChunkPlugin", "../../../../types", "../../../plugins/InjectorPlugin", "../../../../utils/cache", "path", "webpack"], factory);
10
+ define(["require", "exports", "../../../../utils/css", "../../../../utils/getSuffixName", "./sjsEntryPlugin", "./render", "./component", "./renderChunkPlugin", "../../../../types", "../../../plugins/InjectorPlugin", "../../../../utils/cache", "path"], factory);
11
11
  }
12
12
  })(function (require, exports) {
13
13
  "use strict";
@@ -23,7 +23,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
24
24
  const cache_1 = require("../../../../utils/cache");
25
25
  const path_1 = __importDefault(require("path"));
26
- const webpack_1 = require("webpack");
27
26
  const getRenderEntry = (options) => () => {
28
27
  const entries = Object.assign(Object.assign({}, (0, render_1.getRenderEntry)(options)), (0, component_1.getComponentEntry)(options));
29
28
  return entries;
@@ -31,7 +30,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
30
  exports.getRenderEntry = getRenderEntry;
32
31
  const presetRender = (chain, options) => {
33
32
  const { packSetting } = options;
34
- const { enableVDom, env, distDir, cacheDirectory, enableSourcemap, appendSourcemapComment } = packSetting;
33
+ const { enableVDom, env, distDir, cacheDirectory } = packSetting;
35
34
  const mlReg = new RegExp(`.${(0, getSuffixName_1.getMlSuffixName)()}$`);
36
35
  const cssReg = new RegExp(`.${(0, getSuffixName_1.getCssSuffixName)()}$`);
37
36
  const isProd = env === types_1.ENV.production;
@@ -78,12 +77,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
78
77
  chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
79
78
  chain.plugin('SjsEntryPlugin').use(sjsEntryPlugin_1.default, [options]);
80
79
  chain.plugin("RenderChunkPlugin").use(renderChunkPlugin_1.default, [options]);
81
- if (enableSourcemap && !appendSourcemapComment) {
82
- chain.plugin('SourceMapDevToolPlugin').use(webpack_1.SourceMapDevToolPlugin, [{
83
- filename: '[file].map',
84
- append: false
85
- }]);
86
- }
87
80
  };
88
81
  exports.presetRender = presetRender;
89
82
  });
@@ -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) {
@@ -7,7 +7,7 @@ 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", "xhs-mp-pack", "./serviceChunkPlugin", "../../../plugins/InjectorPlugin", "../../../../utils/cache", "webpack"], factory);
10
+ define(["require", "exports", "path", "xhs-mp-pack", "./serviceChunkPlugin", "../../../plugins/InjectorPlugin", "../../../../utils/cache"], factory);
11
11
  }
12
12
  })(function (require, exports) {
13
13
  "use strict";
@@ -18,7 +18,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  const serviceChunkPlugin_1 = __importDefault(require("./serviceChunkPlugin"));
19
19
  const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
20
20
  const cache_1 = require("../../../../utils/cache");
21
- const webpack_1 = require("webpack");
22
21
  const getServiceEntry = options => () => {
23
22
  const entries = {};
24
23
  const { packSetting } = options;
@@ -53,7 +52,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
53
52
  };
54
53
  const presetService = (chain, options) => {
55
54
  const { packSetting } = options;
56
- const { env, distDir, cacheDirectory, enableSourcemap, appendSourcemapComment } = packSetting;
55
+ const { env, distDir, cacheDirectory } = packSetting;
57
56
  const absCacheDirectory = path_1.default.join(cacheDirectory, 'service');
58
57
  chain.merge(Object.assign({
59
58
  // target: 'webworker',
@@ -65,12 +64,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
65
64
  } }, (0, cache_1.getCacheConfig)(absCacheDirectory, options)));
66
65
  chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
67
66
  chain.plugin('ServiceChunkPlugin').use(serviceChunkPlugin_1.default, [options]);
68
- if (enableSourcemap && !appendSourcemapComment) {
69
- chain.plugin('SourceMapDevToolPlugin').use(webpack_1.SourceMapDevToolPlugin, [{
70
- filename: '[file].map',
71
- append: false
72
- }]);
73
- }
74
67
  chain.optimization.runtimeChunk({
75
68
  name: 'runtime'
76
69
  });
@@ -75,8 +75,10 @@ export interface ICompilerConfig {
75
75
  * 打包是否包含 js 文件
76
76
  */
77
77
  noJs?: boolean;
78
+ /**
79
+ * dev 模式是否开启 sourcemap,默认开启
80
+ */
78
81
  enableSourcemap?: boolean;
79
- appendSourcemapComment?: boolean;
80
82
  /**
81
83
  * 服务端口
82
84
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.9.6-beta.0",
3
+ "version": "1.9.7-beta.0",
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.6-beta.0",
98
- "xhs-mp-compiler-utils": "1.9.6-beta.0",
99
- "xhs-mp-pack": "1.9.6-beta.0",
100
- "xhs-mp-project": "1.9.6-beta.0",
101
- "xhs-mp-utils": "1.9.6-beta.0",
102
- "xhs-mp-shared-fs": "1.4.6-beta.0",
103
- "xhs-mp-compiler-sjs-loader": "1.9.6-beta.0",
97
+ "xhs-mp-compiler-ml-loader": "3.0.7-beta.0",
98
+ "xhs-mp-compiler-utils": "1.9.6",
99
+ "xhs-mp-pack": "1.9.7-beta.0",
100
+ "xhs-mp-project": "1.9.6",
101
+ "xhs-mp-utils": "1.9.6",
102
+ "xhs-mp-shared-fs": "1.4.7-beta.0",
103
+ "xhs-mp-compiler-sjs-loader": "1.9.6",
104
104
  "yauzl": "^2.10.0"
105
105
  },
106
106
  "devDependencies": {