Skip to content

Commit

Permalink
feat: add support for less-loader
Browse files Browse the repository at this point in the history
Enables threadloader to be used for less-loader
  • Loading branch information
janlentsap committed Dec 1, 2023
1 parent 2958e41 commit d6bbc30
Show file tree
Hide file tree
Showing 10 changed files with 7,118 additions and 10,321 deletions.
17,256 changes: 6,936 additions & 10,320 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"fix:prettier": "npm run lint:prettier -- --write",
"fix": "npm-run-all -l fix:js fix:prettier",
"test:only": "cross-env NODE_ENV=test jest --forceExit",
"testDebug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand webpack.test.js",
"test:watch": "npm run test:only -- --watch",
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
"pretest": "npm run lint",
Expand Down Expand Up @@ -68,6 +69,8 @@
"eslint-plugin-import": "^2.28.0",
"husky": "^4.3.0",
"jest": "^29.6.2",
"less": "4.2.0",
"less-loader": "11.1.3",
"lint-staged": "^10.5.0",
"lodash": "^4.17.20",
"memfs": "^3.5.1",
Expand All @@ -82,7 +85,7 @@
"sass-loader": "^11.0.1",
"standard-version": "^9.0.0",
"ts-loader": "^9.4.4",
"webpack": "^5.88.2"
"webpack": "5.89.0"
},
"keywords": [
"webpack"
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 @@ class PoolWorker {
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 @@ class PoolWorker {
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 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
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);

0 comments on commit d6bbc30

Please sign in to comment.