From c531af9a883eff173b49429740c8e5b6bafd97d0 Mon Sep 17 00:00:00 2001 From: Sukka Date: Tue, 9 Apr 2024 18:44:44 +0800 Subject: [PATCH 1/4] Remove `is-plain-object` (#627) --- package-lock.json | 5 ----- package.json | 1 - src/viewer.js | 12 ++++++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 12b4dbe3..28979e54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6819,11 +6819,6 @@ "integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==", "dev": true }, - "is-plain-object": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-5.0.0.tgz", - "integrity": "sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==" - }, "is-potential-custom-element-name": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz", diff --git a/package.json b/package.json index 600e9368..478ff4bb 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "escape-string-regexp": "^4.0.0", "gzip-size": "^6.0.0", "html-escaper": "^2.0.2", - "is-plain-object": "^5.0.0", "opener": "^1.5.2", "picocolors": "^1.0.0", "sirv": "^2.0.3", diff --git a/src/viewer.js b/src/viewer.js index 6e47676f..3107136a 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -4,7 +4,6 @@ const http = require('http'); const WebSocket = require('ws'); const sirv = require('sirv'); -const {isPlainObject} = require('is-plain-object'); const {bold} = require('picocolors'); const Logger = require('./Logger'); @@ -190,7 +189,12 @@ function getChartData(analyzerOpts, ...args) { chartData = null; } - if (isPlainObject(chartData) && Object.keys(chartData).length === 0) { + // chartData can either be an array (bundleInfo[]) or null. It can't be an plain object anyway + if ( + // analyzer.getViewerData() doesn't failed in the previous step + chartData + && !Array.isArray(chartData) + ) { logger.error("Could't find any javascript bundles in provided stats file"); chartData = null; } @@ -199,8 +203,8 @@ function getChartData(analyzerOpts, ...args) { } function getEntrypoints(bundleStats) { - if (bundleStats === null || bundleStats === undefined) { + if (bundleStats === null || bundleStats === undefined || !bundleStats.entrypoints) { return []; } - return Object.values(bundleStats.entrypoints || {}).map(entrypoint => entrypoint.name); + return Object.values(bundleStats.entrypoints).map(entrypoint => entrypoint.name); } From cbc26b0d910022d28029f5d2ee29942f72dc5c1b Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Thu, 11 Apr 2024 06:43:43 -0500 Subject: [PATCH 2/4] Fix .cjs files not showing up in bundle analyzer (#512) -------- Co-authored-by: alexander.akait Co-authored-by: Vesa Laakso <482561+valscion@users.noreply.github.com> --- CHANGELOG.md | 3 + src/analyzer.js | 4 +- test/analyzer.js | 5 + test/stats/with-cjs-chunk.json | 312 +++++++++++++++++++++++++++++++++ 4 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 test/stats/with-cjs-chunk.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 35a1a886..ba29dc70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED +* **Bug Fix** + * fix `.cjs` files not being handled ([#512](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/512) by [@Rush](https://github.com/Rush)) + ## 4.10.1 * **Bug Fix** diff --git a/src/analyzer.js b/src/analyzer.js index 323e1777..81eba27b 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -10,7 +10,7 @@ const {parseBundle} = require('./parseUtils'); const {createAssetsFilter} = require('./utils'); const FILENAME_QUERY_REGEXP = /\?.*$/u; -const FILENAME_EXTENSIONS = /\.(js|mjs)$/iu; +const FILENAME_EXTENSIONS = /\.(js|mjs|cjs)$/iu; module.exports = { getViewerData, @@ -50,7 +50,7 @@ function getViewerData(bundleStats, bundleDir, opts) { }); } - // Picking only `*.js or *.mjs` assets from bundle that has non-empty `chunks` array + // Picking only `*.js, *.cjs or *.mjs` assets from bundle that has non-empty `chunks` array bundleStats.assets = bundleStats.assets.filter(asset => { // Filter out non 'asset' type asset if type is provided (Webpack 5 add a type to indicate asset types) if (asset.type && asset.type !== 'asset') { diff --git a/test/analyzer.js b/test/analyzer.js index 89889be9..bd2ea068 100644 --- a/test/analyzer.js +++ b/test/analyzer.js @@ -158,6 +158,11 @@ describe('Analyzer', function () { await expectValidReport({bundleLabel: 'bundle.mjs'}); }); + it('should support stats files with cjs chunk', async function () { + generateReportFrom('with-cjs-chunk.json'); + await expectValidReport({bundleLabel: 'bundle.cjs'}); + }); + it('should properly parse extremely optimized bundle from webpack 5', async function () { generateReportFrom('extremely-optimized-webpack-5-bundle/stats.json'); const chartData = await getChartData(); diff --git a/test/stats/with-cjs-chunk.json b/test/stats/with-cjs-chunk.json new file mode 100644 index 00000000..52043bc2 --- /dev/null +++ b/test/stats/with-cjs-chunk.json @@ -0,0 +1,312 @@ +{ + "errors": [], + "warnings": [], + "version": "1.14.0", + "hash": "4e39ab22a848116a4c15", + "children": [ + { + "errors": [], + "warnings": [], + "version": "1.14.0", + "hash": "4e39ab22a848116a4c15", + "time": 79, + "publicPath": "", + "assetsByChunkName": { + "bundle": "bundle.cjs" + }, + "assets": [ + { + "name": "bundle.cjs", + "size": 1735, + "chunks": [0], + "chunkNames": ["bundle"], + "emitted": true + } + ], + "chunks": [ + { + "id": 0, + "rendered": true, + "initial": true, + "entry": true, + "extraAsync": false, + "size": 141, + "names": ["bundle"], + "files": ["bundle.cjs"], + "hash": "eb0091314b5c4ca75abf", + "parents": [], + "modules": [ + { + "id": 0, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "name": "./src/index.js", + "index": 0, + "index2": 3, + "size": 54, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": null, + "profile": { + "factory": 19, + "building": 15 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [], + "source": "require('./a');\nrequire('./b');\nrequire('./a-clone');\n" + }, + { + "id": 1, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a.js", + "name": "./src/a.js", + "index": 1, + "index2": 0, + "size": 29, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "profile": { + "factory": 8, + "building": 6 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [ + { + "moduleId": 0, + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "module": "./src/index.js", + "moduleName": "./src/index.js", + "type": "cjs require", + "userRequest": "./a", + "loc": "1:0-14" + } + ], + "source": "module.exports = 'module a';\n" + }, + { + "id": 2, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/b.js", + "name": "./src/b.js", + "index": 2, + "index2": 1, + "size": 29, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "profile": { + "factory": 9, + "building": 5 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [ + { + "moduleId": 0, + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "module": "./src/index.js", + "moduleName": "./src/index.js", + "type": "cjs require", + "userRequest": "./b", + "loc": "2:0-14" + } + ], + "source": "module.exports = 'module b';\n" + }, + { + "id": 3, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a-clone.js", + "name": "./src/a-clone.js", + "index": 3, + "index2": 2, + "size": 29, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "profile": { + "factory": 10, + "building": 5 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [ + { + "moduleId": 0, + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "module": "./src/index.js", + "moduleName": "./src/index.js", + "type": "cjs require", + "userRequest": "./a-clone", + "loc": "3:0-20" + } + ], + "source": "module.exports = 'module a';\n" + } + ], + "filteredModules": 0, + "origins": [ + { + "moduleId": 0, + "module": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "moduleName": "./src/index.js", + "loc": "", + "name": "bundle", + "reasons": [] + } + ] + } + ], + "modules": [ + { + "id": 0, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "name": "./src/index.js", + "index": 0, + "index2": 3, + "size": 54, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": null, + "profile": { + "factory": 19, + "building": 15 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [], + "source": "require('./a');\nrequire('./b');\nrequire('./a-clone');\n" + }, + { + "id": 1, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a.js", + "name": "./src/a.js", + "index": 1, + "index2": 0, + "size": 29, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "profile": { + "factory": 8, + "building": 6 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [ + { + "moduleId": 0, + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "module": "./src/index.js", + "moduleName": "./src/index.js", + "type": "cjs require", + "userRequest": "./a", + "loc": "1:0-14" + } + ], + "source": "module.exports = 'module a';\n" + }, + { + "id": 2, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/b.js", + "name": "./src/b.js", + "index": 2, + "index2": 1, + "size": 29, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "profile": { + "factory": 9, + "building": 5 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [ + { + "moduleId": 0, + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "module": "./src/index.js", + "moduleName": "./src/index.js", + "type": "cjs require", + "userRequest": "./b", + "loc": "2:0-14" + } + ], + "source": "module.exports = 'module b';\n" + }, + { + "id": 3, + "identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a-clone.js", + "name": "./src/a-clone.js", + "index": 3, + "index2": 2, + "size": 29, + "cacheable": true, + "built": true, + "optional": false, + "prefetched": false, + "chunks": [0], + "assets": [], + "issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "profile": { + "factory": 10, + "building": 5 + }, + "failed": false, + "errors": 0, + "warnings": 0, + "reasons": [ + { + "moduleId": 0, + "moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js", + "module": "./src/index.js", + "moduleName": "./src/index.js", + "type": "cjs require", + "userRequest": "./a-clone", + "loc": "3:0-20" + } + ], + "source": "module.exports = 'module a';\n" + } + ], + "filteredModules": 0, + "children": [] + } + ] +} From a9f10058858010e6d2ddae688d242b5f39e54dcc Mon Sep 17 00:00:00 2001 From: Vesa Laakso <482561+valscion@users.noreply.github.com> Date: Thu, 11 Apr 2024 14:51:33 +0300 Subject: [PATCH 3/4] Add missing changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba29dc70..3c7b3753 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ * **Bug Fix** * fix `.cjs` files not being handled ([#512](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/512) by [@Rush](https://github.com/Rush)) +* **Internal** + * Remove `is-plain-object` ([#627](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/627) by [@SukkaW](https://github.com/SukkaW)) + ## 4.10.1 * **Bug Fix** From f8d4711d7670ffa9fae11cd34787cc45c538f9bb Mon Sep 17 00:00:00 2001 From: Vesa Laakso <482561+valscion@users.noreply.github.com> Date: Thu, 11 Apr 2024 14:52:09 +0300 Subject: [PATCH 4/4] v4.10.2 --- CHANGELOG.md | 2 ++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c7b3753..2c7e81fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED +## 4.10.2 + * **Bug Fix** * fix `.cjs` files not being handled ([#512](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/512) by [@Rush](https://github.com/Rush)) diff --git a/package-lock.json b/package-lock.json index 28979e54..f6bae55a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "webpack-bundle-analyzer", - "version": "4.10.1", + "version": "4.10.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 478ff4bb..80630f19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-bundle-analyzer", - "version": "4.10.1", + "version": "4.10.2", "description": "Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap", "author": "Yury Grunin ", "license": "MIT",