Skip to content

Commit

Permalink
feat(dev): output esbuild metafiles for bundle analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Jul 5, 2023
1 parent 9762111 commit 312a8dc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .changeset/red-bananas-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@remix-run/dev": minor
---

Output esbuild metafiles for bundle analysis

Written to server build directory (`build/` by default):
- `metafile.css.json`
- `metafile.js.json` (browser JS)
- `metafile.server.json` (server JS)

Metafiles can be uploaded to https://esbuild.github.io/analyze/ for analysis.
17 changes: 17 additions & 0 deletions packages/remix-dev/compiler/analysis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from "node:fs";
import path from "node:path";
import type { Metafile } from "esbuild";

import type { Context } from "./context";

export let writeMetafile = (
ctx: Context,
filename: string,
metafile: Metafile
) => {
let buildDir = path.dirname(ctx.config.serverBuildPath);
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir, { recursive: true });
}
fs.writeFileSync(path.join(buildDir, filename), JSON.stringify(metafile));
};
5 changes: 4 additions & 1 deletion packages/remix-dev/compiler/css/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./plugins/bundleEntry";
import type { Context } from "../context";
import { isBundle } from "./bundle";
import { writeMetafile } from "../analysis";

const getExternals = (config: RemixConfig): string[] => {
// For the browser build, exclude node built-ins that don't have a
Expand Down Expand Up @@ -96,9 +97,11 @@ export let create = async (ctx: Context) => {
let compiler = await esbuild.context({
...createEsbuildConfig(ctx),
write: false,
metafile: true,
});
let compile = async () => {
let { outputFiles } = await compiler.rebuild();
let { outputFiles, metafile } = await compiler.rebuild();
writeMetafile(ctx, "metafile.css.json", metafile);
let bundleOutputFile = outputFiles.find((outputFile) =>
isBundle(ctx, outputFile, ".css")
);
Expand Down
2 changes: 2 additions & 0 deletions packages/remix-dev/compiler/js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import invariant from "../../invariant";
import { hmrPlugin } from "./plugins/hmr";
import type { LazyValue } from "../lazyValue";
import type { Context } from "../context";
import { writeMetafile } from "../analysis";

type Compiler = {
// produce ./public/build/
Expand Down Expand Up @@ -164,6 +165,7 @@ export const create = async (

let compile = async () => {
let { metafile } = await compiler.rebuild();
writeMetafile(ctx, "metafile.js.json", metafile);

let hmr: Manifest["hmr"] | undefined = undefined;
if (ctx.options.mode === "development" && ctx.config.future.v2_dev) {
Expand Down
5 changes: 4 additions & 1 deletion packages/remix-dev/compiler/server/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type * as Channel from "../../channel";
import type { Context } from "../context";
import type { LazyValue } from "../lazyValue";
import { cssBundlePlugin } from "../plugins/cssBundlePlugin";
import { writeMetafile } from "../analysis";

type Compiler = {
// produce ./build/index.js
Expand Down Expand Up @@ -132,9 +133,11 @@ export const create = async (
let compiler = await esbuild.context({
...createEsbuildConfig(ctx, refs),
write: false,
metafile: true,
});
let compile = async () => {
let { outputFiles } = await compiler.rebuild();
let { outputFiles, metafile } = await compiler.rebuild();
writeMetafile(ctx, "metafile.server.json", metafile);
return outputFiles;
};
return {
Expand Down

0 comments on commit 312a8dc

Please sign in to comment.