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

Commit f047ad0

Browse files
authoredJul 29, 2020
refactor: inline option (#267)
BREAKING CHANGE: the `fallback` option was removed in favor the `inline` option, the `inline` option accept only `fallback` and `no-fallback` values
1 parent bb77346 commit f047ad0

9 files changed

+46
-161
lines changed
 

‎src/options.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
}
2525
]
2626
},
27-
"filename": {
28-
"type": "string",
29-
"minLength": 1
30-
},
3127
"publicPath": {
3228
"anyOf": [
3329
{
@@ -38,15 +34,16 @@
3834
}
3935
]
4036
},
37+
"filename": {
38+
"type": "string",
39+
"minLength": 1
40+
},
4141
"chunkFilename": {
4242
"type": "string",
4343
"minLength": 1
4444
},
4545
"inline": {
46-
"type": "boolean"
47-
},
48-
"fallback": {
49-
"type": "boolean"
46+
"enum": ["no-fallback", "fallback"]
5047
},
5148
"esModule": {
5249
"type": "boolean"

‎src/runtime/inline.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/* eslint-env browser */
22
/* eslint-disable no-undef, no-use-before-define, new-cap */
33

4-
module.exports = function inline(
5-
content,
6-
url,
7-
workerConstructor,
8-
workerOptions
9-
) {
4+
const URL = window.URL || window.webkitURL;
5+
6+
module.exports = (content, workerConstructor, workerOptions, url) => {
107
try {
118
try {
129
let blob;
1310

1411
try {
12+
// New API
13+
blob = new window.Blob([content]);
14+
} catch (e) {
1515
// BlobBuilder = Deprecated, but widely implemented
1616
const BlobBuilder =
17-
BlobBuilder || WebKitBlobBuilder || MozBlobBuilder || MSBlobBuilder;
17+
window.BlobBuilder ||
18+
window.WebKitBlobBuilder ||
19+
window.MozBlobBuilder ||
20+
window.MSBlobBuilder;
1821

1922
blob = new BlobBuilder();
2023

2124
blob.append(content);
2225

2326
blob = blob.getBlob();
24-
} catch (e) {
25-
// New API
26-
blob = new Blob([content]);
2727
}
2828

2929
const objectURL = URL.createObjectURL(blob);

‎src/supportWebpack4.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function runAsChild(worker, request, options, callback) {
3333
options
3434
);
3535

36-
if (options.fallback === false) {
36+
if (options.inline === 'no-fallback') {
3737
delete this._compilation.assets[worker.file];
3838
}
3939

‎src/supportWebpack5.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function runAsChild(worker, options, callback) {
2424
return callback(getCacheError);
2525
}
2626

27-
if (options.fallback === false) {
27+
if (options.inline === 'no-fallback') {
2828
delete this._compilation.assets[worker.file];
2929
}
3030

‎src/utils.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ function getExternalsType(compilerOptions) {
3333
}
3434

3535
function getWorker(file, content, options) {
36-
const publicWorkerPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
37-
3836
let workerConstructor;
3937
let workerOptions;
4038

@@ -51,19 +49,22 @@ function getWorker(file, content, options) {
5149
`!!${require.resolve('./runtime/inline.js')}`
5250
);
5351

54-
const fallbackWorkerPath =
55-
options.fallback === false ? 'null' : publicWorkerPath;
52+
let fallbackWorkerPath;
53+
54+
if (options.inline === 'fallback') {
55+
fallbackWorkerPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
56+
}
5657

5758
return `require(${InlineWorkerPath})(${JSON.stringify(
5859
content
59-
)}, ${fallbackWorkerPath}, ${JSON.stringify(
60-
workerConstructor
61-
)}, ${JSON.stringify(workerOptions)})`;
60+
)}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify(
61+
workerOptions
62+
)}, ${fallbackWorkerPath})`;
6263
}
6364

64-
return `new ${workerConstructor}(${publicWorkerPath}${
65-
workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''
66-
})`;
65+
return `new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify(
66+
file
67+
)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''})`;
6768
}
6869

6970
export {

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

-31
This file was deleted.

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

+6-13
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,14 @@ exports[`"inline" option should not work by default: result 1`] = `"{\\"postMess
1313

1414
exports[`"inline" option should not work by default: warnings 1`] = `Array []`;
1515

16-
exports[`"inline" option should not work with "false" value: errors 1`] = `Array []`;
16+
exports[`"inline" option should not work with "no-fallback" value: errors 1`] = `Array []`;
1717

18-
exports[`"inline" option should not work with "false" value: module 1`] = `
19-
"export default function() {
20-
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
21-
};
22-
"
23-
`;
24-
25-
exports[`"inline" option should not work with "false" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
18+
exports[`"inline" option should not work with "no-fallback" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
2619

27-
exports[`"inline" option should not work with "false" value: warnings 1`] = `Array []`;
20+
exports[`"inline" option should not work with "no-fallback" value: warnings 1`] = `Array []`;
2821

29-
exports[`"inline" option should work with "true" value: errors 1`] = `Array []`;
22+
exports[`"inline" option should work with "fallback" value: errors 1`] = `Array []`;
3023

31-
exports[`"inline" option should work with "true" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
24+
exports[`"inline" option should work with "fallback" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
3225

33-
exports[`"inline" option should work with "true" value: warnings 1`] = `Array []`;
26+
exports[`"inline" option should work with "fallback" value: warnings 1`] = `Array []`;

‎test/fallback-option.test.js

-78
This file was deleted.

‎test/inline-option.test.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
describe('"inline" option', () => {
1111
it('should not work by default', async () => {
12-
const compiler = getCompiler('./basic/entry.js', { inline: false });
12+
const compiler = getCompiler('./basic/entry.js');
1313
const stats = await compile(compiler);
1414
const result = await getResultFromBrowser(stats);
1515

@@ -22,25 +22,28 @@ describe('"inline" option', () => {
2222
expect(getErrors(stats)).toMatchSnapshot('errors');
2323
});
2424

25-
it('should work with "true" value', async () => {
26-
const compiler = getCompiler('./basic/entry.js', { inline: true });
25+
it('should not work with "no-fallback" value', async () => {
26+
const compiler = getCompiler('./basic/entry.js', { inline: 'no-fallback' });
2727
const stats = await compile(compiler);
2828
const result = await getResultFromBrowser(stats);
29+
const moduleSource = getModuleSource('./basic/worker.js', stats);
2930

30-
expect(stats.compilation.assets['test.worker.js']).toBeDefined();
31+
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
32+
expect(moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1).toBe(true);
33+
expect(stats.compilation.assets['test.worker.js']).toBeUndefined();
3134
expect(result).toMatchSnapshot('result');
3235
expect(getWarnings(stats)).toMatchSnapshot('warnings');
3336
expect(getErrors(stats)).toMatchSnapshot('errors');
3437
});
3538

36-
it('should not work with "false" value', async () => {
37-
const compiler = getCompiler('./basic/entry.js', { inline: false });
39+
it('should work with "fallback" value', async () => {
40+
const compiler = getCompiler('./basic/entry.js', { inline: 'fallback' });
3841
const stats = await compile(compiler);
3942
const result = await getResultFromBrowser(stats);
43+
const moduleSource = getModuleSource('./basic/worker.js', stats);
4044

41-
expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot(
42-
'module'
43-
);
45+
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
46+
expect(moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0).toBe(true);
4447
expect(stats.compilation.assets['test.worker.js']).toBeDefined();
4548
expect(result).toMatchSnapshot('result');
4649
expect(getWarnings(stats)).toMatchSnapshot('warnings');

0 commit comments

Comments
 (0)
This repository has been archived.