Skip to content

Commit

Permalink
Fix .cjs files not showing up in bundle analyzer (#512)
Browse files Browse the repository at this point in the history
--------

Co-authored-by: alexander.akait <[email protected]>
Co-authored-by: Vesa Laakso <[email protected]>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent c531af9 commit cbc26b0
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions test/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
312 changes: 312 additions & 0 deletions test/stats/with-cjs-chunk.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}

0 comments on commit cbc26b0

Please sign in to comment.