Unverified Commit 5bdef6aa authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

startup performance improvements (#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
parent 65588083
...@@ -4,8 +4,6 @@ import crypto from 'crypto' ...@@ -4,8 +4,6 @@ import crypto from 'crypto'
import { readFileSync, realpathSync } from 'fs' import { readFileSync, realpathSync } from 'fs'
import chalk from 'chalk' import chalk from 'chalk'
import semver from 'next/dist/compiled/semver' import semver from 'next/dist/compiled/semver'
// @ts-ignore No typings yet
import TerserPlugin from './webpack/plugins/terser-webpack-plugin/src/index.js'
import path from 'path' import path from 'path'
import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack' import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { import {
...@@ -42,7 +40,6 @@ import { pluginLoaderOptions } from './webpack/loaders/next-plugin-loader' ...@@ -42,7 +40,6 @@ import { pluginLoaderOptions } from './webpack/loaders/next-plugin-loader'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin' import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import BuildStatsPlugin from './webpack/plugins/build-stats-plugin' import BuildStatsPlugin from './webpack/plugins/build-stats-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin' import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { CssMinimizerPlugin } from './webpack/plugins/css-minimizer-plugin'
import { JsConfigPathsPlugin } from './webpack/plugins/jsconfig-paths-plugin' import { JsConfigPathsPlugin } from './webpack/plugins/jsconfig-paths-plugin'
import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin' import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import' import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
...@@ -332,7 +329,7 @@ export default async function getBaseWebpackConfig( ...@@ -332,7 +329,7 @@ export default async function getBaseWebpackConfig(
// jsconfig is a subset of tsconfig // jsconfig is a subset of tsconfig
if (useTypeScript) { if (useTypeScript) {
const ts = (await import(typeScriptPath!)) as typeof import('typescript') const ts = (await import(typeScriptPath!)) as typeof import('typescript')
const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath) const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath, true)
jsConfig = { compilerOptions: tsConfig.options } jsConfig = { compilerOptions: tsConfig.options }
} }
...@@ -822,24 +819,35 @@ export default async function getBaseWebpackConfig( ...@@ -822,24 +819,35 @@ export default async function getBaseWebpackConfig(
minimize: !(dev || isServer), minimize: !(dev || isServer),
minimizer: [ minimizer: [
// Minify JavaScript // Minify JavaScript
new TerserPlugin({ (compiler: webpack.Compiler) => {
cacheDir: path.join(distDir, 'cache', 'next-minifier'), // @ts-ignore No typings yet
parallel: config.experimental.cpus, const {
terserOptions, TerserPlugin,
}), } = require('./webpack/plugins/terser-webpack-plugin/src/index.js')
new TerserPlugin({
cacheDir: path.join(distDir, 'cache', 'next-minifier'),
parallel: config.experimental.cpus,
terserOptions,
}).apply(compiler)
},
// Minify CSS // Minify CSS
new CssMinimizerPlugin({ (compiler: webpack.Compiler) => {
postcssOptions: { const {
map: { CssMinimizerPlugin,
// `inline: false` generates the source map in a separate file. } = require('./webpack/plugins/css-minimizer-plugin')
// Otherwise, the CSS file is needlessly large. new CssMinimizerPlugin({
inline: false, postcssOptions: {
// `annotation: false` skips appending the `sourceMappingURL` map: {
// to the end of the CSS file. Webpack already handles this. // `inline: false` generates the source map in a separate file.
annotation: false, // Otherwise, the CSS file is needlessly large.
inline: false,
// `annotation: false` skips appending the `sourceMappingURL`
// to the end of the CSS file. Webpack already handles this.
annotation: false,
},
}, },
}, }).apply(compiler)
}), },
], ],
}, },
context: dir, context: dir,
......
...@@ -102,7 +102,7 @@ class Webpack4Cache { ...@@ -102,7 +102,7 @@ class Webpack4Cache {
} }
} }
class TerserPlugin { export class TerserPlugin {
constructor(options = {}) { constructor(options = {}) {
const { cacheDir, terserOptions = {}, parallel } = options const { cacheDir, terserOptions = {}, parallel } = options
...@@ -400,5 +400,3 @@ class TerserPlugin { ...@@ -400,5 +400,3 @@ class TerserPlugin {
}) })
} }
} }
export default TerserPlugin
...@@ -5,7 +5,8 @@ import { FatalTypeScriptError } from './FatalTypeScriptError' ...@@ -5,7 +5,8 @@ import { FatalTypeScriptError } from './FatalTypeScriptError'
export async function getTypeScriptConfiguration( export async function getTypeScriptConfiguration(
ts: typeof import('typescript'), ts: typeof import('typescript'),
tsConfigPath: string tsConfigPath: string,
metaOnly?: boolean
): Promise<import('typescript').ParsedCommandLine> { ): Promise<import('typescript').ParsedCommandLine> {
try { try {
const formatDiagnosticsHost: import('typescript').FormatDiagnosticsHost = { const formatDiagnosticsHost: import('typescript').FormatDiagnosticsHost = {
...@@ -21,9 +22,20 @@ export async function getTypeScriptConfiguration( ...@@ -21,9 +22,20 @@ export async function getTypeScriptConfiguration(
) )
} }
let configToParse: any = config
const result = ts.parseJsonConfigFileContent( const result = ts.parseJsonConfigFileContent(
config, configToParse,
ts.sys, // When only interested in meta info,
// avoid enumerating all files (for performance reasons)
metaOnly
? {
...ts.sys,
readDirectory(_path, extensions, _excludes, _includes, _depth) {
return [extensions ? `file${extensions[0]}` : `file.ts`]
},
}
: ts.sys,
path.dirname(tsConfigPath) path.dirname(tsConfigPath)
) )
......
...@@ -98,7 +98,7 @@ export async function writeConfigurationDefaults( ...@@ -98,7 +98,7 @@ export async function writeConfigurationDefaults(
const { const {
options: tsOptions, options: tsOptions,
raw: rawConfig, raw: rawConfig,
} = await getTypeScriptConfiguration(ts, tsConfigPath) } = await getTypeScriptConfiguration(ts, tsConfigPath, true)
const userTsConfigContent = await fs.readFile(tsConfigPath, { const userTsConfigContent = await fs.readFile(tsConfigPath, {
encoding: 'utf8', encoding: 'utf8',
......
...@@ -6,7 +6,7 @@ import { ...@@ -6,7 +6,7 @@ import {
hasNecessaryDependencies, hasNecessaryDependencies,
NecessaryDependencies, NecessaryDependencies,
} from './typescript/hasNecessaryDependencies' } from './typescript/hasNecessaryDependencies'
import { runTypeCheck, TypeCheckResult } from './typescript/runTypeCheck' import type { TypeCheckResult } from './typescript/runTypeCheck'
import { TypeScriptCompileError } from './typescript/TypeScriptCompileError' import { TypeScriptCompileError } from './typescript/TypeScriptCompileError'
import { writeAppTypeDeclarations } from './typescript/writeAppTypeDeclarations' import { writeAppTypeDeclarations } from './typescript/writeAppTypeDeclarations'
import { writeConfigurationDefaults } from './typescript/writeConfigurationDefaults' import { writeConfigurationDefaults } from './typescript/writeConfigurationDefaults'
...@@ -41,6 +41,8 @@ export async function verifyTypeScriptSetup( ...@@ -41,6 +41,8 @@ export async function verifyTypeScriptSetup(
await writeAppTypeDeclarations(dir) await writeAppTypeDeclarations(dir)
if (typeCheckPreflight) { if (typeCheckPreflight) {
const { runTypeCheck } = require('./typescript/runTypeCheck')
// Verify the project passes type-checking before we go to webpack phase: // Verify the project passes type-checking before we go to webpack phase:
return await runTypeCheck(ts, dir, tsConfigPath) return await runTypeCheck(ts, dir, tsConfigPath)
} }
......
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',
}
}
import { loadEnvConfig } from '@next/env' import path from 'path'
import { Worker } from 'jest-worker' import { Worker } from 'jest-worker'
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' import * as Log from '../../build/output/log'
import type { CheckReasons, CheckResult } from './config-utils-worker'
import { install, shouldLoadWithWebpack5 } from './config-utils-worker'
let installed: boolean = false export { install, shouldLoadWithWebpack5 }
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')
}
type CheckReasons =
| 'test-mode'
| 'no-config'
| 'future-flag'
| 'no-future-flag'
| 'no-webpack-config'
| 'webpack-config'
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',
}
}
function reasonMessage(reason: CheckReasons) { function reasonMessage(reason: CheckReasons) {
switch (reason) { switch (reason) {
...@@ -122,7 +27,13 @@ function reasonMessage(reason: CheckReasons) { ...@@ -122,7 +27,13 @@ function reasonMessage(reason: CheckReasons) {
export async function loadWebpackHook(phase: string, dir: string) { export async function loadWebpackHook(phase: string, dir: string) {
let useWebpack5 = false let useWebpack5 = false
const worker: any = new Worker(__filename, { enableWorkerThreads: false }) const worker: any = new Worker(
path.resolve(__dirname, './config-utils-worker.js'),
{
enableWorkerThreads: false,
numWorkers: 1,
}
)
try { try {
const result: CheckResult = await worker.shouldLoadWithWebpack5(phase, dir) const result: CheckResult = await worker.shouldLoadWithWebpack5(phase, dir)
Log.info( Log.info(
......
import { ReactDevOverlay } from '@next/react-dev-overlay/lib/client'
import crypto from 'crypto' import crypto from 'crypto'
import fs from 'fs' import fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
...@@ -47,6 +46,16 @@ if (typeof React.Suspense === 'undefined') { ...@@ -47,6 +46,16 @@ if (typeof React.Suspense === 'undefined') {
) )
} }
// Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: React.FunctionComponent
const ReactDevOverlay = (props: any) => {
if (ReactDevOverlayImpl === undefined) {
ReactDevOverlayImpl = require('@next/react-dev-overlay/lib/client')
.ReactDevOverlay
}
return ReactDevOverlayImpl(props)
}
export default class DevServer extends Server { export default class DevServer extends Server {
private devReady: Promise<void> private devReady: Promise<void>
private setDevReady?: Function private setDevReady?: Function
......
import Server, { ServerConstructor } from '../next-server/server/next-server' import '../next-server/server/node-polyfill-fetch'
import type {
default as Server,
ServerConstructor,
} from '../next-server/server/next-server'
import { NON_STANDARD_NODE_ENV } from '../lib/constants' import { NON_STANDARD_NODE_ENV } from '../lib/constants'
import * as log from '../build/output/log' import * as log from '../build/output/log'
import loadConfig, { NextConfig } from '../next-server/server/config' import loadConfig, { NextConfig } from '../next-server/server/config'
...@@ -17,6 +21,14 @@ type NextServerConstructor = ServerConstructor & { ...@@ -17,6 +21,14 @@ type NextServerConstructor = ServerConstructor & {
dev?: boolean dev?: boolean
} }
let ServerImpl: typeof Server
const getServerImpl = async () => {
if (ServerImpl === undefined)
ServerImpl = (await import('../next-server/server/next-server')).default
return ServerImpl
}
export class NextServer { export class NextServer {
private serverPromise?: Promise<Server> private serverPromise?: Promise<Server>
private server?: Server private server?: Server
...@@ -93,17 +105,17 @@ export class NextServer { ...@@ -93,17 +105,17 @@ export class NextServer {
return (server as any).close() return (server as any).close()
} }
private createServer( private async createServer(
options: NextServerConstructor & { options: NextServerConstructor & {
conf: NextConfig conf: NextConfig
isNextDevCommand?: boolean isNextDevCommand?: boolean
} }
): Server { ): Promise<Server> {
if (options.dev) { if (options.dev) {
const DevServer = require('./next-dev-server').default const DevServer = require('./next-dev-server').default
return new DevServer(options) return new DevServer(options)
} }
return new Server(options) return new (await getServerImpl())(options)
} }
private async loadConfig() { private async loadConfig() {
...@@ -117,8 +129,9 @@ export class NextServer { ...@@ -117,8 +129,9 @@ export class NextServer {
private async getServer() { private async getServer() {
if (!this.serverPromise) { if (!this.serverPromise) {
setTimeout(getServerImpl, 10)
this.serverPromise = this.loadConfig().then(async (conf) => { this.serverPromise = this.loadConfig().then(async (conf) => {
this.server = this.createServer({ this.server = await this.createServer({
...this.options, ...this.options,
conf, conf,
}) })
......
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