Skip to content

Commit

Permalink
refactor(rsbuild-ssr): simplify client asset stats (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Jul 28, 2024
1 parent fc41633 commit 81699e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
30 changes: 28 additions & 2 deletions rsbuild-ssr/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig, type RequestHandler } from "@rsbuild/core";
import { defineConfig, type RequestHandler, type Rspack } from "@rsbuild/core";
import { pluginReact } from "@rsbuild/plugin-react";
import { webToNodeHandler } from "@hiogawa/utils-node";
import { mkdirSync, writeFileSync } from "fs";

export default defineConfig((env) => ({
plugins: [pluginReact()],
Expand Down Expand Up @@ -29,6 +30,26 @@ export default defineConfig((env) => ({
}
: undefined,
},
tools: {
rspack: {
plugins: [
{
name: "client-assets",
apply(compiler: Rspack.Compiler) {
if (env.command !== "build") return;

const NAME = this.name;
compiler.hooks.done.tap(NAME, (stats) => {
const statsJson = stats.toJson({ all: false, assets: true });
const code = `export default ${JSON.stringify(statsJson, null, 2)}`;
mkdirSync("./dist", { recursive: true });
writeFileSync("./dist/__client_stats.mjs", code);
});
},
},
],
},
},
},
ssr: {
output: {
Expand All @@ -37,7 +58,7 @@ export default defineConfig((env) => ({
root: "dist/server",
},
filename: {
js: "index.cjs",
js: "[name].cjs",
},
minify: false,
},
Expand All @@ -50,6 +71,11 @@ export default defineConfig((env) => ({
"import.meta.env.SSR": true,
},
},
tools: {
rspack: {
dependencies: ["web"],
},
},
},
},
// https://rsbuild.dev/config/dev/setup-middlewares
Expand Down
3 changes: 1 addition & 2 deletions rsbuild-ssr/src/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ async function getStatsJson(): Promise<Rspack.StatsCompilation> {
return stats.toJson();
} else {
const { default: statsJson } = await import(
/* webpackIgnore: true */ "../client/stats.json" as string,
{ with: { type: "json" } }
/* webpackMode: "eager" */ "../dist/__client_stats.mjs" as string
);
return statsJson;
}
Expand Down

0 comments on commit 81699e1

Please sign in to comment.