diff --git a/src/utils.js b/src/utils.js index 7a7a034..9c7d2a5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -48,6 +48,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) { const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true; + const fnName = `${workerConstructor}_fn`; if (options.inline) { const InlineWorkerPath = stringifyRequest( @@ -72,7 +73,7 @@ ${ ${ esModule ? 'export default' : 'module.exports =' -} function() {\n return worker(${JSON.stringify( +} function ${fnName}() {\n return worker(${JSON.stringify( workerSource )}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify( workerOptions @@ -81,7 +82,7 @@ ${ return `${ esModule ? 'export default' : 'module.exports =' - } function() {\n return new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify( + } function ${fnName}() {\n return new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify( workerFilename )}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''});\n}\n`; } diff --git a/test/__snapshots__/chunkFilename-option.test.js.snap b/test/__snapshots__/chunkFilename-option.test.js.snap index 3685e6d..cd0ec30 100644 --- a/test/__snapshots__/chunkFilename-option.test.js.snap +++ b/test/__snapshots__/chunkFilename-option.test.js.snap @@ -3,7 +3,7 @@ exports[`"name" option should chunkFilename suffix be inserted before query parameters: errors 1`] = `Array []`; exports[`"name" option should chunkFilename suffix be inserted before query parameters: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " @@ -18,7 +18,7 @@ exports[`"name" option should chunkFilename suffix be inserted before query para exports[`"name" option should work ("string"): errors 1`] = `Array []`; exports[`"name" option should work ("string"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -31,7 +31,7 @@ exports[`"name" option should work ("string"): warnings 1`] = `Array []`; exports[`"name" option should work and respect the "output.chunkFilename" default value option: errors 1`] = `Array []`; exports[`"name" option should work and respect the "output.chunkFilename" default value option: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " @@ -44,7 +44,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" defaul exports[`"name" option should work and respect the "output.chunkFilename" option ("string"): errors 1`] = `Array []`; exports[`"name" option should work and respect the "output.chunkFilename" option ("string"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " diff --git a/test/__snapshots__/esModule-option.test.js.snap b/test/__snapshots__/esModule-option.test.js.snap index 1c116cb..f1ad3c5 100644 --- a/test/__snapshots__/esModule-option.test.js.snap +++ b/test/__snapshots__/esModule-option.test.js.snap @@ -3,7 +3,7 @@ exports[`"esModule" option should work and generate ES module syntax by default: errors 1`] = `Array []`; exports[`"esModule" option should work and generate ES module syntax by default: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -14,7 +14,7 @@ exports[`"esModule" option should work and generate ES module syntax by default: exports[`"esModule" option should work with "false" value: errors 1`] = `Array []`; exports[`"esModule" option should work with "false" value: module 1`] = ` -"module.exports = function() { +"module.exports = function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -27,7 +27,7 @@ exports[`"esModule" option should work with "false" value: warnings 1`] = `Array exports[`"esModule" option should work with "true" value: errors 1`] = `Array []`; exports[`"esModule" option should work with "true" value: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " diff --git a/test/__snapshots__/filename-options.test.js.snap b/test/__snapshots__/filename-options.test.js.snap index beb0124..1e4f23f 100644 --- a/test/__snapshots__/filename-options.test.js.snap +++ b/test/__snapshots__/filename-options.test.js.snap @@ -3,7 +3,7 @@ exports[`"filename" option should work ("function"): errors 1`] = `Array []`; exports[`"filename" option should work ("function"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " @@ -16,7 +16,7 @@ exports[`"filename" option should work ("function"): warnings 1`] = `Array []`; exports[`"filename" option should work ("string"): errors 1`] = `Array []`; exports[`"filename" option should work ("string"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " @@ -29,7 +29,7 @@ exports[`"filename" option should work ("string"): warnings 1`] = `Array []`; exports[`"filename" option should work and respect the "output.filename" default value option: errors 1`] = `Array []`; exports[`"filename" option should work and respect the "output.filename" default value option: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " @@ -42,7 +42,7 @@ exports[`"filename" option should work and respect the "output.filename" default exports[`"filename" option should work and respect the "output.filename" option ("function"): errors 1`] = `Array []`; exports[`"filename" option should work and respect the "output.filename" option ("function"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " @@ -55,7 +55,7 @@ exports[`"filename" option should work and respect the "output.filename" option exports[`"filename" option should work and respect the "output.filename" option ("string"): errors 1`] = `Array []`; exports[`"filename" option should work and respect the "output.filename" option ("string"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " diff --git a/test/__snapshots__/inline-option.test.js.snap b/test/__snapshots__/inline-option.test.js.snap index 8414722..2fb52a9 100644 --- a/test/__snapshots__/inline-option.test.js.snap +++ b/test/__snapshots__/inline-option.test.js.snap @@ -3,7 +3,7 @@ exports[`"inline" option should not work by default: errors 1`] = `Array []`; exports[`"inline" option should not work by default: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 0a7eea0..d808f2d 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -3,7 +3,7 @@ exports[`worker-loader should work and have the same base file name as the source files: errors 1`] = `Array []`; exports[`worker-loader should work and have the same base file name as the source files: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"TypeDetection.worker.js\\"); } " @@ -16,7 +16,7 @@ exports[`worker-loader should work and have the same base file name as the sourc exports[`worker-loader should work and respect the "devtool" option ("false" value): errors 1`] = `Array []`; exports[`worker-loader should work and respect the "devtool" option ("false" value): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -29,7 +29,7 @@ exports[`worker-loader should work and respect the "devtool" option ("false" val exports[`worker-loader should work and respect the "devtool" option ("source-map" value): errors 1`] = `Array []`; exports[`worker-loader should work and respect the "devtool" option ("source-map" value): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -42,7 +42,7 @@ exports[`worker-loader should work and respect the "devtool" option ("source-map exports[`worker-loader should work with "externals": errors 1`] = `Array []`; exports[`worker-loader should work with "externals": module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -55,7 +55,7 @@ exports[`worker-loader should work with "externals": warnings 1`] = `Array []`; exports[`worker-loader should work with WASM: errors 1`] = `Array []`; exports[`worker-loader should work with WASM: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -68,7 +68,7 @@ exports[`worker-loader should work with WASM: warnings 1`] = `Array []`; exports[`worker-loader should work with async chunks: errors 1`] = `Array []`; exports[`worker-loader should work with async chunks: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -81,7 +81,7 @@ exports[`worker-loader should work with async chunks: warnings 1`] = `Array []`; exports[`worker-loader should work with inline syntax: errors 1`] = `Array []`; exports[`worker-loader should work with inline syntax: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -94,7 +94,7 @@ exports[`worker-loader should work with inline syntax: warnings 1`] = `Array []` exports[`worker-loader should work: errors 1`] = `Array []`; exports[`worker-loader should work: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " diff --git a/test/__snapshots__/publicPath.test.js.snap b/test/__snapshots__/publicPath.test.js.snap index c17f4ab..b19119c 100644 --- a/test/__snapshots__/publicPath.test.js.snap +++ b/test/__snapshots__/publicPath.test.js.snap @@ -5,14 +5,14 @@ exports[`"publicPath" option should work and respect "filename" and "chunkFilena exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: errors 2`] = `Array []`; exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"other-static/js/worker.bundle.worker.js\\"); } " `; exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: module 2`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"other-static/js/worker.worker.js\\"); } " @@ -29,7 +29,7 @@ exports[`"publicPath" option should work and respect "filename" and "chunkFilena exports[`"publicPath" option should work and respect the "output.publicPath" option default value: errors 1`] = `Array []`; exports[`"publicPath" option should work and respect the "output.publicPath" option default value: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -42,7 +42,7 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt exports[`"publicPath" option should work and respect the "output.publicPath" option value ("function"): errors 1`] = `Array []`; exports[`"publicPath" option should work and respect the "output.publicPath" option value ("function"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -55,7 +55,7 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt exports[`"publicPath" option should work and respect the "output.publicPath" option value ("string"): errors 1`] = `Array []`; exports[`"publicPath" option should work and respect the "output.publicPath" option value ("string"): module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -68,7 +68,7 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt 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() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -81,7 +81,7 @@ exports[`"publicPath" option should work and respect the "publicPath" option ("f 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() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -94,7 +94,7 @@ exports[`"publicPath" option should work and respect the "publicPath" option ("s exports[`"publicPath" option should work and use "__webpack_public_path__" by default: errors 1`] = `Array []`; exports[`"publicPath" option should work and use "__webpack_public_path__" by default: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " diff --git a/test/__snapshots__/worker-option.test.js.snap b/test/__snapshots__/worker-option.test.js.snap index 2c165bd..07e4d1c 100644 --- a/test/__snapshots__/worker-option.test.js.snap +++ b/test/__snapshots__/worker-option.test.js.snap @@ -15,7 +15,7 @@ exports[`"workerType" option should support the "Worker" object value for inline exports[`"workerType" option should support the "Worker" object value: errors 1`] = `Array []`; exports[`"workerType" option should support the "Worker" object value: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\", {\\"type\\":\\"classic\\",\\"name\\":\\"worker-name\\"}); } " @@ -28,7 +28,7 @@ exports[`"workerType" option should support the "Worker" object value: warnings exports[`"workerType" option should support the "Worker" string value: errors 1`] = `Array []`; exports[`"workerType" option should support the "Worker" string value: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " @@ -41,7 +41,7 @@ exports[`"workerType" option should support the "Worker" string value: warnings exports[`"workerType" option should use "Worker" by default: errors 1`] = `Array []`; exports[`"workerType" option should use "Worker" by default: module 1`] = ` -"export default function() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } "