From 667bd168798edb682029526148232af3e778c6ee Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 10 Apr 2024 18:39:43 +0300 Subject: [PATCH] test: added --- src/analyzer.js | 2 +- test/analyzer.js | 5 + test/stats/with-cjs-chunk.json | 312 +++++++++++++++++++++++++++++++++ 3 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 test/stats/with-cjs-chunk.json diff --git a/src/analyzer.js b/src/analyzer.js index 4b4d1aa2..81eba27b 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -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": [] + } + ] +}