From 27580071db39da587e244efe868fd29587a0345b Mon Sep 17 00:00:00 2001 From: Tran Sang Date: Sun, 4 Apr 2021 07:54:04 +0900 Subject: [PATCH 1/2] support node worker_thread --- src/utils.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/utils.js b/src/utils.js index bcd6cbf..7836873 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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, @@ -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 )}`; } @@ -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`; } From 825589fea45ec18fdf8f9ec03963abf31d4fc2dc Mon Sep 17 00:00:00 2001 From: Tran Sang Date: Sun, 4 Apr 2021 08:09:54 +0900 Subject: [PATCH 2/2] update snapshot for breaking change of outputPath option. --- test/__snapshots__/publicPath.test.js.snap | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/test/__snapshots__/publicPath.test.js.snap b/test/__snapshots__/publicPath.test.js.snap index b19119c..c4cdf11 100644 --- a/test/__snapshots__/publicPath.test.js.snap +++ b/test/__snapshots__/publicPath.test.js.snap @@ -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\\"); } " `; @@ -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\\"); } " `;