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.
Files changed (94) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +48 -34
  2. package/dist/bin/xhs-mp-cli-dev.js +44 -44
  3. package/dist/bin/xhs-mp-cli.js +22 -12
  4. package/dist/compiler.d.ts +21 -28
  5. package/dist/compiler.js +327 -327
  6. package/dist/compilerImpl.d.ts +19 -0
  7. package/dist/compilerImpl.js +173 -0
  8. package/dist/config/constant.config.d.ts +24 -0
  9. package/dist/config/constant.config.js +82 -30
  10. package/dist/config/dir.config.js +23 -13
  11. package/dist/index.d.ts +9 -9
  12. package/dist/index.js +454 -408
  13. package/dist/packs/index.d.ts +4 -4
  14. package/dist/packs/index.js +38 -30
  15. package/dist/packs/mp-pack/index.js +38 -30
  16. package/dist/packs/webpack/dev-server/index.js +61 -50
  17. package/dist/packs/webpack/dev-server/lib/ensurePort.js +33 -23
  18. package/dist/packs/webpack/dev-server/lib/openBrowser.js +28 -18
  19. package/dist/packs/webpack/index.d.ts +1 -1
  20. package/dist/packs/webpack/index.js +99 -90
  21. package/dist/packs/webpack/webpack.d.ts +2 -1
  22. package/dist/packs/webpack/webpack.js +81 -63
  23. package/dist/presets/babel.d.ts +0 -1
  24. package/dist/presets/babel.js +64 -58
  25. package/dist/presets/common.js +42 -45
  26. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +50 -40
  27. package/dist/presets/configs/minigame/assets/index.js +62 -52
  28. package/dist/presets/configs/minigame/service/index.js +49 -30
  29. package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +33 -23
  30. package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +2 -2
  31. package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -60
  32. package/dist/presets/configs/minigame/service/serviceEntryPlugin.d.ts +3 -3
  33. package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +39 -29
  34. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +120 -113
  35. package/dist/presets/configs/miniprogram/assets/index.js +45 -43
  36. package/dist/presets/configs/miniprogram/render/component.js +51 -41
  37. package/dist/presets/configs/miniprogram/render/index.d.ts +2 -3
  38. package/dist/presets/configs/miniprogram/render/index.js +85 -68
  39. package/dist/presets/configs/miniprogram/render/render.js +53 -43
  40. package/dist/presets/configs/miniprogram/render/renderChunkPlugin.js +84 -49
  41. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
  42. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.js +164 -126
  43. package/dist/presets/configs/miniprogram/service/index.js +82 -65
  44. package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.js +60 -50
  45. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +93 -55
  46. package/dist/presets/index.js +100 -90
  47. package/dist/presets/loaders/mg-entry-loader.js +32 -22
  48. package/dist/presets/loaders/mini-style-loader.js +79 -55
  49. package/dist/presets/loaders/mp-entry-loader.js +149 -140
  50. package/dist/presets/loaders/wxss-loader.js +73 -63
  51. package/dist/presets/plugins/InjectorPlugin.js +23 -13
  52. package/dist/presets/plugins/SourceMapDevToolPlugin.d.ts +1 -0
  53. package/dist/presets/plugins/SourceMapDevToolPlugin.js +23 -0
  54. package/dist/presets/prod.js +88 -78
  55. package/dist/sharedFs.d.ts +2 -0
  56. package/dist/sharedFs.js +24 -0
  57. package/dist/types/index.d.ts +11 -37
  58. package/dist/types/index.js +23 -13
  59. package/dist/utils/{apm/index.d.ts → apm.d.ts} +5 -3
  60. package/dist/utils/apm.js +245 -0
  61. package/dist/utils/cache.d.ts +28 -0
  62. package/dist/utils/cache.js +49 -0
  63. package/dist/utils/common.js +42 -32
  64. package/dist/utils/css.d.ts +1 -1
  65. package/dist/utils/css.js +43 -35
  66. package/dist/utils/depenedHelper.js +95 -85
  67. package/dist/utils/file.js +39 -29
  68. package/dist/utils/loader-utils.js +63 -53
  69. package/dist/utils/postcssRpx2Vw.js +43 -33
  70. package/dist/utils/project.d.ts +3 -2
  71. package/dist/utils/project.js +129 -119
  72. package/dist/utils/projectConfig.js +24 -15
  73. package/dist/utils/utils.js +51 -42
  74. package/dist/utils/workerPool.d.ts +29 -0
  75. package/dist/utils/workerPool.js +125 -0
  76. package/dist/utils/zip.js +239 -229
  77. package/package.json +33 -14
  78. package/dist/compilerCP.d.ts +0 -1
  79. package/dist/compilerCP.js +0 -175
  80. package/dist/presets/loaders/sjs-loader/index.d.ts +0 -12
  81. package/dist/presets/loaders/sjs-loader/index.js +0 -605
  82. package/dist/presets/loaders/sjs-loader/scope.d.ts +0 -36
  83. package/dist/presets/loaders/sjs-loader/scope.js +0 -122
  84. package/dist/presets/loaders/sjs-loader/tranform.d.ts +0 -2
  85. package/dist/presets/loaders/sjs-loader/tranform.js +0 -31
  86. package/dist/utils/apm/index.js +0 -198
  87. package/dist/utils/asyncWebpack.d.ts +0 -3
  88. package/dist/utils/asyncWebpack.js +0 -38
  89. package/dist/utils/const.d.ts +0 -27
  90. package/dist/utils/const.js +0 -47
  91. package/dist/utils/getSuffixName.d.ts +0 -3
  92. package/dist/utils/getSuffixName.js +0 -18
  93. package/dist/utils/tagTransformMap.d.ts +0 -36
  94. package/dist/utils/tagTransformMap.js +0 -39
package/dist/compiler.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -11,355 +10,356 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
10
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
11
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
12
  };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getProcessCompilerClass = void 0;
16
- /**
17
- * 在子进程执行编译
18
- */
19
- const child_process_1 = require("child_process");
20
- const path_1 = require("path");
21
- const stream_1 = require("stream");
22
- const fs_extra_1 = __importDefault(require("fs-extra"));
23
- const constant_config_1 = require("./config/constant.config");
24
- const uuid = require('uuid');
25
- const isMac = process.platform === 'darwin';
26
- class AbstractCompiler extends stream_1.EventEmitter {
27
- constructor(opts, args = {}) {
28
- super();
29
- this._handlerMap = {};
30
- this.createSp = (passData = {}) => {
31
- if (!this._cp) {
32
- this._cp = this.create(Object.assign({}, passData));
33
- this._cp.on('exit', code => {
34
- this._cp = null;
13
+ (function (factory) {
14
+ if (typeof module === "object" && typeof module.exports === "object") {
15
+ var v = factory(require, exports);
16
+ if (v !== undefined) module.exports = v;
17
+ }
18
+ else if (typeof define === "function" && define.amd) {
19
+ define(["require", "exports", "stream", "fs-extra", "./config/constant.config", "xhs-mp-workerpool"], factory);
20
+ }
21
+ })(function (require, exports) {
22
+ "use strict";
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.prepareDevPool = exports.BuildCompiler = exports.WebPackDevCompiler = exports.MPPackDevCompiler = exports.AbstractCompiler = void 0;
25
+ exports.getProjectCompilerClass = getProjectCompilerClass;
26
+ const stream_1 = require("stream");
27
+ const fs_extra_1 = __importDefault(require("fs-extra"));
28
+ const constant_config_1 = require("./config/constant.config");
29
+ const xhs_mp_workerpool_1 = __importDefault(require("xhs-mp-workerpool"));
30
+ let preparedDevPool = null;
31
+ class AbstractCompiler extends stream_1.EventEmitter {
32
+ constructor(opts, args) {
33
+ super();
34
+ // ide侧用web,cli用thread
35
+ this.workerType = 'process';
36
+ this.call = (method, data, options) => {
37
+ data = Object.assign(Object.assign({}, (this.compilerOpts || {})), data);
38
+ return this.pool.exec(method, [data], options);
39
+ };
40
+ this.compilerOpts = opts;
41
+ this.logger = args.logger || console;
42
+ this.workerType = args.workerType || 'process';
43
+ }
44
+ static prepareDevPool() {
45
+ preparedDevPool = AbstractCompiler.createPool('web', {
46
+ minWorkers: 1,
47
+ maxWorkers: 1 // dev最多只有一个worker,不用多个,不然无法复用首次watch初始化的compiler
48
+ });
49
+ }
50
+ static createPool(workerType, poolOpts = {}, processArgs = {}) {
51
+ let workerThreadOpts = {};
52
+ let forkOpts = {};
53
+ let workerOpts = {};
54
+ if (workerType === 'process') {
55
+ forkOpts = AbstractCompiler.createProcessOps(processArgs);
56
+ }
57
+ const pool = xhs_mp_workerpool_1.default.pool(__dirname + '/compilerImpl.js', Object.assign({ workerType: workerType, workerThreadOpts,
58
+ forkOpts,
59
+ workerOpts }, poolOpts));
60
+ let messageBuffers = [];
61
+ const handlerWorker = workerIns => {
62
+ workerIns.worker.on('message', payload => {
63
+ if (payload === null || payload === void 0 ? void 0 : payload.customMessage) {
64
+ if (!pool.emitWorkerMessage) {
65
+ messageBuffers.push(payload);
66
+ return;
67
+ }
68
+ if (messageBuffers.length) {
69
+ messageBuffers.forEach(payload => {
70
+ pool === null || pool === void 0 ? void 0 : pool.emitWorkerMessage(payload.event, payload.data);
71
+ });
72
+ messageBuffers = [];
73
+ }
74
+ pool === null || pool === void 0 ? void 0 : pool.emitWorkerMessage(payload.event, payload.data);
75
+ }
35
76
  });
36
- this._cp.on('message', this.handleMessage);
77
+ };
78
+ pool.workers.forEach(workerIns => {
79
+ handlerWorker(workerIns);
80
+ });
81
+ const push = pool.workers.push.bind(pool.workers);
82
+ pool.workers.push = workerIns => {
83
+ handlerWorker(workerIns);
84
+ push(workerIns);
85
+ };
86
+ return pool;
87
+ }
88
+ close() {
89
+ if (this.pool) {
90
+ this.pool.exec('close');
91
+ }
92
+ }
93
+ kill() {
94
+ if (this.pool) {
95
+ this.pool.terminate(true);
96
+ this.pool = null;
97
+ }
98
+ }
99
+ }
100
+ exports.AbstractCompiler = AbstractCompiler;
101
+ AbstractCompiler.createProcessOps = processArgs => {
102
+ const { type = 'keepalive', inspectPort, nodeJsPath } = processArgs;
103
+ // const presistent = type === 'keepalive'
104
+ const execArgv = ['--expose-gc'];
105
+ const customNodeJs = nodeJsPath && fs_extra_1.default.existsSync(nodeJsPath);
106
+ if (process.env.DEV_IDE_SP) {
107
+ execArgv.push(`--inspect=${inspectPort}`);
108
+ }
109
+ if (customNodeJs) {
110
+ try {
111
+ fs_extra_1.default.chmodSync(nodeJsPath, 755);
37
112
  }
38
- return this._cp;
113
+ catch (error) {
114
+ // do something
115
+ }
116
+ }
117
+ const options = {
118
+ stdio: 'pipe',
119
+ execArgv,
120
+ // detached: presistent,
121
+ windowsHide: true
39
122
  };
40
- this.create = (passData = {}) => {
41
- var _a, _b;
42
- const { type = 'keepalive', inspectPort = this.inspectPort, maxSpaceSaze = 12288, nodeJsPath, } = this.cpArgs;
43
- // const presistent = type === 'keepalive'
44
- const entry = (0, path_1.join)(__dirname, './compilerCP.js');
45
- const execArgv = [`--max-old-space-size=${maxSpaceSaze}`, '--expose-gc'];
46
- const customNodeJs = nodeJsPath && fs_extra_1.default.existsSync(nodeJsPath);
47
- if (process.env.DEV_IDE_SP) {
48
- execArgv.push(`--inspect=${inspectPort}`);
123
+ if (customNodeJs) {
124
+ options.execPath = nodeJsPath;
125
+ }
126
+ return options;
127
+ };
128
+ class MPPackDevCompiler extends AbstractCompiler {
129
+ constructor(props, args) {
130
+ super(props, args);
131
+ this.pending = false;
132
+ this.pendingPkgs = {};
133
+ this.readyPkgs = [];
134
+ this.watching = false;
135
+ this.workerType = (args === null || args === void 0 ? void 0 : args.workerType) || 'web';
136
+ this.inspectPort = 9227;
137
+ if (preparedDevPool) {
138
+ this.pool = preparedDevPool;
139
+ }
140
+ else {
141
+ this.pool = AbstractCompiler.createPool(this.workerType, {
142
+ minWorkers: 1,
143
+ maxWorkers: 1 // dev最多只有一个,不用多个,不然无法复用首次watch初始化的compiler
144
+ });
49
145
  }
50
- if (customNodeJs) {
146
+ this.pool.emitWorkerMessage = this.emit.bind(this);
147
+ }
148
+ reset() {
149
+ this.pendingPkgs = {};
150
+ this.readyPkgs = [];
151
+ this.watching = false;
152
+ this.pending = false;
153
+ }
154
+ doCompilePkgs(opts) {
155
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
156
+ const { compilePkgs = [constant_config_1.MAIN_PKG_ROOT] } = opts;
157
+ const todoPkgs = [];
158
+ let allReady = true;
159
+ compilePkgs.forEach(pkg => {
160
+ var _a, _b;
161
+ if (this.readyPkgs.includes(pkg)) {
162
+ return;
163
+ }
164
+ allReady = false;
165
+ if (this.pendingPkgs[pkg]) {
166
+ (_a = this.pendingPkgs[pkg].resolve) === null || _a === void 0 ? void 0 : _a.push(resolve);
167
+ (_b = this.pendingPkgs[pkg].reject) === null || _b === void 0 ? void 0 : _b.push(reject);
168
+ return;
169
+ }
170
+ this.pendingPkgs[pkg] = {
171
+ resolve: [resolve],
172
+ reject: [reject]
173
+ };
174
+ todoPkgs.push(pkg);
175
+ });
176
+ if (allReady) {
177
+ return resolve(null);
178
+ }
179
+ if (!todoPkgs.length) {
180
+ return;
181
+ }
51
182
  try {
52
- fs_extra_1.default.chmodSync(nodeJsPath, 755);
183
+ if (!this.pending && !this.watching) {
184
+ yield this.compilePkgsAndWatch(Object.assign({ compilePkgs: todoPkgs }, opts));
185
+ }
186
+ else {
187
+ yield this.addPkgsToWatch(Object.assign({ compilePkgs }, opts));
188
+ }
189
+ todoPkgs.forEach(pkg => {
190
+ var _a, _b;
191
+ (_b = (_a = this.pendingPkgs[pkg].resolve) === null || _a === void 0 ? void 0 : _a.forEach) === null || _b === void 0 ? void 0 : _b.call(_a, fn => fn());
192
+ delete this.pendingPkgs[pkg];
193
+ this.readyPkgs.push(pkg);
194
+ });
53
195
  }
54
196
  catch (error) {
55
- // do something
197
+ todoPkgs.forEach(pkg => {
198
+ 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());
200
+ delete this.pendingPkgs[pkg];
201
+ });
202
+ }
203
+ }));
204
+ }
205
+ compilePkgsAndWatch(opts) {
206
+ return __awaiter(this, void 0, void 0, function* () {
207
+ try {
208
+ // if (this.pending || this.watching) {
209
+ // return
210
+ // }
211
+ this.pending = true;
212
+ this.emit('buildStart');
213
+ yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
214
+ this.watching = true;
215
+ this.emit('buildSuccess');
216
+ }
217
+ catch (error) {
218
+ this.watching = false;
219
+ this.emit('buildError', { errors: [error.message] });
220
+ }
221
+ finally {
222
+ this.pending = false;
56
223
  }
57
- }
58
- const options = {
59
- stdio: 'pipe',
60
- execArgv,
61
- // detached: presistent,
62
- windowsHide: true,
63
- };
64
- if (customNodeJs) {
65
- options.execPath = nodeJsPath;
66
- }
67
- const child = (0, child_process_1.fork)(entry, [JSON.stringify(passData)], options);
68
- // if (presistent) {
69
- // child.unref()
70
- // }
71
- (_a = child.stdout) === null || _a === void 0 ? void 0 : _a.on('data', msg => {
72
- this.logger.debug('[compiler stdout]', msg.toString());
73
224
  });
74
- (_b = child.stderr) === null || _b === void 0 ? void 0 : _b.on('data', msg => {
75
- this.logger.error('[compiler stderr]', msg.toString());
225
+ }
226
+ addPkgsToWatch(opts) {
227
+ return __awaiter(this, void 0, void 0, function* () {
228
+ const { compilePkgs = [] } = opts;
229
+ try {
230
+ this.pending = true;
231
+ this.emit('buildStart');
232
+ yield this.call('activatePkgs', { compilePkgs });
233
+ this.emit('buildSuccess');
234
+ }
235
+ catch (error) {
236
+ this.emit('buildError', { errors: [error.message] });
237
+ }
238
+ finally {
239
+ this.pending = false;
240
+ }
76
241
  });
77
- child.on('error', err => {
78
- this.logger.error('sp error:', err);
242
+ }
243
+ changeCompileOptions(options) {
244
+ this.call('changeCompileOptions', options);
245
+ }
246
+ makePkgsReady(opts) {
247
+ return __awaiter(this, void 0, void 0, function* () {
248
+ yield this.doCompilePkgs(opts);
79
249
  });
80
- child.on('exit', code => {
81
- if (code !== 0 && code !== null) {
82
- this.logger.error('sp exit:', code);
83
- this.emit('unexpectedExit', code);
250
+ }
251
+ reCompilePkgs(options) {
252
+ return __awaiter(this, void 0, void 0, function* () {
253
+ this.readyPkgs = [];
254
+ this.changeCompileOptions(options);
255
+ if (!this.pending && !this.watching) {
256
+ this.pendingPkgs = {};
84
257
  }
258
+ yield this.doCompilePkgs(options);
85
259
  });
86
- return child;
87
- };
88
- this.handleMessage = msg => {
89
- var _a, _b, _c, _d;
90
- let data = {};
91
- try {
92
- data = JSON.parse(`${msg}`);
93
- }
94
- catch (error) {
95
- // do something
96
- }
97
- const { uuid, method, success, stats } = data;
98
- if (method === 'callback') {
99
- if (uuid && this._handlerMap[uuid]) {
100
- if (success) {
101
- (_b = (_a = this._handlerMap[uuid]).resolve) === null || _b === void 0 ? void 0 : _b.call(_a, data.data);
102
- }
103
- else {
104
- (_d = (_c = this._handlerMap[uuid]).reject) === null || _d === void 0 ? void 0 : _d.call(_c, stats);
105
- }
106
- delete this._handlerMap[uuid];
107
- }
108
- return;
109
- }
110
- if (method === 'debugger') {
111
- console.log(`[debugger]:`, ...(data.data || []));
112
- return;
113
- }
114
- this.emit(method, data);
115
- };
116
- this.call = (method, data) => new Promise((resolve, reject) => {
117
- const id = uuid.v4();
118
- const final = {
119
- uuid: id,
120
- method,
121
- data: Object.assign(Object.assign({}, (this.compilerOpts || {})), data),
122
- };
123
- this._handlerMap[id] = {
124
- resolve,
125
- reject,
126
- };
127
- this.cp.send(JSON.stringify(final));
128
- });
129
- this.send = (method, data) => {
130
- const final = {
131
- method,
132
- data: Object.assign(Object.assign({}, (this.compilerOpts || {})), data),
133
- };
134
- this.cp.send(JSON.stringify(final));
135
- };
136
- this.compilerOpts = opts;
137
- this.cpArgs = args;
138
- this.logger = args.logger || console;
139
- }
140
- get cp() {
141
- return this.createSp();
142
- }
143
- close() {
144
- if (this._cp) {
145
- this._cp.send(JSON.stringify({ method: 'close' }));
146
260
  }
147
261
  }
148
- kill() {
149
- if (this._cp) {
150
- this._cp.kill();
151
- this._cp = null;
262
+ exports.MPPackDevCompiler = MPPackDevCompiler;
263
+ class WebPackDevCompiler extends AbstractCompiler {
264
+ constructor(props, args) {
265
+ super(props, args);
266
+ this.watching = false;
267
+ this.workerType = (args === null || args === void 0 ? void 0 : args.workerType) || 'web';
268
+ this.inspectPort = 9230;
269
+ if (preparedDevPool) {
270
+ this.pool = preparedDevPool;
271
+ }
272
+ else {
273
+ this.pool = AbstractCompiler.createPool(this.workerType, {
274
+ minWorkers: 1,
275
+ maxWorkers: 1 // dev最多只有一个worker,不用多个,不然无法复用首次watch初始化的compiler
276
+ }, args);
277
+ }
278
+ this.pool.emitWorkerMessage = this.emit.bind(this);
152
279
  }
153
- }
154
- }
155
- class MPPackDevCompiler extends AbstractCompiler {
156
- constructor(props, args) {
157
- super(props, args);
158
- this.pending = false;
159
- this.pendingPkgs = {};
160
- this.readyPkgs = [];
161
- this.watching = false;
162
- this.inspectPort = 9227;
163
- this.createSp();
164
- }
165
- reset() {
166
- this.pendingPkgs = {};
167
- this.readyPkgs = [];
168
- this.watching = false;
169
- this.pending = false;
170
- }
171
- doCompilePkgs(opts) {
172
- // eslint-disable-next-line no-async-promise-executor
173
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
174
- const { compilePkgs = [constant_config_1.MAIN_PKG_ROOT] } = opts;
175
- const todoPkgs = [];
176
- let allReady = true;
177
- compilePkgs.forEach(pkg => {
178
- var _a, _b;
179
- if (this.readyPkgs.includes(pkg)) {
180
- return;
280
+ makePkgsReady(opts) {
281
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
282
+ var _a, _b, _c, _d;
283
+ if (this.watching) {
284
+ return resolve(null);
181
285
  }
182
- allReady = false;
183
- if (this.pendingPkgs[pkg]) {
184
- (_a = this.pendingPkgs[pkg].resolve) === null || _a === void 0 ? void 0 : _a.push(resolve);
185
- (_b = this.pendingPkgs[pkg].reject) === null || _b === void 0 ? void 0 : _b.push(reject);
286
+ if (this.pendingTask) {
287
+ (_a = this.pendingTask.resolve) === null || _a === void 0 ? void 0 : _a.push(resolve);
288
+ (_b = this.pendingTask.reject) === null || _b === void 0 ? void 0 : _b.push(reject);
186
289
  return;
187
290
  }
188
- this.pendingPkgs[pkg] = {
291
+ this.pendingTask = {
189
292
  resolve: [resolve],
190
- reject: [reject],
293
+ reject: [reject]
191
294
  };
192
- todoPkgs.push(pkg);
193
- });
194
- if (allReady) {
195
- return resolve(null);
196
- }
197
- if (!todoPkgs.length) {
198
- return;
199
- }
200
- try {
201
- if (!this.pending && !this.watching) {
202
- yield this.compilePkgsAndWatch(Object.assign({ compilePkgs: todoPkgs }, opts));
295
+ try {
296
+ this.emit('buildStart');
297
+ yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
298
+ this.watching = true;
299
+ (_c = this.pendingTask.resolve) === null || _c === void 0 ? void 0 : _c.forEach(fn => fn());
300
+ this.emit('buildSuccess');
203
301
  }
204
- else {
205
- yield this.addPkgsToWatch(Object.assign({ compilePkgs }, opts));
302
+ catch (error) {
303
+ this.watching = false;
304
+ (_d = this.pendingTask.reject) === null || _d === void 0 ? void 0 : _d.forEach(fn => fn());
305
+ this.kill();
306
+ this.emit('buildError', { errors: [error.message] });
206
307
  }
207
- todoPkgs.forEach(pkg => {
208
- var _a, _b;
209
- (_b = (_a = this.pendingPkgs[pkg].resolve) === null || _a === void 0 ? void 0 : _a.forEach) === null || _b === void 0 ? void 0 : _b.call(_a, fn => fn());
210
- delete this.pendingPkgs[pkg];
211
- this.readyPkgs.push(pkg);
212
- });
213
- }
214
- catch (error) {
215
- todoPkgs.forEach(pkg => {
216
- var _a, _b;
217
- (_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());
218
- delete this.pendingPkgs[pkg];
219
- });
220
- }
221
- }));
222
- }
223
- compilePkgsAndWatch(opts) {
224
- return __awaiter(this, void 0, void 0, function* () {
225
- try {
226
- // if (this.pending || this.watching) {
227
- // return
228
- // }
229
- this.pending = true;
230
- this.emit('buildStart');
231
- yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
232
- this.watching = true;
233
- this.emit('buildSuccess');
234
- }
235
- catch (error) {
236
- this.watching = false;
237
- this.emit('buildError', error);
238
- }
239
- finally {
240
- this.pending = false;
241
- }
242
- });
243
- }
244
- addPkgsToWatch(opts) {
245
- return __awaiter(this, void 0, void 0, function* () {
246
- const { compilePkgs = [] } = opts;
247
- try {
248
- this.pending = true;
249
- this.emit('buildStart');
250
- yield this.call('activatePkgs', { compilePkgs });
251
- this.emit('buildSuccess');
252
- }
253
- catch (error) {
254
- this.emit('buildError', error);
255
- }
256
- finally {
257
- this.pending = false;
258
- }
259
- });
260
- }
261
- changeCompileOptions(options) {
262
- this.call('changeCompileOptions', options);
263
- }
264
- makePkgsReady(opts) {
265
- return __awaiter(this, void 0, void 0, function* () {
266
- yield this.doCompilePkgs(opts);
267
- });
268
- }
269
- reCompilePkgs(options) {
270
- return __awaiter(this, void 0, void 0, function* () {
271
- this.readyPkgs = [];
272
- this.changeCompileOptions(options);
273
- if (!this.pending && !this.watching) {
274
- this.pendingPkgs = {};
275
- }
276
- yield this.doCompilePkgs(options);
277
- });
278
- }
279
- }
280
- class WebPackDevCompiler extends AbstractCompiler {
281
- constructor(props) {
282
- super(props);
283
- this.watching = false;
284
- this.inspectPort = 9230;
285
- this.createSp();
286
- }
287
- makePkgsReady(opts) {
288
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
289
- var _a, _b, _c, _d;
290
- if (this.watching) {
291
- return resolve(null);
292
- }
293
- if (this.pendingTask) {
294
- (_a = this.pendingTask.resolve) === null || _a === void 0 ? void 0 : _a.push(resolve);
295
- (_b = this.pendingTask.reject) === null || _b === void 0 ? void 0 : _b.push(reject);
296
- return;
297
- }
298
- this.pendingTask = {
299
- resolve: [resolve],
300
- reject: [reject]
301
- };
302
- try {
303
- this.emit('buildStart');
304
- yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
305
- this.watching = true;
306
- (_c = this.pendingTask.resolve) === null || _c === void 0 ? void 0 : _c.forEach((fn) => fn());
307
- this.emit('buildSuccess');
308
- }
309
- catch (error) {
310
- this.watching = false;
311
- (_d = this.pendingTask.reject) === null || _d === void 0 ? void 0 : _d.forEach((fn) => fn());
312
- this.kill();
313
- this.emit('buildError', error);
314
- }
315
- finally {
316
- this.pendingTask = undefined;
317
- }
318
- }));
319
- }
320
- reCompilePkgs(opts) {
321
- return __awaiter(this, void 0, void 0, function* () {
322
- if (!this.watching) {
323
- yield this.makePkgsReady(opts);
324
- return;
325
- }
326
- try {
327
- this.emit('buildStart');
328
- yield this.call('runTask', Object.assign({ action: 'dev', scene: 'reWatch' }, opts));
329
- this.emit('buildSuccess');
330
- }
331
- catch (error) {
332
- this.emit('buildError', error);
333
- }
334
- });
308
+ finally {
309
+ this.pendingTask = undefined;
310
+ }
311
+ }));
312
+ }
313
+ reCompilePkgs(opts) {
314
+ return __awaiter(this, void 0, void 0, function* () {
315
+ if (!this.watching) {
316
+ yield this.makePkgsReady(opts);
317
+ return;
318
+ }
319
+ try {
320
+ this.emit('buildStart');
321
+ yield this.call('runTask', Object.assign({ action: 'dev', scene: 'reWatch' }, opts));
322
+ this.emit('buildSuccess');
323
+ }
324
+ catch (error) {
325
+ this.emit('buildError', { errors: [error.message] });
326
+ }
327
+ });
328
+ }
335
329
  }
336
- }
337
- class BuildCompiler extends AbstractCompiler {
338
- constructor(props, args) {
339
- super(props, args);
340
- this.cps = [];
341
- this.inspectPort = 9228;
342
- this.createSp();
330
+ exports.WebPackDevCompiler = WebPackDevCompiler;
331
+ class BuildCompiler extends AbstractCompiler {
332
+ constructor(props, args) {
333
+ super(props, args);
334
+ this.cps = [];
335
+ this.workerType = (args === null || args === void 0 ? void 0 : args.workerType) || 'process';
336
+ this.inspectPort = 9228;
337
+ this.pool = AbstractCompiler.createPool(this.workerType, {
338
+ minWorkers: 1
339
+ }, args);
340
+ this.pool.emitWorkerMessage = this.emit.bind(this);
341
+ }
342
+ build(option) {
343
+ return __awaiter(this, void 0, void 0, function* () {
344
+ try {
345
+ const res = yield this.call('runTask', Object.assign({ action: 'build' }, option));
346
+ return res;
347
+ }
348
+ finally {
349
+ // 进程不退出,手动kill下
350
+ // this.kill()
351
+ }
352
+ });
353
+ }
343
354
  }
344
- build(option) {
345
- return __awaiter(this, void 0, void 0, function* () {
346
- try {
347
- const res = yield this.call('runTask', Object.assign({ action: 'build' }, option));
348
- return res;
349
- }
350
- finally {
351
- // 进程不退出,手动kill下
352
- this.kill();
353
- }
354
- });
355
+ exports.BuildCompiler = BuildCompiler;
356
+ function getProjectCompilerClass(compilerType) {
357
+ const CompilerClass = compilerType === constant_config_1.COMPILER_TYPE.legacy_dev
358
+ ? WebPackDevCompiler
359
+ : compilerType === constant_config_1.COMPILER_TYPE.dev
360
+ ? MPPackDevCompiler
361
+ : BuildCompiler;
362
+ return CompilerClass;
355
363
  }
356
- }
357
- function getProcessCompilerClass(compilerType) {
358
- const CompilerClass = compilerType === constant_config_1.COMPILER_TYPE.legacy_dev
359
- ? WebPackDevCompiler
360
- : compilerType === constant_config_1.COMPILER_TYPE.dev
361
- ? MPPackDevCompiler
362
- : BuildCompiler;
363
- return CompilerClass;
364
- }
365
- exports.getProcessCompilerClass = getProcessCompilerClass;
364
+ exports.prepareDevPool = AbstractCompiler.prepareDevPool;
365
+ });