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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 71 additions & 2 deletions src/utils.js
Expand Up @@ -48,6 +48,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 +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 Expand Up @@ -112,6 +113,74 @@ const sourceURLWebpackRegex = RegExp(
);
/* eslint-enable prefer-template */

/*
* Based on sindresorhus/camelcase v6.2.0
* License: https://github.com/sindresorhus/camelcase/blob/v6.2.0/license
*/
function camelCase(input1) {
let input = input1;
input = input.trim();

if (input.length === 0) {
return '';
}

if (input.length === 1) {
return input.toLocaleLowerCase();
}

const hasUpperCase = input !== input.toLocaleLowerCase();

if (hasUpperCase) {
let isLastCharLower = false;
let isLastCharUpper = false;
let isLastLastCharUpper = false;

for (let i = 0; i < input.length; i++) {
const character = input[i];

if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
input = `${input.slice(0, i)}-${input.slice(i)}`;
isLastCharLower = false;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true;
i += 1;
} else if (
isLastCharUpper &&
isLastLastCharUpper &&
/[\p{Ll}]/u.test(character)
) {
input = `${input.slice(0, i - 1)}-${input.slice(i - 1)}`;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = false;
isLastCharLower = true;
} else {
isLastCharLower =
character.toLocaleLowerCase() === character &&
character.toLocaleUpperCase() !== character;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper =
character.toLocaleUpperCase() === character &&
character.toLocaleLowerCase() !== character;
}
}
}

input = input.replace(/^[_.\- ]+/, '');

input = input.toLocaleLowerCase();

return (
input
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) =>
p1.toLocaleUpperCase()
)
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, (m) => m.toLocaleUpperCase())
// NOTE: addition to replace `/` or `\` with ``.
.replace(/(\/|\\)/g, '')
);
}

export {
getDefaultFilename,
getDefaultChunkFilename,
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/chunkFilename-option.test.js.snap
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
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
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
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
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
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
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