React Compiler & StrictMode
Forget about manual useMemo, useCallback, and React.memo. Dinou pre-bundles and activates babel-plugin-react-compiler out of the box for production builds, paired with reactStrictMode in development to guarantee deterministic parity.
Overview: Automatic Memoization
Dinou ships with the official React Compiler pre-compiled inside its internal vendor toolchain—meaning you do not need to install babel-plugin-react-compiler or @babel/* packages in your project, even under strict pnpm workspaces. At production build time, the compiler analyzes data flow and mutability to automatically memoize derived values, JSX subtrees, and callback references.
react/compiler-runtime) at the statement and conditional branch level—something impossible with manual useMemo calls.Dual-Engine Compilation Scope
In Dinou's Server & Client Component architecture, the React Compiler targets the exact modules where reactive re-renders occur:
- Client Component Subgraphs: Compiles root
"use client"entry files, all imported child React components (even when child files do not repeat"use client"), and custom client hooks (use*). - Server Functions Isolation: Modules marked with
"use server"(module-level or inline) are converted into lightweight RPC proxies before the React Compiler runs, ensuring server code is never analyzed as client UI or leaked into browser chunks. - Server Components & Pre-Compiled Packages: Server Components execute once per request or static cache pass on the server and never re-render on client state updates, so they skip Babel compilation for maximum build speed. At the same time, Dinou's RSC server dispatcher implements React 19's
useMemoCachehook so third-party libraries pre-compiled with the React Compiler execute seamlessly during server rendering.
1. Code Comparison: Plain React vs Hooks
With the React Compiler active, write components and custom hooks naturally. Whether OrderSummary has its own "use client" directive or is imported as a child module inside a client boundary, Dinou compiles it automatically:
// ❌ Legacy React: Manual Memoization Boilerplate
function OrderSummary({ items, filter }: { items: Item[]; filter: string }) {
// Manual memoization required to avoid recalculations on every render
const filtered = useMemo(() => {
return items.filter((item) => item.category === filter);
}, [items, filter]);
// Manual callback to prevent breaking child component memoization
const handleSelect = useCallback((id: string) => {
trackSelection(id);
}, []);
return <OrderList items={filtered} onSelect={handleSelect} />;
}
// ✅ Dinou: Plain Idiomatic React (React Compiler Active)
function OrderSummary({ items, filter }: { items: Item[]; filter: string }) {
// ✨ Automatically memoized by the React Compiler
const filtered = items.filter((item) => item.category === filter);
// ✨ Function reference automatically stabilized
const handleSelect = (id: string) => {
trackSelection(id);
};
return <OrderList items={filtered} onSelect={handleSelect} />;
}2. Bundler Integration Matrix
Across all three supported Dinou bundlers, Dinou uses a unified two-tier compilation strategy that balances instantaneous development HMR with maximum production runtime performance.
Pre-compiles JSX/TS and Fast Refresh via a Rust SWC disk cache (.dinou/swc) and bundles natively with Esbuild in dev for sub-millisecond HMR; runs Babel + React Compiler across the full client graph in production.
Transpiles via Rust-based SWC in dev for fast rebuilds; switches to Babel + React Compiler for production bundles.
Uses Dinou's custom swc-loader in dev to eliminate multi-second rebuilds; activates babel-loader + React Compiler in production.
2.1. Why Dev vs Prod Behavior Differs
The React Compiler performs whole-function control-flow graph (CFG) analysis and Abstract Syntax Tree (AST) lowering through Babel.
- In Development (Esbuild, Rollup & Webpack): Running full Babel AST compilation on every keystroke slows down Hot Module Replacement (HMR). Dinou uses Rust SWC transforms (paired with Esbuild, Rollup, or Webpack bundling) during
dinou devso edits reflect in the browser instantaneously. - In Production (
dinou build): All three bundlers runbabel-plugin-react-compileracross your client module graph, emitting finely memoized production chunks with zero manual hook overhead.
3. StrictMode & Compiler Configuration
Defaults: trueDinou enables both reactStrictMode: true (in development only—automatically omitted in production builds with zero runtime overhead) and reactCompiler: true (in production) by default.
3.1. Why StrictMode is Vital for the React Compiler
Because all three bundlers bypass Babel in development for instant HMR, Dinou relies on reactStrictMode to ensure that what works in development behaves identically when compiled in production.
The React Compiler assumes your components follow the Rules of React—specifically, that rendering is pure and free of side effects.
When reactStrictMode is enabled in development, React intentionally double-invokes component renders, initializer functions, updater callbacks, and effect setup/cleanup cycles:
- Exposes Impure Renders Immediately: If a component mutates external variables or props during render, double invocation surfaces the bug right away in
dinou dev. - Guarantees Production Safety: Code that passes StrictMode in development is safe to memoize. When
dinou buildenables the React Compiler across Esbuild, Rollup, or Webpack, memoized renders produce the exact same UI output.
3.2. Configuring dinou.config.ts
Both reactCompiler and reactStrictMode can be configured globally in dinou.config.ts (or .mjs / .js / .cjs) using defineConfig from "dinou/config" (also re-exported from "dinou"):
import { defineConfig } from "dinou/config";
export default defineConfig({
/**
* Enable or disable babel-plugin-react-compiler during production builds
* across Esbuild, Rollup, and Webpack.
* @default true
*/
reactCompiler: true,
/**
* Enable or disable React <StrictMode> in development mode.
* Double-invokes render and effect lifecycles in dev to surface side effects.
* @default true
*/
reactStrictMode: true,
});import { defineConfig } from "dinou/config";
export default defineConfig({
reactCompiler: true,
reactStrictMode: true,
});4. Guidelines for Compiler Purity
To get the maximum benefit from the React Compiler and StrictMode in Dinou, follow these core principles:
Given the same inputs (props, state, context), a component must always return equivalent JSX. Never mutate module-scope variables, refs (ref.current), or input arguments during render.
Never mutate props.user.name = "new" or sort prop arrays in place (items.sort()). Create fresh copies instead (e.g. { ...user, name: "new" } or [...items].sort()). Locally created objects and arrays inside render can be mutated freely before being returned.
useEffect, or Server FunctionsTimers, DOM subscriptions, and data mutations belong inside event handlers, useEffect, or Server Functions—never directly in the render body.
4.1. Local Opt-Out with "use no memo"
If a specific legacy component or custom hook relies on a third-party library that mutates values incompatibly with memoization, you do not need to set reactCompiler: false globally. Place the "use no memo" directive at the top of that function body to skip compilation locally while keeping the rest of your app optimized:
"use client";
export function LegacyChartAdapter({ mutableConfig }: { mutableConfig: Record<string, unknown> }) {
"use no memo"; // 👈 Skips React Compiler for this component only
return <div data-mode={String(mutableConfig.mode ?? "default")} />;
}Incremental Adoption Tip
"use no memo" on individual components while refactoring legacy code. Reserve reactCompiler: false in dinou.config.ts only when diagnosing application-wide migration issues.