|
1 | 1 | require('v8-compile-cache'); |
2 | 2 | const Pipeline = require('./Pipeline'); |
| 3 | +const child = require('./workerfarm/child'); |
| 4 | +const WorkerFarm = require('./workerfarm/WorkerFarm'); |
3 | 5 |
|
4 | 6 | let pipeline; |
5 | 7 |
|
6 | | -exports.init = function(options, callback) { |
| 8 | +function init(options, isLocal = false) { |
7 | 9 | pipeline = new Pipeline(options || {}); |
8 | 10 | Object.assign(process.env, options.env || {}); |
9 | 11 | process.env.HMR_PORT = options.hmrPort; |
10 | 12 | process.env.HMR_HOSTNAME = options.hmrHostname; |
11 | | - callback(); |
12 | | -}; |
| 13 | + if (isLocal) { |
| 14 | + process.env.WORKER_TYPE = 'parcel-worker'; |
| 15 | + } |
| 16 | +} |
13 | 17 |
|
14 | | -exports.run = async function(path, pkg, options, isWarmUp, callback) { |
| 18 | +async function run(path, pkg, options, isWarmUp) { |
15 | 19 | try { |
16 | 20 | 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; |
24 | 25 | } |
25 | | -}; |
| 26 | +} |
26 | 27 |
|
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); |
31 | 34 | } |
32 | | -}); |
| 35 | +} |
| 36 | + |
| 37 | +exports.init = init; |
| 38 | +exports.run = run; |
| 39 | +exports.addCall = addCall; |
0 commit comments