forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
startup performance improvements (vercel#24129)
* import next-server logic during the time the configuration is loaded * load minimizer plugins only when used * load ReactDevOverlay only when used * load only meta information of tsconfig for validation * make worker for configuration loading lighter * only load runTypeCheck when used * load postcss config only when used
- Loading branch information
1 parent
97fcc93
commit df1eec3
Showing
9 changed files
with
188 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/next/next-server/server/config-utils-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { loadEnvConfig } from '@next/env' | ||
import findUp from 'next/dist/compiled/find-up' | ||
import { init as initWebpack } from 'next/dist/compiled/webpack/webpack' | ||
import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../lib/constants' | ||
import { NextConfig, normalizeConfig } from './config-shared' | ||
import * as Log from '../../build/output/log' | ||
|
||
let installed: boolean = false | ||
|
||
export function install(useWebpack5: boolean) { | ||
if (installed) { | ||
return | ||
} | ||
installed = true | ||
|
||
initWebpack(useWebpack5) | ||
|
||
// hook the Node.js require so that webpack requires are | ||
// routed to the bundled and now initialized webpack version | ||
require('../../build/webpack/require-hook') | ||
} | ||
|
||
export type CheckReasons = | ||
| 'test-mode' | ||
| 'no-config' | ||
| 'future-flag' | ||
| 'no-future-flag' | ||
| 'no-webpack-config' | ||
| 'webpack-config' | ||
|
||
export type CheckResult = { | ||
enabled: boolean | ||
reason: CheckReasons | ||
} | ||
|
||
export async function shouldLoadWithWebpack5( | ||
phase: string, | ||
dir: string | ||
): Promise<CheckResult> { | ||
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log) | ||
|
||
const path = await findUp(CONFIG_FILE, { | ||
cwd: dir, | ||
}) | ||
|
||
if (Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0) { | ||
return { | ||
enabled: true, | ||
reason: 'test-mode', | ||
} | ||
} | ||
|
||
// No `next.config.js`: | ||
if (!path?.length) { | ||
// Uncomment to add auto-enable when there is no next.config.js | ||
// Use webpack 5 by default in new apps: | ||
return { | ||
enabled: true, | ||
reason: 'no-config', | ||
} | ||
} | ||
|
||
// Default to webpack 4 for backwards compatibility on boot: | ||
install(false) | ||
|
||
const userConfigModule = require(path) | ||
const userConfig: Partial<NextConfig> = normalizeConfig( | ||
phase, | ||
userConfigModule.default || userConfigModule | ||
) | ||
|
||
// Opted-in manually | ||
if (userConfig.future?.webpack5 === true) { | ||
return { | ||
enabled: true, | ||
reason: 'future-flag', | ||
} | ||
} | ||
|
||
// Opted-out manually | ||
if (userConfig.future?.webpack5 === false) { | ||
return { | ||
enabled: false, | ||
reason: 'no-future-flag', | ||
} | ||
} | ||
|
||
// Uncomment to add auto-enable when there is no custom webpack config | ||
// The user isn't configuring webpack | ||
if (!userConfig.webpack) { | ||
return { | ||
enabled: true, | ||
reason: 'no-webpack-config', | ||
} | ||
} | ||
|
||
return { | ||
enabled: false, | ||
reason: 'webpack-config', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.