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