xhs-mp-compiler-cli 1.5.0 → 1.6.0-beta.2

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 (45) hide show
  1. package/dist/compiler.d.ts +15 -13
  2. package/dist/compiler.js +96 -40
  3. package/dist/compilerImpl.d.ts +1 -0
  4. package/dist/{compilerCP.js → compilerImpl.js} +29 -42
  5. package/dist/compilerImplChildProcess.js +33 -0
  6. package/dist/compilerImplThreadWorker.d.ts +1 -0
  7. package/dist/compilerImplThreadWorker.js +30 -0
  8. package/dist/compilerImplWebWorker.d.ts +6 -0
  9. package/dist/compilerImplWebWorker.js +25 -0
  10. package/dist/dev-server/compatibleAPI.d.ts +25 -0
  11. package/dist/dev-server/compatibleAPI.js +56 -0
  12. package/dist/dev-server/escapeHtml.d.ts +5 -0
  13. package/dist/dev-server/escapeHtml.js +50 -0
  14. package/dist/dev-server/etag.d.ts +10 -0
  15. package/dist/dev-server/etag.js +69 -0
  16. package/dist/dev-server/getFilenameFromUrl.d.ts +9 -0
  17. package/dist/dev-server/getFilenameFromUrl.js +102 -0
  18. package/dist/dev-server/getPaths.d.ts +6 -0
  19. package/dist/dev-server/getPaths.js +20 -0
  20. package/dist/dev-server/index.d.ts +98 -0
  21. package/dist/dev-server/index.js +90 -0
  22. package/dist/dev-server/memorize.d.ts +7 -0
  23. package/dist/dev-server/memorize.js +23 -0
  24. package/dist/dev-server/middleware.d.ts +2 -0
  25. package/dist/dev-server/middleware.js +477 -0
  26. package/dist/dev-server/ready.d.ts +4 -0
  27. package/dist/dev-server/ready.js +11 -0
  28. package/dist/index.d.ts +3 -4
  29. package/dist/index.js +5 -5
  30. package/dist/packs/mp-pack/index.js +0 -1
  31. package/dist/packs/webpack/dev-server/index.js +1 -0
  32. package/dist/packs/webpack/dev-server/lib/openChrome.applescript +78 -0
  33. package/dist/packs/webpack/index.d.ts +1 -1
  34. package/dist/packs/webpack/index.js +3 -4
  35. package/dist/packs/webpack/webpack.d.ts +1 -1
  36. package/dist/packs/webpack/webpack.js +6 -2
  37. package/dist/presets/configs/miniprogram/render/index.d.ts +2 -3
  38. package/dist/presets/configs/miniprogram/render/index.js +2 -2
  39. package/dist/presets/configs/miniprogram/service/index.js +1 -0
  40. package/dist/sharedFs.d.ts +2 -0
  41. package/dist/sharedFs.js +4 -0
  42. package/dist/utils/workerPool.d.ts +29 -0
  43. package/dist/utils/workerPool.js +115 -0
  44. package/package.json +26 -12
  45. /package/dist/{compilerCP.d.ts → compilerImplChildProcess.d.ts} +0 -0
@@ -1,7 +1,5 @@
1
1
  /// <reference types="node/child_process" />
2
- /// <reference types=".pnpm/artifactory.devops.xiaohongshu.com+@types+node@20.12.2/node_modules/@types/node/child_process" />
3
2
  /// <reference types="node/stream" />
4
- /// <reference types=".pnpm/artifactory.devops.xiaohongshu.com+@types+node@20.12.2/node_modules/@types/node/stream" />
5
3
  /**
6
4
  * 在子进程执行编译
7
5
  */
@@ -31,24 +29,28 @@ export interface ICheckOptions {
31
29
  enableV2?: boolean;
32
30
  enableV1?: boolean;
33
31
  }
34
- declare abstract class AbstractCompiler extends EventEmitter {
32
+ export declare abstract class AbstractCompiler extends EventEmitter {
35
33
  protected inspectPort: any;
36
- private _cp;
34
+ private _impl;
35
+ protected _implType: 'thread_worker' | 'web_worker' | 'cp';
37
36
  private compilerOpts;
38
- private cpArgs;
37
+ private implArgs;
39
38
  private logger;
40
39
  private _handlerMap;
41
40
  constructor(opts: ICompilerOption, args?: ICompilerArgs);
42
- get cp(): any;
43
- protected createSp: (passData?: {}) => any;
44
- protected create: (passData?: {}) => ChildProcess;
41
+ get impl(): any;
42
+ protected createImpl: (passData?: {}) => any;
43
+ protected createCP: (passData?: {}) => ChildProcess;
44
+ protected createThreadWorker: (passData?: {}) => any;
45
+ protected createWebWorker: (passData?: {}) => any;
46
+ private postMessage;
45
47
  close(): void;
46
48
  kill(): void;
47
49
  private handleMessage;
48
50
  call: (method: any, data: any) => Promise<unknown>;
49
51
  send: (method: any, data: any) => void;
50
52
  }
51
- declare class MPPackDevCompiler extends AbstractCompiler {
53
+ export declare class MPPackDevCompiler extends AbstractCompiler {
52
54
  private pending;
53
55
  private pendingPkgs;
54
56
  private readyPkgs;
@@ -62,17 +64,17 @@ declare class MPPackDevCompiler extends AbstractCompiler {
62
64
  makePkgsReady(opts: ICheckOptions): Promise<void>;
63
65
  reCompilePkgs(options: any): Promise<void>;
64
66
  }
65
- declare class WebPackDevCompiler extends AbstractCompiler {
67
+ export declare class WebPackDevCompiler extends AbstractCompiler {
66
68
  private watching;
67
69
  private pendingTask?;
68
- constructor(props: any);
70
+ constructor(props: any, args: any);
69
71
  makePkgsReady(opts: any): Promise<unknown>;
70
72
  reCompilePkgs(opts: any): Promise<void>;
71
73
  }
72
- declare class BuildCompiler extends AbstractCompiler {
74
+ export declare class BuildCompiler extends AbstractCompiler {
73
75
  cps: any;
74
76
  constructor(props: any, args: any);
75
77
  build(option: ICheckOptions): Promise<unknown>;
76
78
  }
77
- export declare function getProcessCompilerClass(compilerType: any): typeof MPPackDevCompiler | typeof WebPackDevCompiler | typeof BuildCompiler;
79
+ export declare function getProjectCompilerClass(compilerType: any): typeof MPPackDevCompiler | typeof WebPackDevCompiler | typeof BuildCompiler;
78
80
  export {};
package/dist/compiler.js CHANGED
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getProcessCompilerClass = void 0;
15
+ exports.getProjectCompilerClass = exports.BuildCompiler = exports.WebPackDevCompiler = exports.MPPackDevCompiler = exports.AbstractCompiler = void 0;
16
16
  /**
17
17
  * 在子进程执行编译
18
18
  */
@@ -26,23 +26,42 @@ const isMac = process.platform === 'darwin';
26
26
  class AbstractCompiler extends stream_1.EventEmitter {
27
27
  constructor(opts, args = {}) {
28
28
  super();
29
+ // ide侧用web_worker,cli用thread_worker
30
+ this._implType = 'web_worker';
29
31
  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;
35
- });
36
- this._cp.on('message', this.handleMessage);
32
+ this.createImpl = (passData = {}) => {
33
+ if (!this._impl) {
34
+ if (this._implType === 'thread_worker') {
35
+ this._impl = this.createThreadWorker(passData);
36
+ this._impl.on('exit', code => {
37
+ this._impl = null;
38
+ });
39
+ this._impl.on('message', this.handleMessage);
40
+ }
41
+ else if (this._implType === 'web_worker') {
42
+ this._impl = this.createWebWorker(passData);
43
+ this._impl.onerror = err => {
44
+ console.error(err);
45
+ };
46
+ this._impl.onmessage = this.handleMessage;
47
+ }
48
+ else {
49
+ this._impl = this.createCP(passData);
50
+ this._impl.on('exit', code => {
51
+ this._impl = null;
52
+ });
53
+ this._impl.on('message', this.handleMessage);
54
+ }
55
+ // console.log('create impl done:', this._impl)
37
56
  }
38
- return this._cp;
57
+ return this._impl;
39
58
  };
40
- this.create = (passData = {}) => {
59
+ this.createCP = (passData = {}) => {
41
60
  var _a, _b;
42
- const { type = 'keepalive', inspectPort = this.inspectPort, maxSpaceSaze = 12288, nodeJsPath, } = this.cpArgs;
61
+ const { type = 'keepalive', inspectPort = this.inspectPort, maxSpaceSaze = 12288, nodeJsPath } = this.implArgs;
43
62
  // const presistent = type === 'keepalive'
44
- const entry = (0, path_1.join)(__dirname, './compilerCP.js');
45
- const execArgv = [`--max-old-space-size=${maxSpaceSaze}`, '--expose-gc'];
63
+ const entry = (0, path_1.join)(__dirname, './compilerImplChildProcess.js');
64
+ const execArgv = ['--expose-gc'];
46
65
  const customNodeJs = nodeJsPath && fs_extra_1.default.existsSync(nodeJsPath);
47
66
  if (process.env.DEV_IDE_SP) {
48
67
  execArgv.push(`--inspect=${inspectPort}`);
@@ -59,7 +78,7 @@ class AbstractCompiler extends stream_1.EventEmitter {
59
78
  stdio: 'pipe',
60
79
  execArgv,
61
80
  // detached: presistent,
62
- windowsHide: true,
81
+ windowsHide: true
63
82
  };
64
83
  if (customNodeJs) {
65
84
  options.execPath = nodeJsPath;
@@ -75,18 +94,36 @@ class AbstractCompiler extends stream_1.EventEmitter {
75
94
  this.logger.error('[compiler stderr]', msg.toString());
76
95
  });
77
96
  child.on('error', err => {
78
- this.logger.error('sp error:', err);
97
+ this.logger.error('impl error:', err);
79
98
  });
80
99
  child.on('exit', code => {
81
100
  if (code !== 0 && code !== null) {
82
- this.logger.error('sp exit:', code);
101
+ this.logger.error('impl exit:', code);
83
102
  this.emit('unexpectedExit', code);
84
103
  }
85
104
  });
86
105
  return child;
87
106
  };
107
+ this.createThreadWorker = (passData = {}) => {
108
+ const { Worker } = require('worker_threads');
109
+ const entry = (0, path_1.join)(__dirname, './compilerImplThreadWorker.js');
110
+ const worker = new Worker(entry, { workerData: passData });
111
+ return worker;
112
+ };
113
+ // electron渲染进程不能使用worker_threads
114
+ this.createWebWorker = (passData = {}) => {
115
+ const entry = (0, path_1.join)(__dirname, './compilerImplWebWorker.js');
116
+ // @ts-ignore
117
+ const worker = new Worker(entry);
118
+ worker.postMessage(JSON.stringify({ passData }));
119
+ return worker;
120
+ };
88
121
  this.handleMessage = msg => {
89
122
  var _a, _b, _c, _d;
123
+ if (this._implType === 'web_worker') {
124
+ msg = msg.data;
125
+ }
126
+ // console.log('handle compielr message', msg)
90
127
  let data = {};
91
128
  try {
92
129
  data = JSON.parse(`${msg}`);
@@ -108,7 +145,7 @@ class AbstractCompiler extends stream_1.EventEmitter {
108
145
  return;
109
146
  }
110
147
  if (method === 'debugger') {
111
- console.log(`[debugger]:`, ...(data.data || []));
148
+ // console.log(`[debugger]:`, ...(data.data || []))
112
149
  return;
113
150
  }
114
151
  this.emit(method, data);
@@ -118,40 +155,54 @@ class AbstractCompiler extends stream_1.EventEmitter {
118
155
  const final = {
119
156
  uuid: id,
120
157
  method,
121
- data: Object.assign(Object.assign({}, (this.compilerOpts || {})), data),
158
+ data: Object.assign(Object.assign({}, (this.compilerOpts || {})), data)
122
159
  };
123
160
  this._handlerMap[id] = {
124
161
  resolve,
125
- reject,
162
+ reject
126
163
  };
127
- this.cp.send(JSON.stringify(final));
164
+ this.postMessage(JSON.stringify(final));
128
165
  });
129
166
  this.send = (method, data) => {
130
167
  const final = {
131
168
  method,
132
- data: Object.assign(Object.assign({}, (this.compilerOpts || {})), data),
169
+ data: Object.assign(Object.assign({}, (this.compilerOpts || {})), data)
133
170
  };
134
- this.cp.send(JSON.stringify(final));
171
+ this.postMessage(JSON.stringify(final));
135
172
  };
136
173
  this.compilerOpts = opts;
137
- this.cpArgs = args;
174
+ this.implArgs = args;
138
175
  this.logger = args.logger || console;
139
176
  }
140
- get cp() {
141
- return this.createSp();
177
+ get impl() {
178
+ return this.createImpl();
179
+ }
180
+ postMessage(msg) {
181
+ if (this._implType === 'thread_worker' || this._implType === 'web_worker') {
182
+ this.impl.postMessage(msg);
183
+ }
184
+ else {
185
+ this.impl.send(msg);
186
+ }
142
187
  }
143
188
  close() {
144
- if (this._cp) {
145
- this._cp.send(JSON.stringify({ method: 'close' }));
189
+ if (this._impl) {
190
+ this.postMessage(JSON.stringify({ method: 'close' }));
146
191
  }
147
192
  }
148
193
  kill() {
149
- if (this._cp) {
150
- this._cp.kill();
151
- this._cp = null;
194
+ if (this._impl) {
195
+ if (this._implType === 'cp') {
196
+ this._impl.kill();
197
+ }
198
+ else {
199
+ this._impl.terminate();
200
+ }
201
+ this._impl = null;
152
202
  }
153
203
  }
154
204
  }
205
+ exports.AbstractCompiler = AbstractCompiler;
155
206
  class MPPackDevCompiler extends AbstractCompiler {
156
207
  constructor(props, args) {
157
208
  super(props, args);
@@ -159,8 +210,9 @@ class MPPackDevCompiler extends AbstractCompiler {
159
210
  this.pendingPkgs = {};
160
211
  this.readyPkgs = [];
161
212
  this.watching = false;
213
+ this._implType = (args === null || args === void 0 ? void 0 : args.implType) || 'web_worker';
162
214
  this.inspectPort = 9227;
163
- this.createSp();
215
+ this.createImpl();
164
216
  }
165
217
  reset() {
166
218
  this.pendingPkgs = {};
@@ -169,7 +221,6 @@ class MPPackDevCompiler extends AbstractCompiler {
169
221
  this.pending = false;
170
222
  }
171
223
  doCompilePkgs(opts) {
172
- // eslint-disable-next-line no-async-promise-executor
173
224
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
174
225
  const { compilePkgs = [constant_config_1.MAIN_PKG_ROOT] } = opts;
175
226
  const todoPkgs = [];
@@ -187,7 +238,7 @@ class MPPackDevCompiler extends AbstractCompiler {
187
238
  }
188
239
  this.pendingPkgs[pkg] = {
189
240
  resolve: [resolve],
190
- reject: [reject],
241
+ reject: [reject]
191
242
  };
192
243
  todoPkgs.push(pkg);
193
244
  });
@@ -277,12 +328,14 @@ class MPPackDevCompiler extends AbstractCompiler {
277
328
  });
278
329
  }
279
330
  }
331
+ exports.MPPackDevCompiler = MPPackDevCompiler;
280
332
  class WebPackDevCompiler extends AbstractCompiler {
281
- constructor(props) {
333
+ constructor(props, args) {
282
334
  super(props);
283
335
  this.watching = false;
336
+ this._implType = (args === null || args === void 0 ? void 0 : args.implType) || 'web_worker';
284
337
  this.inspectPort = 9230;
285
- this.createSp();
338
+ this.createImpl();
286
339
  }
287
340
  makePkgsReady(opts) {
288
341
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
@@ -303,12 +356,12 @@ class WebPackDevCompiler extends AbstractCompiler {
303
356
  this.emit('buildStart');
304
357
  yield this.call('runTask', Object.assign({ scene: 'watch', action: 'dev' }, opts));
305
358
  this.watching = true;
306
- (_c = this.pendingTask.resolve) === null || _c === void 0 ? void 0 : _c.forEach((fn) => fn());
359
+ (_c = this.pendingTask.resolve) === null || _c === void 0 ? void 0 : _c.forEach(fn => fn());
307
360
  this.emit('buildSuccess');
308
361
  }
309
362
  catch (error) {
310
363
  this.watching = false;
311
- (_d = this.pendingTask.reject) === null || _d === void 0 ? void 0 : _d.forEach((fn) => fn());
364
+ (_d = this.pendingTask.reject) === null || _d === void 0 ? void 0 : _d.forEach(fn => fn());
312
365
  this.kill();
313
366
  this.emit('buildError', error);
314
367
  }
@@ -334,12 +387,14 @@ class WebPackDevCompiler extends AbstractCompiler {
334
387
  });
335
388
  }
336
389
  }
390
+ exports.WebPackDevCompiler = WebPackDevCompiler;
337
391
  class BuildCompiler extends AbstractCompiler {
338
392
  constructor(props, args) {
339
393
  super(props, args);
340
394
  this.cps = [];
395
+ this._implType = (args === null || args === void 0 ? void 0 : args.implType) || 'cp';
341
396
  this.inspectPort = 9228;
342
- this.createSp();
397
+ this.createImpl();
343
398
  }
344
399
  build(option) {
345
400
  return __awaiter(this, void 0, void 0, function* () {
@@ -354,7 +409,8 @@ class BuildCompiler extends AbstractCompiler {
354
409
  });
355
410
  }
356
411
  }
357
- function getProcessCompilerClass(compilerType) {
412
+ exports.BuildCompiler = BuildCompiler;
413
+ function getProjectCompilerClass(compilerType) {
358
414
  const CompilerClass = compilerType === constant_config_1.COMPILER_TYPE.legacy_dev
359
415
  ? WebPackDevCompiler
360
416
  : compilerType === constant_config_1.COMPILER_TYPE.dev
@@ -362,4 +418,4 @@ function getProcessCompilerClass(compilerType) {
362
418
  : BuildCompiler;
363
419
  return CompilerClass;
364
420
  }
365
- exports.getProcessCompilerClass = getProcessCompilerClass;
421
+ exports.getProjectCompilerClass = getProjectCompilerClass;
@@ -0,0 +1 @@
1
+ export declare function runCompilerImpl(initData: any, onMessage: any, _sendToMaster: any): void;
@@ -9,21 +9,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.runCompilerImpl = void 0;
12
13
  const packs_1 = require("./packs");
13
- let globalConfig = JSON.parse(process.argv[2]);
14
- let hadWatchBuilt = false;
14
+ let globalConfig = {};
15
15
  let projectCompiler;
16
- /* 发送消息给父进程 */
17
- const sendToMaster = (method, data = {}) => {
18
- var _a;
19
- (_a = process.send) === null || _a === void 0 ? void 0 : _a.call(process, JSON.stringify(Object.assign({ method }, data)));
20
- };
16
+ let sendToMaster;
21
17
  const tryGC = () => {
22
18
  if (global.gc) {
23
19
  global.gc();
24
20
  }
25
21
  };
26
- function formatWebpackMessages(err, stats) {
22
+ function formatOutputMessages(err, stats) {
27
23
  if (err) {
28
24
  return { errors: [err.message], warnings: [] };
29
25
  }
@@ -49,7 +45,7 @@ const handlers = {
49
45
  const hasError = err || ((_a = stats === null || stats === void 0 ? void 0 : stats.hasErrors) === null || _a === void 0 ? void 0 : _a.call(stats));
50
46
  if (hasError) {
51
47
  sendToMaster('emitCompileStats', {
52
- stats: formatWebpackMessages(err, stats)
48
+ stats: formatOutputMessages(err, stats)
53
49
  });
54
50
  return;
55
51
  }
@@ -61,11 +57,10 @@ const handlers = {
61
57
  sendToMaster('callback', {
62
58
  uuid,
63
59
  success: false,
64
- stats: formatWebpackMessages(err, stats)
60
+ stats: formatOutputMessages(err, stats)
65
61
  });
66
62
  }
67
63
  else {
68
- hadWatchBuilt = true;
69
64
  sendToMaster('callback', {
70
65
  uuid,
71
66
  success: true
@@ -87,11 +82,10 @@ const handlers = {
87
82
  sendToMaster('callback', {
88
83
  uuid,
89
84
  success: false,
90
- stats: formatWebpackMessages(err, stats)
85
+ stats: formatOutputMessages(err, stats)
91
86
  });
92
87
  }
93
88
  else {
94
- hadWatchBuilt = true;
95
89
  sendToMaster('callback', {
96
90
  uuid,
97
91
  success: true
@@ -104,17 +98,15 @@ const handlers = {
104
98
  if (!projectCompiler) {
105
99
  projectCompiler = (0, packs_1.createCompiler)(data);
106
100
  }
107
- projectCompiler.on('progress', progress => {
108
- sendToMaster('compilePercent', {
109
- percentage: progress
110
- });
101
+ projectCompiler.on('progress', (data) => {
102
+ sendToMaster('compilePercent', data);
111
103
  });
112
104
  projectCompiler.build((err, stats) => {
113
105
  var _a;
114
106
  const hasError = err || ((_a = stats === null || stats === void 0 ? void 0 : stats.hasErrors) === null || _a === void 0 ? void 0 : _a.call(stats));
115
107
  console.log('sp run build finish', stats);
116
108
  if (hasError) {
117
- const statsInfo = formatWebpackMessages(err, stats);
109
+ const statsInfo = formatOutputMessages(err, stats);
118
110
  sendToMaster('callback', {
119
111
  uuid,
120
112
  success: false,
@@ -148,28 +140,23 @@ const handlers = {
148
140
  projectCompiler = null;
149
141
  }
150
142
  };
151
- process.on('message', (message) => {
152
- let objData = {};
153
- try {
154
- objData = JSON.parse(message);
155
- }
156
- catch (error) {
157
- // do something
158
- }
159
- const { uuid, method, data = {} } = objData;
160
- if (!handlers[method]) {
161
- return;
162
- }
163
- handlers[method](objData);
164
- });
165
- process.on('uncaughtException', error => {
166
- console.error(error);
167
- sendToMaster('emitCompileStats', {
168
- stats: {
169
- errors: [error.message]
143
+ function runCompilerImpl(initData, onMessage, _sendToMaster) {
144
+ globalConfig = initData;
145
+ sendToMaster = _sendToMaster;
146
+ const handleMessage = objData => {
147
+ objData = typeof objData === 'string' ? JSON.parse(objData) : objData;
148
+ const { passData, uuid, method, data = {} } = objData;
149
+ // 初始化数据
150
+ if (passData) {
151
+ globalConfig = passData;
152
+ return;
170
153
  }
171
- });
172
- });
173
- process.on('disconnect', () => {
174
- process.exit(-1);
175
- });
154
+ // call handler
155
+ if (!handlers[method]) {
156
+ return;
157
+ }
158
+ handlers[method](objData);
159
+ };
160
+ onMessage(handleMessage);
161
+ }
162
+ exports.runCompilerImpl = runCompilerImpl;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const compilerImpl_1 = require("./compilerImpl");
4
+ let initData = JSON.parse(process.argv[2]);
5
+ /* 发送消息给父进程 */
6
+ const sendToMaster = (method, data = {}) => {
7
+ var _a;
8
+ (_a = process.send) === null || _a === void 0 ? void 0 : _a.call(process, JSON.stringify(Object.assign({ method }, data)));
9
+ };
10
+ function onMessage(handler) {
11
+ process.on('message', (message) => {
12
+ let objData = {};
13
+ try {
14
+ objData = JSON.parse(message);
15
+ }
16
+ catch (error) {
17
+ // do something
18
+ }
19
+ handler(message);
20
+ });
21
+ }
22
+ process.on('uncaughtException', error => {
23
+ console.error(error);
24
+ sendToMaster('emitCompileStats', {
25
+ stats: {
26
+ errors: [error.message]
27
+ }
28
+ });
29
+ });
30
+ process.on('disconnect', () => {
31
+ process.exit(-1);
32
+ });
33
+ (0, compilerImpl_1.runCompilerImpl)(initData, onMessage, sendToMaster);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const compilerImpl_1 = require("./compilerImpl");
4
+ const { workerData, parentPort } = require('worker_threads');
5
+ const initData = workerData || {};
6
+ /* 发送消息主线程 */
7
+ const sendToMaster = (method, data = {}) => {
8
+ parentPort.postMessage(JSON.stringify(Object.assign({ method }, data)));
9
+ };
10
+ const onMessage = handler => {
11
+ parentPort.on('message', (message) => {
12
+ let objData = {};
13
+ try {
14
+ objData = JSON.parse(message);
15
+ }
16
+ catch (error) {
17
+ // do something
18
+ }
19
+ handler(objData);
20
+ });
21
+ };
22
+ parentPort.on('uncaughtException', error => {
23
+ console.error(error);
24
+ sendToMaster('emitCompileStats', {
25
+ stats: {
26
+ errors: [error.message]
27
+ }
28
+ });
29
+ });
30
+ (0, compilerImpl_1.runCompilerImpl)(initData, onMessage, sendToMaster);
@@ -0,0 +1,6 @@
1
+ declare const runCompilerImpl: any;
2
+ declare const createCompiler: any;
3
+ declare let globalConfig: any;
4
+ declare let projectCompiler: any;
5
+ declare const sendToMaster: (method: any, data?: {}) => void;
6
+ declare const onMessage: (handler: any) => void;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ const { runCompilerImpl } = require('./compilerImpl');
3
+ const { createCompiler } = require('./packs');
4
+ let globalConfig = {};
5
+ let projectCompiler;
6
+ /* 发送消息主线程 */
7
+ const sendToMaster = (method, data = {}) => {
8
+ // @ts-ignore
9
+ postMessage(JSON.stringify(Object.assign({ method }, data)));
10
+ };
11
+ const onMessage = handler => {
12
+ // @ts-ignore
13
+ onmessage = (e) => {
14
+ const message = e.data;
15
+ let objData = {};
16
+ try {
17
+ objData = JSON.parse(message);
18
+ }
19
+ catch (error) {
20
+ // do something
21
+ }
22
+ handler(objData);
23
+ };
24
+ };
25
+ runCompilerImpl(null, onMessage, sendToMaster);
@@ -0,0 +1,25 @@
1
+ /// <reference types="node/http.js" />
2
+ /// <reference types="node/fs.js" />
3
+ import { Response } from "./index.js";
4
+ import { ReadStream, createReadStream as fsCreateReadStream } from "fs";
5
+ import { ServerResponse } from "http";
6
+ export interface ExpectedResponse {
7
+ status?: (status: number) => void;
8
+ send?: (data: any) => void;
9
+ pipeInto?: (data: any) => void;
10
+ }
11
+ declare function setStatusCode(res: ServerResponse & ExpectedResponse, code: number): void;
12
+ declare function pipe(res: ServerResponse & ExpectedResponse, bufferOrStream: ReadStream): void;
13
+ declare function send(res: Response, bufferOrStream: string | Buffer): void;
14
+ interface OutputFileSystem {
15
+ createReadStream?: typeof fsCreateReadStream;
16
+ readFileSync?: (path: string | Buffer, options?: {
17
+ encoding?: null;
18
+ flag?: string;
19
+ }) => Buffer;
20
+ }
21
+ declare function createReadStreamOrReadFileSync(filename: string, outputFileSystem: OutputFileSystem, start: number, end: number): {
22
+ bufferOrStream: Buffer | ReadStream;
23
+ byteLength: number;
24
+ };
25
+ export { setStatusCode, send, pipe, createReadStreamOrReadFileSync };
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createReadStreamOrReadFileSync = exports.pipe = exports.send = exports.setStatusCode = void 0;
4
+ // Function to set status code
5
+ function setStatusCode(res, code) {
6
+ // Pseudo API
7
+ if (typeof res.status === "function") {
8
+ res.status(code);
9
+ return;
10
+ }
11
+ // Node.js API
12
+ res.statusCode = code;
13
+ }
14
+ exports.setStatusCode = setStatusCode;
15
+ // Function to pipe data to response
16
+ function pipe(res, bufferOrStream) {
17
+ // Pseudo API and Koa API
18
+ if (typeof res.pipeInto === "function") {
19
+ res.pipeInto(bufferOrStream);
20
+ return;
21
+ }
22
+ // Node.js API and Express API and Hapi API
23
+ bufferOrStream.pipe(res);
24
+ }
25
+ exports.pipe = pipe;
26
+ // Function to send data to response
27
+ function send(res, bufferOrStream) {
28
+ // Pseudo API and Express API and Koa API
29
+ if (typeof res.send === "function") {
30
+ res.send(bufferOrStream);
31
+ return;
32
+ }
33
+ res.end(bufferOrStream);
34
+ }
35
+ exports.send = send;
36
+ // Function to create read stream or read file synchronously
37
+ function createReadStreamOrReadFileSync(filename, outputFileSystem, start, end) {
38
+ let bufferOrStream;
39
+ let byteLength;
40
+ // Stream logic
41
+ const isFsSupportsStream = typeof outputFileSystem.createReadStream === "function";
42
+ if (isFsSupportsStream) {
43
+ bufferOrStream = outputFileSystem.createReadStream(filename, {
44
+ start,
45
+ end,
46
+ });
47
+ // Handle files with zero bytes
48
+ byteLength = end === 0 ? 0 : end - start + 1;
49
+ }
50
+ else {
51
+ bufferOrStream = outputFileSystem.readFileSync(filename);
52
+ byteLength = bufferOrStream.byteLength;
53
+ }
54
+ return { bufferOrStream, byteLength };
55
+ }
56
+ exports.createReadStreamOrReadFileSync = createReadStreamOrReadFileSync;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Escapes a string for use in HTML.
3
+ */
4
+ declare function escapeHtml(string: string): string;
5
+ export default escapeHtml;