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

feat: worker option #255

Merged
merged 1 commit into from
Jul 10, 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
15 changes: 15 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,20 @@ module.exports = (api) => {
},
],
],
overrides: [
{
test: './src/runtime',
presets: [
[
'@babel/preset-env',
{
targets: {
node: '0.12',
},
},
],
],
},
],
};
};
26 changes: 23 additions & 3 deletions src/options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
{
"type": "object",
"properties": {
"worker": {
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"minLength": 1
},
"options": {
"additionalProperties": true,
"type": "object"
}
},
"required": ["type"]
}
]
},
"name": {
"type": "string"
},
Expand All @@ -12,9 +35,6 @@
},
"publicPath": {
"type": "string"
},
"workerType": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
48 changes: 48 additions & 0 deletions src/runtime/inline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env browser */
/* eslint-disable no-undef, no-use-before-define, new-cap */

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

try {
// BlobBuilder = Deprecated, but widely implemented
const BlobBuilder =
BlobBuilder || WebKitBlobBuilder || MozBlobBuilder || MSBlobBuilder;

blob = new BlobBuilder();

blob.append(content);

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

const objectURL = URL.createObjectURL(blob);
const worker = new window[workerConstructor](objectURL, workerOptions);

URL.revokeObjectURL(objectURL);

return worker;
} catch (e) {
return new window[workerConstructor](
`data:application/javascript,${encodeURIComponent(content)}`,
workerOptions
);
}
} catch (e) {
if (!url) {
throw Error('Inline worker is not supported');
}

return new window[workerConstructor](url, workerOptions);
}
};
2 changes: 1 addition & 1 deletion src/supportWebpack4.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getWorker from './workers';
import { getWorker } from './utils';

export default function runAsChild(worker, request, options, callback) {
const subCache = `subcache ${__dirname} ${request}`;
Expand Down
2 changes: 1 addition & 1 deletion src/supportWebpack5.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getWorker from './workers';
import { getWorker } from './utils';

export default function runAsChild(worker, options, callback) {
// eslint-disable-next-line import/no-unresolved, global-require
Expand Down
40 changes: 40 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function getWorker(file, content, options) {
const publicPath =
typeof options.publicPath === 'undefined'
? '__webpack_public_path__'
: JSON.stringify(options.publicPath);
const publicWorkerPath = `${publicPath} + ${JSON.stringify(file)}`;

let workerConstructor;
let workerOptions;

if (typeof options.worker === 'undefined') {
workerConstructor = 'Worker';
} else if (typeof options.worker === 'string') {
workerConstructor = options.worker;
} else {
({ type: workerConstructor, options: workerOptions } = options.worker);
}

if (options.inline) {
const InlineWorkerPath = JSON.stringify(
`!!${require.resolve('./runtime/inline.js')}`
);

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

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

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

// eslint-disable-next-line import/prefer-default-export
export { getWorker };
57 changes: 0 additions & 57 deletions src/workers/InlineWorker.js

This file was deleted.

39 changes: 0 additions & 39 deletions src/workers/index.js

This file was deleted.

43 changes: 43 additions & 0 deletions test/__snapshots__/worker-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"workerType" option should support the "Worker" object value for inline workers: errors 1`] = `Array []`;

exports[`"workerType" option should support the "Worker" object value for inline workers: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"workerType" option should support the "Worker" object value for inline workers: warnings 1`] = `Array []`;

exports[`"workerType" option should support the "Worker" object value: errors 1`] = `Array []`;

exports[`"workerType" option should support the "Worker" object value: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\", {\\"type\\":\\"classic\\",\\"name\\":\\"worker-name\\"});
};"
`;

exports[`"workerType" option should support the "Worker" object value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"workerType" option should support the "Worker" object value: warnings 1`] = `Array []`;

exports[`"workerType" option should support the "Worker" string value: errors 1`] = `Array []`;

exports[`"workerType" option should support the "Worker" string value: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`"workerType" option should support the "Worker" string value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"workerType" option should support the "Worker" string value: warnings 1`] = `Array []`;

exports[`"workerType" option should use "Worker" by default: errors 1`] = `Array []`;

exports[`"workerType" option should use "Worker" by default: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`"workerType" option should use "Worker" by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"workerType" option should use "Worker" by default: warnings 1`] = `Array []`;
13 changes: 0 additions & 13 deletions test/__snapshots__/workerType-option.test.js.snap

This file was deleted.

19 changes: 19 additions & 0 deletions test/helpers/getResultFromBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ export default async function getResultFromBrowser(stats) {

const browser = await puppeteer.launch();
const page = await browser.newPage();

page
.on('console', (message) =>
// eslint-disable-next-line no-console
console.log(message)
)
.on('pageerror', ({ message }) =>
// eslint-disable-next-line no-console
console.log(message)
)
// .on('response', (response) =>
// // eslint-disable-next-line no-console
// console.log(`${response.status()} ${response.url()}`)
// )
.on('requestfailed', (request) =>
// eslint-disable-next-line no-console
console.log(`${request.failure().errorText} ${request.url()}`)
);

await page.goto(`http://127.0.0.1:${port}/`);
await page.waitForSelector('button');
await page.click('button');
Expand Down
Loading