Unverified Commit 0a8a143f authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

misc: refactor build context/webpack build step (4/6) (#45458)

Refactoring the ugliness of the previous diff into a more manageable
build context. This will let us avoid passing down 10 params at a time.

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
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`](https://github.com/vercel/next.js/blob/canary/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)
parent 074b7e4d
import { LoadedEnvFiles } from '@next/env'
import { Ora } from 'next/dist/compiled/ora'
import { Rewrite } from '../lib/load-custom-routes'
import { __ApiPreviewProps } from '../server/api-utils'
import { NextConfigComplete } from '../server/config-shared'
import { Span } from '../trace'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
// a global object to store context for the current build
// this is used to pass data between different steps of the build without having
// to pass it through function arguments.
// Not exhaustive, but should be extended to as needed whilst refactoring
export const NextBuildContext: Partial<{
// core fields
dir: string
buildId: string
config: NextConfigComplete
appDir: string
pagesDir: string
rewrites: {
fallback: Rewrite[]
afterFiles: Rewrite[]
beforeFiles: Rewrite[]
}
loadedEnvFiles: LoadedEnvFiles
previewProps: __ApiPreviewProps
mappedPages:
| {
[page: string]: string
}
| undefined
mappedAppPages:
| {
[page: string]: string
}
| undefined
mappedRootPaths: {
[page: string]: string
}
// misc fields
telemetryPlugin: TelemetryPlugin
buildSpinner: Ora
nextBuildSpan: Span
// cli fields
reactProductionProfiling: boolean
noMangling: boolean
appDirOnly: boolean
}> = {}
......@@ -86,7 +86,7 @@ import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import * as Log from './output/log'
import createSpinner from './spinner'
import { trace, flushAllTraces, setGlobal, Span } from '../trace'
import { trace, flushAllTraces, setGlobal } from '../trace'
import {
detectConflictingPaths,
computeFromManifest,
......@@ -104,7 +104,6 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { NextConfigComplete } from '../server/config-shared'
import isError, { NextError } from '../lib/is-error'
import { isEdgeRuntime } from '../lib/is-edge-runtime'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
import { MiddlewareManifest } from './webpack/plugins/middleware-plugin'
import { recursiveCopy } from '../lib/recursive-copy'
import { recursiveReadDir } from '../lib/recursive-readdir'
......@@ -122,6 +121,7 @@ import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { AppBuildManifest } from './webpack/plugins/app-build-manifest-plugin'
import { RSC, RSC_VARY_HEADER } from '../client/components/app-router-headers'
import { webpackBuild } from './webpack-build'
import { NextBuildContext } from './build-context'
export type SsgRoute = {
initialRevalidateSeconds: number | false
......@@ -144,13 +144,6 @@ export type PrerenderManifest = {
preview: __ApiPreviewProps
}
export const NextBuildContext: Partial<{
telemetryPlugin: TelemetryPlugin
buildSpinner: any
nextBuildSpan: Span
dir: string
}> = {}
/**
* typescript will be loaded in "next/lib/verifyTypeScriptSetup" and
* then passed to "next/lib/typescript/runTypeCheck" as a parameter.
......@@ -252,33 +245,40 @@ export default async function build(
const nextBuildSpan = trace('next-build', undefined, {
version: process.env.__NEXT_VERSION as string,
})
NextBuildContext.nextBuildSpan = nextBuildSpan
NextBuildContext.dir = dir
NextBuildContext.appDirOnly = appDirOnly
NextBuildContext.reactProductionProfiling = reactProductionProfiling
NextBuildContext.noMangling = noMangling
const buildResult = await nextBuildSpan.traceAsyncFn(async () => {
// attempt to load global env values so they are available in next.config.js
const { loadedEnvFiles } = nextBuildSpan
.traceChild('load-dotenv')
.traceFn(() => loadEnvConfig(dir, false, Log))
NextBuildContext.loadedEnvFiles = loadedEnvFiles
const config: NextConfigComplete = await nextBuildSpan
.traceChild('load-next-config')
.traceAsyncFn(() => loadConfig(PHASE_PRODUCTION_BUILD, dir))
NextBuildContext.config = config
const distDir = path.join(dir, config.distDir)
setGlobal('phase', PHASE_PRODUCTION_BUILD)
setGlobal('distDir', distDir)
const { target } = config
const buildId: string = await nextBuildSpan
.traceChild('generate-buildid')
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
NextBuildContext.buildId = buildId
const customRoutes: CustomRoutes = await nextBuildSpan
.traceChild('load-custom-routes')
.traceAsyncFn(() => loadCustomRoutes(config))
const { headers, rewrites, redirects } = customRoutes
NextBuildContext.rewrites = rewrites
const cacheDir = path.join(distDir, 'cache')
if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) {
......@@ -325,6 +325,9 @@ export default async function build(
})
const { pagesDir, appDir } = findPagesDir(dir, isAppDirEnabled)
NextBuildContext.pagesDir = pagesDir
NextBuildContext.appDir = appDir
const isSrcDir = path
.relative(dir, pagesDir || appDir || '')
.startsWith('src')
......@@ -517,6 +520,7 @@ export default async function build(
previewModeSigningKey: crypto.randomBytes(32).toString('hex'),
previewModeEncryptionKey: crypto.randomBytes(32).toString('hex'),
}
NextBuildContext.previewProps = previewProps
const mappedPages = nextBuildSpan
.traceChild('create-pages-mapping')
......@@ -529,6 +533,7 @@ export default async function build(
pagesDir,
})
)
NextBuildContext.mappedPages = mappedPages
let mappedAppPages: { [page: string]: string } | undefined
let denormalizedAppPages: string[] | undefined
......@@ -545,6 +550,7 @@ export default async function build(
pagesDir: pagesDir,
})
)
NextBuildContext.mappedAppPages = mappedAppPages
}
let mappedRootPaths: { [page: string]: string } = {}
......@@ -557,6 +563,7 @@ export default async function build(
pagesDir: pagesDir,
})
}
NextBuildContext.mappedRootPaths = mappedRootPaths
const pagesPageKeys = Object.keys(mappedPages)
......@@ -920,32 +927,7 @@ export default async function build(
ignore: [] as string[],
}))
const webpackBuildDuration = await webpackBuild(
{
buildId,
config,
pagesDir,
reactProductionProfiling,
rewrites,
target,
appDir,
noMangling,
},
{
buildId,
config,
envFiles: loadedEnvFiles,
isDev: false,
pages: mappedPages,
pagesDir,
previewMode: previewProps,
rootDir: dir,
rootPaths: mappedRootPaths,
appDir,
appPaths: mappedAppPages,
pageExtensions: config.pageExtensions,
}
)
const webpackBuildDuration = await webpackBuild()
telemetry.record(
eventBuildCompleted(pagesPaths, {
......
This diff is collapsed.
......@@ -438,6 +438,9 @@ const configSchema = {
mdxRs: {
type: 'boolean',
},
webpackBuildWorker: {
type: 'boolean',
},
turbopackLoaders: {
type: 'object',
},
......
......@@ -178,6 +178,10 @@ export interface ExperimentalConfig {
memoryLimit?: number
}
mdxRs?: boolean
/**
* This option is to enable running the Webpack build in a worker thread.
*/
webpackBuildWorker?: boolean
}
export type ExportPathMap = {
......
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