Skip to content

Commit

Permalink
Custom workerfarm, BI-Directional IPC (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper De Moor authored and devongovett committed Apr 15, 2018
1 parent 25a054f commit 69625e0
Show file tree
Hide file tree
Showing 20 changed files with 920 additions and 179 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"tomlify-j0.4": "^3.0.0",
"uglify-es": "^3.2.1",
"v8-compile-cache": "^1.1.0",
"worker-farm": "^1.5.2",
"ws": "^4.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/Bundler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('./utils/fs');
const Resolver = require('./Resolver');
const Parser = require('./Parser');
const WorkerFarm = require('./WorkerFarm');
const WorkerFarm = require('./workerfarm/WorkerFarm');
const Path = require('path');
const Bundle = require('./Bundle');
const {FSWatcher} = require('chokidar');
Expand Down
16 changes: 10 additions & 6 deletions src/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,19 @@ function stringWidth(string) {
// If we are in a worker, make a proxy class which will
// send the logger calls to the main process via IPC.
// These are handled in WorkerFarm and directed to handleMessage above.
if (process.send) {
if (process.send && process.env.WORKER_TYPE === 'parcel-worker') {
const worker = require('./worker');
class LoggerProxy {}
for (let method of Object.getOwnPropertyNames(Logger.prototype)) {
LoggerProxy.prototype[method] = (...args) => {
process.send({
type: 'logger',
method,
args
});
worker.addCall(
{
location: require.resolve('./Logger'),
method,
args
},
false
);
};
}

Expand Down
141 changes: 0 additions & 141 deletions src/WorkerFarm.js

This file was deleted.

7 changes: 5 additions & 2 deletions src/utils/localRequire.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {dirname} = require('path');
const resolve = require('resolve');
const install = require('./installPackage');
const worker = require('../worker');

const cache = new Map();

Expand All @@ -13,7 +13,10 @@ async function localRequire(name, path, triedInstall = false) {
resolved = resolve.sync(name, {basedir});
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && !triedInstall) {
await install([name], path);
await worker.addCall({
location: require.resolve('./installPackage.js'),
args: [[name], path]
});
return localRequire(name, path, true);
}
throw e;
Expand Down
41 changes: 24 additions & 17 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
require('v8-compile-cache');
const Pipeline = require('./Pipeline');
const child = require('./workerfarm/child');
const WorkerFarm = require('./workerfarm/WorkerFarm');

let pipeline;

exports.init = function(options, callback) {
function init(options, isLocal = false) {
pipeline = new Pipeline(options || {});
Object.assign(process.env, options.env || {});
process.env.HMR_PORT = options.hmrPort;
process.env.HMR_HOSTNAME = options.hmrHostname;
callback();
};
if (isLocal) {
process.env.WORKER_TYPE = 'parcel-worker';
}
}

exports.run = async function(path, pkg, options, isWarmUp, callback) {
async function run(path, pkg, options, isWarmUp) {
try {
options.isWarmUp = isWarmUp;
var result = await pipeline.process(path, pkg, options);

callback(null, result);
} catch (err) {
let returned = err;
returned.fileName = path;
callback(returned);
return await pipeline.process(path, pkg, options);
} catch (e) {
e.fileName = path;
throw e;
}
};
}

process.on('unhandledRejection', function(err) {
// ERR_IPC_CHANNEL_CLOSED happens when the worker is killed before it finishes processing
if (err.code !== 'ERR_IPC_CHANNEL_CLOSED') {
console.error('Unhandled promise rejection:', err.stack);
// request.location is a module path relative to src or lib
async function addCall(request, awaitResponse = true) {
if (process.send && process.env.WORKER_TYPE === 'parcel-worker') {
return child.addCall(request, awaitResponse);
} else {
return WorkerFarm.getShared().processRequest(request);
}
});
}

exports.init = init;
exports.run = run;
exports.addCall = addCall;
Loading

0 comments on commit 69625e0

Please sign in to comment.