-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathnext-build.ts
executable file
·117 lines (103 loc) · 2.94 KB
/
next-build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
import '../server/lib/cpu-profile'
import { existsSync } from 'fs'
import { italic } from '../lib/picocolors'
import build from '../build'
import { warn } from '../build/output/log'
import { printAndExit } from '../server/lib/utils'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { enableMemoryDebuggingMode } from '../lib/memory/startup'
import { disableMemoryDebuggingMode } from '../lib/memory/shutdown'
type NextBuildOptions = {
debug?: boolean
profile?: boolean
lint: boolean
mangling: boolean
experimentalDebugMemoryUsage: boolean
experimentalAppOnly?: boolean
experimentalTurbo?: boolean
experimentalBuildMode: 'default' | 'compile' | 'generate'
experimentalUploadTrace?: string
}
const nextBuild = (options: NextBuildOptions, directory?: string) => {
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))
const {
debug,
experimentalDebugMemoryUsage,
profile,
lint,
mangling,
experimentalAppOnly,
experimentalTurbo,
experimentalBuildMode,
experimentalUploadTrace,
} = options
let traceUploadUrl: string | undefined
if (experimentalUploadTrace && !process.env.NEXT_TRACE_UPLOAD_DISABLED) {
traceUploadUrl = experimentalUploadTrace
}
if (!lint) {
warn('Linting is disabled.')
}
if (!mangling) {
warn(
'Mangling is disabled. Note: This may affect performance and should only be used for debugging purposes.'
)
}
if (profile) {
warn(
`Profiling is enabled. ${italic('Note: This may affect performance.')}`
)
}
if (experimentalDebugMemoryUsage) {
process.env.EXPERIMENTAL_DEBUG_MEMORY_USAGE = '1'
enableMemoryDebuggingMode()
}
const dir = getProjectDir(directory)
// Check if the provided directory exists
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}
if (experimentalTurbo) {
process.env.TURBOPACK = '1'
}
return build(
dir,
profile,
debug || Boolean(process.env.NEXT_DEBUG_BUILD),
lint,
!mangling,
experimentalAppOnly,
!!process.env.TURBOPACK,
experimentalBuildMode,
traceUploadUrl
)
.catch((err) => {
if (experimentalDebugMemoryUsage) {
disableMemoryDebuggingMode()
}
console.error('')
if (
isError(err) &&
(err.code === 'INVALID_RESOLVE_ALIAS' ||
err.code === 'WEBPACK_ERRORS' ||
err.code === 'BUILD_OPTIMIZATION_FAILED' ||
err.code === 'NEXT_EXPORT_ERROR' ||
err.code === 'NEXT_STATIC_GEN_BAILOUT' ||
err.code === 'EDGE_RUNTIME_UNSUPPORTED_API')
) {
printAndExit(`> ${err.message}`)
} else {
console.error('> Build error occurred')
printAndExit(err)
}
})
.finally(() => {
if (experimentalDebugMemoryUsage) {
disableMemoryDebuggingMode()
}
})
}
export { nextBuild }