Next.js Compiler and Turbopack
Understand how Next.js builds your code with Turbopack, SWC compiler options, and the React Compiler.
TL;DR
- Run
next devandnext build, which both use Turbopack by default. - Tune compile-time transforms with the
compileroption innext.config.ts. - Enable automatic memoization by setting
reactCompiler: truein your config.
Turbopack
next devUse the plain commands, since Turbopack is already the default bundler.
// package.json
{
"scripts": {
"dev": "next dev",
"build": "next build"
}
}--webpackOpt out of Turbopack when a project still depends on custom Webpack config.
next dev --webpack
next build --webpackFile System CacheReuse compiler work between runs with the on-disk cache, enabled by default.
const nextConfig = {
experimental: {
turbopackFileSystemCacheForDev: true,
turbopackFileSystemCacheForBuild: true,
},
};Turbopack Config
turbopack.rulesApply a Webpack-style loader to a file type, such as SVG files.
const nextConfig = {
turbopack: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
};turbopack.resolveAliasSwap one import for another across the whole project.
const nextConfig = {
turbopack: {
resolveAlias: {
underscore: 'lodash',
},
},
};turbopack.resolveExtensionsChange which file extensions are tried when an import has none.
const nextConfig = {
turbopack: {
resolveExtensions: ['.mdx', '.tsx', '.ts', '.js'],
},
};SWC Compiler Options
removeConsoleStrip console calls from the build while keeping error logging.
const nextConfig = {
compiler: {
removeConsole: { exclude: ['error'] },
},
};styledComponentsEnable server rendering and readable class names for styled-components.
const nextConfig = {
compiler: {
styledComponents: true,
},
};reactRemovePropertiesRemove test attributes such as data-testid from production markup.
const nextConfig = {
compiler: {
reactRemoveProperties: true,
},
};React Compiler
babel-plugin-react-compilerInstall the compiler plugin as a dev dependency before enabling it.
npm install -D babel-plugin-react-compilerreactCompilerTurn on automatic memoization for every component in the app.
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
reactCompiler: true,
};
export default nextConfig;Less Manual MemoWrite plain code and let the compiler decide what to memoize.
// Before: manual memoization
const total = useMemo(() => sum(items), [items]);
// After: the compiler handles it
const total = sum(items);'use no memo'Opt a single component out when the compiler causes a problem.
function LegacyWidget() {
'use no memo';
return <OldChart />;
}Tips
- Move custom Webpack loaders into
turbopack.rules, so your project can stay on the default bundler. - Use
compiler.removeConsolein production builds to strip debug logging without touching any of your source files.
Warnings
- A custom
webpackfunction innext.config.tsmakesnext buildfail in Next.js 16 unless you pass--webpack. - A
babel.config.jsfile adds a Babel pass to every build, which slows compilation, so remove it unless you need it.
In Practice
Combine Turbopack, SWC, and React Compiler settings in one typed config file.
- The NextConfig type catches misspelled options before you build.
- reactCompiler turns on automatic memoization across the app.
- removeConsole only runs in production, so logs still work in development.
- The turbopack rule lets you import SVG files as React components.
// next.config.ts
import type { NextConfig } from 'next';
const isProd = process.env.NODE_ENV === 'production';
const nextConfig: NextConfig = {
reactCompiler: true,
compiler: {
removeConsole: isProd ? { exclude: ['error'] } : false,
},
turbopack: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
};
export default nextConfig;FAQ
Turbopack is the bundler that Next.js uses to build your app. It is written in Rust and is the default for both next dev and next build since Next.js 16.
SWC is a fast Rust-based compiler. Next.js uses it to transform TypeScript and JSX and to minify code. You configure its extra transforms with the compiler option.
Add the --webpack flag to your scripts, for example next build --webpack. Without the flag, a project with a custom webpack config fails to build.
It is stable but off by default. It memoizes components automatically, which can reduce re-renders. Install babel-plugin-react-compiler, set reactCompiler: true, and compare build times before and after.