Skip to content

Commit cbc26b0

Browse files
Rushalexander-akaitvalscion
authored
Fix .cjs files not showing up in bundle analyzer (#512)
-------- Co-authored-by: alexander.akait <[email protected]> Co-authored-by: Vesa Laakso <[email protected]>
1 parent c531af9 commit cbc26b0

File tree

4 files changed

+322
-2
lines changed

4 files changed

+322
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
1212

1313
## UNRELEASED
1414

15+
* **Bug Fix**
16+
* fix `.cjs` files not being handled ([#512](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/512) by [@Rush](https://github.com/Rush))
17+
1518
## 4.10.1
1619

1720
* **Bug Fix**

src/analyzer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {parseBundle} = require('./parseUtils');
1010
const {createAssetsFilter} = require('./utils');
1111

1212
const FILENAME_QUERY_REGEXP = /\?.*$/u;
13-
const FILENAME_EXTENSIONS = /\.(js|mjs)$/iu;
13+
const FILENAME_EXTENSIONS = /\.(js|mjs|cjs)$/iu;
1414

1515
module.exports = {
1616
getViewerData,
@@ -50,7 +50,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
5050
});
5151
}
5252

53-
// Picking only `*.js or *.mjs` assets from bundle that has non-empty `chunks` array
53+
// Picking only `*.js, *.cjs or *.mjs` assets from bundle that has non-empty `chunks` array
5454
bundleStats.assets = bundleStats.assets.filter(asset => {
5555
// Filter out non 'asset' type asset if type is provided (Webpack 5 add a type to indicate asset types)
5656
if (asset.type && asset.type !== 'asset') {

test/analyzer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ describe('Analyzer', function () {
158158
await expectValidReport({bundleLabel: 'bundle.mjs'});
159159
});
160160

161+
it('should support stats files with cjs chunk', async function () {
162+
generateReportFrom('with-cjs-chunk.json');
163+
await expectValidReport({bundleLabel: 'bundle.cjs'});
164+
});
165+
161166
it('should properly parse extremely optimized bundle from webpack 5', async function () {
162167
generateReportFrom('extremely-optimized-webpack-5-bundle/stats.json');
163168
const chartData = await getChartData();

test/stats/with-cjs-chunk.json

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
{
2+
"errors": [],
3+
"warnings": [],
4+
"version": "1.14.0",
5+
"hash": "4e39ab22a848116a4c15",
6+
"children": [
7+
{
8+
"errors": [],
9+
"warnings": [],
10+
"version": "1.14.0",
11+
"hash": "4e39ab22a848116a4c15",
12+
"time": 79,
13+
"publicPath": "",
14+
"assetsByChunkName": {
15+
"bundle": "bundle.cjs"
16+
},
17+
"assets": [
18+
{
19+
"name": "bundle.cjs",
20+
"size": 1735,
21+
"chunks": [0],
22+
"chunkNames": ["bundle"],
23+
"emitted": true
24+
}
25+
],
26+
"chunks": [
27+
{
28+
"id": 0,
29+
"rendered": true,
30+
"initial": true,
31+
"entry": true,
32+
"extraAsync": false,
33+
"size": 141,
34+
"names": ["bundle"],
35+
"files": ["bundle.cjs"],
36+
"hash": "eb0091314b5c4ca75abf",
37+
"parents": [],
38+
"modules": [
39+
{
40+
"id": 0,
41+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
42+
"name": "./src/index.js",
43+
"index": 0,
44+
"index2": 3,
45+
"size": 54,
46+
"cacheable": true,
47+
"built": true,
48+
"optional": false,
49+
"prefetched": false,
50+
"chunks": [0],
51+
"assets": [],
52+
"issuer": null,
53+
"profile": {
54+
"factory": 19,
55+
"building": 15
56+
},
57+
"failed": false,
58+
"errors": 0,
59+
"warnings": 0,
60+
"reasons": [],
61+
"source": "require('./a');\nrequire('./b');\nrequire('./a-clone');\n"
62+
},
63+
{
64+
"id": 1,
65+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a.js",
66+
"name": "./src/a.js",
67+
"index": 1,
68+
"index2": 0,
69+
"size": 29,
70+
"cacheable": true,
71+
"built": true,
72+
"optional": false,
73+
"prefetched": false,
74+
"chunks": [0],
75+
"assets": [],
76+
"issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
77+
"profile": {
78+
"factory": 8,
79+
"building": 6
80+
},
81+
"failed": false,
82+
"errors": 0,
83+
"warnings": 0,
84+
"reasons": [
85+
{
86+
"moduleId": 0,
87+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
88+
"module": "./src/index.js",
89+
"moduleName": "./src/index.js",
90+
"type": "cjs require",
91+
"userRequest": "./a",
92+
"loc": "1:0-14"
93+
}
94+
],
95+
"source": "module.exports = 'module a';\n"
96+
},
97+
{
98+
"id": 2,
99+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/b.js",
100+
"name": "./src/b.js",
101+
"index": 2,
102+
"index2": 1,
103+
"size": 29,
104+
"cacheable": true,
105+
"built": true,
106+
"optional": false,
107+
"prefetched": false,
108+
"chunks": [0],
109+
"assets": [],
110+
"issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
111+
"profile": {
112+
"factory": 9,
113+
"building": 5
114+
},
115+
"failed": false,
116+
"errors": 0,
117+
"warnings": 0,
118+
"reasons": [
119+
{
120+
"moduleId": 0,
121+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
122+
"module": "./src/index.js",
123+
"moduleName": "./src/index.js",
124+
"type": "cjs require",
125+
"userRequest": "./b",
126+
"loc": "2:0-14"
127+
}
128+
],
129+
"source": "module.exports = 'module b';\n"
130+
},
131+
{
132+
"id": 3,
133+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a-clone.js",
134+
"name": "./src/a-clone.js",
135+
"index": 3,
136+
"index2": 2,
137+
"size": 29,
138+
"cacheable": true,
139+
"built": true,
140+
"optional": false,
141+
"prefetched": false,
142+
"chunks": [0],
143+
"assets": [],
144+
"issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
145+
"profile": {
146+
"factory": 10,
147+
"building": 5
148+
},
149+
"failed": false,
150+
"errors": 0,
151+
"warnings": 0,
152+
"reasons": [
153+
{
154+
"moduleId": 0,
155+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
156+
"module": "./src/index.js",
157+
"moduleName": "./src/index.js",
158+
"type": "cjs require",
159+
"userRequest": "./a-clone",
160+
"loc": "3:0-20"
161+
}
162+
],
163+
"source": "module.exports = 'module a';\n"
164+
}
165+
],
166+
"filteredModules": 0,
167+
"origins": [
168+
{
169+
"moduleId": 0,
170+
"module": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
171+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
172+
"moduleName": "./src/index.js",
173+
"loc": "",
174+
"name": "bundle",
175+
"reasons": []
176+
}
177+
]
178+
}
179+
],
180+
"modules": [
181+
{
182+
"id": 0,
183+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
184+
"name": "./src/index.js",
185+
"index": 0,
186+
"index2": 3,
187+
"size": 54,
188+
"cacheable": true,
189+
"built": true,
190+
"optional": false,
191+
"prefetched": false,
192+
"chunks": [0],
193+
"assets": [],
194+
"issuer": null,
195+
"profile": {
196+
"factory": 19,
197+
"building": 15
198+
},
199+
"failed": false,
200+
"errors": 0,
201+
"warnings": 0,
202+
"reasons": [],
203+
"source": "require('./a');\nrequire('./b');\nrequire('./a-clone');\n"
204+
},
205+
{
206+
"id": 1,
207+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a.js",
208+
"name": "./src/a.js",
209+
"index": 1,
210+
"index2": 0,
211+
"size": 29,
212+
"cacheable": true,
213+
"built": true,
214+
"optional": false,
215+
"prefetched": false,
216+
"chunks": [0],
217+
"assets": [],
218+
"issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
219+
"profile": {
220+
"factory": 8,
221+
"building": 6
222+
},
223+
"failed": false,
224+
"errors": 0,
225+
"warnings": 0,
226+
"reasons": [
227+
{
228+
"moduleId": 0,
229+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
230+
"module": "./src/index.js",
231+
"moduleName": "./src/index.js",
232+
"type": "cjs require",
233+
"userRequest": "./a",
234+
"loc": "1:0-14"
235+
}
236+
],
237+
"source": "module.exports = 'module a';\n"
238+
},
239+
{
240+
"id": 2,
241+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/b.js",
242+
"name": "./src/b.js",
243+
"index": 2,
244+
"index2": 1,
245+
"size": 29,
246+
"cacheable": true,
247+
"built": true,
248+
"optional": false,
249+
"prefetched": false,
250+
"chunks": [0],
251+
"assets": [],
252+
"issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
253+
"profile": {
254+
"factory": 9,
255+
"building": 5
256+
},
257+
"failed": false,
258+
"errors": 0,
259+
"warnings": 0,
260+
"reasons": [
261+
{
262+
"moduleId": 0,
263+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
264+
"module": "./src/index.js",
265+
"moduleName": "./src/index.js",
266+
"type": "cjs require",
267+
"userRequest": "./b",
268+
"loc": "2:0-14"
269+
}
270+
],
271+
"source": "module.exports = 'module b';\n"
272+
},
273+
{
274+
"id": 3,
275+
"identifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/a-clone.js",
276+
"name": "./src/a-clone.js",
277+
"index": 3,
278+
"index2": 2,
279+
"size": 29,
280+
"cacheable": true,
281+
"built": true,
282+
"optional": false,
283+
"prefetched": false,
284+
"chunks": [0],
285+
"assets": [],
286+
"issuer": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
287+
"profile": {
288+
"factory": 10,
289+
"building": 5
290+
},
291+
"failed": false,
292+
"errors": 0,
293+
"warnings": 0,
294+
"reasons": [
295+
{
296+
"moduleId": 0,
297+
"moduleIdentifier": "/Volumes/Work/webpack-bundle-analyzer/test/src/index.js",
298+
"module": "./src/index.js",
299+
"moduleName": "./src/index.js",
300+
"type": "cjs require",
301+
"userRequest": "./a-clone",
302+
"loc": "3:0-20"
303+
}
304+
],
305+
"source": "module.exports = 'module a';\n"
306+
}
307+
],
308+
"filteredModules": 0,
309+
"children": []
310+
}
311+
]
312+
}

0 commit comments

Comments
 (0)