Skip to content

Commit

Permalink
fix: Stats with multiple times nested children
Browse files Browse the repository at this point in the history
Closes: #13
  • Loading branch information
d4rkr00t committed Apr 20, 2018
1 parent 751b9e1 commit a599ce1
Show file tree
Hide file tree
Showing 7 changed files with 624 additions and 44 deletions.
439 changes: 439 additions & 0 deletions fixtures/nested-children-stats2.json

Large diffs are not rendered by default.

132 changes: 124 additions & 8 deletions lib/__tests__/__snapshots__/print.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,45 @@ Generated by [AVA](https://ava.li).
'--------------------',
'',
'',
` MODULE bootstrap␊
├─ imported: 2 times␊
├─ size: 235 KiB [for all included files]␊
├─ type: [direct]␊
├─ chunks: main␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/bootstrap/␊
│ ␊
├─ files: ␊
│ ├─ css ../node_modules/.registry.npmjs.org/css-loader/0.28.10/node_modules/css-loader!../node_modules/.registry.npmjs.org/postcss-loader/2.1.1/node_modules/postcss-loader/lib!../node_modules/.registry.npmjs.org/bootstrap/4.0.0/node_modules/bootstrap/dist/css/bootstrap.min.css␊
│ ├─ ../node_modules/.registry.npmjs.org/bootstrap/4.0.0/node_modules/bootstrap/dist/css/bootstrap.min.css␊
│ └─ ../node_modules/.registry.npmjs.org/css-loader/0.28.10/node_modules/css-loader!../node_modules/.registry.npmjs.org/postcss-loader/2.1.1/node_modules/postcss-loader/lib!../node_modules/.registry.npmjs.org/bootstrap/4.0.0/node_modules/bootstrap/dist/css/bootstrap.min.css␊
│ ␊
└─ reasons:␊
└─ ./index.js + 14 modules 3:0-46 [harmony side effect evaluation]␊
`,
'--------------------',
'',
'',
` MODULE webpack␊
├─ imported: 2 times␊
├─ size: 2 KiB [for all included files]␊
├─ type: [transitive]␊
│ ␊
├─ chunks: main␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/webpack/␊
│ ␊
├─ files: ␊
│ ├─ ../node_modules/.registry.npmjs.org/webpack/4.1.1/node_modules/webpack/buildin/global.js␊
│ └─ ../node_modules/.registry.npmjs.org/webpack/4.1.1/node_modules/webpack/buildin/module.js␊
│ ␊
└─ reasons:␊
└─ lodash␊
└─ ../node_modules/.registry.npmjs.org/lodash/4.17.5/node_modules/lodash/lodash.js 1:0-77 [cjs require]␊
`,
'--------------------',
'',
'',
` FILE ./index.js + 14 modules [entry]␊
├─ imported: 1 time␊
├─ size: 36 KiB␊
Expand All @@ -1433,20 +1472,36 @@ Generated by [AVA](https://ava.li).
'--------------------',
'',
'',
` MODULE bootstrap
` MODULE html-webpack-plugin [entry]
├─ imported: 1 time␊
├─ size: 118 KiB [for all included files]␊
├─ type: [direct]␊
├─ size: 2 KiB [for all included files]␊
├─ type: [transitive]␊
│ ␊
├─ chunks: main␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/bootstrap/␊
│ └─ ../node_modules/.registry.npmjs.org/html-webpack-plugin/␊
│ ␊
└─ files: ␊
│ └─ ../node_modules/.registry.npmjs.org/html-webpack-plugin/3.0.6/node_modules/html-webpack-plugin/lib/loader.js!./index.ejs␊
│ `,
'--------------------',
'',
'',
` MODULE lodash␊
├─ imported: 1 time␊
├─ size: 527 KiB [for all included files]␊
├─ type: [transitive]␊
│ ␊
├─ chunks: main␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/lodash/␊
│ ␊
├─ files: ␊
│ ├─ css ../node_modules/.registry.npmjs.org/css-loader/0.28.10/node_modules/css-loader!../node_modules/.registry.npmjs.org/postcss-loader/2.1.1/node_modules/postcss-loader/lib!../node_modules/.registry.npmjs.org/bootstrap/4.0.0/node_modules/bootstrap/dist/css/bootstrap.min.css␊
│ └─ ../node_modules/.registry.npmjs.org/bootstrap/4.0.0/node_modules/bootstrap/dist/css/bootstrap.min.css␊
│ └─ ../node_modules/.registry.npmjs.org/lodash/4.17.5/node_modules/lodash/lodash.js␊
│ ␊
└─ reasons:␊
└─ ./index.js + 14 modules 3:0-46 [harmony side effect evaluation]␊
└─ html-webpack-plugin␊
└─ ../node_modules/.registry.npmjs.org/html-webpack-plugin/3.0.6/node_modules/html-webpack-plugin/lib/loader.js!./index.ejs 1:8-98 [cjs require]␊
`,
'--------------------',
'',
Expand All @@ -1458,4 +1513,65 @@ Generated by [AVA](https://ava.li).
'--------------------',
'',
'',
]


## should properly print stats.json with nested children second example

> Snapshot 1
[
'',
` MODULE html-webpack-plugin [entry]␊
├─ imported: 1 time␊
├─ size: 2 KiB [for all included files]␊
├─ type: [transitive]␊
│ ␊
├─ chunks: 1␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/html-webpack-plugin/␊
│ ␊
└─ files: ␊
│ └─ ../node_modules/.registry.npmjs.org/html-webpack-plugin/3.2.0/node_modules/html-webpack-plugin/lib/loader.js!./index.ejs␊
│ `,
'--------------------',
'',
'',
` MODULE lodash␊
├─ imported: 1 time␊
├─ size: 527 KiB [for all included files]␊
├─ type: [transitive]␊
│ ␊
├─ chunks: 1␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/lodash/␊
│ ␊
├─ files: ␊
│ └─ ../node_modules/.registry.npmjs.org/lodash/4.17.5/node_modules/lodash/lodash.js␊
│ ␊
└─ reasons:␊
└─ html-webpack-plugin␊
└─ ../node_modules/.registry.npmjs.org/html-webpack-plugin/3.2.0/node_modules/html-webpack-plugin/lib/loader.js!./index.ejs 1:8-100 [cjs require]␊
`,
'--------------------',
'',
'',
` MODULE webpack␊
├─ imported: 1 time␊
├─ size: 1 KiB [for all included files]␊
├─ type: [transitive]␊
│ ␊
├─ chunks: 1␊
├─ locations: ␊
│ └─ ../node_modules/.registry.npmjs.org/webpack/␊
│ ␊
├─ files: ␊
│ └─ ../node_modules/.registry.npmjs.org/webpack/4.5.0/node_modules/webpack/buildin/module.js␊
│ ␊
└─ reasons:␊
└─ lodash␊
└─ ../node_modules/.registry.npmjs.org/lodash/4.17.5/node_modules/lodash/lodash.js 1:0-77 [cjs require]␊
`,
'--------------------',
'',
'',
]
Binary file modified lib/__tests__/__snapshots__/print.js.snap
Binary file not shown.
7 changes: 7 additions & 0 deletions lib/__tests__/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ test("should properly print stats.json with nested children", t => {
print(stats.modules, stats.chunks, {}, 0, logger);
t.snapshot(logger());
});

test("should properly print stats.json with nested children second example", t => {
const stats = analyze(getStats(f.find("nested-children-stats2.json")));
const logger = createPrint();
print(stats.modules, stats.chunks, {}, 0, logger);
t.snapshot(logger());
});
32 changes: 4 additions & 28 deletions lib/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { UpdateProgressBar } from "./console/progress-bar";
export type WebpackStats = {
chunks?: Array<WebpackChunk>,
modules: Array<WebpackModule>,
children?: Array<WebpackStats>
children?: Array<WebpackStats & { id?: string }>
};
export type WebpackChunk = {
Expand Down Expand Up @@ -83,6 +83,7 @@ export type Chunks = {

const path = require("path");
const mm = require("micromatch");
const flattenStats = require("./flatten-stats");

const DEFAULT_IGNORE = ["multi *", "*-loader*"];

Expand All @@ -92,7 +93,7 @@ const toArray = (report /*: { [string]: Module } */) =>
);

const getModuleName = (identifier /*: string */) => {
const path = identifier.split("node_modules/").pop();
const path = (identifier || "").split("node_modules/").pop();
if (!path || path === identifier) return "";
if (path.startsWith("@")) {
const [ns, name] = path.split("/");
Expand Down Expand Up @@ -294,25 +295,6 @@ const postProcessModules = (modules, ignore, updateProgressBar) => {
}, {});
};

function joinStats(children) {
const flattenChildren = (children, id = "0") =>
children.reduce((acc, child, index) => {
child.id = `${id}.${index}`;
acc = acc.concat(child);
acc = acc.concat(flattenChildren(child.children || [], child.id));
}, []);
return children.reduce(
(acc, child) => {
acc.chunks = child.chunks ? acc.chunks.concat(child.chunks) : acc.chunks;
acc.modules = child.modules
? acc.modules.concat(child.modules)
: acc.modules;
return acc;
},
{ chunks: [], modules: [] }
);
}

function getChunksData(stats /*: WebpackStats */) {
return stats.chunks
? stats.chunks.reduce((acc, chunk) => {
Expand All @@ -331,13 +313,7 @@ module.exports = function analyze(
ignore /*: Array<string> */ = [],
updateProgressBar /*: UpdateProgressBar */ = () => {}
) {
const stats =
rawStats.children &&
rawStats.children.length &&
(!rawStats.chunks || !rawStats.chunks.length)
? joinStats(rawStats.children)
: rawStats;

const stats = flattenStats(rawStats);
const chunks = getChunksData(stats);
const rawModules = flattenChunks(stats);
const ignorePatterns = [].concat(DEFAULT_IGNORE).concat(ignore);
Expand Down
37 changes: 37 additions & 0 deletions lib/flatten-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* @flow */

/*::
import type { WebpackStats } from "./analyze";
*/

module.exports = function flattenStats(stats /*: WebpackStats */) {
if (
!stats.children ||
!stats.children.length ||
(stats.chunks && stats.chunks.length)
) {
return stats;
}

const flattenChildren = (children, id = "0") =>
children.reduce((acc, child, index) => {
child.id = `${id}.${index}`;
acc = acc.concat(child);
acc = acc.concat(flattenChildren(child.children || [], child.id));
return acc;
}, []);

const children = flattenChildren(stats.children);

return children.reduce(
(acc, child) => {
acc.chunks = child.chunks ? acc.chunks.concat(child.chunks) : acc.chunks;
acc.modules = child.modules
? acc.modules.concat(child.modules)
: acc.modules;
return acc;
},
{ chunks: [], modules: [] }
);
};
21 changes: 13 additions & 8 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import type { WebpackStats } from "./analyze";
*/

const flattenStats = require("./flatten-stats");

function has(obj) {
return function(prop) {
return obj.hasOwnProperty(prop);
Expand All @@ -16,21 +18,24 @@ function isValidModule(sample) {
return true;
}

module.exports = function vlidateStatsJson(stats /*: WebpackStats */) {
module.exports = function vlidateStatsJson(rawStats /*: WebpackStats */) {
if (
!stats ||
((!stats.chunks || !stats.chunks[0].modules) &&
!stats.modules &&
(!stats.children ||
((!stats.children[0].chunks || !stats.children[0].chunks[0].modules) &&
!stats.children[0].modules)))
!rawStats ||
((!rawStats.chunks || !rawStats.chunks || !rawStats.chunks[0].modules) &&
!rawStats.modules &&
(!rawStats.children ||
((!rawStats.children[0].chunks ||
!rawStats.children[0].chunks[0] ||
!rawStats.children[0].chunks[0].modules) &&
!rawStats.children[0].modules)))
) {
return false;
}

const stats = flattenStats(rawStats);
let modules = [];

if (stats.modules) {
if (stats.modules && stats.modules.length) {
modules = stats.modules;
} else if (stats.chunks) {
modules = stats.chunks[0].modules;
Expand Down

0 comments on commit a599ce1

Please sign in to comment.