Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getLogger not defined in child loader #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
322 changes: 321 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"eslint-plugin-import": "^2.22.1",
"husky": "^4.3.0",
"jest": "^26.6.1",
"less-loader": "^10.2.0",
"lint-staged": "^10.5.0",
"lodash": "^4.17.20",
"memfs": "^3.2.0",
Expand Down
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
31 changes: 31 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,37 @@ 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) {
writeLoggerJson('error', message);
},

warn(message) {
writeLoggerJson('warn', message);
},

log(message) {
writeLoggerJson('log', message);
},

debug(message) {
writeLoggerJson('debug', message);
},
};
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should run webpack logger here, otherwise options from configuration for logging will not work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we map methods created logger, we can have more them error/warn/log/debug

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just bumping this PR for some clarification.

Can we map methods created logger, we can have more them error/warn/log/debug

@alexander-akait Do you mean to just loop through the list of methods instead of manually specifying them? e.g.

const methods = ['error', 'warn', 'log', 'debug']];
const logger = {}

for (const key of methods ) {
    logger[key] = ...
}

return logger;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait Nudging this PR to see what's blocking it?

emitWarning: (warning) => {
writeJson({
type: 'emitWarning',
Expand Down
3 changes: 3 additions & 0 deletions test/less-loader-example/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
30 changes: 30 additions & 0 deletions test/less-loader-example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require('path');

const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // eslint-disable-line import/no-extraneous-dependencies

module.exports = {
mode: 'none',
context: __dirname,
entry: ['./style.less'],
output: {
path: path.resolve('dist'),
},
module: {
rules: [
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
path.resolve(__dirname, '../../dist/index.js'),
'css-loader',
'less-loader',
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
}),
],
};
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;
19 changes: 19 additions & 0 deletions test/webpack.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import webpack from 'webpack';

import sassLoaderConfig from './sass-loader-example/webpack.config';
import lessLoaderConfig from './less-loader-example/webpack.config';

test("Processes sass-loader's @import correctly", (done) => {
const config = sassLoaderConfig({});
Expand All @@ -21,3 +22,21 @@ test("Processes sass-loader's @import correctly", (done) => {
done();
});
}, 30000);

test('Works with less-loader', (done) => {
webpack(lessLoaderConfig, (err, stats) => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
}

expect(err).toBe(null);

if (stats.hasErrors()) {
// eslint-disable-next-line no-console
console.error(stats.toJson().errors);
}
expect(stats.hasErrors()).toBe(false);
done();
});
}, 30000);