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

Implement getResolve #99

Merged
merged 2 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,35 @@ class PoolWorker {
break;
}
case 'resolve': {
const { context, request, questionId } = message;
const { context, request, options, questionId } = message;
const { data } = this.jobs[id];
data.resolve(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
if (options) {
data.getResolve(options)(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
});
});
});
} else {
data.resolve(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
});
});
}
finalCallback();
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function pitch() {
emitWarning: this.emitWarning,
loadModule: this.loadModule,
resolve: this.resolve,
getResolve: this.getResolve,
target: this.target,
minimize: this.minimize,
resourceQuery: this.resourceQuery,
Expand Down
35 changes: 26 additions & 9 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ function writeJson(data) {

const queue = asyncQueue(({ id, data }, taskCallback) => {
try {
const resolveWithOptions = (context, request, callback, options) => {
callbackMap[nextQuestionId] = callback;
writeJson({
type: 'resolve',
id,
questionId: nextQuestionId,
context,
request,
options,
});
nextQuestionId += 1;
};
loaderRunner.runLoaders({
loaders: data.loaders,
resource: data.resource,
Expand All @@ -119,15 +131,20 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
nextQuestionId += 1;
},
resolve: (context, request, callback) => {
callbackMap[nextQuestionId] = callback;
writeJson({
type: 'resolve',
id,
questionId: nextQuestionId,
context,
request,
});
nextQuestionId += 1;
resolveWithOptions(context, request, callback);
},
// eslint-disable-next-line consistent-return
getResolve: options => (context, request, callback) => {
if (callback) {
resolveWithOptions(context, request, callback, options);
} else {
return new Promise((resolve, reject) => {
resolveWithOptions(context, request, (err, result) => {
if (err) reject(err);
else resolve(result);
}, options);
});
}
},
emitWarning: (warning) => {
writeJson({
Expand Down
3 changes: 3 additions & 0 deletions test/sass-loader-example/_shared.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
34 changes: 34 additions & 0 deletions test/sass-loader-example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable import/no-unresolved */
// some file
import './style.scss?00';
import './style.scss?01';
import './style.scss?02';
import './style.scss?03';
import './style.scss?04';
import './style.scss?05';
import './style.scss?06';
import './style.scss?07';
import './style.scss?08';
import './style.scss?09';

import './style.scss?10';
import './style.scss?11';
import './style.scss?12';
import './style.scss?13';
import './style.scss?14';
import './style.scss?15';
import './style.scss?16';
import './style.scss?17';
import './style.scss?18';
import './style.scss?19';

import './style.scss?20';
import './style.scss?21';
import './style.scss?22';
import './style.scss?23';
import './style.scss?24';
import './style.scss?25';
import './style.scss?26';
import './style.scss?27';
import './style.scss?28';
import './style.scss?29';
60 changes: 60 additions & 0 deletions test/sass-loader-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/sass-loader-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"lodash-es": "^4.17.14",
"react": "^16.6.3"
}
}
5 changes: 5 additions & 0 deletions test/sass-loader-example/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '_shared';

body {
background: red;
}
62 changes: 62 additions & 0 deletions test/sass-loader-example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // eslint-disable-line import/no-extraneous-dependencies
const threadLoader = require('../../src'); // eslint-disable-line import/no-extraneous-dependencies

module.exports = (env) => {
const workerPool = {
workers: +env.threads,
poolTimeout: env.watch ? Infinity : 2000,
};
const workerPoolSass = {
workers: +env.threads,
workerParallelJobs: 2,
poolTimeout: env.watch ? Infinity : 2000,
};
if (+env.threads > 0) {
threadLoader.warmup(workerPool, ['babel-loader', 'babel-preset-env']);
threadLoader.warmup(workerPoolSass, ['sass-loader', 'css-loader']);
}
return {
mode: 'none',
context: __dirname,
entry: ['./index.js'],
output: {
path: path.resolve('dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
use: [
env.threads !== 0 && {
loader: path.resolve(__dirname, '../../dist/index.js'),
options: workerPool,
},
'babel-loader',
].filter(Boolean),
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
env.threads !== 0 && {
loader: path.resolve(__dirname, '../../dist/index.js'),
options: workerPoolSass,
},
'css-loader',
'sass-loader',
].filter(Boolean),
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
}),
],
stats: {
children: false,
},
};
};
18 changes: 18 additions & 0 deletions test/webpack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import webpack from 'webpack';
import sassLoaderConfig from './sass-loader-example/webpack.config';

test("Processes sass-loader's @import correctly", (done) => {
const config = sassLoaderConfig({});
webpack(config, (err, stats) => {
// eslint-disable-next-line no-console
if (err) 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);