Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
test: chunkfilename query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Sep 21, 2020
1 parent 17f81c1 commit 6c8f8ef
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 10 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/inline.js
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions 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`] = `
Expand Down
49 changes: 49 additions & 0 deletions test/chunkFilename-option.test.js
Expand Up @@ -36,6 +36,7 @@ describe('"name" option', () => {
output: {
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
filename: '[name].js',
publicPath: '',
},
module: {
rules: [
Expand Down Expand Up @@ -72,6 +73,7 @@ describe('"name" option', () => {
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
filename: '[name].js',
chunkFilename: '[name].chunk.js',
publicPath: '',
},
module: {
rules: [
Expand All @@ -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');
});
});
3 changes: 3 additions & 0 deletions test/filename-options.test.js
Expand Up @@ -51,6 +51,7 @@ describe('"filename" option', () => {
output: {
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
chunkFilename: '[name].chunk.js',
publicPath: '',
},
module: {
rules: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -128,6 +130,7 @@ describe('"filename" option', () => {

return '[name].js';
},
publicPath: '',
},
module: {
rules: [
Expand Down
1 change: 1 addition & 0 deletions test/helpers/getCompiler.js
Expand Up @@ -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: [
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/getResultFromBrowser.js
Expand Up @@ -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') {
Expand Down
1 change: 1 addition & 0 deletions test/publicPath.test.js
Expand Up @@ -67,6 +67,7 @@ describe('"publicPath" option', () => {
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
publicPath: '',
},
}
);
Expand Down

0 comments on commit 6c8f8ef

Please sign in to comment.