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

Commit 905ed7b

Browse files
authoredJul 28, 2020
feat: the chunkFilename option
1 parent 8d7cae0 commit 905ed7b

12 files changed

+129
-7
lines changed
 

‎setupTest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
jest.setTimeout(60000);
1+
jest.setTimeout(90000);

‎src/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ export function pitch(request) {
5454

5555
const worker = {};
5656
const compilerOptions = this._compiler.options || {};
57-
const chunkFilename = compilerOptions.output.chunkFilename.replace(
58-
/\.([a-z]+)(\?.+)?$/i,
59-
'.worker.$1$2'
60-
);
57+
const chunkFilename = options.chunkFilename
58+
? options.chunkFilename
59+
: compilerOptions.output.chunkFilename.replace(
60+
/\.([a-z]+)(\?.+)?$/i,
61+
'.worker.$1$2'
62+
);
6163

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

‎src/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
"publicPath": {
3232
"type": "string"
3333
},
34+
"chunkFilename": {
35+
"type": "string",
36+
"minLength": 1
37+
},
3438
"inline": {
3539
"type": "boolean"
3640
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"name" option should work: errors 1`] = `Array []`;
4+
5+
exports[`"name" option should work: module 1`] = `
6+
"export default function() {
7+
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8+
};
9+
"
10+
`;
11+
12+
exports[`"name" option should work: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
13+
14+
exports[`"name" option should work: warnings 1`] = `Array []`;

‎test/__snapshots__/loader.test.js.snap

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ exports[`worker-loader should work with WASM: result 1`] = `"{\\"postMessage\\":
1313

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

16+
exports[`worker-loader should work with async chunks: errors 1`] = `Array []`;
17+
18+
exports[`worker-loader should work with async chunks: module 1`] = `
19+
"export default function() {
20+
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
21+
};
22+
"
23+
`;
24+
25+
exports[`worker-loader should work with async chunks: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
26+
27+
exports[`worker-loader should work with async chunks: warnings 1`] = `Array []`;
28+
1629
exports[`worker-loader should work with inline syntax: errors 1`] = `Array []`;
1730

1831
exports[`worker-loader should work with inline syntax: module 1`] = `

‎test/chunkFilename-option.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
compile,
3+
getCompiler,
4+
getErrors,
5+
getModuleSource,
6+
getResultFromBrowser,
7+
getWarnings,
8+
} from './helpers';
9+
10+
describe('"name" option', () => {
11+
it('should work', async () => {
12+
const compiler = getCompiler('./chunks/entry.js', {
13+
chunkFilename: 'test.worker.chunk.js',
14+
});
15+
const stats = await compile(compiler);
16+
const result = await getResultFromBrowser(stats);
17+
18+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
19+
'module'
20+
);
21+
expect(result).toMatchSnapshot('result');
22+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
23+
expect(getErrors(stats)).toMatchSnapshot('errors');
24+
});
25+
});

‎test/fixtures/chunks/chunk.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function returnTrue() {
2+
return true;
3+
}

‎test/fixtures/chunks/entry.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Worker from './worker.js';
2+
3+
const worker = new Worker();
4+
5+
let result;
6+
7+
worker.onmessage = function (event) {
8+
if (!result) {
9+
result = document.createElement("div");
10+
result.setAttribute('id', 'result');
11+
12+
document.body.append(result);
13+
}
14+
15+
result.innerText = JSON.stringify(event.data)
16+
};
17+
18+
const button = document.getElementById('button');
19+
20+
button.addEventListener('click', () => {
21+
worker.postMessage({ postMessage: true })
22+
});

‎test/fixtures/chunks/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Webpack App</title>
6+
<meta name="viewport" content="width=device-width,initial-scale=1">
7+
</head>
8+
<body>
9+
10+
<button id="button">Run Action</button>
11+
12+
</body>
13+
</html>

‎test/fixtures/chunks/worker.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
async function loadChunk() {
2+
return import(/* webpackMode: "lazy" */ './chunk');
3+
}
4+
5+
onmessage = async function(event) {
6+
const { returnTrue } = await loadChunk();
7+
8+
const workerResult = event.data;
9+
10+
workerResult.onmessage = returnTrue();
11+
12+
postMessage(workerResult);
13+
};

‎test/helpers/getResultFromBrowser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export default async function getResultFromBrowser(stats) {
4747
);
4848

4949
await page.goto(`http://127.0.0.1:${port}/`);
50-
await page.waitForSelector('button');
50+
await page.waitForSelector('button', { timeout: 90000 });
5151
await page.click('button');
52-
await page.waitForSelector('#result');
52+
await page.waitForSelector('#result', { timeout: 90000 });
5353

5454
const addedFromWorkerText = await page.$eval(
5555
'#result',

‎test/loader.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,17 @@ describe('worker-loader', () => {
4646
expect(getWarnings(stats)).toMatchSnapshot('warnings');
4747
expect(getErrors(stats)).toMatchSnapshot('errors');
4848
});
49+
50+
it('should work with async chunks', async () => {
51+
const compiler = getCompiler('./chunks/entry.js');
52+
const stats = await compile(compiler);
53+
const result = await getResultFromBrowser(stats);
54+
55+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
56+
'module'
57+
);
58+
expect(result).toMatchSnapshot('result');
59+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
60+
expect(getErrors(stats)).toMatchSnapshot('errors');
61+
});
4962
});

0 commit comments

Comments
 (0)
This repository has been archived.