xhs-mp-compiler-cli 2.0.5 → 2.0.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.
@@ -51,9 +51,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
51
51
  console.log(`资源编译:${data.resource}`);
52
52
  }
53
53
  });
54
- projectPacker.watch({
55
- scene: 'watch',
56
- }, (err, stats) => {
54
+ projectPacker.watch({}, (err, stats) => {
57
55
  console.log(stats.toString({ warnings: true, errors: true, all: false }));
58
56
  });
59
57
  });
@@ -31,11 +31,14 @@ export declare abstract class AbstractCompiler extends EventEmitter {
31
31
  protected workerType: IWorkerType;
32
32
  private compilerOpts;
33
33
  protected logger: any;
34
- protected pool: any;
34
+ protected _pool: any;
35
35
  constructor(opts: ICompilerOption, args: ICompilerArgs);
36
+ get pool(): any;
37
+ set pool(val: any);
36
38
  static prepareDevPool(): void;
37
39
  static createPool(workerType: any, poolOpts?: {}, processArgs?: {}): any;
38
40
  static createProcessOps: (processArgs: any) => any;
41
+ abstract makePoolReady(args?: any): any;
39
42
  close(): void;
40
43
  kill(): void;
41
44
  call: (method: any, data: any, options?: any) => any;
@@ -46,6 +49,7 @@ export declare class MPPackDevCompiler extends AbstractCompiler {
46
49
  private readyPkgs;
47
50
  private watching;
48
51
  constructor(props: any, args: any);
52
+ makePoolReady(): void;
49
53
  reset(): void;
50
54
  doCompilePkgs(opts: ICheckOptions): Promise<unknown>;
51
55
  private compilePkgsAndWatch;
@@ -58,12 +62,14 @@ export declare class WebPackDevCompiler extends AbstractCompiler {
58
62
  private watching;
59
63
  private pendingTask?;
60
64
  constructor(props: any, args: any);
65
+ makePoolReady(args: any): void;
61
66
  makePkgsReady(opts: any): Promise<unknown>;
62
67
  reCompilePkgs(opts: any): Promise<void>;
63
68
  }
64
69
  export declare class BuildCompiler extends AbstractCompiler {
65
70
  cps: any;
66
71
  constructor(props: any, args: any);
72
+ makePoolReady(args: any): void;
67
73
  build(option: ICheckOptions): Promise<any>;
68
74
  }
69
75
  export declare function getProjectCompilerClass(compilerType: any): typeof MPPackDevCompiler | typeof WebPackDevCompiler | typeof BuildCompiler;
package/dist/compiler.js CHANGED
@@ -41,6 +41,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
41
41
  this.logger = args.logger || console;
42
42
  this.workerType = args.workerType || 'process';
43
43
  }
44
+ get pool() {
45
+ if (!this._pool) {
46
+ this.makePoolReady();
47
+ }
48
+ return this._pool;
49
+ }
50
+ set pool(val) {
51
+ this._pool = val;
52
+ }
44
53
  static prepareDevPool() {
45
54
  preparedDevPool = AbstractCompiler.createPool('web', {
46
55
  minWorkers: 1,
@@ -134,16 +143,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
134
143
  this.watching = false;
135
144
  this.workerType = (args === null || args === void 0 ? void 0 : args.workerType) || 'web';
136
145
  this.inspectPort = 9227;
146
+ this.makePoolReady();
147
+ }
148
+ makePoolReady() {
137
149
  if (preparedDevPool) {
138
- this.pool = preparedDevPool;
150
+ this._pool = preparedDevPool;
139
151
  }
140
152
  else {
141
- this.pool = AbstractCompiler.createPool(this.workerType, {
153
+ this._pool = AbstractCompiler.createPool(this.workerType, {
142
154
  minWorkers: 1,
143
155
  maxWorkers: 1 // dev最多只有一个,不用多个,不然无法复用首次watch初始化的compiler
144
156
  });
145
157
  }
146
- this.pool.emitWorkerMessage = this.emit.bind(this);
158
+ this._pool.emitWorkerMessage = this.emit.bind(this);
147
159
  }
148
160
  reset() {
149
161
  this.pendingPkgs = {};
@@ -196,7 +208,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
196
208
  catch (error) {
197
209
  todoPkgs.forEach(pkg => {
198
210
  var _a, _b;
199
- (_b = (_a = this.pendingPkgs[pkg].reject) === null || _a === void 0 ? void 0 : _a.forEach) === null || _b === void 0 ? void 0 : _b.call(_a, fn => fn());
211
+ (_b = (_a = this.pendingPkgs[pkg].reject) === null || _a === void 0 ? void 0 : _a.forEach) === null || _b === void 0 ? void 0 : _b.call(_a, fn => fn(error));
200
212
  delete this.pendingPkgs[pkg];
201
213
  });
202
214
  }
@@ -210,13 +222,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
210
222
  // }
211
223
  this.pending = true;
212
224
  this.emit('buildStart');
213
- yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
225
+ yield this.call('runTask', Object.assign({ action: 'dev' }, opts));
214
226
  this.watching = true;
215
227
  this.emit('buildSuccess');
216
228
  }
217
229
  catch (error) {
218
230
  this.watching = false;
219
231
  this.emit('buildError', { errors: [error.message] });
232
+ throw error;
220
233
  }
221
234
  finally {
222
235
  this.pending = false;
@@ -234,6 +247,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
234
247
  }
235
248
  catch (error) {
236
249
  this.emit('buildError', { errors: [error.message] });
250
+ throw error;
237
251
  }
238
252
  finally {
239
253
  this.pending = false;
@@ -266,6 +280,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
266
280
  this.watching = false;
267
281
  this.workerType = (args === null || args === void 0 ? void 0 : args.workerType) || 'web';
268
282
  this.inspectPort = 9230;
283
+ this.makePoolReady(args);
284
+ }
285
+ makePoolReady(args) {
269
286
  if (preparedDevPool) {
270
287
  this.pool = preparedDevPool;
271
288
  }
@@ -294,16 +311,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
294
311
  };
295
312
  try {
296
313
  this.emit('buildStart');
297
- yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
314
+ yield this.call('runTask', Object.assign({ action: 'dev' }, opts));
298
315
  this.watching = true;
299
316
  (_c = this.pendingTask.resolve) === null || _c === void 0 ? void 0 : _c.forEach(fn => fn());
300
317
  this.emit('buildSuccess');
301
318
  }
302
319
  catch (error) {
303
320
  this.watching = false;
304
- (_d = this.pendingTask.reject) === null || _d === void 0 ? void 0 : _d.forEach(fn => fn());
305
- this.kill();
321
+ (_d = this.pendingTask.reject) === null || _d === void 0 ? void 0 : _d.forEach(fn => fn(error));
306
322
  this.emit('buildError', { errors: [error.message] });
323
+ throw error;
307
324
  }
308
325
  finally {
309
326
  this.pendingTask = undefined;
@@ -318,11 +335,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
318
335
  }
319
336
  try {
320
337
  this.emit('buildStart');
321
- yield this.call('runTask', Object.assign({ action: 'dev', scene: 'reWatch' }, opts));
338
+ yield this.call('runTask', Object.assign({ action: 'dev' }, opts));
322
339
  this.emit('buildSuccess');
323
340
  }
324
341
  catch (error) {
325
342
  this.emit('buildError', { errors: [error.message] });
343
+ throw error;
326
344
  }
327
345
  });
328
346
  }
@@ -334,6 +352,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
334
352
  this.cps = [];
335
353
  this.workerType = (args === null || args === void 0 ? void 0 : args.workerType) || 'process';
336
354
  this.inspectPort = 9228;
355
+ this.makePoolReady(args);
356
+ }
357
+ makePoolReady(args) {
337
358
  this.pool = AbstractCompiler.createPool(this.workerType, {
338
359
  minWorkers: 1
339
360
  }, args);
@@ -9,9 +9,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const workerpool = require('xhs-mp-workerpool');
12
- sendBuildInfoMessage('load-packer', '开始加载compiler');
12
+ sendBuildInfoMessage('load-packer', '开始加载编译器');
13
13
  const { createPacker } = require('./packs');
14
- sendBuildInfoMessage('load-packer', '加载compiler完成');
14
+ sendBuildInfoMessage('load-packer', '加载编译器完成');
15
15
  let globalConfig = {};
16
16
  let projectPacker;
17
17
  function formatOutputMessages(err, stats) {
@@ -94,9 +94,7 @@ const handlers = {
94
94
  console.log('[compiler] create compiler');
95
95
  createProjectPacker(data);
96
96
  sendBuildInfoMessage('run-watch', '执行编译');
97
- const { err, stats } = (yield projectPacker.watch({
98
- scene: data.scene
99
- }, (err, stats) => {
97
+ const { err, stats } = (yield projectPacker.watch({}, (err, stats) => {
100
98
  var _a;
101
99
  console.log('[compiler] file change...', err, stats);
102
100
  // 修改文件时构建
@@ -310,7 +310,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
310
310
  result: 'fail',
311
311
  entryType,
312
312
  config: JSON.stringify(config),
313
- reason: error.message
313
+ reason: error === null || error === void 0 ? void 0 : error.message
314
314
  });
315
315
  throw error;
316
316
  }
@@ -351,7 +351,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
351
351
  result: 'fail',
352
352
  entryType,
353
353
  config: JSON.stringify(config),
354
- reason: error.message
354
+ reason: error === null || error === void 0 ? void 0 : error.message
355
355
  });
356
356
  throw error;
357
357
  }
@@ -59,9 +59,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
59
59
  this.watcher.close(() => { });
60
60
  this.watcher = null;
61
61
  }
62
- if (config.scene === 'reWatch') {
63
- this.hadWatch = false;
64
- }
62
+ this.hadWatch = false;
65
63
  this.watcher = this.compiler.watch(Object.assign(Object.assign({}, watchOptions), config), (err, stats) => {
66
64
  if (!this.hadWatch) {
67
65
  this.hadWatch = true;
@@ -113,12 +113,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
113
113
  `;
114
114
  };
115
115
  function genServiceLoderScript(project, context, opts) {
116
+ var _a;
116
117
  const { appJSON, projectMiniprogramPath, jsonDependencies } = project;
117
118
  const { pkg, root } = opts;
118
119
  const modules = [];
119
- let name = 'XHS_SERVICE';
120
+ let moduleName = 'XHS_SERVICE';
120
121
  if (pkg === xhs_mp_pack_1.CONSTANTS.SUB_PKG) {
121
- name = `XHS_SERVICE_${root}`;
122
+ moduleName = `XHS_SERVICE_${root}`;
122
123
  }
123
124
  for (const file of jsonDependencies) {
124
125
  context.addDependency(file);
@@ -126,20 +127,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
126
127
  const addAppJs = () => {
127
128
  if (fs_extra_1.default.existsSync(`${projectMiniprogramPath}/app.js`) ||
128
129
  fs_extra_1.default.existsSync(`${projectMiniprogramPath}/app.ts`)) {
129
- modules.unshift(`const loadAppJs = () => require('${(0, xhs_mp_compiler_utils_1.toUnixPath)(`${projectMiniprogramPath}/app`)}');` +
130
- `globalThis['${name}'].default['app'] = globalThis.xhsLazyAppJs ? loadAppJs : loadAppJs()`);
130
+ const loadAppJs = 'loadAppJs';
131
+ modules.unshift(`;const ${loadAppJs} = () => require('${(0, xhs_mp_compiler_utils_1.toUnixPath)(`${projectMiniprogramPath}/app`)}')`, `;globalThis['${moduleName}'].default['app'] = globalThis.xhsLazyAppJs ? ${loadAppJs} : ${loadAppJs}()`);
131
132
  }
132
133
  };
133
- const addPage = (page) => {
134
- const resourcePath = (0, xhs_mp_compiler_utils_1.toUnixPath)(`${projectMiniprogramPath}/${page.path}`);
135
- const content = `;globalThis['${name}'].default['${page.path}'] = () => require('${resourcePath}')`;
134
+ // 添加 page 或者 component 的 js 资源
135
+ const addJSResource = (shortPath) => {
136
+ const resourcePath = (0, xhs_mp_compiler_utils_1.toUnixPath)(`${projectMiniprogramPath}/${shortPath}`);
137
+ const content = `;globalThis['${moduleName}'].default['${shortPath}'] = () => require('${resourcePath}')`;
136
138
  modules.push(content);
137
139
  };
138
140
  // 主包
139
141
  if (pkg === xhs_mp_pack_1.CONSTANTS.MAIN_PKG) {
140
142
  addAppJs();
141
143
  for (const page of appJSON.pages) {
142
- addPage(page);
144
+ // 注意 经过转换后的 page 全部处理为 { path: string } 对象格式
145
+ addJSResource(page.path);
143
146
  }
144
147
  }
145
148
  // 分包
@@ -147,30 +150,35 @@ var __importStar = (this && this.__importStar) || function (mod) {
147
150
  for (const subPackage of appJSON.subPackages) {
148
151
  if (subPackage.root === root) {
149
152
  for (const page of subPackage.pages) {
150
- addPage(page);
153
+ addJSResource(page.path);
151
154
  }
152
155
  }
153
156
  }
154
157
  }
155
158
  Object.keys(appJSON.componentsMap).forEach(compPath => {
156
- const resourcePath = (0, xhs_mp_compiler_utils_1.toUnixPath)(`${projectMiniprogramPath}/${compPath}`);
157
- const content = `;globalThis['${name}'].default['${compPath}'] = () => require('${resourcePath}')`;
158
159
  if (pkg === xhs_mp_pack_1.CONSTANTS.MAIN_PKG) {
159
160
  const subPackageRoots = appJSON.subPackages.map(pkg => pkg.root);
160
161
  if (subPackageRoots.every(root => !compPath.startsWith(`${root}/`))) {
161
- modules.push(content);
162
+ addJSResource(compPath);
162
163
  }
163
164
  }
164
165
  if (pkg === xhs_mp_pack_1.CONSTANTS.SUB_PKG) {
165
166
  if (compPath.startsWith(`${root}/`)) {
166
- modules.push(content);
167
+ addJSResource(compPath);
167
168
  }
168
169
  }
169
170
  });
170
- return `;globalThis['__MP_APP_JSON__'] = ${JSON.stringify(appJSON)};
171
- ;(globalThis['${name}'] || (globalThis['${name}'] = { default: {} }))
172
- ${modules.join('\n ')}
173
- `;
171
+ let injectAppJson = '';
172
+ if (pkg === xhs_mp_pack_1.CONSTANTS.MAIN_PKG ||
173
+ (pkg === xhs_mp_pack_1.CONSTANTS.SUB_PKG && ((_a = appJSON.subPackages.find(sub => sub.root === root)) === null || _a === void 0 ? void 0 : _a.independent))) {
174
+ // 非独立分包加载必须依赖主包先加载,因此没必要进行__MP_APP_JSON__重复注入
175
+ injectAppJson = `;globalThis['__MP_APP_JSON__'] = ${JSON.stringify(appJSON)}`;
176
+ }
177
+ return [
178
+ injectAppJson,
179
+ `;(globalThis['${moduleName}'] || (globalThis['${moduleName}'] = { default: {} }))`,
180
+ ...modules
181
+ ].filter(Boolean).join('\n');
174
182
  }
175
183
  function genSketchLoaderScript(project, context, opts) {
176
184
  const { projectMiniprogramPath } = project;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "2.0.5",
3
+ "version": "2.0.7-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.1.3",
91
- "xhs-mp-ml-loader": "2.0.5",
92
- "xhs-mp-compiler-utils": "2.0.5",
93
- "xhs-mp-pack": "2.0.5",
94
- "xhs-mp-project": "2.0.5",
95
- "xhs-mp-shared": "2.0.5",
96
- "xhs-mp-shared-fs": "2.0.5",
97
- "xhs-mp-sjs-loader": "2.0.5",
98
- "xhs-mp-sketch-loader": "2.0.5",
91
+ "xhs-mp-ml-loader": "2.0.7-beta.0",
92
+ "xhs-mp-compiler-utils": "2.0.7-beta.0",
93
+ "xhs-mp-pack": "2.0.7-beta.0",
94
+ "xhs-mp-project": "2.0.7-beta.0",
95
+ "xhs-mp-shared": "2.0.7-beta.0",
96
+ "xhs-mp-shared-fs": "2.0.7-beta.0",
97
+ "xhs-mp-sjs-loader": "2.0.7-beta.0",
98
+ "xhs-mp-sketch-loader": "2.0.7-beta.0",
99
99
  "yauzl": "^2.10.0"
100
100
  },
101
101
  "peerDependencies": {
102
- "xhs-mp-ml-parser": "2.0.5"
102
+ "xhs-mp-ml-parser": "2.0.7-beta.0"
103
103
  },
104
104
  "devDependencies": {
105
105
  "@types/babel__generator": "7.6.3",
@@ -123,7 +123,7 @@
123
123
  "typescript": "5.1.6",
124
124
  "vue3-jest": "27.0.0-alpha.2",
125
125
  "webpack-dev-server": "4.0.0-beta.3",
126
- "xhs-mp-ml-parser": "2.0.5"
126
+ "xhs-mp-ml-parser": "2.0.7-beta.0"
127
127
  },
128
128
  "scripts": {
129
129
  "version": "formula changelog && git add .",