Repository navigation
How to create plugin for bun-bundler to extract chunks of output in webpack-stats.json format #23138
Replies: 2 comments
|
Hi! I think I can explain why your approach isn't working, and there is a supported way to get the graph you want. Why traversing The interface BuildArtifact extends Blob {
path: string;
loader: Loader;
hash: string | null;
kind: "entry-point" | "chunk" | "asset" | "sourcemap" | "bytecode";
sourcemap: BuildArtifact | null;
}So following Use the
In other words, the entry → chunk edges are const result = await Bun.build({
entrypoints: ["./src/main.ts"],
outdir: "./dist",
splitting: true,
metafile: true, // <-- this is the key
});
if (!result.success) {
console.error(result.logs);
process.exit(1);
}
const metafile = typeof result.metafile === "string"
? JSON.parse(result.metafile)
: result.metafile;
// Group chunks by entry point, following the metafile's import edges
const chunks: Record<string, string[]> = {};
for (const [outputPath, output] of Object.entries(metafile.outputs)) {
if (output.entryPoint) {
const entryName = output.entryPoint
.split("/")
.pop()!
.replace(/\.(ts|tsx|js|jsx)$/, "");
chunks[entryName] = [outputPath];
const queue = output.imports.map((i) => i.path);
const seen = new Set(queue);
while (queue.length > 0) {
const chunkPath = queue.shift()!;
chunks[entryName].push(chunkPath);
for (const imp of metafile.outputs[chunkPath]?.imports ?? []) {
if (!seen.has(imp.path)) {
seen.add(imp.path);
queue.push(imp.path);
}
}
}
}
}
const stats = {
status: "done",
publicPath: "/static/dist/",
chunks,
};
await Bun.write("./dist/webpack-stats.json", JSON.stringify(stats, null, 2));A few notes on the sketch above:
One caveat: the metafile only exists when Hope that unblocks your References: |
|
Use the metafile, not a loader plugin. On recent Bun versions, import type { BunPlugin } from "bun";
const base = (p: string) => p.split(/[\\/]/).pop()!;
export const statsPlugin: BunPlugin = {
name: "webpack-stats"
setup(build) {
build.onEnd(async ({ success, metafile }) => {
if (!success || !metafile) return;
const outputs = metafile.outputs;
const visit = (file: string, seen = new Set<string>()): string[] => {
if (seen.has(file) || !outputs[file]) return [];
seen.add(file);
return [
...(outputs[file].imports ?? [])
.filter(i => i.kind !== "dynamic-import")
.flatMap(i => visit(i.path, seen))
file
];
};
const chunks: Record<string, string[]> = {};
for (const [file, out] of Object.entries(outputs)) {
if (!out.entryPoint || !file.endsWith(".js")) continue;
const name = base(out.entryPoint).replace(/\.[^.]+$/, "");
chunks[name] = visit(file).filter(f => f.endsWith(".js")).map(base);
}
await Bun.write(`${build.config.outdir ?? "."}/webpack-stats.json`, JSON.stringify({ chunks }));
});
}
};Build with — |
Uh oh!
There was an error while loading. Please reload this page.
Hi, I need to make stats.json for my website, to use generated outputs and chunks in template generator. I was using webpack for create webpack-stats.json which has following format :
so I can then use this data to generate <script> tags by using somethis like this
render_bundle("main")I've created plugin for that, but stuck with this and need help:
I was expecting to take all entry artifacts (kind == 'entry-point')
and traverse them down following sourcemap field
but bundler happens to not be forming graph of relations I expected.
So, my question is, if it's there any way to get the relations graph.
All reactions