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

Commit c117a7c

Browse files
authoredJul 28, 2020
feat: default value of filename from output.filename (#263)
BREAKING CHANGE: default value of the `filename` option based on `output.filename`
1 parent 905ed7b commit c117a7c

6 files changed

+267
-13
lines changed
 

‎src/index.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlug
88
import schema from './options.json';
99
import supportWebpack5 from './supportWebpack5';
1010
import supportWebpack4 from './supportWebpack4';
11+
import { getDefaultFilename, getDefaultChunkFilename } from './utils';
1112

1213
let FetchCompileWasmPlugin;
1314
let FetchCompileAsyncWasmPlugin;
@@ -46,20 +47,14 @@ export function pitch(request) {
4647
baseDataPath: 'options',
4748
});
4849

49-
const filename = loaderUtils.interpolateName(
50-
this,
51-
options.filename || '[hash].worker.js',
52-
{ context: this.rootContext }
53-
);
54-
5550
const worker = {};
5651
const compilerOptions = this._compiler.options || {};
52+
const filename = options.filename
53+
? options.filename
54+
: getDefaultFilename(compilerOptions.output.filename);
5755
const chunkFilename = options.chunkFilename
5856
? options.chunkFilename
59-
: compilerOptions.output.chunkFilename.replace(
60-
/\.([a-z]+)(\?.+)?$/i,
61-
'.worker.$1$2'
62-
);
57+
: getDefaultChunkFilename(compilerOptions.output.chunkFilename);
6358

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

‎src/utils.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
function getDefaultFilename(filename) {
2+
if (typeof filename === 'function') {
3+
return filename;
4+
}
5+
6+
return filename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2');
7+
}
8+
9+
function getDefaultChunkFilename(chunkFilename) {
10+
return chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2');
11+
}
12+
113
function getWorker(file, content, options) {
214
const publicPath =
315
typeof options.publicPath === 'undefined'
@@ -37,4 +49,4 @@ function getWorker(file, content, options) {
3749
}
3850

3951
// eslint-disable-next-line import/prefer-default-export
40-
export { getWorker };
52+
export { getDefaultFilename, getDefaultChunkFilename, getWorker };

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

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`"name" option should work and respect the "output.chunkFilename" default value option: errors 1`] = `Array []`;
4+
5+
exports[`"name" option should work and respect the "output.chunkFilename" default value option: module 1`] = `
6+
"export default function() {
7+
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
8+
};
9+
"
10+
`;
11+
12+
exports[`"name" option should work and respect the "output.chunkFilename" default value option: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
13+
14+
exports[`"name" option should work and respect the "output.chunkFilename" default value option: warnings 1`] = `Array []`;
15+
16+
exports[`"name" option should work and respect the "output.chunkFilename" option value: errors 1`] = `Array []`;
17+
18+
exports[`"name" option should work and respect the "output.chunkFilename" option value: module 1`] = `
19+
"export default function() {
20+
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
21+
};
22+
"
23+
`;
24+
25+
exports[`"name" option should work and respect the "output.chunkFilename" option value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
26+
27+
exports[`"name" option should work and respect the "output.chunkFilename" option value: warnings 1`] = `Array []`;
28+
329
exports[`"name" option should work: errors 1`] = `Array []`;
430

531
exports[`"name" option should work: module 1`] = `

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

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`"filename" option should work and respect the "output.filename" as a function: errors 1`] = `Array []`;
4+
5+
exports[`"filename" option should work and respect the "output.filename" as a function: warnings 1`] = `Array []`;
6+
7+
exports[`"filename" option should work and respect the "output.filename" default value option: errors 1`] = `Array []`;
8+
9+
exports[`"filename" option should work and respect the "output.filename" default value option: module 1`] = `
10+
"export default function() {
11+
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
12+
};
13+
"
14+
`;
15+
16+
exports[`"filename" option should work and respect the "output.filename" default value option: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
17+
18+
exports[`"filename" option should work and respect the "output.filename" default value option: warnings 1`] = `Array []`;
19+
20+
exports[`"filename" option should work and respect the "output.filename" option value: errors 1`] = `Array []`;
21+
22+
exports[`"filename" option should work and respect the "output.filename" option value: module 1`] = `
23+
"export default function() {
24+
return new Worker(__webpack_public_path__ + \\"main.bundle.worker.js\\");
25+
};
26+
"
27+
`;
28+
29+
exports[`"filename" option should work and respect the "output.filename" option value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
30+
31+
exports[`"filename" option should work and respect the "output.filename" option value: warnings 1`] = `Array []`;
32+
333
exports[`"filename" option should work: errors 1`] = `Array []`;
434

535
exports[`"filename" option should work: module 1`] = `

‎test/chunkFilename-option.test.js

+75
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import path from 'path';
2+
3+
import { customAlphabet } from 'nanoid';
4+
15
import {
26
compile,
37
getCompiler,
@@ -22,4 +26,75 @@ describe('"name" option', () => {
2226
expect(getWarnings(stats)).toMatchSnapshot('warnings');
2327
expect(getErrors(stats)).toMatchSnapshot('errors');
2428
});
29+
30+
it('should work and respect the "output.chunkFilename" default value option', async () => {
31+
const nanoid = customAlphabet('1234567890abcdef', 10);
32+
const compiler = getCompiler(
33+
'./chunks/entry.js',
34+
{},
35+
{
36+
output: {
37+
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
38+
filename: '[name].js',
39+
},
40+
module: {
41+
rules: [
42+
{
43+
test: /worker\.js$/i,
44+
rules: [
45+
{
46+
loader: path.resolve(__dirname, '../src'),
47+
},
48+
],
49+
},
50+
],
51+
},
52+
}
53+
);
54+
const stats = await compile(compiler);
55+
const result = await getResultFromBrowser(stats);
56+
57+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
58+
'module'
59+
);
60+
expect(result).toMatchSnapshot('result');
61+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
62+
expect(getErrors(stats)).toMatchSnapshot('errors');
63+
});
64+
65+
it('should work and respect the "output.chunkFilename" option value', async () => {
66+
const nanoid = customAlphabet('1234567890abcdef', 10);
67+
const compiler = getCompiler(
68+
'./chunks/entry.js',
69+
{},
70+
{
71+
output: {
72+
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
73+
filename: '[name].js',
74+
chunkFilename: '[name].chunk.js',
75+
},
76+
module: {
77+
rules: [
78+
{
79+
test: /worker\.js$/i,
80+
rules: [
81+
{
82+
loader: path.resolve(__dirname, '../src'),
83+
},
84+
],
85+
},
86+
],
87+
},
88+
}
89+
);
90+
const stats = await compile(compiler);
91+
const result = await getResultFromBrowser(stats);
92+
93+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
94+
'module'
95+
);
96+
expect(result).toMatchSnapshot('result');
97+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
98+
expect(getErrors(stats)).toMatchSnapshot('errors');
99+
});
25100
});

‎test/filename-options.test.js

+118-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import path from 'path';
2+
3+
import { customAlphabet } from 'nanoid';
4+
15
import {
26
compile,
37
getCompiler,
@@ -9,17 +13,129 @@ import {
913

1014
describe('"filename" option', () => {
1115
it('should work', async () => {
12-
const compiler = getCompiler('./basic/entry.js', {
16+
const compiler = getCompiler('./chunks/entry.js', {
1317
filename: 'my-custom-name.js',
1418
});
1519
const stats = await compile(compiler);
1620
const result = await getResultFromBrowser(stats);
1721

18-
expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot(
22+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
23+
'module'
24+
);
25+
expect(result).toMatchSnapshot('result');
26+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
27+
expect(getErrors(stats)).toMatchSnapshot('errors');
28+
});
29+
30+
it('should work and respect the "output.filename" default value option', async () => {
31+
const nanoid = customAlphabet('1234567890abcdef', 10);
32+
const compiler = getCompiler(
33+
'./chunks/entry.js',
34+
{},
35+
{
36+
output: {
37+
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
38+
chunkFilename: '[name].chunk.js',
39+
},
40+
module: {
41+
rules: [
42+
{
43+
test: /worker\.js$/i,
44+
rules: [
45+
{
46+
loader: path.resolve(__dirname, '../src'),
47+
},
48+
],
49+
},
50+
],
51+
},
52+
}
53+
);
54+
const stats = await compile(compiler);
55+
const result = await getResultFromBrowser(stats);
56+
57+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
1958
'module'
2059
);
2160
expect(result).toMatchSnapshot('result');
2261
expect(getWarnings(stats)).toMatchSnapshot('warnings');
2362
expect(getErrors(stats)).toMatchSnapshot('errors');
2463
});
64+
65+
it('should work and respect the "output.filename" option value', async () => {
66+
const nanoid = customAlphabet('1234567890abcdef', 10);
67+
const compiler = getCompiler(
68+
'./chunks/entry.js',
69+
{},
70+
{
71+
output: {
72+
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
73+
filename: '[name].bundle.js',
74+
chunkFilename: '[name].chunk.js',
75+
},
76+
module: {
77+
rules: [
78+
{
79+
test: /worker\.js$/i,
80+
rules: [
81+
{
82+
loader: path.resolve(__dirname, '../src'),
83+
},
84+
],
85+
},
86+
],
87+
},
88+
}
89+
);
90+
const stats = await compile(compiler);
91+
const result = await getResultFromBrowser(stats);
92+
93+
expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
94+
'module'
95+
);
96+
expect(result).toMatchSnapshot('result');
97+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
98+
expect(getErrors(stats)).toMatchSnapshot('errors');
99+
});
100+
101+
it('should work and respect the "output.filename" as a function', async () => {
102+
const nanoid = customAlphabet('1234567890abcdef', 10);
103+
const compiler = getCompiler(
104+
'./chunks/entry.js',
105+
{},
106+
{
107+
output: {
108+
path: path.resolve(__dirname, './outputs', `test_${nanoid()}`),
109+
filename: (pathData) => {
110+
if (/worker\.js$/.test(pathData.chunk.entryModule.resource)) {
111+
return '[name].worker.js';
112+
}
113+
114+
return '[name].js';
115+
},
116+
},
117+
module: {
118+
rules: [
119+
{
120+
test: /worker\.js$/i,
121+
rules: [
122+
{
123+
loader: path.resolve(__dirname, '../src'),
124+
},
125+
],
126+
},
127+
],
128+
},
129+
}
130+
);
131+
const stats = await compile(compiler);
132+
// const result = await getResultFromBrowser(stats);
133+
//
134+
// expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot(
135+
// 'module'
136+
// );
137+
// expect(result).toMatchSnapshot('result');
138+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
139+
expect(getErrors(stats)).toMatchSnapshot('errors');
140+
});
25141
});

0 commit comments

Comments
 (0)
This repository has been archived.