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

feat: add support for less-loader #198

Open
wants to merge 3 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
282 changes: 177 additions & 105 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"fix": "npm-run-all -l fix:js fix:prettier",
"test:only": "cross-env NODE_ENV=test jest --forceExit",
"test:watch": "npm run test:only -- --watch",
"test:debug": "node --inspect-brk /node_modules/jest/bin/jest.js —runInBand webpack.test.js",
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
"pretest": "npm run lint",
"test": "npm run test:coverage",
Expand All @@ -45,6 +46,8 @@
},
"dependencies": {
"json-parse-better-errors": "^1.0.2",
"less": "^4.2.0",
"less-loader": "^11.1.3",
"loader-runner": "^4.1.0",
"neo-async": "^2.6.2",
"schema-utils": "^4.2.0"
Expand Down
58 changes: 58 additions & 0 deletions src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
this.activeJobs = 0;
this.onJobDone = onJobDone;
this.id = workerId;
this.logger = Object.create(null);

workerId += 1;
// Empty or invalid node args would break the child process
Expand Down Expand Up @@ -271,6 +272,63 @@
finalCallback();
break;
}
case 'getLogger': {
const { data: name } = message;
const { data: jobData } = this.jobs[id];

Check warning on line 277 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L275-L277

Added lines #L275 - L277 were not covered by tests
if (!this.logger[name]) {
this.logger[name] = jobData.getLogger(name);

Check warning on line 279 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L279

Added line #L279 was not covered by tests
}
finalCallback();
break;

Check warning on line 282 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L281-L282

Added lines #L281 - L282 were not covered by tests
}
case 'emitLoggingWarning': {

Check warning on line 284 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L284

Added line #L284 was not covered by tests
const {
data: { message: warningMessage, name },
} = message;
const { data: jobData } = this.jobs[id];

Check warning on line 288 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L287-L288

Added lines #L287 - L288 were not covered by tests
if (!this.logger[name]) {
this.logger[name] = jobData.getLogger(name);

Check warning on line 290 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L290

Added line #L290 was not covered by tests
}
this.logger[name].warn(warningMessage);
finalCallback();
break;

Check warning on line 294 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L292-L294

Added lines #L292 - L294 were not covered by tests
}
case 'emitLoggingError': {

Check warning on line 296 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L296

Added line #L296 was not covered by tests
const {
data: { message: errorMessage, name },
} = message;
const { data: jobData } = this.jobs[id];

Check warning on line 300 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L299-L300

Added lines #L299 - L300 were not covered by tests
if (!this.logger[name]) {
this.logger[name] = jobData.getLogger(name);

Check warning on line 302 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L302

Added line #L302 was not covered by tests
}
this.logger[name].error(errorMessage);
finalCallback();
break;

Check warning on line 306 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L304-L306

Added lines #L304 - L306 were not covered by tests
}
case 'emitLoggingLog': {

Check warning on line 308 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L308

Added line #L308 was not covered by tests
const {
data: { message: logMessage, name },
} = message;
const { data: jobData } = this.jobs[id];

Check warning on line 312 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L311-L312

Added lines #L311 - L312 were not covered by tests
if (!this.logger[name]) {
this.logger[name] = jobData.getLogger(name);

Check warning on line 314 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L314

Added line #L314 was not covered by tests
}
this.logger[name].info(logMessage);
finalCallback();
break;

Check warning on line 318 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L316-L318

Added lines #L316 - L318 were not covered by tests
}
case 'emitLoggingDebug': {

Check warning on line 320 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L320

Added line #L320 was not covered by tests
const {
data: { message: debugMessage, name },
} = message;
const { data: jobData } = this.jobs[id];

Check warning on line 324 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L323-L324

Added lines #L323 - L324 were not covered by tests
if (!this.logger[name]) {
this.logger[name] = jobData.getLogger(name);

Check warning on line 326 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L326

Added line #L326 was not covered by tests
}
this.logger[name].debug(debugMessage);
finalCallback();
break;

Check warning on line 330 in src/WorkerPool.js

View check run for this annotation

Codecov / codecov/patch

src/WorkerPool.js#L328-L330

Added lines #L328 - L330 were not covered by tests
}
case 'emitWarning': {
const { data } = message;
const { data: jobData } = this.jobs[id];
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function pitch() {
resourceQuery: this.resourceQuery,
optionsContext: this.rootContext || this.options.context,
rootContext: this.rootContext,
getLogger: this.getLogger,
},
(err, r) => {
if (r) {
Expand Down
49 changes: 49 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,55 @@
context: {
version: 2,
fs,
getLogger: (name) => {
writeJson({

Check warning on line 134 in src/worker.js

View check run for this annotation

Codecov / codecov/patch

src/worker.js#L133-L134

Added lines #L133 - L134 were not covered by tests
type: 'getLogger',
id,
data: name,
});
return {
warn: (warningMessage) => {
writeJson({

Check warning on line 141 in src/worker.js

View check run for this annotation

Codecov / codecov/patch

src/worker.js#L139-L141

Added lines #L139 - L141 were not covered by tests
type: 'emitLoggingWarning',
id,
data: {
message: warningMessage,
name,
},
});
},
log: (logMesssage) => {
writeJson({

Check warning on line 151 in src/worker.js

View check run for this annotation

Codecov / codecov/patch

src/worker.js#L150-L151

Added lines #L150 - L151 were not covered by tests
type: 'emitLoggingLog',
id,
data: {
message: logMesssage,
name,
},
});
},
debug: (debugMesssage) => {
writeJson({

Check warning on line 161 in src/worker.js

View check run for this annotation

Codecov / codecov/patch

src/worker.js#L160-L161

Added lines #L160 - L161 were not covered by tests
type: 'emitLoggingDebug',
id,
data: {
message: debugMesssage,
name,
},
});
},
error: (errorMesssage) => {
writeJson({

Check warning on line 171 in src/worker.js

View check run for this annotation

Codecov / codecov/patch

src/worker.js#L170-L171

Added lines #L170 - L171 were not covered by tests
type: 'emitLoggingError',
id,
data: {
message: errorMesssage,
name,
},
});
},
};
},
loadModule: (request, callback) => {
callbackMap[nextQuestionId] = (error, result) =>
callback(error, ...result);
Expand Down
3 changes: 3 additions & 0 deletions test/less-loader-example/assets/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
1 change: 1 addition & 0 deletions test/less-loader-example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './assets/style.less';
1 change: 1 addition & 0 deletions test/less-loader-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
44 changes: 44 additions & 0 deletions test/less-loader-example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const path = require('path');

const threadLoader = require('../../src'); // eslint-disable-line import/no-extraneous-dependencies

module.exports = (env) => {
const workerPool = {
workers: +env.threads,
poolTimeout: env.watch ? Infinity : 2000,
};
if (+env.threads > 0) {
threadLoader.warmup(workerPool, ['less-loader']);
}
return {
experiments: {
css: true,
},
mode: 'none',
context: __dirname,
devtool: false,
entry: ['./index.js'],
output: {
path: path.resolve('dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.less$/,
use: [
env.threads !== 0 && {
loader: path.resolve(__dirname, '../../dist/index.js'),
options: workerPool,
},
'less-loader',
],
type: 'css/auto',
},
],
},
stats: {
children: false,
},
};
};
21 changes: 21 additions & 0 deletions test/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import webpack from 'webpack';

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

test("Processes sass-loader's @import correctly", (done) => {
const config = sassLoaderConfig({});
Expand Down Expand Up @@ -42,3 +43,23 @@ test('Processes ts-loader correctly', (done) => {
done();
});
}, 30000);

test('Processes less-loader correctly', (done) => {
const config = lessLoaderConfig({});

webpack(config, (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);