Last active
September 20, 2023 20:45
-
-
Save kamranayub/1cef1362632f3f259bf0b4874bfa40d8 to your computer and use it in GitHub Desktop.
React Production Profiling Support for Next.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// See: https://kentcdodds.com/blog/profile-a-react-app-for-performance#build-and-measure-the-production-app | |
// See: https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config | |
const TerserPlugin = require('next/dist/compiled/terser-webpack-plugin'); | |
module.exports = { | |
webpack: (config, options) => { | |
// | |
// Use profiler-enabled React builds | |
// | |
config.resolve.alias = { | |
...config.resolve.alias, | |
'react-dom$': 'react-dom/profiling', | |
'scheduler/tracing': 'scheduler/tracing-profiling', | |
}; | |
// | |
// Disable mangling for easier profiling | |
// WARNING: This increases bundle size, DO NOT DO THIS in production! | |
// | |
const terser = config.optimization.minimizer.find((plugin) => plugin instanceof TerserPlugin); | |
if (terser) { | |
terser.options.terserOptions = { | |
...terser.options.terserOptions, | |
keep_classnames: true, | |
keep_fnames: true, | |
}; | |
} | |
return config; | |
} | |
} |
I had to patch the minify function directly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to work with Next 11. Next appears to handle minification via Terser internally now, rather than adding a plugin to the Webpack config:
https://github.com/vercel/next.js/blob/0af3b526408bae26d6b3f8cab75c4229998bf7cb/packages/next/build/webpack/plugins/terser-webpack-plugin/src/minify.js