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

Commit 2b9e2fd

Browse files
authoredJul 10, 2020
feat: worker option (#255)
1 parent 800b074 commit 2b9e2fd

13 files changed

+270
-137
lines changed
 

‎babel.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,20 @@ module.exports = (api) => {
1515
},
1616
],
1717
],
18+
overrides: [
19+
{
20+
test: './src/runtime',
21+
presets: [
22+
[
23+
'@babel/preset-env',
24+
{
25+
targets: {
26+
node: '0.12',
27+
},
28+
},
29+
],
30+
],
31+
},
32+
],
1833
};
1934
};

‎src/options.json

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
{
22
"type": "object",
33
"properties": {
4+
"worker": {
5+
"anyOf": [
6+
{
7+
"type": "string",
8+
"minLength": 1
9+
},
10+
{
11+
"type": "object",
12+
"additionalProperties": false,
13+
"properties": {
14+
"type": {
15+
"type": "string",
16+
"minLength": 1
17+
},
18+
"options": {
19+
"additionalProperties": true,
20+
"type": "object"
21+
}
22+
},
23+
"required": ["type"]
24+
}
25+
]
26+
},
427
"name": {
528
"type": "string"
629
},
@@ -12,9 +35,6 @@
1235
},
1336
"publicPath": {
1437
"type": "string"
15-
},
16-
"workerType": {
17-
"type": "string"
1838
}
1939
},
2040
"additionalProperties": false

‎src/runtime/inline.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-env browser */
2+
/* eslint-disable no-undef, no-use-before-define, new-cap */
3+
4+
module.exports = function inline(
5+
content,
6+
url,
7+
workerConstructor,
8+
workerOptions
9+
) {
10+
try {
11+
try {
12+
let blob;
13+
14+
try {
15+
// BlobBuilder = Deprecated, but widely implemented
16+
const BlobBuilder =
17+
BlobBuilder || WebKitBlobBuilder || MozBlobBuilder || MSBlobBuilder;
18+
19+
blob = new BlobBuilder();
20+
21+
blob.append(content);
22+
23+
blob = blob.getBlob();
24+
} catch (e) {
25+
// New API
26+
blob = new Blob([content]);
27+
}
28+
29+
const objectURL = URL.createObjectURL(blob);
30+
const worker = new window[workerConstructor](objectURL, workerOptions);
31+
32+
URL.revokeObjectURL(objectURL);
33+
34+
return worker;
35+
} catch (e) {
36+
return new window[workerConstructor](
37+
`data:application/javascript,${encodeURIComponent(content)}`,
38+
workerOptions
39+
);
40+
}
41+
} catch (e) {
42+
if (!url) {
43+
throw Error('Inline worker is not supported');
44+
}
45+
46+
return new window[workerConstructor](url, workerOptions);
47+
}
48+
};

‎src/supportWebpack4.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getWorker from './workers';
1+
import { getWorker } from './utils';
22

33
export default function runAsChild(worker, request, options, callback) {
44
const subCache = `subcache ${__dirname} ${request}`;

‎src/supportWebpack5.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getWorker from './workers';
1+
import { getWorker } from './utils';
22

33
export default function runAsChild(worker, options, callback) {
44
// eslint-disable-next-line import/no-unresolved, global-require

‎src/utils.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function getWorker(file, content, options) {
2+
const publicPath =
3+
typeof options.publicPath === 'undefined'
4+
? '__webpack_public_path__'
5+
: JSON.stringify(options.publicPath);
6+
const publicWorkerPath = `${publicPath} + ${JSON.stringify(file)}`;
7+
8+
let workerConstructor;
9+
let workerOptions;
10+
11+
if (typeof options.worker === 'undefined') {
12+
workerConstructor = 'Worker';
13+
} else if (typeof options.worker === 'string') {
14+
workerConstructor = options.worker;
15+
} else {
16+
({ type: workerConstructor, options: workerOptions } = options.worker);
17+
}
18+
19+
if (options.inline) {
20+
const InlineWorkerPath = JSON.stringify(
21+
`!!${require.resolve('./runtime/inline.js')}`
22+
);
23+
24+
const fallbackWorkerPath =
25+
options.fallback === false ? 'null' : publicWorkerPath;
26+
27+
return `require(${InlineWorkerPath})(${JSON.stringify(
28+
content
29+
)}, ${fallbackWorkerPath}, ${JSON.stringify(
30+
workerConstructor
31+
)}, ${JSON.stringify(workerOptions)})`;
32+
}
33+
34+
return `new ${workerConstructor}(${publicWorkerPath}${
35+
workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''
36+
})`;
37+
}
38+
39+
// eslint-disable-next-line import/prefer-default-export
40+
export { getWorker };

‎src/workers/InlineWorker.js

-57
This file was deleted.

‎src/workers/index.js

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"workerType" option should support the "Worker" object value for inline workers: errors 1`] = `Array []`;
4+
5+
exports[`"workerType" option should support the "Worker" object value for inline workers: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
6+
7+
exports[`"workerType" option should support the "Worker" object value for inline workers: warnings 1`] = `Array []`;
8+
9+
exports[`"workerType" option should support the "Worker" object value: errors 1`] = `Array []`;
10+
11+
exports[`"workerType" option should support the "Worker" object value: module 1`] = `
12+
"module.exports = function() {
13+
return new Worker(__webpack_public_path__ + \\"test.worker.js\\", {\\"type\\":\\"classic\\",\\"name\\":\\"worker-name\\"});
14+
};"
15+
`;
16+
17+
exports[`"workerType" option should support the "Worker" object value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
18+
19+
exports[`"workerType" option should support the "Worker" object value: warnings 1`] = `Array []`;
20+
21+
exports[`"workerType" option should support the "Worker" string value: errors 1`] = `Array []`;
22+
23+
exports[`"workerType" option should support the "Worker" string value: module 1`] = `
24+
"module.exports = function() {
25+
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
26+
};"
27+
`;
28+
29+
exports[`"workerType" option should support the "Worker" string value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
30+
31+
exports[`"workerType" option should support the "Worker" string value: warnings 1`] = `Array []`;
32+
33+
exports[`"workerType" option should use "Worker" by default: errors 1`] = `Array []`;
34+
35+
exports[`"workerType" option should use "Worker" by default: module 1`] = `
36+
"module.exports = function() {
37+
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
38+
};"
39+
`;
40+
41+
exports[`"workerType" option should use "Worker" by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
42+
43+
exports[`"workerType" option should use "Worker" by default: warnings 1`] = `Array []`;

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

-13
This file was deleted.

‎test/helpers/getResultFromBrowser.js

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ export default async function getResultFromBrowser(stats) {
2727

2828
const browser = await puppeteer.launch();
2929
const page = await browser.newPage();
30+
31+
page
32+
.on('console', (message) =>
33+
// eslint-disable-next-line no-console
34+
console.log(message)
35+
)
36+
.on('pageerror', ({ message }) =>
37+
// eslint-disable-next-line no-console
38+
console.log(message)
39+
)
40+
// .on('response', (response) =>
41+
// // eslint-disable-next-line no-console
42+
// console.log(`${response.status()} ${response.url()}`)
43+
// )
44+
.on('requestfailed', (request) =>
45+
// eslint-disable-next-line no-console
46+
console.log(`${request.failure().errorText} ${request.url()}`)
47+
);
48+
3049
await page.goto(`http://127.0.0.1:${port}/`);
3150
await page.waitForSelector('button');
3251
await page.click('button');

0 commit comments

Comments
 (0)
This repository has been archived.