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

refactor: inline option #267

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
}
]
},
"filename": {
"type": "string",
"minLength": 1
},
"publicPath": {
"anyOf": [
{
Expand All @@ -38,15 +34,16 @@
}
]
},
"filename": {
"type": "string",
"minLength": 1
},
"chunkFilename": {
"type": "string",
"minLength": 1
},
"inline": {
"type": "boolean"
},
"fallback": {
"type": "boolean"
"enum": ["no-fallback", "fallback"]
},
"esModule": {
"type": "boolean"
Expand Down
20 changes: 10 additions & 10 deletions src/runtime/inline.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/* eslint-env browser */
/* eslint-disable no-undef, no-use-before-define, new-cap */

module.exports = function inline(
content,
url,
workerConstructor,
workerOptions
) {
const URL = window.URL || window.webkitURL;

module.exports = (content, workerConstructor, workerOptions, url) => {
try {
try {
let blob;

try {
// New API
blob = new window.Blob([content]);
} catch (e) {
// BlobBuilder = Deprecated, but widely implemented
const BlobBuilder =
BlobBuilder || WebKitBlobBuilder || MozBlobBuilder || MSBlobBuilder;
window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;

blob = new BlobBuilder();

blob.append(content);

blob = blob.getBlob();
} catch (e) {
// New API
blob = new Blob([content]);
}

const objectURL = URL.createObjectURL(blob);
Expand Down
2 changes: 1 addition & 1 deletion src/supportWebpack4.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function runAsChild(worker, request, options, callback) {
options
);

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

Expand Down
2 changes: 1 addition & 1 deletion src/supportWebpack5.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function runAsChild(worker, options, callback) {
return callback(getCacheError);
}

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

Expand Down
21 changes: 11 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ function getExternalsType(compilerOptions) {
}

function getWorker(file, content, options) {
const publicWorkerPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;

let workerConstructor;
let workerOptions;

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

const fallbackWorkerPath =
options.fallback === false ? 'null' : publicWorkerPath;
let fallbackWorkerPath;

if (options.inline === 'fallback') {
fallbackWorkerPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
}

return `require(${InlineWorkerPath})(${JSON.stringify(
content
)}, ${fallbackWorkerPath}, ${JSON.stringify(
workerConstructor
)}, ${JSON.stringify(workerOptions)})`;
)}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify(
workerOptions
)}, ${fallbackWorkerPath})`;
}

return `new ${workerConstructor}(${publicWorkerPath}${
workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''
})`;
return `new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify(
file
)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''})`;
}

export {
Expand Down
31 changes: 0 additions & 31 deletions test/__snapshots__/fallback-option.test.js.snap

This file was deleted.

19 changes: 6 additions & 13 deletions test/__snapshots__/inline-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@ exports[`"inline" option should not work by default: result 1`] = `"{\\"postMess

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

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

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

exports[`"inline" option should not work with "false" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
exports[`"inline" option should not work with "no-fallback" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

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

exports[`"inline" option should work with "true" value: errors 1`] = `Array []`;
exports[`"inline" option should work with "fallback" value: errors 1`] = `Array []`;

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

exports[`"inline" option should work with "true" value: warnings 1`] = `Array []`;
exports[`"inline" option should work with "fallback" value: warnings 1`] = `Array []`;
78 changes: 0 additions & 78 deletions test/fallback-option.test.js

This file was deleted.

21 changes: 12 additions & 9 deletions test/inline-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

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

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

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

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

it('should not work with "false" value', async () => {
const compiler = getCompiler('./basic/entry.js', { inline: false });
it('should work with "fallback" value', async () => {
const compiler = getCompiler('./basic/entry.js', { inline: 'fallback' });
const stats = await compile(compiler);
const result = await getResultFromBrowser(stats);
const moduleSource = getModuleSource('./basic/worker.js', stats);

expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot(
'module'
);
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
expect(moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0).toBe(true);
expect(stats.compilation.assets['test.worker.js']).toBeDefined();
expect(result).toMatchSnapshot('result');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
Expand Down