Unverified Commit e3e76a45 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add experimental compile/generate handling (#49491)

This implements experimental`compile`/`generate` commands to allow
separate the compilation and static generation steps of the build.
Initially this is for testing and follows same experimental rules which
aren't covered by semver.

Note: most changes are just spacing changes so disabling that in the
diff will be helpful.

x-ref: [initial
thread](https://www.notion.so/vercel/bbdbba2016ed464eb5263d325fecc02f?pvs=4)
x-ref: [slack
thread](https://vercel.slack.com/archives/C03DQ3QFV7C/p1676591260523589?thread_ts=1676554904.334789&cid=C03DQ3QFV7C)
parent ef2b8f86
......@@ -62,6 +62,11 @@ if (!foundCommand && args['--help']) {
}
const command = foundCommand ? args._[0] : defaultCommand
if (['experimental-compile', 'experimental-generate'].includes(command)) {
args._.push('--build-mode', command)
}
const forwardedArgs = foundCommand ? args._.slice(1) : args._
if (args['--inspect'])
......
......@@ -210,8 +210,12 @@ export default async function build(
runLint = true,
noMangling = false,
appDirOnly = false,
turboNextBuild = false
turboNextBuild = false,
buildMode: 'default' | 'experimental-compile' | 'experimental-generate'
): Promise<void> {
const isCompile = buildMode === 'experimental-compile'
const isGenerate = buildMode === 'experimental-generate'
let hasAppDir = false
try {
const nextBuildSpan = trace('next-build', undefined, {
......@@ -250,9 +254,18 @@ export default async function build(
setGlobal('phase', PHASE_PRODUCTION_BUILD)
setGlobal('distDir', distDir)
const buildId: string = await nextBuildSpan
let buildId: string = ''
if (isGenerate) {
buildId = await promises.readFile(
path.join(distDir, 'BUILD_ID'),
'utf8'
)
} else {
buildId = await nextBuildSpan
.traceChild('generate-buildid')
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
}
NextBuildContext.buildId = buildId
const customRoutes: CustomRoutes = await nextBuildSpan
......@@ -350,7 +363,7 @@ export default async function build(
// For app directory, we run type checking after build. That's because
// we dynamically generate types for each layout and page in the app
// directory.
if (!appDir) await startTypeChecking(typeCheckingOptions)
if (!appDir && !isCompile) await startTypeChecking(typeCheckingOptions)
if (appDir && 'exportPathMap' in config) {
Log.error(
......@@ -368,10 +381,17 @@ export default async function build(
eventName: EVENT_BUILD_FEATURE_USAGE,
payload: buildLintEvent,
})
let buildSpinner: ReturnType<typeof createSpinner> = {
stopAndPersist() {
return this
},
} as any
const buildSpinner = createSpinner({
if (!isGenerate) {
buildSpinner = createSpinner({
prefixText: `${Log.prefixes.info} Creating an optimized production build`,
})
}
NextBuildContext.buildSpinner = buildSpinner
......@@ -802,7 +822,7 @@ export default async function build(
)
}
if (config.cleanDistDir) {
if (config.cleanDistDir && !isGenerate) {
await recursiveDelete(distDir, /^cache/)
}
......@@ -941,6 +961,10 @@ export default async function build(
return { duration, turbotraceContext: null }
}
let runTurbotrace = async (_staticPages: Set<string>) => {}
let turboTasksForTrace: unknown
if (!isGenerate) {
const { duration: webpackBuildDuration, turbotraceContext } =
turboNextBuild ? await turbopackBuild() : await webpackBuild()
......@@ -951,9 +975,7 @@ export default async function build(
})
)
let turboTasksForTrace: unknown
async function runTurbotrace(staticPages: Set<string>) {
runTurbotrace = async function (staticPages: Set<string>) {
if (!turbotraceContext) {
return
}
......@@ -1047,9 +1069,10 @@ export default async function build(
}
}
}
}
// For app directory, we run type checking after build.
if (appDir) {
if (appDir && !(isCompile || isGenerate)) {
await startTypeChecking(typeCheckingOptions)
}
......@@ -1233,6 +1256,16 @@ export default async function build(
hasSsrAmpPages,
hasNonStaticErrorPage,
} = await staticCheckSpan.traceAsyncFn(async () => {
if (isCompile) {
return {
customAppGetInitialProps: false,
namedExports: [],
isNextImageImported: true,
hasSsrAmpPages: !!pagesDir,
hasNonStaticErrorPage: true,
}
}
const { configFileName, publicRuntimeConfig, serverRuntimeConfig } =
config
const runtimeEnvConfig = { publicRuntimeConfig, serverRuntimeConfig }
......@@ -1366,7 +1399,6 @@ export default async function build(
let isServerComponent = false
let isHybridAmp = false
let ssgPageRoutes: string[] | null = null
let pagePath = ''
if (pageType === 'pages') {
......@@ -1413,6 +1445,7 @@ export default async function build(
? 'edge'
: staticInfo?.runtime
if (!isCompile) {
isServerComponent =
pageType === 'app' &&
staticInfo?.rsc !== RSC_MODULE_TYPES.client
......@@ -1647,6 +1680,7 @@ export default async function build(
serverAppPagesCount++
}
}
}
pageInfos.set(page, {
size: selfSize,
......@@ -1720,7 +1754,7 @@ export default async function build(
'next-server.js.nft.json'
)
if (config.outputFileTracing) {
if (!isGenerate && config.outputFileTracing) {
let nodeFileTrace: any
if (config.experimental.turbotrace) {
if (!binding?.isWasm) {
......@@ -2231,10 +2265,11 @@ export default async function build(
// - getStaticProps paths
// - experimental app is enabled
if (
combinedPages.length > 0 ||
!isCompile &&
(combinedPages.length > 0 ||
useStatic404 ||
useDefaultStatic500 ||
isAppDirEnabled
isAppDirEnabled)
) {
const staticGenerationSpan =
nextBuildSpan.traceChild('static-generation')
......
......@@ -18,6 +18,7 @@ const nextBuild: CliCommand = (argv) => {
'--no-mangling': Boolean,
'--experimental-app-only': Boolean,
'--experimental-turbo': Boolean,
'--build-mode': String,
// Aliases
'-h': '--help',
'-d': '--debug',
......@@ -78,7 +79,8 @@ const nextBuild: CliCommand = (argv) => {
!args['--no-lint'],
args['--no-mangling'],
args['--experimental-app-only'],
args['--experimental-turbo']
args['--experimental-turbo'],
args['--build-mode'] || 'default'
).catch((err) => {
console.error('')
if (
......
......@@ -9,4 +9,8 @@ export const commands: { [command: string]: () => Promise<CliCommand> } = {
telemetry: () =>
Promise.resolve(require('../cli/next-telemetry').nextTelemetry),
info: () => Promise.resolve(require('../cli/next-info').nextInfo),
'experimental-compile': () =>
Promise.resolve(require('../cli/next-build').nextBuild),
'experimental-generate': () =>
Promise.resolve(require('../cli/next-build').nextBuild),
}
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