rsnext/packages/next/cli/next-build.ts
Jimmy Lai bccf186145
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)
2022-11-08 17:53:29 +00:00

95 lines
2.4 KiB
JavaScript
Executable file

#!/usr/bin/env node
import { existsSync } from 'fs'
import arg from 'next/dist/compiled/arg/index.js'
import * as Log from '../build/output/log'
import { cliCommand } from '../lib/commands'
import build from '../build'
import { printAndExit } from '../server/lib/utils'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
const nextBuild: cliCommand = (argv) => {
const validArgs: arg.Spec = {
// Types
'--help': Boolean,
'--profile': Boolean,
'--debug': Boolean,
'--no-lint': Boolean,
'--no-mangling': Boolean,
// Aliases
'-h': '--help',
'-d': '--debug',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
if (args['--help']) {
printAndExit(
`
Description
Compiles the application for production deployment
Usage
$ next build <dir>
<dir> represents the directory of the Next.js application.
If no directory is provided, the current directory will be used.
Options
--profile Can be used to enable React Production Profiling
--no-lint Disable linting
--no-mangling Disable mangling
`,
0
)
}
if (args['--profile']) {
Log.warn('Profiling is enabled. Note: This may affect performance')
}
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
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}
return build(
dir,
null,
args['--profile'],
args['--debug'],
!args['--no-lint'],
args['--no-mangling']
).catch((err) => {
console.error('')
if (
isError(err) &&
(err.code === 'INVALID_RESOLVE_ALIAS' ||
err.code === 'WEBPACK_ERRORS' ||
err.code === 'BUILD_OPTIMIZATION_FAILED' ||
err.code === 'EDGE_RUNTIME_UNSUPPORTED_API')
) {
printAndExit(`> ${err.message}`)
} else {
console.error('> Build error occurred')
printAndExit(err)
}
})
}
export { nextBuild }