diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b54811..dbd3e52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [3.0.1](https://github.com/webpack-contrib/worker-loader/compare/v3.0.0...v3.0.1) (2020-08-05) + + +### Bug Fixes + +* compatibility with webpack@5 cache ([#279](https://github.com/webpack-contrib/worker-loader/issues/279)) ([ee519b1](https://github.com/webpack-contrib/worker-loader/commit/ee519b1d283dbb599385fe2932c99c929b09db36)) +* interpolation `[name]` for the `filename` option ([#277](https://github.com/webpack-contrib/worker-loader/issues/277)) ([5efa77a](https://github.com/webpack-contrib/worker-loader/commit/5efa77a64d8fbce123b289461234ac3a8812fb54)) + ## [3.0.0](https://github.com/webpack-contrib/worker-loader/compare/v2.0.0...v3.0.0) (2020-08-01) diff --git a/package-lock.json b/package-lock.json index 23af9d8..7845c57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "worker-loader", - "version": "3.0.0", + "version": "3.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4b20dde..1666d9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "worker-loader", - "version": "3.0.0", + "version": "3.0.1", "description": "worker loader module for webpack", "license": "MIT", "repository": "webpack-contrib/worker-loader", diff --git a/src/index.js b/src/index.js index 0429fc3..d7cb59a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ +import path from 'path'; + import { getOptions } from 'loader-utils'; import validateOptions from 'schema-utils'; @@ -99,9 +101,11 @@ export function pitch(request) { ).apply(workerContext.compiler); } - new SingleEntryPlugin(this.context, `!!${request}`, 'main').apply( - workerContext.compiler - ); + new SingleEntryPlugin( + this.context, + `!!${request}`, + path.parse(this.resourcePath).name + ).apply(workerContext.compiler); workerContext.request = request; diff --git a/src/supportWebpack4.js b/src/supportWebpack4.js index cbdd762..fec90d4 100644 --- a/src/supportWebpack4.js +++ b/src/supportWebpack4.js @@ -42,6 +42,10 @@ export default function runAsChild( return callback(null, workerCode); } - return callback(null, null); + return callback( + new Error( + `Failed to compile web worker "${workerContext.request}" request` + ) + ); }); } diff --git a/src/supportWebpack5.js b/src/supportWebpack5.js index 9d9164a..6c6f910 100644 --- a/src/supportWebpack5.js +++ b/src/supportWebpack5.js @@ -1,5 +1,3 @@ -import { stringifyRequest } from 'loader-utils'; - import { workerGenerator, sourceMappingURLRegex } from './utils'; export default function runAsChild( @@ -8,9 +6,6 @@ export default function runAsChild( options, callback ) { - // eslint-disable-next-line import/no-unresolved, global-require - const getLazyHashedEtag = require('webpack/lib/cache/getLazyHashedEtag'); - workerContext.compiler.runAsChild((error, entries, compilation) => { if (error) { return callback(error); @@ -18,68 +13,66 @@ export default function runAsChild( if (entries[0]) { const [workerFilename] = [...entries[0].files]; - const requestIdent = stringifyRequest( - { context: loaderContext.rootContext }, - workerContext.request + const cache = workerContext.compiler.getCache('worker-loader'); + const cacheIdent = workerFilename; + const cacheETag = cache.getLazyHashedEtag( + compilation.assets[workerFilename] ); - const cacheIdent = `${workerContext.compiler.compilerPath}/worker-loader|${requestIdent}`; - const cacheETag = getLazyHashedEtag(compilation.assets[workerFilename]); - // TODO not working, need fix on webpack@5 side - return workerContext.compiler.cache.get( - cacheIdent, - cacheETag, - (getCacheError, content) => { - if (getCacheError) { - return callback(getCacheError); - } - - if (options.inline === 'no-fallback') { - // eslint-disable-next-line no-underscore-dangle, no-param-reassign - delete loaderContext._compilation.assets[workerFilename]; + return cache.get(cacheIdent, cacheETag, (getCacheError, content) => { + if (getCacheError) { + return callback(getCacheError); + } - // TODO improve this, we should store generated source maps files for file in `assetInfo` - // eslint-disable-next-line no-underscore-dangle - if (loaderContext._compilation.assets[`${workerFilename}.map`]) { - // eslint-disable-next-line no-underscore-dangle, no-param-reassign - delete loaderContext._compilation.assets[`${workerFilename}.map`]; - } - } + if (options.inline === 'no-fallback') { + // eslint-disable-next-line no-underscore-dangle, no-param-reassign + delete loaderContext._compilation.assets[workerFilename]; - if (content) { - return callback(null, content); + // TODO improve this, we should store generated source maps files for file in `assetInfo` + // eslint-disable-next-line no-underscore-dangle + if (loaderContext._compilation.assets[`${workerFilename}.map`]) { + // eslint-disable-next-line no-underscore-dangle, no-param-reassign + delete loaderContext._compilation.assets[`${workerFilename}.map`]; } + } - let workerSource = compilation.assets[workerFilename].source(); + if (content) { + return callback(null, content); + } - if (options.inline === 'no-fallback') { - // Remove `/* sourceMappingURL=url */` comment - workerSource = workerSource.replace(sourceMappingURLRegex, ''); - } + let workerSource = compilation.assets[workerFilename].source(); - const workerCode = workerGenerator( - loaderContext, - workerFilename, - workerSource, - options - ); + if (options.inline === 'no-fallback') { + // Remove `/* sourceMappingURL=url */` comment + workerSource = workerSource.replace(sourceMappingURLRegex, ''); + } - return workerContext.compiler.cache.store( - cacheIdent, - cacheETag, - workerCode, - (storeCacheError) => { - if (storeCacheError) { - return callback(storeCacheError); - } + const workerCode = workerGenerator( + loaderContext, + workerFilename, + workerSource, + options + ); - return callback(null, workerCode); + return cache.store( + cacheIdent, + cacheETag, + workerCode, + (storeCacheError) => { + if (storeCacheError) { + return callback(storeCacheError); } - ); - } - ); + + return callback(null, workerCode); + } + ); + }); } - return callback(null, null); + return callback( + new Error( + `Failed to compile web worker "${workerContext.request}" request` + ) + ); }); } diff --git a/test/__snapshots__/chunkFilename-option.test.js.snap b/test/__snapshots__/chunkFilename-option.test.js.snap index fdbdf0c..577fecc 100644 --- a/test/__snapshots__/chunkFilename-option.test.js.snap +++ b/test/__snapshots__/chunkFilename-option.test.js.snap @@ -17,7 +17,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" defaul exports[`"name" option should work and respect the "output.chunkFilename" default value option: module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.worker.js\\"); + return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " `; @@ -30,7 +30,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" option exports[`"name" option should work and respect the "output.chunkFilename" option ("string"): module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.worker.js\\"); + return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " `; diff --git a/test/__snapshots__/filename-options.test.js.snap b/test/__snapshots__/filename-options.test.js.snap index cc533c1..beb0124 100644 --- a/test/__snapshots__/filename-options.test.js.snap +++ b/test/__snapshots__/filename-options.test.js.snap @@ -4,7 +4,7 @@ exports[`"filename" option should work ("function"): errors 1`] = `Array []`; exports[`"filename" option should work ("function"): module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\"); + return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " `; @@ -17,7 +17,7 @@ exports[`"filename" option should work ("string"): errors 1`] = `Array []`; exports[`"filename" option should work ("string"): module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\"); + return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " `; @@ -30,7 +30,7 @@ exports[`"filename" option should work and respect the "output.filename" default exports[`"filename" option should work and respect the "output.filename" default value option: module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.worker.js\\"); + return new Worker(__webpack_public_path__ + \\"worker.worker.js\\"); } " `; @@ -43,7 +43,7 @@ exports[`"filename" option should work and respect the "output.filename" option exports[`"filename" option should work and respect the "output.filename" option ("function"): module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\"); + return new Worker(__webpack_public_path__ + \\"worker.custom.worker.js\\"); } " `; @@ -56,7 +56,7 @@ exports[`"filename" option should work and respect the "output.filename" option exports[`"filename" option should work and respect the "output.filename" option ("string"): module 1`] = ` "export default function() { - return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\"); + 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 7aeb673..9af5142 100644 --- a/test/__snapshots__/inline-option.test.js.snap +++ b/test/__snapshots__/inline-option.test.js.snap @@ -27,10 +27,16 @@ exports[`"inline" option should work with "fallback" value and "esModule" with " exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): errors 1`] = `Array []`; +exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): errors 2`] = `Array []`; + exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; +exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): result 2`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): warnings 1`] = `Array []`; +exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): warnings 2`] = `Array []`; + exports[`"inline" option should work with "fallback" value: errors 1`] = `Array []`; exports[`"inline" option should work with "fallback" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 044d170..0a7eea0 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -1,5 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +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() { + return new Worker(__webpack_public_path__ + \\"TypeDetection.worker.js\\"); +} +" +`; + +exports[`worker-loader should work and have the same base file name as the source files: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + +exports[`worker-loader should work and have the same base file name as the source files: warnings 1`] = `Array []`; + 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`] = ` @@ -13,6 +26,19 @@ exports[`worker-loader should work and respect the "devtool" option ("false" val exports[`worker-loader should work and respect the "devtool" option ("false" value): warnings 1`] = `Array []`; +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() { + return new Worker(__webpack_public_path__ + \\"test.worker.js\\"); +} +" +`; + +exports[`worker-loader should work and respect the "devtool" option ("source-map" value): result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + +exports[`worker-loader should work and respect the "devtool" option ("source-map" value): warnings 1`] = `Array []`; + exports[`worker-loader should work with "externals": errors 1`] = `Array []`; exports[`worker-loader should work with "externals": module 1`] = ` diff --git a/test/__snapshots__/publicPath.test.js.snap b/test/__snapshots__/publicPath.test.js.snap index 3011711..c17f4ab 100644 --- a/test/__snapshots__/publicPath.test.js.snap +++ b/test/__snapshots__/publicPath.test.js.snap @@ -1,5 +1,31 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: errors 1`] = `Array []`; + +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() { + 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() { + return new Worker(__webpack_public_path__ + \\"other-static/js/worker.worker.js\\"); +} +" +`; + +exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + +exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: result 2`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`; + +exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: warnings 1`] = `Array []`; + +exports[`"publicPath" option should work and respect "filename" and "chunkFilename" option values: warnings 2`] = `Array []`; + 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`] = ` diff --git a/test/fixtures/name/TypeDetection.js b/test/fixtures/name/TypeDetection.js new file mode 100644 index 0000000..8d67d1d --- /dev/null +++ b/test/fixtures/name/TypeDetection.js @@ -0,0 +1,7 @@ +onmessage = function(event) { + const workerResult = event.data; + + workerResult.onmessage = true; + + postMessage(workerResult); +}; diff --git a/test/fixtures/name/entry.js b/test/fixtures/name/entry.js new file mode 100644 index 0000000..aad9b1a --- /dev/null +++ b/test/fixtures/name/entry.js @@ -0,0 +1,22 @@ +import Worker from './TypeDetection.js'; + +const worker = new Worker(); + +let result; + +worker.onmessage = function (event) { + if (!result) { + result = document.createElement("div"); + result.setAttribute('id', 'result'); + + document.body.append(result); + } + + result.innerText = JSON.stringify(event.data) +}; + +const button = document.getElementById('button'); + +button.addEventListener('click', () => { + worker.postMessage({ postMessage: true }) +}); diff --git a/test/fixtures/name/index.html b/test/fixtures/name/index.html new file mode 100644 index 0000000..786cafc --- /dev/null +++ b/test/fixtures/name/index.html @@ -0,0 +1,13 @@ + + +
+ +Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: