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(dev): output esbuild metafiles for bundle analysis #6772

Merged
merged 1 commit into from
Jul 5, 2023
Merged
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
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