Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rollup): add support for generating build meta #296

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Stub `dist` once using [jiti](https://github.com/unjs/jiti) and you can try and

Integration with [untyped](https://github.com/unjs/untyped).

### πŸ“Š Analyze bundle size

Generate rollup build metadata in JSON format to visualize with [Bundle Buddy](https://www.bundle-buddy.com).

### βœ”οΈ Secure builds

Automatically check for various build issues such as potential **missing** and **unused** [dependencies](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#dependencies) and fail CI.
Expand Down
1 change: 1 addition & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async function _build(
replace: {},
failOnWarn: true,
sourcemap: false,
metafile: false,
rollup: {
emitCJS: false,
cjsBridge: false,
Expand Down
41 changes: 41 additions & 0 deletions src/builder/plugins/metafile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { writeFile } from "node:fs/promises";
import type { Plugin } from "rollup";
import { resolve } from "pathe";

export interface MetafileOptions {
rootDir: string;
outDir: string;
}

export interface MetaInfo {
source: string;
target: string;
}

export function metafilePlugin(opts: MetafileOptions): Plugin {
return {
name: "unbuild:metafile",
async buildEnd() {
const deps: MetaInfo[] = [];

for (const id of this.getModuleIds()) {
const m = this.getModuleInfo(id);
if (m != null && !m.isExternal) {
for (const target of m.importedIds) {
deps.push({
source: id,
target,
});
}
}
}

if (Array.isArray(deps) && deps.length === 0) {
return;
}

const outPath = resolve(opts.outDir, `graph.json`);
await writeFile(outPath, JSON.stringify(deps, null, 2), "utf8");
},
} as Plugin;
}
11 changes: 11 additions & 0 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { arrayIncludes, getpkg, tryResolve, warn } from "../utils";
import type { BuildContext } from "../types";
import { esbuild } from "./plugins/esbuild";
import { JSONPlugin } from "./plugins/json";
import { metafilePlugin } from "./plugins/metafile";
import { rawPlugin } from "./plugins/raw";
import { cjsPlugin } from "./plugins/cjs";
import { shebangPlugin, makeExecutable, getShebang } from "./plugins/shebang";
Expand Down Expand Up @@ -187,6 +188,10 @@ export async function rollupBuild(ctx: BuildContext) {

rollupOptions.plugins.push(dts(ctx.options.rollup.dts));

rollupOptions.plugins = rollupOptions.plugins.filter(
(p) => p.name !== "unbuild:metafile",
);

await ctx.hooks.callHook("rollup:dts:options", ctx, rollupOptions);
const typesBuild = await rollup(rollupOptions);
await ctx.hooks.callHook("rollup:dts:build", ctx, typesBuild);
Expand Down Expand Up @@ -333,6 +338,12 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
...ctx.options.rollup.json,
}),

ctx.options.metafile &&
metafilePlugin({
rootDir: ctx.options.rootDir,
outDir: resolve(ctx.options.rootDir, ctx.options.outDir),
}),

shebangPlugin(),

ctx.options.rollup.esbuild &&
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ const main = defineCommand({
type: "boolean",
description: "Generate sourcemaps (experimental)",
},
metafile: {
type: "boolean",
description: "Generate meta file (experimental)",
},
},
async run({ args }) {
const rootDir = resolve(process.cwd(), args.dir || ".");
await build(rootDir, args.stub, {
sourcemap: args.sourcemap,
metafile: args.metafile,
rollup: {
esbuild: {
minify: args.minify,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface BuildOptions {
clean: boolean;
/** @experimental */
sourcemap: boolean;
/** @experimental */
metafile: boolean;
/**
* * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts".
* * `node16` means "src/index.ts" will generate "dist/index.d.mts" and "dist/index.d.cts".
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ export default defineBuildConfig([
},
},
},
// With sourcemaps and metafile
{
name: "metafile",
entries: ["src/index"],
outDir: "dist",
sourcemap: true,
metafile: true,
},
]);