From 7e019cc72338887b41c913a785da6a2c5d19877c Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Tue, 31 Jan 2023 21:44:35 +0100 Subject: [PATCH] misc: remove IIFE from webpack build fn --- packages/next/src/build/webpack-build.ts | 277 +++++++++++------------ 1 file changed, 137 insertions(+), 140 deletions(-) diff --git a/packages/next/src/build/webpack-build.ts b/packages/next/src/build/webpack-build.ts index 96c5689f41c7c2..f146026cb6d35d 100644 --- a/packages/next/src/build/webpack-build.ts +++ b/packages/next/src/build/webpack-build.ts @@ -44,155 +44,152 @@ export async function webpackBuild(): Promise { const buildSpinner = NextBuildContext.buildSpinner const dir = NextBuildContext.dir! - await (async () => { - // IIFE to isolate locals and avoid retaining memory too long - const runWebpackSpan = nextBuildSpan.traceChild('run-webpack-compiler') - - const entrypoints = await nextBuildSpan - .traceChild('create-entrypoints') - .traceAsyncFn(() => - createEntrypoints({ - buildId: NextBuildContext.buildId!, - config: NextBuildContext.config!, - envFiles: NextBuildContext.loadedEnvFiles!, - isDev: false, - rootDir: dir, - pageExtensions: NextBuildContext.config!.pageExtensions!, - pagesDir: NextBuildContext.pagesDir!, - appDir: NextBuildContext.appDir!, - pages: NextBuildContext.mappedPages!, - appPaths: NextBuildContext.mappedAppPages!, - previewMode: NextBuildContext.previewProps!, - rootPaths: NextBuildContext.mappedRootPaths!, - }) - ) + const runWebpackSpan = nextBuildSpan.traceChild('run-webpack-compiler') + + const entrypoints = await nextBuildSpan + .traceChild('create-entrypoints') + .traceAsyncFn(() => + createEntrypoints({ + buildId: NextBuildContext.buildId!, + config: NextBuildContext.config!, + envFiles: NextBuildContext.loadedEnvFiles!, + isDev: false, + rootDir: dir, + pageExtensions: NextBuildContext.config!.pageExtensions!, + pagesDir: NextBuildContext.pagesDir!, + appDir: NextBuildContext.appDir!, + pages: NextBuildContext.mappedPages!, + appPaths: NextBuildContext.mappedAppPages!, + previewMode: NextBuildContext.previewProps!, + rootPaths: NextBuildContext.mappedRootPaths!, + }) + ) + + const commonWebpackOptions = { + isServer: false, + buildId: NextBuildContext.buildId!, + config: NextBuildContext.config!, + target: NextBuildContext.config!.target!, + appDir: NextBuildContext.appDir!, + pagesDir: NextBuildContext.pagesDir!, + rewrites: NextBuildContext.rewrites!, + reactProductionProfiling: NextBuildContext.reactProductionProfiling!, + noMangling: NextBuildContext.noMangling!, + } - const commonWebpackOptions = { - isServer: false, - buildId: NextBuildContext.buildId!, - config: NextBuildContext.config!, - target: NextBuildContext.config!.target!, - appDir: NextBuildContext.appDir!, - pagesDir: NextBuildContext.pagesDir!, - rewrites: NextBuildContext.rewrites!, - reactProductionProfiling: NextBuildContext.reactProductionProfiling!, - noMangling: NextBuildContext.noMangling!, - } + const configs = await runWebpackSpan + .traceChild('generate-webpack-config') + .traceAsyncFn(() => + Promise.all([ + getBaseWebpackConfig(dir, { + ...commonWebpackOptions, + middlewareMatchers: entrypoints.middlewareMatchers, + runWebpackSpan, + compilerType: COMPILER_NAMES.client, + entrypoints: entrypoints.client, + }), + getBaseWebpackConfig(dir, { + ...commonWebpackOptions, + runWebpackSpan, + middlewareMatchers: entrypoints.middlewareMatchers, + compilerType: COMPILER_NAMES.server, + entrypoints: entrypoints.server, + }), + getBaseWebpackConfig(dir, { + ...commonWebpackOptions, + runWebpackSpan, + middlewareMatchers: entrypoints.middlewareMatchers, + compilerType: COMPILER_NAMES.edgeServer, + entrypoints: entrypoints.edgeServer, + }), + ]) + ) + + const clientConfig = configs[0] + + if ( + clientConfig.optimization && + (clientConfig.optimization.minimize !== true || + (clientConfig.optimization.minimizer && + clientConfig.optimization.minimizer.length === 0)) + ) { + Log.warn( + `Production code optimization has been disabled in your project. Read more: https://nextjs.org/docs/messages/minification-disabled` + ) + } - const configs = await runWebpackSpan - .traceChild('generate-webpack-config') - .traceAsyncFn(() => - Promise.all([ - getBaseWebpackConfig(dir, { - ...commonWebpackOptions, - middlewareMatchers: entrypoints.middlewareMatchers, - runWebpackSpan, - compilerType: COMPILER_NAMES.client, - entrypoints: entrypoints.client, - }), - getBaseWebpackConfig(dir, { - ...commonWebpackOptions, - runWebpackSpan, - middlewareMatchers: entrypoints.middlewareMatchers, - compilerType: COMPILER_NAMES.server, - entrypoints: entrypoints.server, - }), - getBaseWebpackConfig(dir, { - ...commonWebpackOptions, - runWebpackSpan, - middlewareMatchers: entrypoints.middlewareMatchers, - compilerType: COMPILER_NAMES.edgeServer, - entrypoints: entrypoints.edgeServer, - }), - ]) - ) + webpackBuildStart = process.hrtime() - const clientConfig = configs[0] + // We run client and server compilation separately to optimize for memory usage + await runWebpackSpan.traceAsyncFn(async () => { + // Run the server compilers first and then the client + // compiler to track the boundary of server/client components. + let clientResult: SingleCompilerResult | null = null - if ( - clientConfig.optimization && - (clientConfig.optimization.minimize !== true || - (clientConfig.optimization.minimizer && - clientConfig.optimization.minimizer.length === 0)) - ) { - Log.warn( - `Production code optimization has been disabled in your project. Read more: https://nextjs.org/docs/messages/minification-disabled` - ) - } - - webpackBuildStart = process.hrtime() - - // We run client and server compilation separately to optimize for memory usage - await runWebpackSpan.traceAsyncFn(async () => { - // Run the server compilers first and then the client - // compiler to track the boundary of server/client components. - let clientResult: SingleCompilerResult | null = null + // During the server compilations, entries of client components will be + // injected to this set and then will be consumed by the client compiler. + injectedClientEntries.clear() - // During the server compilations, entries of client components will be - // injected to this set and then will be consumed by the client compiler. - injectedClientEntries.clear() + const serverResult = await runCompiler(configs[1], { + runWebpackSpan, + }) + const edgeServerResult = configs[2] + ? await runCompiler(configs[2], { runWebpackSpan }) + : null + + // Only continue if there were no errors + if (!serverResult.errors.length && !edgeServerResult?.errors.length) { + injectedClientEntries.forEach((value, key) => { + const clientEntry = clientConfig.entry as webpack.EntryObject + if (key === APP_CLIENT_INTERNALS) { + clientEntry[CLIENT_STATIC_FILES_RUNTIME_MAIN_APP] = [ + // TODO-APP: cast clientEntry[CLIENT_STATIC_FILES_RUNTIME_MAIN_APP] to type EntryDescription once it's available from webpack + // @ts-expect-error clientEntry['main-app'] is type EntryDescription { import: ... } + ...clientEntry[CLIENT_STATIC_FILES_RUNTIME_MAIN_APP].import, + value, + ] + } else { + clientEntry[key] = { + dependOn: [CLIENT_STATIC_FILES_RUNTIME_MAIN_APP], + import: value, + } + } + }) - const serverResult = await runCompiler(configs[1], { + clientResult = await runCompiler(clientConfig, { runWebpackSpan, }) - const edgeServerResult = configs[2] - ? await runCompiler(configs[2], { runWebpackSpan }) - : null - - // Only continue if there were no errors - if (!serverResult.errors.length && !edgeServerResult?.errors.length) { - injectedClientEntries.forEach((value, key) => { - const clientEntry = clientConfig.entry as webpack.EntryObject - if (key === APP_CLIENT_INTERNALS) { - clientEntry[CLIENT_STATIC_FILES_RUNTIME_MAIN_APP] = [ - // TODO-APP: cast clientEntry[CLIENT_STATIC_FILES_RUNTIME_MAIN_APP] to type EntryDescription once it's available from webpack - // @ts-expect-error clientEntry['main-app'] is type EntryDescription { import: ... } - ...clientEntry[CLIENT_STATIC_FILES_RUNTIME_MAIN_APP].import, - value, - ] - } else { - clientEntry[key] = { - dependOn: [CLIENT_STATIC_FILES_RUNTIME_MAIN_APP], - import: value, - } - } - }) + } - clientResult = await runCompiler(clientConfig, { - runWebpackSpan, - }) - } - - result = { - warnings: ([] as any[]) - .concat( - clientResult?.warnings, - serverResult?.warnings, - edgeServerResult?.warnings - ) - .filter(nonNullable), - errors: ([] as any[]) - .concat( - clientResult?.errors, - serverResult?.errors, - edgeServerResult?.errors - ) - .filter(nonNullable), - stats: [ - clientResult?.stats, - serverResult?.stats, - edgeServerResult?.stats, - ], - } - }) - result = nextBuildSpan - .traceChild('format-webpack-messages') - .traceFn(() => formatWebpackMessages(result, true)) - - NextBuildContext.telemetryPlugin = ( - clientConfig as webpack.Configuration - ).plugins?.find(isTelemetryPlugin) - })() + result = { + warnings: ([] as any[]) + .concat( + clientResult?.warnings, + serverResult?.warnings, + edgeServerResult?.warnings + ) + .filter(nonNullable), + errors: ([] as any[]) + .concat( + clientResult?.errors, + serverResult?.errors, + edgeServerResult?.errors + ) + .filter(nonNullable), + stats: [ + clientResult?.stats, + serverResult?.stats, + edgeServerResult?.stats, + ], + } + }) + result = nextBuildSpan + .traceChild('format-webpack-messages') + .traceFn(() => formatWebpackMessages(result, true)) as CompilerResult + + NextBuildContext.telemetryPlugin = ( + clientConfig as webpack.Configuration + ).plugins?.find(isTelemetryPlugin) const webpackBuildEnd = process.hrtime(webpackBuildStart) if (buildSpinner) {