Skip to content

Commit

Permalink
fix: loadModule and fs are now available in a loader context (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesoptier committed Jun 29, 2020
1 parent 488300f commit ea5c9ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/WorkerPool.js
Expand Up @@ -188,6 +188,29 @@ class PoolWorker {
});
break;
}
case 'loadModule': {
const { request, questionId } = message;
const { data } = this.jobs[id];
data.loadModule(request, (error, source, sourceMap, module) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result: [
source,
sourceMap,
// TODO: Serialize module?
// module,
],
});
});
finalCallback();
break;
}
case 'resolve': {
const { context, request, questionId } = message;
const { data } = this.jobs[id];
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -20,6 +20,7 @@ function pitch() {
sourceMap: this.sourceMap,
emitError: this.emitError,
emitWarning: this.emitWarning,
loadModule: this.loadModule,
resolve: this.resolve,
target: this.target,
minimize: this.minimize,
Expand Down
11 changes: 11 additions & 0 deletions src/worker.js
Expand Up @@ -107,6 +107,17 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
readResource: fs.readFile.bind(fs),
context: {
version: 2,
fs,
loadModule: (request, callback) => {
callbackMap[nextQuestionId] = (error, result) => callback(error, ...result);
writeJson({
type: 'loadModule',
id,
questionId: nextQuestionId,
request,
});
nextQuestionId += 1;
},
resolve: (context, request, callback) => {
callbackMap[nextQuestionId] = callback;
writeJson({
Expand Down

0 comments on commit ea5c9ad

Please sign in to comment.