Skip to content

Commit

Permalink
fix: Handle chunks with empty modules
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
d4rkr00t committed Aug 20, 2018
1 parent a36756f commit 497be76
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
44 changes: 44 additions & 0 deletions fixtures/valid-with-empty-modules-in-chunks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"errors": [],
"warnings": [],
"version": "1.15.0",
"hash": "9184c60078374c771f6c",
"time": 16625,
"publicPath": "http://localhost:8000/",
"assetsByChunkName": {
"commons": ["commons.js", "commons.js.map"]
},
"assets": [
{
"name": "commons.js",
"size": 3797689,
"chunks": [0],
"chunkNames": ["commons"],
"emitted": true
}
],
"chunks": [
{
"id": 0,
"rendered": true,
"initial": true,
"entry": true,
"extraAsync": false,
"size": 3492266,
"names": ["commons"],
"files": ["commons.js", "commons.js.map"],
"hash": "5088ce796a9301f64f06",
"parents": []
}
],
"modules": [
{
"id": "module1",
"name": "module-name",
"size": 500,
"reasons": []
}
],
"filteredModules": 0,
"children": []
}
33 changes: 33 additions & 0 deletions lib/__tests__/__snapshots__/analyze.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Snapshot report for `lib/__tests__/analyze.js`

The actual snapshot is saved in `analyze.js.snap`.

Generated by [AVA](https://ava.li).

## should handle stats file with a chunk which has empty modules

> Snapshot 1
{
chunks: {
0: {
id: 0,
names: [
'commons',
],
size: 3492266,
},
},
modules: [
{
chunks: undefined,
deps: 0,
depsChains: [],
imported: 0,
name: 'module-name',
reasons: [],
size: 500,
type: 'file',
},
],
}
Binary file added lib/__tests__/__snapshots__/analyze.js.snap
Binary file not shown.
10 changes: 10 additions & 0 deletions lib/__tests__/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ test("should call updateProgressBar correct number of times", t => {
);
t.is(calls, 2);
});

test("should handle stats file with a chunk which has empty modules", t => {
t.snapshot(
analyze(
getStats(f.find("valid-with-empty-modules-in-chunks.json")),
[],
() => {}
)
);
});
26 changes: 18 additions & 8 deletions lib/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const getModuleType = (name /*?: string */) =>
: isNodeModules(name) ? "module" : "file";

const flattenChunks = (stats /*: WebpackStats */) /*: Array<WebpackModule> */ =>
stats.chunks
stats.chunks && stats.chunks[0].modules
? stats.chunks.reduce((modules, chunk) => modules.concat(chunk.modules), [])
: stats.modules;

Expand Down Expand Up @@ -229,7 +229,7 @@ const joinModules = (modules) /*: { [string]: Module } */ =>
}, {});

const pickFromModules = (modules) /*: Array<PreModule> */ =>
modules.map(module => {
modules.filter(Boolean).map(module => {
const clearName = getModuleName(module.name);
return {
clearName,
Expand Down Expand Up @@ -268,7 +268,11 @@ const buildModuleDepsChains = (modules, name) => {
}, []);
};

const postProcessModules = (modules, ignore, updateProgressBar) => {
const postProcessModules = (
modules /*: { [key: string]: Module }*/,
ignore /*: Array<string> */,
updateProgressBar /*: Function */
) => {
return Object.keys(modules).reduce((acc, name, index) => {
updateProgressBar(index + 1, "processing", name);

Expand Down Expand Up @@ -296,13 +300,19 @@ const postProcessModules = (modules, ignore, updateProgressBar) => {
}
});

module.reasons = module.reasons.filter(
reason => !mm.isMatch(reason.clearName || reason.moduleName || "", ignore)
);

if (module.reasons.length) {
if (!module.reasons) {
acc[name] = module;
} else {
module.reasons = module.reasons.filter(
reason =>
!mm.isMatch(reason.clearName || reason.moduleName || "", ignore)
);

if (module.reasons.length) {
acc[name] = module;
}
}

return acc;
}, {});
};
Expand Down

0 comments on commit 497be76

Please sign in to comment.