diff --git a/package-lock.json b/package-lock.json index 84c5325..5774465 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7812,9 +7812,20 @@ "dev": true }, "node_modules/fast-uri": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.0.1.tgz", - "integrity": "sha512-MWipKbbYiYI0UC7cl8m/i/IWTqfC8YXsqjzybjddLsFjStroQzsHXkc73JutMvBiXmOvapk+axIl79ig5t55Bw==" + "version": "3.1.8", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz", + "integrity": "sha512-GZMtZUTNRpOVIECoXwLNZS5xUGE+mVNbTB8h/7Rwh2TFWcBQiPzTgyZi05BF9UMZKkLJv8XBRJTlU7zg8+ZfMg==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" }, "node_modules/fastq": { "version": "1.17.1", @@ -21372,9 +21383,9 @@ "dev": true }, "fast-uri": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.0.1.tgz", - "integrity": "sha512-MWipKbbYiYI0UC7cl8m/i/IWTqfC8YXsqjzybjddLsFjStroQzsHXkc73JutMvBiXmOvapk+axIl79ig5t55Bw==" + "version": "3.1.8", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz", + "integrity": "sha512-GZMtZUTNRpOVIECoXwLNZS5xUGE+mVNbTB8h/7Rwh2TFWcBQiPzTgyZi05BF9UMZKkLJv8XBRJTlU7zg8+ZfMg==" }, "fastq": { "version": "1.17.1", diff --git a/src/worker.js b/src/worker.js index 903a2f5..3f6acc1 100644 --- a/src/worker.js +++ b/src/worker.js @@ -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]; @@ -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; diff --git a/test/options-check.test.js b/test/options-check.test.js new file mode 100644 index 0000000..234c49c --- /dev/null +++ b/test/options-check.test.js @@ -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/, + ); +}); diff --git a/test/options-check/file.js b/test/options-check/file.js new file mode 100644 index 0000000..3a8edd0 --- /dev/null +++ b/test/options-check/file.js @@ -0,0 +1 @@ +module.exports = 'file'; diff --git a/test/options-check/options-check-loader.js b/test/options-check/options-check-loader.js new file mode 100644 index 0000000..e0b5c14 --- /dev/null +++ b/test/options-check/options-check-loader.js @@ -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)};`; +};