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

Commit

Permalink
fix: set a name to exported function (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramasilveyra authored Dec 2, 2020
1 parent 464bf30 commit 15cf407
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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`;
}
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/chunkFilename-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/esModule-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand Down
10 changes: 5 additions & 5 deletions test/__snapshots__/filename-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/inline-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
}
"
Expand Down
16 changes: 8 additions & 8 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand Down
16 changes: 8 additions & 8 deletions test/__snapshots__/publicPath.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/worker-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\"});
}
"
Expand All @@ -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\\");
}
"
Expand All @@ -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\\");
}
"
Expand Down

0 comments on commit 15cf407

Please sign in to comment.