From 6c8f8ef5ea6dc1071b1554feea18468d035f50f6 Mon Sep 17 00:00:00 2001 From: Alexey Lavinsky Date: Mon, 21 Sep 2020 16:02:24 +0300 Subject: [PATCH] test: chunkfilename query parameters --- README.md | 16 +++--- src/runtime/inline.js | 2 +- .../chunkFilename-option.test.js.snap | 15 ++++++ test/chunkFilename-option.test.js | 49 +++++++++++++++++++ test/filename-options.test.js | 3 ++ test/helpers/getCompiler.js | 1 + test/helpers/getResultFromBrowser.js | 4 +- test/publicPath.test.js | 1 + 8 files changed, 81 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 905e37a..ddb67e6 100644 --- a/README.md +++ b/README.md @@ -66,14 +66,14 @@ And run `webpack` via your preferred method. ## Options -| Name | Type | Default | Description | -| :-----------------------------------: | :--------------------------: | :-----------------------------: | :-------------------------------------------------------------------------------- | -| **[`worker`](#worker)** | `{String\|Object}` | `Worker` | Allows to set web worker constructor name and options | -| **[`publicPath`](#publicpath)** | `{String\|Function}` | based on `output.publicPath` | specifies the public URL address of the output files when referenced in a browser | -| **[`filename`](#filename)** | `{String\|Function}` | based on `output.filename` | The filename of entry chunks for web workers | -| **[`chunkFilename`](#chunkfilename)** | `{String}` | based on `output.chunkFilename` | The filename of non-entry chunks for web workers | -| **[`inline`](#inline)** | `'no-fallback'\|'fallback'` | `undefined` | Allow to inline the worker as a `BLOB` | -| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax | +| Name | Type | Default | Description | +| :-----------------------------------: | :-------------------------: | :-----------------------------: | :-------------------------------------------------------------------------------- | +| **[`worker`](#worker)** | `{String\|Object}` | `Worker` | Allows to set web worker constructor name and options | +| **[`publicPath`](#publicpath)** | `{String\|Function}` | based on `output.publicPath` | specifies the public URL address of the output files when referenced in a browser | +| **[`filename`](#filename)** | `{String\|Function}` | based on `output.filename` | The filename of entry chunks for web workers | +| **[`chunkFilename`](#chunkfilename)** | `{String}` | based on `output.chunkFilename` | The filename of non-entry chunks for web workers | +| **[`inline`](#inline)** | `'no-fallback'\|'fallback'` | `undefined` | Allow to inline the worker as a `BLOB` | +| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax | ### `worker` diff --git a/src/runtime/inline.js b/src/runtime/inline.js index 0372f08..d1259c1 100644 --- a/src/runtime/inline.js +++ b/src/runtime/inline.js @@ -23,7 +23,7 @@ module.exports = (content, workerConstructor, workerOptions, url) => { blob = blob.getBlob(); } - + const URL = window.URL || window.webkitURL; const objectURL = URL.createObjectURL(blob); const worker = new window[workerConstructor](objectURL, workerOptions); diff --git a/test/__snapshots__/chunkFilename-option.test.js.snap b/test/__snapshots__/chunkFilename-option.test.js.snap index 577fecc..3685e6d 100644 --- a/test/__snapshots__/chunkFilename-option.test.js.snap +++ b/test/__snapshots__/chunkFilename-option.test.js.snap @@ -1,5 +1,20 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`"name" option should chunkFilename suffix be inserted before query parameters: errors 1`] = `Array []`; + +exports[`"name" option should chunkFilename suffix be inserted before query parameters: module 1`] = ` +"export default function() { + return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); +} +" +`; + +exports[`"name" option should chunkFilename suffix be inserted before query parameters: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + +exports[`"name" option should chunkFilename suffix be inserted before query parameters: result 2`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + +exports[`"name" option should chunkFilename suffix be inserted before query parameters: warnings 1`] = `Array []`; + exports[`"name" option should work ("string"): errors 1`] = `Array []`; exports[`"name" option should work ("string"): module 1`] = ` diff --git a/test/chunkFilename-option.test.js b/test/chunkFilename-option.test.js index e535ef1..6dcd708 100644 --- a/test/chunkFilename-option.test.js +++ b/test/chunkFilename-option.test.js @@ -36,6 +36,7 @@ describe('"name" option', () => { output: { path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), filename: '[name].js', + publicPath: '', }, module: { rules: [ @@ -72,6 +73,7 @@ describe('"name" option', () => { path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), filename: '[name].js', chunkFilename: '[name].chunk.js', + publicPath: '', }, module: { rules: [ @@ -97,4 +99,51 @@ describe('"name" option', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); expect(getErrors(stats)).toMatchSnapshot('errors'); }); + + it('should chunkFilename suffix be inserted before query parameters', async () => { + const nanoid = customAlphabet('1234567890abcdef', 10); + const compiler = getCompiler( + './chunks/entry.js', + {}, + { + output: { + path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), + filename: '[name].js', + chunkFilename: '[name].chunk.js?foo=bar&baz=bar', + publicPath: '', + }, + module: { + rules: [ + { + test: /worker\.js$/i, + rules: [ + { + loader: path.resolve(__dirname, '../src'), + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + const result = await getResultFromBrowser(stats); + + let hasChankName = false; + + Object.keys(stats.compilation.assets).forEach((asset) => { + if (asset.endsWith('chunk.worker.js?foo=bar&baz=bar')) { + hasChankName = true; + } + }); + + expect(hasChankName).toBe(true); + expect(result).toMatchSnapshot('result'); + expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( + 'module' + ); + expect(result).toMatchSnapshot('result'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); }); diff --git a/test/filename-options.test.js b/test/filename-options.test.js index d0ab25d..a44cf0c 100644 --- a/test/filename-options.test.js +++ b/test/filename-options.test.js @@ -51,6 +51,7 @@ describe('"filename" option', () => { output: { path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), chunkFilename: '[name].chunk.js', + publicPath: '', }, module: { rules: [ @@ -87,6 +88,7 @@ describe('"filename" option', () => { path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), filename: '[name].custom.js', chunkFilename: '[name].chunk.js', + publicPath: '', }, module: { rules: [ @@ -128,6 +130,7 @@ describe('"filename" option', () => { return '[name].js'; }, + publicPath: '', }, module: { rules: [ diff --git a/test/helpers/getCompiler.js b/test/helpers/getCompiler.js index 8810b19..894748a 100644 --- a/test/helpers/getCompiler.js +++ b/test/helpers/getCompiler.js @@ -16,6 +16,7 @@ export default (fixture, loaderOptions = {}, config = {}) => { path: path.resolve(__dirname, '../outputs', `test_${nanoid()}`), filename: '[name].bundle.js', chunkFilename: '[name].chunk.js', + publicPath: '', }, module: { rules: [ diff --git a/test/helpers/getResultFromBrowser.js b/test/helpers/getResultFromBrowser.js index 7fbee2c..64771b1 100644 --- a/test/helpers/getResultFromBrowser.js +++ b/test/helpers/getResultFromBrowser.js @@ -20,7 +20,9 @@ export default async function getResultFromBrowser(stats) { ); for (const asset of assets) { - const [route] = asset; + let [route] = asset; + [route] = route.split('?'); + const existsAt = path.resolve(stats.compilation.outputOptions.path, route); if (route === 'index.html') { diff --git a/test/publicPath.test.js b/test/publicPath.test.js index b8e78a3..a487957 100644 --- a/test/publicPath.test.js +++ b/test/publicPath.test.js @@ -67,6 +67,7 @@ describe('"publicPath" option', () => { path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), filename: '[name].bundle.js', chunkFilename: '[name].chunk.js', + publicPath: '', }, } );