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

support node worker_thread #313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) {
typeof options.esModule !== "undefined" ? options.esModule : true;
const fnName = `${workerConstructor}_fn`;

const publicPath = options.publicPath ? JSON.stringify(options.publicPath) : '__webpack_public_path__';

if (options.inline) {
const InlineWorkerPath = stringifyRequest(
loaderContext,
Expand All @@ -59,7 +61,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) {
let fallbackWorkerPath;

if (options.inline === "fallback") {
fallbackWorkerPath = `__webpack_public_path__ + ${JSON.stringify(
fallbackWorkerPath = `${publicPath} + ${JSON.stringify(
workerFilename
)}`;
}
Expand All @@ -80,9 +82,13 @@ ${
)}, ${fallbackWorkerPath});\n}\n`;
}

return `${
return `${loaderContext.target === 'node'
? esModule
? "import {Worker} from 'worker_threads'\n"
: "var {Worker} = require('worker_threads')\n"
: ''}${
esModule ? "export default" : "module.exports ="
} function ${fnName}() {\n return new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify(
} function ${fnName}() {\n return new ${workerConstructor}(${publicPath} + ${JSON.stringify(
workerFilename
)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ""});\n}\n`;
}
Expand Down
17 changes: 2 additions & 15 deletions test/__snapshots__/publicPath.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`"publicPath" option should work and respect "filename" and "chunkFilena

exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: module 2`] = `
"export default function Worker_fn() {
return new Worker(__webpack_public_path__ + \\"other-static/js/worker.worker.js\\");
return new Worker(\\"/public-path-static-other/\\" + \\"other-static/js/worker.worker.js\\");
}
"
`;
Expand Down Expand Up @@ -65,24 +65,11 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt

exports[`"publicPath" option should work and respect the "output.publicPath" option value ("string"): warnings 1`] = `Array []`;

exports[`"publicPath" option should work and respect the "publicPath" option ("function"): errors 1`] = `Array []`;

exports[`"publicPath" option should work and respect the "publicPath" option ("function"): module 1`] = `
"export default function Worker_fn() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
}
"
`;

exports[`"publicPath" option should work and respect the "publicPath" option ("function"): result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"publicPath" option should work and respect the "publicPath" option ("function"): warnings 1`] = `Array []`;

exports[`"publicPath" option should work and respect the "publicPath" option ("string"): errors 1`] = `Array []`;

exports[`"publicPath" option should work and respect the "publicPath" option ("string"): module 1`] = `
"export default function Worker_fn() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
return new Worker(\\"/public-path-static/\\" + \\"test.worker.js\\");
}
"
`;
Expand Down