Skip to content

Commit

Permalink
feat(rollup): show size of bundled npm packages in cli output (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Mar 31, 2023
1 parent 8e81e2a commit 9039fe4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,8 @@ export async function build(
let line =
` ${chalk.bold(rPath(entry.path))} (` +
[
entry.bytes && `size: ${chalk.cyan(prettyBytes(entry.bytes))}`,
totalBytes !== entry.bytes &&
`total size: ${chalk.cyan(prettyBytes(totalBytes))}`,
totalBytes && `total size: ${chalk.cyan(prettyBytes(totalBytes))}`,
entry.bytes && `chunk size: ${chalk.cyan(prettyBytes(entry.bytes))}`,
entry.exports?.length &&
`exports: ${chalk.gray(entry.exports.join(", "))}`,
]
Expand All @@ -243,7 +242,22 @@ export async function build(
return chalk.gray(
" └─ " +
rPath(p) +
(chunk.bytes ? ` (${prettyBytes(chunk?.bytes)})` : "")
chalk.bold(chunk.bytes ? ` (${prettyBytes(chunk?.bytes)})` : "")
);
})
.join("\n");
}
if (entry.modules?.length) {
line +=
"\n" +
entry.modules
.filter((m) => m.id.includes("node_modules"))
.sort((a, b) => (b.bytes || 0) - (a.bytes || 0))
.map((m) => {
return chalk.gray(
" 📦 " +
rPath(m.id) +
chalk.bold(m.bytes ? ` (${prettyBytes(m.bytes)})` : "")
);
})
.join("\n");
Expand Down
4 changes: 4 additions & 0 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export async function rollupBuild(ctx: BuildContext) {
chunks: entry.imports.filter((i) =>
outputChunks.find((c) => c.fileName === i)
),
modules: Object.entries(entry.modules).map(([id, mod]) => ({
id,
bytes: mod.renderedLength,
})),
path: entry.fileName,
bytes: Buffer.byteLength(entry.code, "utf8"),
exports: entry.exports,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface BuildContext {
exports?: string[];
chunks?: string[];
chunk?: boolean;
modules?: { id: string; bytes: number }[];
}[];
usedImports: Set<string>;
warnings: Set<string>;
Expand Down

0 comments on commit 9039fe4

Please sign in to comment.