8000 refactor: show dist info in cli output · unjs/obuild@a6d475b · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit a6d475b

Browse files
committed
refactor: show dist info in cli output
1 parent 4170d54 commit a6d475b

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

src/build.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export async function build(
2222
const pkg = await readJSON(join(pkgDir, "package.json")).catch(() => ({}));
2323
const ctx: BuildContext = { pkg, pkgDir };
2424

25-
consola.info(
26-
`Building \`${ctx.pkg.name || "<no name>"}\` (in \`${ctx.pkgDir}\`)...`,
25+
consola.log(
26+
`📦 Building \`${ctx.pkg.name || "<no name>"}\` (\`${ctx.pkgDir}\`)`,
2727
);
2828

2929
await hooks.start?.(ctx);
@@ -51,7 +51,7 @@ export async function build(
5151
}
5252
}
5353
for (const outDir of outDirs) {
54-
consola.log(`Cleaning up \`${fmtPath(outDir)}\``);
54+
consola.log(`🧻 Cleaning up \`${fmtPath(outDir)}\``);
5555
await rm(outDir, { recursive: true, force: true });
5656
}
5757

@@ -63,7 +63,7 @@ export async function build(
6363

6464
await hooks.end?.(ctx);
6565

66-
consola.log(`Build finished in ${Date.now() - start}ms`);
66+
consola.log(`\n✅ obuild finished in ${Date.now() - start}ms`);
6767
}
6868

6969
// --- utils ---

src/builders/bundle.ts

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { builtinModules } from "node:module";
22
import { dirname, relative, join, basename, extname, resolve } from "node:path";
33
import { consola } from "consola";
4+
import { colors as c } from "consola/utils";
45
import { rolldown } from "rolldown";
56
import { dts } from "rolldown-plugin-dts";
67
import { fmtPath } from "../utils.ts";
78
import { resolveModulePath } from "exsolve";
89

9-
import type { Plugin } from "rolldown";
10+
import type { OutputChunk, Plugin } from "rolldown";
1011
import type { BuildContext, 8000 BuildHooks, BundleEntry } from "../types.ts";
1112
import type { InputOptions, OutputOptions } from "rolldown";
1213

@@ -15,8 +16,6 @@ export async function rolldownBuild(
1516
entry: BundleEntry,
1617
hooks: BuildHooks,
1718
): Promise<void> {
18-
const start = Date.now();
19-
2019
const inputs: Record<string, string> = {};
2120

2221
for (let src of Array.isArray(entry.input) ? entry.input : [entry.input]) {
@@ -70,8 +69,10 @@ export async function rolldownBuild(
7069

7170
const res = await rolldown(rolldownConfig);
7271

72+
const outDir = resolve(ctx.pkgDir, entry.outDir || "dist");
73+
7374
const outConfig: OutputOptions = {
74-
dir: entry.outDir,
75+
dir: outDir,
7576
entryFileNames: "[name].mjs",
7677
chunkFileNames: "_chunks/[name]-[hash].mjs",
7778
minify: entry.minify,
@@ -83,15 +84,67 @@ export async function rolldownBuild(
8384

8485
await res.close();
8586

87+
const outputEntries: {
88+
name: string;
89+
exports: string[];
90+
deps: string[];
91+
size: number;
92+
}[] = [];
93+
94+
const depsCache = new Map<OutputChunk, Set<string>>();
95+
const resolveDeps = (chunk: OutputChunk) => {
96+
if (!depsCache.has(chunk)) {
97+
depsCache.set(chunk, new Set<string>());
98+
}
99+
const deps = depsCache.get(chunk)!;
100+
for (const id of chunk.imports) {
101+
if (builtinModules.includes(id) || id.startsWith("node:")) {
102+
deps.add(`[Node.js]`);
103+
continue;
104+
}
105+
const depChunk = output.find(
106+
(o) => o.type === "chunk" && o.fileName === id,
107+
) as OutputChunk | undefined;
108+
if (depChunk) {
109+
for (const dep of resolveDeps(depChunk)) {
110+
deps.add(dep);
111+
}
112+
continue;
113+
}
114+
deps.add(id);
115+
}
116+
return [...deps].sort();
117+
};
118+
119+
for (const chunk of output) {
120+
if (chunk.type !== "chunk" || !chunk.isEntry) continue;
121+
if (chunk.fileName.endsWith("ts")) continue;
122+
outputEntries.push({
123+
name: chunk.fileName,
124+
exports: chunk.exports,
125+
deps: resolveDeps(chunk),
126+
size: chunk.code.length,
127+
});
128+
}
129+
86130
consola.log(
87-
`📦 Bundled in ${Date.now() - start}ms:\n${output
88-
.filter(
89-
(o) => o.type === "chunk" && o.isEntry && o.fileName.endsWith("js"),
90-
)
91-
.map(
92-
(o) =>
93-
` - ${fmtPath(resolve(ctx.pkgDir, entry.outDir || "dist", o.fileName))}`,
131+
`\n${outputEntries
132+
.map((o) =>
133+
[
134+
c.magenta(`[bundle] `) +
135+
`${c.underline(fmtPath(join(outDir, o.name)))}`,
136+
o.exports.length > 0
137+
? c.dim(
138+
`${c.bold("Exports:")} ${o.exports.map((e) => e).join(", ")}`,
139+
)
140+
: "",
141+
o.deps.length > 0
142+
? c.dim(`${c.bold("Dependencies:")} ${o.deps.join(", ")}`)
143+
: "",
144+
]
145+
.filter(Boolean)
146+
.join("\n"),
94147
)
95-
.join("\n")} `,
148+
.join("\n\n")}`,
96149
);
97150
}

src/builders/transform.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { pathToFileURL } from "node:url";
66
import { dirname, extname, join, relative } from "node:path";
77
import { mkdir, readFile, writeFile } from "node:fs/promises";
88
import { consola } from "consola";
9+
import { colors as c } from "consola/utils";
910
import { resolveModulePath } from "exsolve";
1011
import MagicString from "magic-string";
1112
import oxcTransform from "oxc-transform";
@@ -21,9 +22,7 @@ export async function transformDir(
2122
ctx: BuildContext,
2223
entry: TransformEntry,
2324
): Promise<void> {
24-
const start = Date.now();
25-
26-
const promises: Promise<void>[] = [];
25+
const promises: Promise<string>[] = [];
2726

2827
for await (const entryName of await glob("**/*.*", { cwd: entry.input })) {
2928
promises.push(
@@ -51,26 +50,28 @@ export async function transformDir(
5150
"utf8",
5251
);
5352
}
53+
return entryDistPath;
5454
}
55-
break;
5655
}
5756
default: {
5857
{
5958
const entryDistPath = join(entry.outDir!, entryName);
6059
await mkdir(dirname(entryDistPath), { recursive: true });
6160
await writeFile(entryDistPath, await readFile(entryPath), "utf8");
61+
return entryDistPath;
6262
}
63-
break;
6463
}
6564
}
6665
})(),
6766
);
6867
}
6968

70-
await Promise.all(promises);
69+
const writtenFiles = await Promise.all(promises);
7170

7271
consola.log(
73-
`Transformed ${promises.length} files from \`${fmtPath(entry.input!)}\` to \`${fmtPath(entry.outDir!)}\` in ${Date.now() - start}ms`,
72+
`\n${c.magenta("[transform] ")}${c.underline(fmtPath(entry.outDir!) + "/")}\n${writtenFiles
73+
.map((f) => c.dim(fmtPath(f)))
74+
.join("\n\n")}`,
7475
);
7576
}
7677

0 commit comments

Comments
 (0)
0