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

fix: set a name to exported fn #299

Merged
merged 5 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
"webpack": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"schema-utils": "^3.0.0",
"loader-utils": "^2.0.0"
"loader-utils": "^2.0.0",
"lodash.camelcase": "^4.3.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using this module, it is deprecated, we don't need extra module for simple logic

Copy link
Contributor Author

@ramasilveyra ramasilveyra Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi Sorry for my delay.

I've tried to use simpler solutions like https://stackoverflow.com/a/2970667, but they can fail with RangeError.

So since lodash.camelcase is deprecated I have remplace it with camelcase.

camelcase:

If still doesn't work, I can try inlining this code https://github.com/sindresorhus/camelcase/blob/master/index.js here.

"schema-utils": "^3.0.0"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
Expand Down
6 changes: 4 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { stringifyRequest } from 'loader-utils';
import camelcase from 'lodash.camelcase';

function getDefaultFilename(filename) {
if (typeof filename === 'function') {
Expand Down Expand Up @@ -48,6 +49,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) {

const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : true;
const fnName = `${camelcase(workerFilename)}Worker`;

if (options.inline) {
const InlineWorkerPath = stringifyRequest(
Expand All @@ -72,7 +74,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 +83,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 workerWorkerJsWorker() {
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 testWorkerJsWorker() {
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 workerWorkerJsWorker() {
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 workerWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 workerCustomWorkerJsWorker() {
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 workerCustomWorkerJsWorker() {
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 workerWorkerJsWorker() {
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 workerCustomWorkerJsWorker() {
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 workerCustomWorkerJsWorker() {
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 testWorkerJsWorker() {
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 typeDetectionWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 otherStaticJsWorkerBundleWorkerJsWorker() {
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 otherStaticJsWorkerWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
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 testWorkerJsWorker() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
}
"
Expand Down