Skip to content

Commit

Permalink
feat: add --no-mangling to next build + fix --profile option (#42633)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
feedthejim committed Nov 8, 2022
1 parent 25d5d8c commit bccf186
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/next/build/index.ts
Expand Up @@ -254,7 +254,8 @@ export default async function build(
conf = null,
reactProductionProfiling = false,
debugOutput = false,
runLint = true
runLint = true,
noMangling = false
): Promise<void> {
try {
const nextBuildSpan = trace('next-build', undefined, {
Expand Down Expand Up @@ -932,6 +933,7 @@ export default async function build(
runWebpackSpan,
target,
appDir,
noMangling,
middlewareMatchers: entrypoints.middlewareMatchers,
}

Expand Down
10 changes: 7 additions & 3 deletions packages/next/build/webpack-config.ts
Expand Up @@ -579,6 +579,7 @@ export default async function getBaseWebpackConfig(
target = COMPILER_NAMES.server,
appDir,
middlewareMatchers,
noMangling = false,
}: {
buildId: string
config: NextConfigComplete
Expand All @@ -593,6 +594,7 @@ export default async function getBaseWebpackConfig(
target?: string
appDir?: string
middlewareMatchers?: MiddlewareMatcher[]
noMangling?: boolean
}
): Promise<webpack.Configuration> {
const isClient = compilerType === COMPILER_NAMES.client
Expand Down Expand Up @@ -981,7 +983,7 @@ export default async function getBaseWebpackConfig(
},
mangle: {
safari10: true,
...(process.env.__NEXT_MANGLING_DEBUG
...(process.env.__NEXT_MANGLING_DEBUG || noMangling
? {
toplevel: true,
module: true,
Expand All @@ -996,7 +998,7 @@ export default async function getBaseWebpackConfig(
comments: false,
// Fixes usage of Emoji and certain Regex
ascii_only: true,
...(process.env.__NEXT_MANGLING_DEBUG
...(process.env.__NEXT_MANGLING_DEBUG || noMangling
? {
beautify: true,
}
Expand Down Expand Up @@ -1753,7 +1755,9 @@ export default async function getBaseWebpackConfig(
resolve: {
alias: {
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$':
'next/dist/compiled/react-dom/client',
},
Expand Down
10 changes: 9 additions & 1 deletion packages/next/cli/next-build.ts
Expand Up @@ -15,6 +15,7 @@ const nextBuild: cliCommand = (argv) => {
'--profile': Boolean,
'--debug': Boolean,
'--no-lint': Boolean,
'--no-mangling': Boolean,
// Aliases
'-h': '--help',
'-d': '--debug',
Expand Down Expand Up @@ -44,6 +45,7 @@ const nextBuild: cliCommand = (argv) => {
Options
--profile Can be used to enable React Production Profiling
--no-lint Disable linting
--no-mangling Disable mangling
`,
0
)
Expand All @@ -54,6 +56,11 @@ const nextBuild: cliCommand = (argv) => {
if (args['--no-lint']) {
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])

// Check if the provided directory exists
Expand All @@ -66,7 +73,8 @@ const nextBuild: cliCommand = (argv) => {
null,
args['--profile'],
args['--debug'],
!args['--no-lint']
!args['--no-lint'],
args['--no-mangling']
).catch((err) => {
console.error('')
if (
Expand Down

0 comments on commit bccf186

Please sign in to comment.