Skip to content

Commit

Permalink
build.js: support outputting esbuild metadata file
Browse files Browse the repository at this point in the history
ESbuild supports outputting a metadata file which can be used to view
bundle size information using https://esbuild.github.io/analyze/
  • Loading branch information
jelly committed Jun 16, 2024
1 parent bcb9cc8 commit 49c3688
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const esbuild = (await import(useWasm ? 'esbuild-wasm' : 'esbuild')).default;
const parser = (await import('argparse')).default.ArgumentParser();
parser.add_argument('-r', '--rsync', { help: "rsync bundles to ssh target after build", metavar: "HOST" });
parser.add_argument('-w', '--watch', { action: 'store_true', help: "Enable watch mode", default: process.env.ESBUILD_WATCH === "true" });
parser.add_argument('-m', '--metafile', { help: "Enable bundle size information file", metavar: "FILE" });
const args = parser.parse_args();

if (args.rsync)
Expand Down Expand Up @@ -82,6 +83,7 @@ const context = await esbuild.context({
external: ['*.woff', '*.woff2', '*.jpg', '*.svg', '../../assets*'], // Allow external font files which live in ../../static/fonts
legalComments: 'external', // Move all legal comments to a .LEGAL.txt file
loader: { ".js": "jsx" },
metafile: !!args.metafile,
minify: production,
nodePaths,
outdir,
Expand All @@ -105,7 +107,10 @@ const context = await esbuild.context({
});

try {
await context.rebuild();
const result = await context.rebuild();
if (args.metafile) {
fs.writeFileSync(args.metafile, JSON.stringify(result.metafile));
}
} catch (e) {
if (!args.watch)
process.exit(1);
Expand Down

0 comments on commit 49c3688

Please sign in to comment.