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

Commit

Permalink
feat: the chunkFilename option
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jul 28, 2020
1 parent 8d7cae0 commit 905ed7b
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setupTest.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
jest.setTimeout(60000);
jest.setTimeout(90000);
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ export function pitch(request) {

const worker = {};
const compilerOptions = this._compiler.options || {};
const chunkFilename = compilerOptions.output.chunkFilename.replace(
/\.([a-z]+)(\?.+)?$/i,
'.worker.$1$2'
);
const chunkFilename = options.chunkFilename
? options.chunkFilename
: compilerOptions.output.chunkFilename.replace(
/\.([a-z]+)(\?.+)?$/i,
'.worker.$1$2'
);

worker.options = { filename, chunkFilename, globalObject: 'self' };

Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"publicPath": {
"type": "string"
},
"chunkFilename": {
"type": "string",
"minLength": 1
},
"inline": {
"type": "boolean"
},
Expand Down
14 changes: 14 additions & 0 deletions test/__snapshots__/chunkFilename-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"name" option should work: errors 1`] = `Array []`;

exports[`"name" option should work: module 1`] = `
"export default function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};
"
`;

exports[`"name" option should work: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"name" option should work: warnings 1`] = `Array []`;
13 changes: 13 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ exports[`worker-loader should work with WASM: result 1`] = `"{\\"postMessage\\":

exports[`worker-loader should work with WASM: warnings 1`] = `Array []`;

exports[`worker-loader should work with async chunks: errors 1`] = `Array []`;

exports[`worker-loader should work with async chunks: module 1`] = `
"export default function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};
"
`;

exports[`worker-loader should work with async chunks: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`worker-loader should work with async chunks: warnings 1`] = `Array []`;

exports[`worker-loader should work with inline syntax: errors 1`] = `Array []`;

exports[`worker-loader should work with inline syntax: module 1`] = `
Expand Down
25 changes: 25 additions & 0 deletions test/chunkFilename-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
compile,
getCompiler,
getErrors,
getModuleSource,
getResultFromBrowser,
getWarnings,
} from './helpers';

describe('"name" option', () => {
it('should work', async () => {
const compiler = getCompiler('./chunks/entry.js', {
chunkFilename: 'test.worker.chunk.js',
});
const stats = await compile(compiler);
const result = await getResultFromBrowser(stats);

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/fixtures/chunks/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function returnTrue() {
return true;
}
22 changes: 22 additions & 0 deletions test/fixtures/chunks/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Worker from './worker.js';

const worker = new Worker();

let result;

worker.onmessage = function (event) {
if (!result) {
result = document.createElement("div");
result.setAttribute('id', 'result');

document.body.append(result);
}

result.innerText = JSON.stringify(event.data)
};

const button = document.getElementById('button');

button.addEventListener('click', () => {
worker.postMessage({ postMessage: true })
});
13 changes: 13 additions & 0 deletions test/fixtures/chunks/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Webpack App</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>

<button id="button">Run Action</button>

</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/chunks/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
async function loadChunk() {
return import(/* webpackMode: "lazy" */ './chunk');
}

onmessage = async function(event) {
const { returnTrue } = await loadChunk();

const workerResult = event.data;

workerResult.onmessage = returnTrue();

postMessage(workerResult);
};
4 changes: 2 additions & 2 deletions test/helpers/getResultFromBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export default async function getResultFromBrowser(stats) {
);

await page.goto(`http://127.0.0.1:${port}/`);
await page.waitForSelector('button');
await page.waitForSelector('button', { timeout: 90000 });
await page.click('button');
await page.waitForSelector('#result');
await page.waitForSelector('#result', { timeout: 90000 });

const addedFromWorkerText = await page.$eval(
'#result',
Expand Down
13 changes: 13 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,17 @@ describe('worker-loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work with async chunks', async () => {
const compiler = getCompiler('./chunks/entry.js');
const stats = await compile(compiler);
const result = await getResultFromBrowser(stats);

expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
'module'
);
expect(result).toMatchSnapshot('result');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit 905ed7b

Please sign in to comment.