From 939ddc816b03cab70e3693dc943876cc48489014 Mon Sep 17 00:00:00 2001 From: Gytis Vinclovas Date: Tue, 19 May 2020 16:01:51 +0300 Subject: [PATCH] Added support for Web worker chunk template plugin (#353) * Support WebWorkerChunkTemplatePlugin * changelog --- CHANGELOG.md | 3 +++ src/parseUtils.js | 21 +++++++++++++++++++ ...ncChunkWithWebWorkerChunkTemplatePlugin.js | 1 + ...hWebWorkerChunkTemplatePlugin.modules.json | 5 +++++ 4 files changed, 30 insertions(+) create mode 100644 test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.js create mode 100644 test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.modules.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ea7a3b1..6ab609c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ + * **New Feature** + * Support [WebWorkerChunkTemplatePlugin](https://github.com/webpack/webpack/blob/c9d4ff7b054fc581c96ce0e53432d44f9dd8ca72/lib/webworker/WebWorkerChunkTemplatePlugin.js) ([#353](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/353) by [@Gongreg](https://github.com/Gongreg)) + * **Bug Fix** * Support any custom `globalObject` option in Webpack Config. ([#352](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/352) by [@Gongreg](https://github.com/Gongreg)) diff --git a/src/parseUtils.js b/src/parseUtils.js index c31e6745..f5989105 100644 --- a/src/parseUtils.js +++ b/src/parseUtils.js @@ -79,6 +79,15 @@ function parseBundle(bundlePath) { return; } + // Webpack v4 WebWorkerChunkTemplatePlugin + // globalObject.chunkCallbackName([],, ...); + // Both globalObject and chunkCallbackName can be changed through the config, so we can't check them. + if (isAsyncWebWorkerChunkExpression(node)) { + state.locations = getModulesLocations(args[1]); + return; + } + + // Walking into arguments because some of plugins (e.g. `DedupePlugin`) or some Webpack // features (e.g. `umd` library output) can wrap modules list into additional IIFE. _.each(args, arg => c(arg, state)); @@ -212,6 +221,18 @@ function mayBeAsyncChunkArguments(args) { ); } +function isAsyncWebWorkerChunkExpression(node) { + const {callee, type, arguments: args} = node; + + return ( + type === 'CallExpression' && + callee.type === 'MemberExpression' && + args.length === 2 && + isChunkIds(args[0]) && + isModulesList(args[1]) + ); +} + function getModulesLocations(node) { if (node.type === 'ObjectExpression') { // Modules hash diff --git a/test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.js b/test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.js new file mode 100644 index 00000000..5e84dced --- /dev/null +++ b/test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.js @@ -0,0 +1 @@ +self.chunkCallbackName([27],{1:function(e,n,t){console.log("Chuck Norris")}}); diff --git a/test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.modules.json b/test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.modules.json new file mode 100644 index 00000000..cabfce7a --- /dev/null +++ b/test/bundles/validWebpack4AsyncChunkWithWebWorkerChunkTemplatePlugin.modules.json @@ -0,0 +1,5 @@ +{ + "modules": { + "1": "function(e,n,t){console.log(\"Chuck Norris\")}" + } +}