Unverified Commit bccf1861 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

feat: add --no-mangling to next build + fix --profile option (#42633)

This PR adds a `--no-mangling` option to `next build` and fixes the `--profile` option so that you can use the React profiling build on production.

The advantage of disabling only mangling versus disabling minification altogether is that we still run terser and still benefit from DCE with this approach, so the `--no-mangling` does not impact performance as much.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 25d5d8c2
...@@ -254,7 +254,8 @@ export default async function build( ...@@ -254,7 +254,8 @@ export default async function build(
conf = null, conf = null,
reactProductionProfiling = false, reactProductionProfiling = false,
debugOutput = false, debugOutput = false,
runLint = true runLint = true,
noMangling = false
): Promise<void> { ): Promise<void> {
try { try {
const nextBuildSpan = trace('next-build', undefined, { const nextBuildSpan = trace('next-build', undefined, {
...@@ -932,6 +933,7 @@ export default async function build( ...@@ -932,6 +933,7 @@ export default async function build(
runWebpackSpan, runWebpackSpan,
target, target,
appDir, appDir,
noMangling,
middlewareMatchers: entrypoints.middlewareMatchers, middlewareMatchers: entrypoints.middlewareMatchers,
} }
......
...@@ -579,6 +579,7 @@ export default async function getBaseWebpackConfig( ...@@ -579,6 +579,7 @@ export default async function getBaseWebpackConfig(
target = COMPILER_NAMES.server, target = COMPILER_NAMES.server,
appDir, appDir,
middlewareMatchers, middlewareMatchers,
noMangling = false,
}: { }: {
buildId: string buildId: string
config: NextConfigComplete config: NextConfigComplete
...@@ -593,6 +594,7 @@ export default async function getBaseWebpackConfig( ...@@ -593,6 +594,7 @@ export default async function getBaseWebpackConfig(
target?: string target?: string
appDir?: string appDir?: string
middlewareMatchers?: MiddlewareMatcher[] middlewareMatchers?: MiddlewareMatcher[]
noMangling?: boolean
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const isClient = compilerType === COMPILER_NAMES.client const isClient = compilerType === COMPILER_NAMES.client
...@@ -981,7 +983,7 @@ export default async function getBaseWebpackConfig( ...@@ -981,7 +983,7 @@ export default async function getBaseWebpackConfig(
}, },
mangle: { mangle: {
safari10: true, safari10: true,
...(process.env.__NEXT_MANGLING_DEBUG ...(process.env.__NEXT_MANGLING_DEBUG || noMangling
? { ? {
toplevel: true, toplevel: true,
module: true, module: true,
...@@ -996,7 +998,7 @@ export default async function getBaseWebpackConfig( ...@@ -996,7 +998,7 @@ export default async function getBaseWebpackConfig(
comments: false, comments: false,
// Fixes usage of Emoji and certain Regex // Fixes usage of Emoji and certain Regex
ascii_only: true, ascii_only: true,
...(process.env.__NEXT_MANGLING_DEBUG ...(process.env.__NEXT_MANGLING_DEBUG || noMangling
? { ? {
beautify: true, beautify: true,
} }
...@@ -1753,7 +1755,9 @@ export default async function getBaseWebpackConfig( ...@@ -1753,7 +1755,9 @@ export default async function getBaseWebpackConfig(
resolve: { resolve: {
alias: { alias: {
react: 'next/dist/compiled/react', react: 'next/dist/compiled/react',
'react-dom$': 'next/dist/compiled/react-dom', 'react-dom$': reactProductionProfiling
? 'next/dist/compiled/react-dom/cjs/react-dom.profiling.min'
: 'next/dist/compiled/react-dom',
'react-dom/client$': 'react-dom/client$':
'next/dist/compiled/react-dom/client', 'next/dist/compiled/react-dom/client',
}, },
......
...@@ -15,6 +15,7 @@ const nextBuild: cliCommand = (argv) => { ...@@ -15,6 +15,7 @@ const nextBuild: cliCommand = (argv) => {
'--profile': Boolean, '--profile': Boolean,
'--debug': Boolean, '--debug': Boolean,
'--no-lint': Boolean, '--no-lint': Boolean,
'--no-mangling': Boolean,
// Aliases // Aliases
'-h': '--help', '-h': '--help',
'-d': '--debug', '-d': '--debug',
...@@ -44,6 +45,7 @@ const nextBuild: cliCommand = (argv) => { ...@@ -44,6 +45,7 @@ const nextBuild: cliCommand = (argv) => {
Options Options
--profile Can be used to enable React Production Profiling --profile Can be used to enable React Production Profiling
--no-lint Disable linting --no-lint Disable linting
--no-mangling Disable mangling
`, `,
0 0
) )
...@@ -54,6 +56,11 @@ const nextBuild: cliCommand = (argv) => { ...@@ -54,6 +56,11 @@ const nextBuild: cliCommand = (argv) => {
if (args['--no-lint']) { if (args['--no-lint']) {
Log.warn('Linting is disabled') Log.warn('Linting is disabled')
} }
if (args['--no-mangling']) {
Log.warn(
'Mangling is disabled. Note: This may affect performance and should only be used for debugging purposes'
)
}
const dir = getProjectDir(args._[0]) const dir = getProjectDir(args._[0])
// Check if the provided directory exists // Check if the provided directory exists
...@@ -66,7 +73,8 @@ const nextBuild: cliCommand = (argv) => { ...@@ -66,7 +73,8 @@ const nextBuild: cliCommand = (argv) => {
null, null,
args['--profile'], args['--profile'],
args['--debug'], args['--debug'],
!args['--no-lint'] !args['--no-lint'],
args['--no-mangling']
).catch((err) => { ).catch((err) => {
console.error('') console.error('')
if ( if (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment