Skip to content

Commit

Permalink
feat: Output module and file sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Apr 7, 2018
1 parent d263536 commit e4545da
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/__tests__/__snapshots__/print.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Generated by [AVA](https://ava.li).
'',
` MODULE react-tap-event-plugin␊
├─ imported: 1 time␊
├─ size: 1 KiB [for all included files]␊
├─ type: [direct]␊
├─ locations: ␊
│ └─ delegated ./node_modules/react-tap-event-plugin/␊
Expand Down
Binary file modified lib/__tests__/__snapshots__/print.js.snap
Binary file not shown.
11 changes: 10 additions & 1 deletion lib/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type PreModule = {
export type Module = {
name: string,
size: number,
clearName: string,
imported: number,
type: string,
Expand Down Expand Up @@ -106,11 +107,14 @@ const flattenChunks = (stats /*: WebpackStats */) /*: Array<WebpackModule> */ =>
const isNodeModules = (identifier /*: string */) =>
identifier.indexOf("node_modules") > -1;

const safeModuleSize = (size /*?: number */) => (size ? size : 0);

const joinModules = (modules) /*: { [string]: Module } */ =>
modules.reduce((acc, module /*: PreModule */) => {
if (module.type === "file") {
const joined = acc[module.name] || {
type: module.type,
size: safeModuleSize(module.size),
imported: 0,
reasons: [],
depsChains: []
Expand All @@ -131,6 +135,7 @@ const joinModules = (modules) /*: { [string]: Module } */ =>
const joined = acc[module.clearName] || {
imported: 0,
type: module.type,
size: 0,
depsType: "unknown",
reasons: [],
locations: [],
Expand Down Expand Up @@ -193,6 +198,7 @@ const joinModules = (modules) /*: { [string]: Module } */ =>
: "transitive";
joined.locations.push(module.location);
joined.filesIncluded.push(module.name);
joined.size += safeModuleSize(module.size);

acc[module.clearName] = joined;
}
Expand Down Expand Up @@ -288,7 +294,10 @@ module.exports = function analyze(
ignore /*: Array<string> */ = [],
updateProgressBar /*: UpdateProgressBar */ = () => {}
) {
const stats = rawStats.children ? joinStats(rawStats.children) : rawStats;
const stats =
rawStats.children && rawStats.children.length
? joinStats(rawStats.children)
: rawStats;
const rawModules = flattenChunks(stats);
const ignorePatterns = [].concat(DEFAULT_IGNORE).concat(ignore);
const modules = pickFromModules(rawModules);
Expand Down
15 changes: 15 additions & 0 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,24 @@ const printType = (module, by, limit) => {
return type;
};

const printSize = (size /*: number */, mod = false) => {
const sizeInKiB = Math.ceil(size / 1024);
const level =
sizeInKiB > 20
? "red"
: sizeInKiB < 20 && sizeInKiB > 10 ? "yellow" : "default";
const sizeFormatted =
level === "default" ? sizeInKiB + " KiB" : chalk[level](sizeInKiB + " KiB");
return `${chalk.magenta("size")}: ${sizeFormatted}${
mod ? chalk.dim(" [for all included files]") : ""
}`;
};

const printFile = (module /*: Module */, limit, by) => {
return [
`${fileBadge()} ${chalk.green(module.name)}`,
`├─ ${chalk.magenta("imported")}: ${printImportedCount(module.imported)}`,
`├─ ${printSize(module.size)}`,
`└─ ${chalk.magenta("reasons")}:`,
indent(printReasons(module.reasons, limit, by), " ").join("\n")
];
Expand All @@ -167,6 +181,7 @@ const printModule = (module /*: Module */, limit, by) => {
return [
`${moduleBadge()} ${chalk.yellow(module.name)}`,
`├─ ${chalk.magenta("imported")}: ${printImportedCount(module.imported)}`,
`├─ ${printSize(module.size, true)}`,
printType(module, by, limit).join("\n"),
`├─ ${chalk.magenta("locations")}: ${
module.locations.length > 1 ? redBadge("multiple") : ""
Expand Down

0 comments on commit e4545da

Please sign in to comment.