Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
}
},
// Not an arrow function because it uses this
getOptions(schema) {
getOptions(schema, check) {
// loaders, loaderIndex will be defined by runLoaders
const loader = this.loaders[this.loaderIndex];

Expand Down Expand Up @@ -240,7 +240,10 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
options = {};
}

if (schema) {
// A precompiled check that passes skips the schema, as webpack's own
// `getOptions` does; one that fails still gets the full validation
// and its error messages.
if (schema && !(typeof check === 'function' && check(options))) {
let name = 'Loader';
let baseDataPath = 'options';
let match;
Expand Down
57 changes: 57 additions & 0 deletions test/options-check.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os from 'os';
import path from 'path';

import webpack from 'webpack';

const threadLoader = require('../dist');

const loader = require.resolve('./options-check/options-check-loader');

const run = (options) =>
new Promise((resolve, reject) => {
webpack(
{
mode: 'none',
context: path.resolve(__dirname, 'options-check'),
devtool: false,
entry: './file.js',
output: {
path: path.join(os.tmpdir(), 'thread-loader-options-check'),
},
module: {
rules: [
{
test: /file\.js$/,
use: [
{
loader: path.resolve(__dirname, '../dist/index.js'),
options: { workers: 1, poolTimeout: 2000 },
},
{ loader, options },
],
},
],
},
},
(err, stats) => (err ? reject(err) : resolve(stats)),
);
});

beforeAll(() => {
threadLoader.warmup({ workers: 1, poolTimeout: 2000 }, [loader]);
});

test('skips the schema when the precompiled check passes', async () => {
const stats = await run({ trusted: true });

expect(stats.compilation.errors).toHaveLength(0);
});

test('validates against the schema when the precompiled check fails', async () => {
const stats = await run({ name: 1 });

expect(stats.compilation.errors).toHaveLength(1);
expect(stats.compilation.errors[0].message).toMatch(
/options\.name should be a string/,
);
});
1 change: 1 addition & 0 deletions test/options-check/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'file';
11 changes: 11 additions & 0 deletions test/options-check/options-check-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const schema = {
type: 'object',
properties: { name: { type: 'string' } },
additionalProperties: false,
};

module.exports = function optionsCheckLoader() {
const options = this.getOptions(schema, (value) => value.trusted === true);

return `module.exports = ${JSON.stringify(options)};`;
};
Loading