Skip to content

Instantly share code, notes, and snippets.

@javascripter
Created April 24, 2025 14:15
Show Gist options
  • Save javascripter/490fa9f70026a75717b84bfd1698ef3e to your computer and use it in GitHub Desktop.
Save javascripter/490fa9f70026a75717b84bfd1698ef3e to your computer and use it in GitHub Desktop.
React Strict DOM + Next.js with support for both Webpack and Turbopack
import type { NextConfig } from 'next'
function getBabelLoader() {
const dev = process.env.NODE_ENV !== 'production'
return {
loader: 'babel-loader',
options: {
parserOpts: {
plugins: ['typescript', 'jsx'],
},
presets: [
[
'react-strict-dom/babel-preset',
{
debug: dev,
dev: dev,
rootDir: process.cwd(),
},
],
],
},
}
}
function withReactStrictPlugin(nextConfig: NextConfig): NextConfig {
return {
...nextConfig,
transpilePackages: [
...(nextConfig.transpilePackages ?? []),
'react-strict-dom',
],
webpack: (config, context) => {
config.module.rules.push({
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules(?!\/react-strict-dom)/,
use: [getBabelLoader()],
})
return typeof nextConfig.webpack === 'function'
? nextConfig.webpack(config, context)
: config
},
turbopack: {
...nextConfig.turbopack,
rules: {
...nextConfig.turbopack?.rules,
'*.{js,jsx,ts,tsx}': {
foreign: false,
default: {
loaders: [getBabelLoader()],
},
},
},
},
}
}
function withPlugins(nextConfig: NextConfig) {
return plugins.reduce((config, plugin) => plugin(config), nextConfig)
}
const nextConfig: NextConfig = {
experimental: {
reactCompiler: true,
},
}
const plugins = [withReactStrictPlugin]
module.exports = withPlugins(nextConfig)
const dev = process.env.NODE_ENV !== 'production'
module.exports = {
plugins: {
'postcss-react-strict-dom': {
include: [
// Include source files to watch for style changes
'src/**/*.{js,jsx,mjs,ts,tsx}',
],
babelConfig: {
babelrc: false,
parserOpts: {
plugins: ['typescript', 'jsx'],
},
presets: [
[
'react-strict-dom/babel-preset',
{
debug: dev,
dev,
rootDir: process.cwd(),
},
],
],
},
},
autoprefixer: {},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment