Unverified Commit 141bdf87 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Avoid loading project config multiple times (#45637)

While debugging another thing I found that we are loading `loadJsConfig`
3 times during a build. Inside `loadJsConfig` we load TypeScript and
call `ts.readConfigFile(tsConfigPath, ts.sys.readFile)` and
`ts.parseJsonConfigFileContent`, which are expensive (411ms, about 10%
of the build time of a demo app):


![CleanShot-2023-02-07-90Jwesnx@2x](https://user-images.githubusercontent.com/3676859/217114840-510c254b-6a00-44b0-ab62-138f32398849.png)

In one process these things are the same for all compilers and we should
reuse them.

NEXT-469

## 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 d54d43d4
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { runCompiler } from './compiler' import { runCompiler } from './compiler'
import * as Log from './output/log' import * as Log from './output/log'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig, { loadProjectInfo } from './webpack-config'
import { NextError } from '../lib/is-error' import { NextError } from '../lib/is-error'
import { injectedClientEntries } from './webpack/plugins/flight-client-entry-plugin' import { injectedClientEntries } from './webpack/plugins/flight-client-entry-plugin'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin' import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
...@@ -81,14 +81,20 @@ async function webpackBuildImpl(): Promise<number> { ...@@ -81,14 +81,20 @@ async function webpackBuildImpl(): Promise<number> {
const configs = await runWebpackSpan const configs = await runWebpackSpan
.traceChild('generate-webpack-config') .traceChild('generate-webpack-config')
.traceAsyncFn(() => .traceAsyncFn(async () => {
Promise.all([ const info = await loadProjectInfo({
dir,
config: commonWebpackOptions.config,
dev: false,
})
return Promise.all([
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
...commonWebpackOptions, ...commonWebpackOptions,
middlewareMatchers: entrypoints.middlewareMatchers, middlewareMatchers: entrypoints.middlewareMatchers,
runWebpackSpan, runWebpackSpan,
compilerType: COMPILER_NAMES.client, compilerType: COMPILER_NAMES.client,
entrypoints: entrypoints.client, entrypoints: entrypoints.client,
...info,
}), }),
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
...commonWebpackOptions, ...commonWebpackOptions,
...@@ -96,6 +102,7 @@ async function webpackBuildImpl(): Promise<number> { ...@@ -96,6 +102,7 @@ async function webpackBuildImpl(): Promise<number> {
middlewareMatchers: entrypoints.middlewareMatchers, middlewareMatchers: entrypoints.middlewareMatchers,
compilerType: COMPILER_NAMES.server, compilerType: COMPILER_NAMES.server,
entrypoints: entrypoints.server, entrypoints: entrypoints.server,
...info,
}), }),
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
...commonWebpackOptions, ...commonWebpackOptions,
...@@ -103,9 +110,10 @@ async function webpackBuildImpl(): Promise<number> { ...@@ -103,9 +110,10 @@ async function webpackBuildImpl(): Promise<number> {
middlewareMatchers: entrypoints.middlewareMatchers, middlewareMatchers: entrypoints.middlewareMatchers,
compilerType: COMPILER_NAMES.edgeServer, compilerType: COMPILER_NAMES.edgeServer,
entrypoints: entrypoints.edgeServer, entrypoints: entrypoints.edgeServer,
...info,
}), }),
]) ])
) })
const clientConfig = configs[0] const clientConfig = configs[0]
......
...@@ -575,6 +575,24 @@ export async function resolveExternal( ...@@ -575,6 +575,24 @@ export async function resolveExternal(
return { res, isEsm } return { res, isEsm }
} }
export async function loadProjectInfo({
dir,
config,
dev,
}: {
dir: string
config: NextConfigComplete
dev: boolean
}) {
const { jsConfig, resolvedBaseUrl } = await loadJsConfig(dir, config)
const supportedBrowsers = await getSupportedBrowsers(dir, dev, config)
return {
jsConfig,
resolvedBaseUrl,
supportedBrowsers,
}
}
export default async function getBaseWebpackConfig( export default async function getBaseWebpackConfig(
dir: string, dir: string,
{ {
...@@ -592,6 +610,9 @@ export default async function getBaseWebpackConfig( ...@@ -592,6 +610,9 @@ export default async function getBaseWebpackConfig(
appDir, appDir,
middlewareMatchers, middlewareMatchers,
noMangling = false, noMangling = false,
jsConfig,
resolvedBaseUrl,
supportedBrowsers,
}: { }: {
buildId: string buildId: string
config: NextConfigComplete config: NextConfigComplete
...@@ -607,15 +628,15 @@ export default async function getBaseWebpackConfig( ...@@ -607,15 +628,15 @@ export default async function getBaseWebpackConfig(
appDir?: string appDir?: string
middlewareMatchers?: MiddlewareMatcher[] middlewareMatchers?: MiddlewareMatcher[]
noMangling?: boolean noMangling?: boolean
jsConfig: any
resolvedBaseUrl: string | undefined
supportedBrowsers: string[] | undefined
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const isClient = compilerType === COMPILER_NAMES.client const isClient = compilerType === COMPILER_NAMES.client
const isEdgeServer = compilerType === COMPILER_NAMES.edgeServer const isEdgeServer = compilerType === COMPILER_NAMES.edgeServer
const isNodeServer = compilerType === COMPILER_NAMES.server const isNodeServer = compilerType === COMPILER_NAMES.server
const { jsConfig, resolvedBaseUrl } = await loadJsConfig(dir, config)
const supportedBrowsers = await getSupportedBrowsers(dir, dev, config)
const hasRewrites = const hasRewrites =
rewrites.beforeFiles.length > 0 || rewrites.beforeFiles.length > 0 ||
rewrites.afterFiles.length > 0 || rewrites.afterFiles.length > 0 ||
......
...@@ -17,7 +17,9 @@ import { ...@@ -17,7 +17,9 @@ import {
} from '../../build/entries' } from '../../build/entries'
import { watchCompilers } from '../../build/output' import { watchCompilers } from '../../build/output'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
import getBaseWebpackConfig from '../../build/webpack-config' import getBaseWebpackConfig, {
loadProjectInfo,
} from '../../build/webpack-config'
import { APP_DIR_ALIAS, WEBPACK_LAYERS } from '../../lib/constants' import { APP_DIR_ALIAS, WEBPACK_LAYERS } from '../../lib/constants'
import { recursiveDelete } from '../../lib/recursive-delete' import { recursiveDelete } from '../../lib/recursive-delete'
import { import {
...@@ -478,32 +480,45 @@ export default class HotReloader { ...@@ -478,32 +480,45 @@ export default class HotReloader {
return webpackConfigSpan return webpackConfigSpan
.traceChild('generate-webpack-config') .traceChild('generate-webpack-config')
.traceAsyncFn(() => .traceAsyncFn(async () => {
Promise.all([ const info = await loadProjectInfo({
dir: this.dir,
config: commonWebpackOptions.config,
dev: true,
})
return Promise.all([
// order is important here // order is important here
getBaseWebpackConfig(this.dir, { getBaseWebpackConfig(this.dir, {
...commonWebpackOptions, ...commonWebpackOptions,
compilerType: COMPILER_NAMES.client, compilerType: COMPILER_NAMES.client,
entrypoints: entrypoints.client, entrypoints: entrypoints.client,
...info,
}), }),
getBaseWebpackConfig(this.dir, { getBaseWebpackConfig(this.dir, {
...commonWebpackOptions, ...commonWebpackOptions,
compilerType: COMPILER_NAMES.server, compilerType: COMPILER_NAMES.server,
entrypoints: entrypoints.server, entrypoints: entrypoints.server,
...info,
}), }),
getBaseWebpackConfig(this.dir, { getBaseWebpackConfig(this.dir, {
...commonWebpackOptions, ...commonWebpackOptions,
compilerType: COMPILER_NAMES.edgeServer, compilerType: COMPILER_NAMES.edgeServer,
entrypoints: entrypoints.edgeServer, entrypoints: entrypoints.edgeServer,
...info,
}), }),
]) ])
) })
}) })
} }
public async buildFallbackError(): Promise<void> { public async buildFallbackError(): Promise<void> {
if (this.fallbackWatcher) return if (this.fallbackWatcher) return
const info = await loadProjectInfo({
dir: this.dir,
config: this.config,
dev: true,
})
const fallbackConfig = await getBaseWebpackConfig(this.dir, { const fallbackConfig = await getBaseWebpackConfig(this.dir, {
runWebpackSpan: this.hotReloaderSpan, runWebpackSpan: this.hotReloaderSpan,
dev: true, dev: true,
...@@ -534,6 +549,7 @@ export default class HotReloader { ...@@ -534,6 +549,7 @@ export default class HotReloader {
pageExtensions: this.config.pageExtensions, pageExtensions: this.config.pageExtensions,
}) })
).client, ).client,
...info,
}) })
const fallbackCompiler = webpack(fallbackConfig) const fallbackCompiler = webpack(fallbackConfig)
......
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