Skip to content

Commit 69625e0

Browse files
Jasper De Moordevongovett
authored andcommitted
Custom workerfarm, BI-Directional IPC (#1105)
1 parent 25a054f commit 69625e0

20 files changed

Lines changed: 920 additions & 179 deletions

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"tomlify-j0.4": "^3.0.0",
6060
"uglify-es": "^3.2.1",
6161
"v8-compile-cache": "^1.1.0",
62-
"worker-farm": "^1.5.2",
6362
"ws": "^4.0.0"
6463
},
6564
"devDependencies": {

src/Bundler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('./utils/fs');
22
const Resolver = require('./Resolver');
33
const Parser = require('./Parser');
4-
const WorkerFarm = require('./WorkerFarm');
4+
const WorkerFarm = require('./workerfarm/WorkerFarm');
55
const Path = require('path');
66
const Bundle = require('./Bundle');
77
const {FSWatcher} = require('chokidar');

src/Logger.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,19 @@ function stringWidth(string) {
195195
// If we are in a worker, make a proxy class which will
196196
// send the logger calls to the main process via IPC.
197197
// These are handled in WorkerFarm and directed to handleMessage above.
198-
if (process.send) {
198+
if (process.send && process.env.WORKER_TYPE === 'parcel-worker') {
199+
const worker = require('./worker');
199200
class LoggerProxy {}
200201
for (let method of Object.getOwnPropertyNames(Logger.prototype)) {
201202
LoggerProxy.prototype[method] = (...args) => {
202-
process.send({
203-
type: 'logger',
204-
method,
205-
args
206-
});
203+
worker.addCall(
204+
{
205+
location: require.resolve('./Logger'),
206+
method,
207+
args
208+
},
209+
false
210+
);
207211
};
208212
}
209213

src/WorkerFarm.js

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/utils/localRequire.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {dirname} = require('path');
22
const resolve = require('resolve');
3-
const install = require('./installPackage');
3+
const worker = require('../worker');
44

55
const cache = new Map();
66

@@ -13,7 +13,10 @@ async function localRequire(name, path, triedInstall = false) {
1313
resolved = resolve.sync(name, {basedir});
1414
} catch (e) {
1515
if (e.code === 'MODULE_NOT_FOUND' && !triedInstall) {
16-
await install([name], path);
16+
await worker.addCall({
17+
location: require.resolve('./installPackage.js'),
18+
args: [[name], path]
19+
});
1720
return localRequire(name, path, true);
1821
}
1922
throw e;

src/worker.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
require('v8-compile-cache');
22
const Pipeline = require('./Pipeline');
3+
const child = require('./workerfarm/child');
4+
const WorkerFarm = require('./workerfarm/WorkerFarm');
35

46
let pipeline;
57

6-
exports.init = function(options, callback) {
8+
function init(options, isLocal = false) {
79
pipeline = new Pipeline(options || {});
810
Object.assign(process.env, options.env || {});
911
process.env.HMR_PORT = options.hmrPort;
1012
process.env.HMR_HOSTNAME = options.hmrHostname;
11-
callback();
12-
};
13+
if (isLocal) {
14+
process.env.WORKER_TYPE = 'parcel-worker';
15+
}
16+
}
1317

14-
exports.run = async function(path, pkg, options, isWarmUp, callback) {
18+
async function run(path, pkg, options, isWarmUp) {
1519
try {
1620
options.isWarmUp = isWarmUp;
17-
var result = await pipeline.process(path, pkg, options);
18-
19-
callback(null, result);
20-
} catch (err) {
21-
let returned = err;
22-
returned.fileName = path;
23-
callback(returned);
21+
return await pipeline.process(path, pkg, options);
22+
} catch (e) {
23+
e.fileName = path;
24+
throw e;
2425
}
25-
};
26+
}
2627

27-
process.on('unhandledRejection', function(err) {
28-
// ERR_IPC_CHANNEL_CLOSED happens when the worker is killed before it finishes processing
29-
if (err.code !== 'ERR_IPC_CHANNEL_CLOSED') {
30-
console.error('Unhandled promise rejection:', err.stack);
28+
// request.location is a module path relative to src or lib
29+
async function addCall(request, awaitResponse = true) {
30+
if (process.send && process.env.WORKER_TYPE === 'parcel-worker') {
31+
return child.addCall(request, awaitResponse);
32+
} else {
33+
return WorkerFarm.getShared().processRequest(request);
3134
}
32-
});
35+
}
36+
37+
exports.init = init;
38+
exports.run = run;
39+
exports.addCall = addCall;

0 commit comments

Comments
 (0)