Skip to content

Commit

Permalink
fix: plumb log messages back through to parent logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Hodgson committed Dec 16, 2021
1 parent b4418ae commit ce90da5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ class PoolWorker {
finalCallback();
break;
}
case 'getLogger': {
// initialise logger by name in jobData
const { data } = message;
const { data: jobData } = this.jobs[id];
if (!Object.hasOwnProperty.call(jobData.loggers, data.name)) {
jobData.loggers[data.name] = jobData.getLogger(data.name);
}
break;
}
case 'logger': {
const { data } = message;
const { data: jobData } = this.jobs[id];
jobData.loggers[data.name][data.severity](data.message);
break;
}
default: {
console.error(`Unexpected worker message ${type} in WorkerPool.`);
finalCallback();
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function pitch() {
sourceMap: this.sourceMap,
emitError: this.emitError,
emitWarning: this.emitWarning,
getLogger: this.getLogger,
loggers: {},
loadModule: this.loadModule,
resolve: this.resolve,
getResolve: this.getResolve,
Expand Down
20 changes: 16 additions & 4 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,33 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
return options;
},
getLogger: (name) => {
function writeLoggerJson(severity, message) {
writeJson({
type: 'logger',
id,
data: { severity, name, message },
});
}
writeJson({
type: 'getLogger',
id,
data: { name },
});
return {
error(message) {
console.error(`${name}: ${message}`);
writeLoggerJson('error', message);
},

warn(message) {
console.warn(`${name}: ${message}`);
writeLoggerJson('warn', message);
},

log(message) {
console.log(`${name}: ${message}`);
writeLoggerJson('log', message);
},

debug(message) {
console.debug(`${name}: ${message}`);
writeLoggerJson('debug', message);
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion test/sass-loader-example/assets/color_palette.scss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$white: #FFFFFF;
$white: #ffffff;

0 comments on commit ce90da5

Please sign in to comment.