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

Commit 5efa77a

Browse files
fix: interpolation [name] for the filename option (#277)
* fix: interpolation `[name]` for the `filename` option
1 parent bc99955 commit 5efa77a

File tree

9 files changed

+85
-13
lines changed

9 files changed

+85
-13
lines changed

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
13
import { getOptions } from 'loader-utils';
24
import validateOptions from 'schema-utils';
35

@@ -99,9 +101,11 @@ export function pitch(request) {
99101
).apply(workerContext.compiler);
100102
}
101103

102-
new SingleEntryPlugin(this.context, `!!${request}`, 'main').apply(
103-
workerContext.compiler
104-
);
104+
new SingleEntryPlugin(
105+
this.context,
106+
`!!${request}`,
107+
path.parse(this.resourcePath).name
108+
).apply(workerContext.compiler);
105109

106110
workerContext.request = request;
107111

test/__snapshots__/chunkFilename-option.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" defaul
1717

1818
exports[`"name" option should work and respect the "output.chunkFilename" default value option: module 1`] = `
1919
"export default function() {
20-
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
20+
return new Worker(__webpack_public_path__ + \\"worker.worker.js\\");
2121
}
2222
"
2323
`;
@@ -30,7 +30,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" option
3030

3131
exports[`"name" option should work and respect the "output.chunkFilename" option ("string"): module 1`] = `
3232
"export default function() {
33-
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
33+
return new Worker(__webpack_public_path__ + \\"worker.worker.js\\");
3434
}
3535
"
3636
`;

test/__snapshots__/filename-options.test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`"filename" option should work ("function"): errors 1`] = `Array []`;
44

55
exports[`"filename" option should work ("function"): module 1`] = `
66
"export default function() {
7-
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
7+
return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\");
88
}
99
"
1010
`;
@@ -17,7 +17,7 @@ exports[`"filename" option should work ("string"): errors 1`] = `Array []`;
1717

1818
exports[`"filename" option should work ("string"): module 1`] = `
1919
"export default function() {
20-
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
20+
return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\");
2121
}
2222
"
2323
`;
@@ -30,7 +30,7 @@ exports[`"filename" option should work and respect the "output.filename" default
3030

3131
exports[`"filename" option should work and respect the "output.filename" default value option: module 1`] = `
3232
"export default function() {
33-
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
33+
return new Worker(__webpack_public_path__ + \\"worker.worker.js\\");
3434
}
3535
"
3636
`;
@@ -43,7 +43,7 @@ exports[`"filename" option should work and respect the "output.filename" option
4343

4444
exports[`"filename" option should work and respect the "output.filename" option ("function"): module 1`] = `
4545
"export default function() {
46-
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
46+
return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\");
4747
}
4848
"
4949
`;
@@ -56,7 +56,7 @@ exports[`"filename" option should work and respect the "output.filename" option
5656

5757
exports[`"filename" option should work and respect the "output.filename" option ("string"): module 1`] = `
5858
"export default function() {
59-
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
59+
return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\");
6060
}
6161
"
6262
`;

test/__snapshots__/loader.test.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`worker-loader should work and have the same base file name as the source files: errors 1`] = `Array []`;
4+
5+
exports[`worker-loader should work and have the same base file name as the source files: module 1`] = `
6+
"export default function() {
7+
return new Worker(__webpack_public_path__ + \\"TypeDetection.worker.js\\");
8+
}
9+
"
10+
`;
11+
12+
exports[`worker-loader should work and have the same base file name as the source files: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
13+
14+
exports[`worker-loader should work and have the same base file name as the source files: warnings 1`] = `Array []`;
15+
316
exports[`worker-loader should work and respect the "devtool" option ("false" value): errors 1`] = `Array []`;
417

518
exports[`worker-loader should work and respect the "devtool" option ("false" value): module 1`] = `

test/fixtures/name/TypeDetection.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
onmessage = function(event) {
2+
const workerResult = event.data;
3+
4+
workerResult.onmessage = true;
5+
6+
postMessage(workerResult);
7+
};

test/fixtures/name/entry.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Worker from './TypeDetection.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/name/index.html

Lines changed: 13 additions & 0 deletions
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/helpers/getCompiler.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
2020
module: {
2121
rules: [
2222
{
23-
test: /worker\.js$/i,
23+
test: /(worker|TypeDetection)\.js$/i,
2424
rules: [
2525
{
2626
loader: path.resolve(__dirname, '../../src'),
@@ -48,8 +48,6 @@ export default (fixture, loaderOptions = {}, config = {}) => {
4848
fullConfig.experiments = {};
4949
}
5050

51-
fullConfig.experiments.importAsync = true;
52-
fullConfig.experiments.importAwait = true;
5351
fullConfig.experiments.asyncWebAssembly = true;
5452
}
5553

test/loader.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,19 @@ describe('worker-loader', () => {
113113
expect(getWarnings(stats)).toMatchSnapshot('warnings');
114114
expect(getErrors(stats)).toMatchSnapshot('errors');
115115
});
116+
117+
it('should work and have the same base file name as the source files', async () => {
118+
const compiler = getCompiler('./name/entry.js', {
119+
filename: '[name].worker.js',
120+
});
121+
const stats = await compile(compiler);
122+
const result = await getResultFromBrowser(stats);
123+
124+
expect(getModuleSource('./name/TypeDetection.js', stats)).toMatchSnapshot(
125+
'module'
126+
);
127+
expect(result).toMatchSnapshot('result');
128+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
129+
expect(getErrors(stats)).toMatchSnapshot('errors');
130+
});
116131
});

0 commit comments

Comments
 (0)