From 6a64b569caa68d7af6b7c508abe0d2bbdcfa0a06 Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Mon, 16 Nov 2020 20:06:13 -0300 Subject: [PATCH 1/5] fix: set a name to exported fn this can help debugging and also removes the warning https://nextjs.link/codemod-ndc from next.js --- package-lock.json | 5 +++++ package.json | 5 +++-- src/utils.js | 12 +++++++++--- .../chunkFilename-option.test.js.snap | 8 ++++---- test/__snapshots__/esModule-option.test.js.snap | 6 +++--- test/__snapshots__/filename-options.test.js.snap | 10 +++++----- test/__snapshots__/inline-option.test.js.snap | 2 +- test/__snapshots__/loader.test.js.snap | 16 ++++++++-------- test/__snapshots__/publicPath.test.js.snap | 16 ++++++++-------- test/__snapshots__/worker-option.test.js.snap | 6 +++--- 10 files changed, 49 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 176f20f..91f64c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10391,6 +10391,11 @@ "integrity": "sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=", "dev": true }, + "lodash.camelcase": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", + "integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=" + }, "lodash.ismatch": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz", diff --git a/package.json b/package.json index a9e2830..8950b7f 100644 --- a/package.json +++ b/package.json @@ -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", + "schema-utils": "^3.0.0" }, "devDependencies": { "@babel/cli": "^7.11.6", diff --git a/src/utils.js b/src/utils.js index 7a7a034..260817b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,5 @@ -import { stringifyRequest } from 'loader-utils'; +import { stringifyRequest, interpolateName } from 'loader-utils'; +import camelcase from 'lodash.camelcase'; function getDefaultFilename(filename) { if (typeof filename === 'function') { @@ -48,6 +49,11 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) { const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true; + const fnName = interpolateName( + loaderContext, + `${camelcase(workerFilename)}Worker[hash:7]`, + { content: workerSource } + ); if (options.inline) { const InlineWorkerPath = stringifyRequest( @@ -72,7 +78,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 +87,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..3d02478 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 workerWorkerJsWorkere97b0d2() { 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 testWorkerJsWorker5182e60() { 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 workerWorkerJsWorker8de1ef2() { 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 workerWorkerJsWorker8250cd8() { 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..1ca9ded 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 testWorkerJsWorker78b84b4() { 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 testWorkerJsWorker78b84b4() { 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 testWorkerJsWorker78b84b4() { 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..4e01a63 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 workerCustomWorkerJsWorker8250cd8() { 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 workerCustomWorkerJsWorker8250cd8() { 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 workerWorkerJsWorker8250cd8() { 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 workerCustomWorkerJsWorker225d46c() { 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 workerCustomWorkerJsWorker8250cd8() { 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..e7a0936 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 testWorkerJsWorker78b84b4() { 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..e80b726 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 typeDetectionWorkerJsWorker3d6ec69() { 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 testWorkerJsWorker78b84b4() { 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 testWorkerJsWorker409f1ae() { 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 testWorkerJsWorker0ad18cc() { 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 testWorkerJsWorker9b7b307() { 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 testWorkerJsWorker8250cd8() { 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 testWorkerJsWorker348cb02() { 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 testWorkerJsWorker78b84b4() { 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..feb9433 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 otherStaticJsWorkerBundleWorkerJsWorker57f1fc8() { 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 otherStaticJsWorkerWorkerJsWorker57f1fc8() { 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 testWorkerJsWorker8250cd8() { 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 testWorkerJsWorkerb7c903a() { 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 testWorkerJsWorkerb7c903a() { 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 testWorkerJsWorkerb7c903a() { 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 testWorkerJsWorkerb7c903a() { 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 testWorkerJsWorker8250cd8() { 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..d323cdc 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 testWorkerJsWorker78b84b4() { 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 testWorkerJsWorker78b84b4() { 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 testWorkerJsWorker78b84b4() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " From 9d8ad01c78aeeb884dddf2031112f63232d770d5 Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Mon, 16 Nov 2020 20:47:05 -0300 Subject: [PATCH 2/5] fix: hash is not idempotent with different os or webpack or node versions --- src/utils.js | 8 ++------ .../chunkFilename-option.test.js.snap | 8 ++++---- test/__snapshots__/esModule-option.test.js.snap | 6 +++--- test/__snapshots__/filename-options.test.js.snap | 10 +++++----- test/__snapshots__/inline-option.test.js.snap | 2 +- test/__snapshots__/loader.test.js.snap | 16 ++++++++-------- test/__snapshots__/publicPath.test.js.snap | 16 ++++++++-------- test/__snapshots__/worker-option.test.js.snap | 6 +++--- 8 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/utils.js b/src/utils.js index 260817b..33b4d69 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ -import { stringifyRequest, interpolateName } from 'loader-utils'; +import { stringifyRequest } from 'loader-utils'; import camelcase from 'lodash.camelcase'; function getDefaultFilename(filename) { @@ -49,11 +49,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) { const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true; - const fnName = interpolateName( - loaderContext, - `${camelcase(workerFilename)}Worker[hash:7]`, - { content: workerSource } - ); + const fnName = `${camelcase(workerFilename)}Worker`; if (options.inline) { const InlineWorkerPath = stringifyRequest( diff --git a/test/__snapshots__/chunkFilename-option.test.js.snap b/test/__snapshots__/chunkFilename-option.test.js.snap index 3d02478..7b59d13 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 workerWorkerJsWorkere97b0d2() { +"export default function workerWorkerJsWorker() { 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 testWorkerJsWorker5182e60() { +"export default function testWorkerJsWorker() { 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 workerWorkerJsWorker8de1ef2() { +"export default function workerWorkerJsWorker() { 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 workerWorkerJsWorker8250cd8() { +"export default function workerWorkerJsWorker() { 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 1ca9ded..46a9ff0 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker78b84b4() { +"module.exports = function testWorkerJsWorker() { 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 4e01a63..e5abfb8 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 workerCustomWorkerJsWorker8250cd8() { +"export default function workerCustomWorkerJsWorker() { 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 workerCustomWorkerJsWorker8250cd8() { +"export default function workerCustomWorkerJsWorker() { 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 workerWorkerJsWorker8250cd8() { +"export default function workerWorkerJsWorker() { 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 workerCustomWorkerJsWorker225d46c() { +"export default function workerCustomWorkerJsWorker() { 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 workerCustomWorkerJsWorker8250cd8() { +"export default function workerCustomWorkerJsWorker() { 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 e7a0936..7a56e9c 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 e80b726..2080514 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 typeDetectionWorkerJsWorker3d6ec69() { +"export default function typeDetectionWorkerJsWorker() { 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker409f1ae() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker0ad18cc() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker9b7b307() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker8250cd8() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker348cb02() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 feb9433..5639922 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 otherStaticJsWorkerBundleWorkerJsWorker57f1fc8() { +"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 otherStaticJsWorkerWorkerJsWorker57f1fc8() { +"export default function otherStaticJsWorkerWorkerJsWorker() { 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 testWorkerJsWorker8250cd8() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorkerb7c903a() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorkerb7c903a() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorkerb7c903a() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorkerb7c903a() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker8250cd8() { +"export default function testWorkerJsWorker() { 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 d323cdc..78bda86 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { 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 testWorkerJsWorker78b84b4() { +"export default function testWorkerJsWorker() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } " From bdeaadc3ada95353594c32b7cce36c3eed259fc9 Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Thu, 26 Nov 2020 01:45:53 -0300 Subject: [PATCH 3/5] fix: use camelcase dep instead of lodash.camelcase --- package-lock.json | 36 ++++++++++++++++------ package.json | 2 +- src/utils.js | 4 +-- test/__snapshots__/publicPath.test.js.snap | 4 +-- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 91f64c2..2aad1ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1535,6 +1535,14 @@ "get-package-type": "^0.1.0", "js-yaml": "^3.13.1", "resolve-from": "^5.0.0" + }, + "dependencies": { + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + } } }, "@istanbuljs/schema": { @@ -3697,10 +3705,9 @@ } }, "camelcase": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", - "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", - "dev": true + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.2.0.tgz", + "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==" }, "camelcase-keys": { "version": "6.2.2", @@ -3711,6 +3718,14 @@ "camelcase": "^5.3.1", "map-obj": "^4.0.0", "quick-lru": "^4.0.1" + }, + "dependencies": { + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + } } }, "caniuse-lite": { @@ -10391,11 +10406,6 @@ "integrity": "sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=", "dev": true }, - "lodash.camelcase": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", - "integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=" - }, "lodash.ismatch": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz", @@ -14746,6 +14756,14 @@ "requires": { "camelcase": "^5.0.0", "decamelize": "^1.2.0" + }, + "dependencies": { + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + } } }, "yauzl": { diff --git a/package.json b/package.json index 8950b7f..6d09b63 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,8 @@ "webpack": "^4.0.0 || ^5.0.0" }, "dependencies": { + "camelcase": "^6.2.0", "loader-utils": "^2.0.0", - "lodash.camelcase": "^4.3.0", "schema-utils": "^3.0.0" }, "devDependencies": { diff --git a/src/utils.js b/src/utils.js index 33b4d69..379a368 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,5 @@ import { stringifyRequest } from 'loader-utils'; -import camelcase from 'lodash.camelcase'; +import camelcase from 'camelcase'; function getDefaultFilename(filename) { if (typeof filename === 'function') { @@ -49,7 +49,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) { const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true; - const fnName = `${camelcase(workerFilename)}Worker`; + const fnName = `${camelcase(workerFilename.replace(/\//g, ''))}Worker`; if (options.inline) { const InlineWorkerPath = stringifyRequest( diff --git a/test/__snapshots__/publicPath.test.js.snap b/test/__snapshots__/publicPath.test.js.snap index 5639922..1380b8a 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 otherStaticJsWorkerBundleWorkerJsWorker() { +"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 otherStaticJsWorkerWorkerJsWorker() { +"export default function otherStaticjsworkerWorkerJsWorker() { return new Worker(__webpack_public_path__ + \\"other-static/js/worker.worker.js\\"); } " From d01618cd876e7f6711d222ebc15e32d9f60e8d28 Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Thu, 26 Nov 2020 17:23:13 -0300 Subject: [PATCH 4/5] fix: inline camelcase dep --- package-lock.json | 31 +++------------------ package.json | 5 ++-- src/utils.js | 71 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2aad1ea..176f20f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1535,14 +1535,6 @@ "get-package-type": "^0.1.0", "js-yaml": "^3.13.1", "resolve-from": "^5.0.0" - }, - "dependencies": { - "camelcase": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", - "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", - "dev": true - } } }, "@istanbuljs/schema": { @@ -3705,9 +3697,10 @@ } }, "camelcase": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.2.0.tgz", - "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==" + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true }, "camelcase-keys": { "version": "6.2.2", @@ -3718,14 +3711,6 @@ "camelcase": "^5.3.1", "map-obj": "^4.0.0", "quick-lru": "^4.0.1" - }, - "dependencies": { - "camelcase": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", - "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", - "dev": true - } } }, "caniuse-lite": { @@ -14756,14 +14741,6 @@ "requires": { "camelcase": "^5.0.0", "decamelize": "^1.2.0" - }, - "dependencies": { - "camelcase": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", - "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", - "dev": true - } } }, "yauzl": { diff --git a/package.json b/package.json index 6d09b63..a9e2830 100644 --- a/package.json +++ b/package.json @@ -41,9 +41,8 @@ "webpack": "^4.0.0 || ^5.0.0" }, "dependencies": { - "camelcase": "^6.2.0", - "loader-utils": "^2.0.0", - "schema-utils": "^3.0.0" + "schema-utils": "^3.0.0", + "loader-utils": "^2.0.0" }, "devDependencies": { "@babel/cli": "^7.11.6", diff --git a/src/utils.js b/src/utils.js index 379a368..d1dabfc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,4 @@ import { stringifyRequest } from 'loader-utils'; -import camelcase from 'camelcase'; function getDefaultFilename(filename) { if (typeof filename === 'function') { @@ -49,7 +48,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) { const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true; - const fnName = `${camelcase(workerFilename.replace(/\//g, ''))}Worker`; + const fnName = `${camelCase(workerFilename)}Worker`; if (options.inline) { const InlineWorkerPath = stringifyRequest( @@ -114,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, From 38888c6a5f85736f98b827532ea084eeec37fb10 Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Tue, 1 Dec 2020 22:17:04 -0300 Subject: [PATCH 5/5] fix: use workerConstructor as name --- src/utils.js | 70 +------------------ .../chunkFilename-option.test.js.snap | 8 +-- .../esModule-option.test.js.snap | 6 +- .../filename-options.test.js.snap | 10 +-- test/__snapshots__/inline-option.test.js.snap | 2 +- test/__snapshots__/loader.test.js.snap | 16 ++--- test/__snapshots__/publicPath.test.js.snap | 16 ++--- test/__snapshots__/worker-option.test.js.snap | 6 +- 8 files changed, 33 insertions(+), 101 deletions(-) diff --git a/src/utils.js b/src/utils.js index d1dabfc..9c7d2a5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -48,7 +48,7 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) { const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true; - const fnName = `${camelCase(workerFilename)}Worker`; + const fnName = `${workerConstructor}_fn`; if (options.inline) { const InlineWorkerPath = stringifyRequest( @@ -113,74 +113,6 @@ 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, diff --git a/test/__snapshots__/chunkFilename-option.test.js.snap b/test/__snapshots__/chunkFilename-option.test.js.snap index 7b59d13..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 workerWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 workerWorkerJsWorker() { +"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 workerWorkerJsWorker() { +"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 46a9ff0..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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 e5abfb8..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 workerCustomWorkerJsWorker() { +"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 workerCustomWorkerJsWorker() { +"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 workerWorkerJsWorker() { +"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 workerCustomWorkerJsWorker() { +"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 workerCustomWorkerJsWorker() { +"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 7a56e9c..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 testWorkerJsWorker() { +"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 2080514..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 typeDetectionWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 1380b8a..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 otherStaticjsworkerBundleWorkerJsWorker() { +"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 otherStaticjsworkerWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 78bda86..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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"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 testWorkerJsWorker() { +"export default function Worker_fn() { return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); } "