@parcel/workers 2.0.0-nightly.130 → 2.0.0-nightly.1303

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.
@@ -5,44 +5,62 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _worker_threads = require("worker_threads");
8
+ function _worker_threads() {
9
+ const data = require("worker_threads");
9
10
 
10
- var _nullthrows = _interopRequireDefault(require("nullthrows"));
11
+ _worker_threads = function () {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ function _nullthrows() {
19
+ const data = _interopRequireDefault(require("nullthrows"));
20
+
21
+ _nullthrows = function () {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
11
27
 
12
28
  var _childState = require("../childState");
13
29
 
14
30
  var _child = require("../child");
15
31
 
16
- var _core = require("@parcel/core");
32
+ function _core() {
33
+ const data = require("@parcel/core");
17
34
 
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
+ _core = function () {
36
+ return data;
37
+ };
38
+
39
+ return data;
40
+ }
19
41
 
20
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
42
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
43
 
22
44
  class ThreadsChild {
23
45
  constructor(onMessage, onExit) {
24
- _defineProperty(this, "onMessage", void 0);
25
-
26
- _defineProperty(this, "onExit", void 0);
27
-
28
- if (_worker_threads.isMainThread || !_worker_threads.parentPort) {
46
+ if (_worker_threads().isMainThread || !_worker_threads().parentPort) {
29
47
  throw new Error('Only create ThreadsChild instances in a worker!');
30
48
  }
31
49
 
32
50
  this.onMessage = onMessage;
33
51
  this.onExit = onExit;
34
52
 
35
- _worker_threads.parentPort.on('message', data => this.handleMessage(data));
53
+ _worker_threads().parentPort.on('message', data => this.handleMessage(data));
36
54
 
37
- _worker_threads.parentPort.on('close', this.onExit);
55
+ _worker_threads().parentPort.on('close', this.onExit);
38
56
  }
39
57
 
40
58
  handleMessage(data) {
41
- this.onMessage((0, _core.restoreDeserializedObject)(data));
59
+ this.onMessage((0, _core().restoreDeserializedObject)(data));
42
60
  }
43
61
 
44
62
  send(data) {
45
- (0, _nullthrows.default)(_worker_threads.parentPort).postMessage((0, _core.prepareForSerialization)(data));
63
+ (0, _nullthrows().default)(_worker_threads().parentPort).postMessage((0, _core().prepareForSerialization)(data));
46
64
  }
47
65
 
48
66
  }
@@ -5,30 +5,42 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _worker_threads = require("worker_threads");
8
+ function _worker_threads() {
9
+ const data = require("worker_threads");
9
10
 
10
- var _path = _interopRequireDefault(require("path"));
11
+ _worker_threads = function () {
12
+ return data;
13
+ };
11
14
 
12
- var _core = require("@parcel/core");
15
+ return data;
16
+ }
13
17
 
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
+ function _path() {
19
+ const data = _interopRequireDefault(require("path"));
15
20
 
16
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
+ _path = function () {
22
+ return data;
23
+ };
17
24
 
18
- const WORKER_PATH = _path.default.join(__dirname, 'ThreadsChild.js');
25
+ return data;
26
+ }
19
27
 
20
- class ThreadsWorker {
21
- constructor(execArgv, onMessage, onError, onExit) {
22
- _defineProperty(this, "execArgv", void 0);
28
+ function _core() {
29
+ const data = require("@parcel/core");
23
30
 
24
- _defineProperty(this, "onMessage", void 0);
31
+ _core = function () {
32
+ return data;
33
+ };
25
34
 
26
- _defineProperty(this, "onError", void 0);
35
+ return data;
36
+ }
27
37
 
28
- _defineProperty(this, "onExit", void 0);
38
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
39
 
30
- _defineProperty(this, "worker", void 0);
40
+ const WORKER_PATH = _path().default.join(__dirname, 'ThreadsChild.js');
31
41
 
42
+ class ThreadsWorker {
43
+ constructor(execArgv, onMessage, onError, onExit) {
32
44
  this.execArgv = execArgv;
33
45
  this.onMessage = onMessage;
34
46
  this.onError = onError;
@@ -36,7 +48,7 @@ class ThreadsWorker {
36
48
  }
37
49
 
38
50
  start() {
39
- this.worker = new _worker_threads.Worker(WORKER_PATH, {
51
+ this.worker = new (_worker_threads().Worker)(WORKER_PATH, {
40
52
  execArgv: this.execArgv,
41
53
  env: process.env
42
54
  });
@@ -55,11 +67,11 @@ class ThreadsWorker {
55
67
  }
56
68
 
57
69
  handleMessage(data) {
58
- this.onMessage((0, _core.restoreDeserializedObject)(data));
70
+ this.onMessage((0, _core().restoreDeserializedObject)(data));
59
71
  }
60
72
 
61
73
  send(data) {
62
- this.worker.postMessage((0, _core.prepareForSerialization)(data));
74
+ this.worker.postMessage((0, _core().prepareForSerialization)(data));
63
75
  }
64
76
 
65
77
  }
package/package.json CHANGED
@@ -1,29 +1,40 @@
1
1
  {
2
2
  "name": "@parcel/workers",
3
- "version": "2.0.0-nightly.130+870013ca",
3
+ "version": "2.0.0-nightly.1303+cd4336412",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
+ "funding": {
10
+ "type": "opencollective",
11
+ "url": "https://opencollective.com/parcel"
12
+ },
9
13
  "repository": {
10
14
  "type": "git",
11
15
  "url": "https://github.com/parcel-bundler/parcel.git"
12
16
  },
13
17
  "main": "lib/index.js",
14
18
  "source": "src/index.js",
19
+ "types": "index.d.ts",
15
20
  "engines": {
16
- "node": ">= 10.0.0"
21
+ "node": ">= 12.0.0"
17
22
  },
18
23
  "dependencies": {
19
- "@parcel/diagnostic": "2.0.0-nightly.130+870013ca",
20
- "@parcel/logger": "2.0.0-nightly.130+870013ca",
21
- "@parcel/utils": "2.0.0-nightly.130+870013ca",
22
- "chrome-trace-event": "^1.0.2",
24
+ "@parcel/diagnostic": "2.0.0-nightly.1303+cd4336412",
25
+ "@parcel/logger": "2.0.0-nightly.1303+cd4336412",
26
+ "@parcel/profiler": "2.8.4-nightly.2926+cd4336412",
27
+ "@parcel/types": "2.0.0-nightly.1303+cd4336412",
28
+ "@parcel/utils": "2.0.0-nightly.1303+cd4336412",
23
29
  "nullthrows": "^1.1.1"
24
30
  },
25
31
  "peerDependencies": {
26
- "@parcel/core": "^2.0.0-alpha.3.1"
32
+ "@parcel/core": "2.0.0-nightly.1301+cd4336412"
33
+ },
34
+ "browser": {
35
+ "./src/cpuCount.js": false,
36
+ "./src/process/ProcessWorker.js": false,
37
+ "./src/threads/ThreadsWorker.js": false
27
38
  },
28
- "gitHead": "870013cab1003cb422fd425af50c934407df9ecb"
39
+ "gitHead": "cd4336412242b6d6da40ef6cba8e7ac99c72b090"
29
40
  }
package/src/Handle.js CHANGED
@@ -1,20 +1,16 @@
1
- // @flow
2
-
3
- import type {WorkerApi} from './';
4
-
1
+ // @flow strict-local
5
2
  import {registerSerializableClass} from '@parcel/core';
6
-
7
- import {child} from './childState';
3
+ // $FlowFixMe
8
4
  import packageJson from '../package.json';
9
5
 
10
6
  let HANDLE_ID = 0;
11
-
7
+ // $FlowFixMe
12
8
  export type HandleFunction = (...args: Array<any>) => any;
13
9
 
14
10
  type HandleOpts = {|
15
- fn: HandleFunction,
11
+ fn?: HandleFunction,
16
12
  childId?: ?number,
17
- workerApi: WorkerApi,
13
+ id?: number,
18
14
  |};
19
15
 
20
16
  const handleById: Map<number, Handle> = new Map();
@@ -22,14 +18,12 @@ const handleById: Map<number, Handle> = new Map();
22
18
  export default class Handle {
23
19
  id: number;
24
20
  childId: ?number;
25
- fn: HandleFunction;
26
- workerApi: WorkerApi;
21
+ fn: ?HandleFunction;
27
22
 
28
23
  constructor(opts: HandleOpts) {
29
- this.id = ++HANDLE_ID;
24
+ this.id = opts.id ?? ++HANDLE_ID;
30
25
  this.fn = opts.fn;
31
26
  this.childId = opts.childId;
32
- this.workerApi = opts.workerApi;
33
27
  handleById.set(this.id, this);
34
28
  }
35
29
 
@@ -37,38 +31,15 @@ export default class Handle {
37
31
  handleById.delete(this.id);
38
32
  }
39
33
 
40
- serialize() {
34
+ serialize(): {|childId: ?number, id: number|} {
41
35
  return {
42
36
  id: this.id,
43
37
  childId: this.childId,
44
38
  };
45
39
  }
46
40
 
47
- static deserialize(opts: {|id: number, childId?: number|}) {
48
- return function(...args: Array<mixed>) {
49
- let workerApi;
50
- if (child) {
51
- workerApi = child.workerApi;
52
- } else {
53
- let handle = handleById.get(opts.id);
54
- if (!handle) {
55
- throw new Error(
56
- 'Corresponding Handle was not found. It may have been disposed.',
57
- );
58
- }
59
- workerApi = handle.workerApi;
60
- }
61
-
62
- if (opts.childId != null && child) {
63
- throw new Error('Cannot call another child from a child');
64
- }
65
-
66
- if (opts.childId != null && workerApi.callChild) {
67
- return workerApi.callChild(opts.childId, {handle: opts.id, args});
68
- }
69
-
70
- return workerApi.callMaster({handle: opts.id, args}, true);
71
- };
41
+ static deserialize(opts: HandleOpts): Handle {
42
+ return new Handle(opts);
72
43
  }
73
44
  }
74
45
 
package/src/Worker.js CHANGED
@@ -1,8 +1,10 @@
1
1
  // @flow
2
2
 
3
3
  import type {FilePath} from '@parcel/types';
4
- import type {WorkerMessage, WorkerImpl, BackendType} from './types';
4
+ import type {BackendType, WorkerImpl, WorkerMessage} from './types';
5
+ import type {SharedReference} from './WorkerFarm';
5
6
 
7
+ import nullthrows from 'nullthrows';
6
8
  import EventEmitter from 'events';
7
9
  import ThrowableDiagnostic from '@parcel/diagnostic';
8
10
  import {getWorkerBackend} from './backend';
@@ -12,6 +14,7 @@ export type WorkerCall = {|
12
14
  handle?: number,
13
15
  args: $ReadOnlyArray<any>,
14
16
  retries: number,
17
+ skipReadyCheck?: boolean,
15
18
  resolve: (result: Promise<any> | any) => void,
16
19
  reject: (error: any) => void,
17
20
  |};
@@ -19,7 +22,9 @@ export type WorkerCall = {|
19
22
  type WorkerOpts = {|
20
23
  forcedKillTime: number,
21
24
  backend: BackendType,
22
- patchConsole?: boolean,
25
+ shouldPatchConsole?: boolean,
26
+ shouldTrace?: boolean,
27
+ sharedReferences: $ReadOnlyMap<SharedReference, mixed>,
23
28
  |};
24
29
 
25
30
  let WORKER_ID = 0;
@@ -27,14 +32,15 @@ export default class Worker extends EventEmitter {
27
32
  +options: WorkerOpts;
28
33
  worker: WorkerImpl;
29
34
  id: number = WORKER_ID++;
35
+ sentSharedReferences: Set<SharedReference> = new Set();
30
36
 
31
37
  calls: Map<number, WorkerCall> = new Map();
32
- exitCode = null;
33
- callId = 0;
38
+ exitCode: ?number = null;
39
+ callId: number = 0;
34
40
 
35
- ready = false;
36
- stopped = false;
37
- isStopping = false;
41
+ ready: boolean = false;
42
+ stopped: boolean = false;
43
+ isStopping: boolean = false;
38
44
 
39
45
  constructor(options: WorkerOpts) {
40
46
  super();
@@ -43,20 +49,45 @@ export default class Worker extends EventEmitter {
43
49
 
44
50
  async fork(forkModule: FilePath) {
45
51
  let filteredArgs = process.execArgv.filter(
46
- v => !/^--(debug|inspect)/.test(v),
52
+ v => !/^--(debug|inspect|max-old-space-size=)/.test(v),
47
53
  );
48
54
 
49
55
  for (let i = 0; i < filteredArgs.length; i++) {
50
56
  let arg = filteredArgs[i];
51
- if (
52
- (arg === '-r' || arg === '--require') &&
53
- filteredArgs[i + 1] === '@parcel/register'
54
- ) {
57
+ let isArgWithParam =
58
+ ((arg === '-r' || arg === '--require') &&
59
+ filteredArgs[i + 1] === '@parcel/register') ||
60
+ arg === '--title';
61
+ if (isArgWithParam) {
55
62
  filteredArgs.splice(i, 2);
56
63
  i--;
57
64
  }
58
65
  }
59
66
 
67
+ // Workaround for https://github.com/nodejs/node/issues/29117
68
+ if (process.env.NODE_OPTIONS) {
69
+ // arg parsing logic adapted from https://stackoverflow.com/a/46946420/2352201
70
+ let opts = [''];
71
+ let quote = false;
72
+ for (let c of nullthrows(process.env.NODE_OPTIONS.match(/.|^$/g))) {
73
+ if (c === '"') {
74
+ quote = !quote;
75
+ } else if (!quote && c === ' ') {
76
+ opts.push('');
77
+ } else {
78
+ opts[opts.length - 1] += c.replace(/\\(.)/, '$1');
79
+ }
80
+ }
81
+
82
+ for (let i = 0; i < opts.length; i++) {
83
+ let opt = opts[i];
84
+ if (opt === '-r' || opt === '--require') {
85
+ filteredArgs.push(opt, opts[i + 1]);
86
+ i++;
87
+ }
88
+ }
89
+ }
90
+
60
91
  let onMessage = data => this.receive(data);
61
92
  let onExit = code => {
62
93
  this.exitCode = code;
@@ -77,19 +108,49 @@ export default class Worker extends EventEmitter {
77
108
  args: [
78
109
  forkModule,
79
110
  {
80
- patchConsole: !!this.options.patchConsole,
111
+ shouldPatchConsole: !!this.options.shouldPatchConsole,
112
+ shouldTrace: !!this.options.shouldTrace,
81
113
  },
82
114
  ],
83
115
  retries: 0,
116
+ skipReadyCheck: true,
84
117
  resolve,
85
118
  reject,
86
119
  });
87
120
  });
88
121
 
122
+ let sharedRefs = this.options.sharedReferences;
123
+ let refsShared = new Set();
124
+ // in case more refs are created while initial refs are sending
125
+ while (refsShared.size < sharedRefs.size) {
126
+ await Promise.all(
127
+ [...sharedRefs]
128
+ .filter(([ref]) => !refsShared.has(ref))
129
+ .map(async ([ref, value]) => {
130
+ await this.sendSharedReference(ref, value);
131
+ refsShared.add(ref);
132
+ }),
133
+ );
134
+ }
135
+
89
136
  this.ready = true;
90
137
  this.emit('ready');
91
138
  }
92
139
 
140
+ sendSharedReference(ref: SharedReference, value: mixed): Promise<any> {
141
+ this.sentSharedReferences.add(ref);
142
+ return new Promise((resolve, reject) => {
143
+ this.call({
144
+ method: 'createSharedReference',
145
+ args: [ref, value],
146
+ resolve,
147
+ reject,
148
+ retries: 0,
149
+ skipReadyCheck: true,
150
+ });
151
+ });
152
+ }
153
+
93
154
  send(data: WorkerMessage): void {
94
155
  this.worker.send(data);
95
156
  }
@@ -102,14 +163,20 @@ export default class Worker extends EventEmitter {
102
163
  let idx = this.callId++;
103
164
  this.calls.set(idx, call);
104
165
 
105
- this.send({
166
+ let msg = {
106
167
  type: 'request',
107
168
  idx: idx,
108
169
  child: this.id,
109
170
  handle: call.handle,
110
171
  method: call.method,
111
172
  args: call.args,
112
- });
173
+ };
174
+
175
+ if (this.ready || call.skipReadyCheck === true) {
176
+ this.send(msg);
177
+ } else {
178
+ this.once('ready', () => this.send(msg));
179
+ }
113
180
  }
114
181
 
115
182
  receive(message: WorkerMessage): void {