UNPKG

next

Version:

The React Framework

915 lines (914 loc) • 133 kB
import '../lib/setup-exception-listeners'; import { loadEnvConfig } from '@next/env'; import { bold, yellow } from '../lib/picocolors'; import crypto from 'crypto'; import { makeRe } from 'next/dist/compiled/picomatch'; import { existsSync, promises as fs } from 'fs'; import os from 'os'; import { Worker } from '../lib/worker'; import { defaultConfig } from '../server/config-shared'; import devalue from 'next/dist/compiled/devalue'; import findUp from 'next/dist/compiled/find-up'; import { nanoid } from 'next/dist/compiled/nanoid/index.cjs'; import { Sema } from 'next/dist/compiled/async-sema'; import path from 'path'; import { STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR, PUBLIC_DIR_MIDDLEWARE_CONFLICT, MIDDLEWARE_FILENAME, PAGES_DIR_ALIAS, INSTRUMENTATION_HOOK_FILENAME, RSC_PREFETCH_SUFFIX, RSC_SUFFIX, NEXT_RESUME_HEADER, PRERENDER_REVALIDATE_HEADER, PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER, NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER, NEXT_CACHE_REVALIDATED_TAGS_HEADER, MATCHED_PATH_HEADER } from '../lib/constants'; import { FileType, fileExists } from '../lib/file-exists'; import { findPagesDir } from '../lib/find-pages-dir'; import loadCustomRoutes, { normalizeRouteRegex } from '../lib/load-custom-routes'; import { nonNullable } from '../lib/non-nullable'; import { recursiveDelete } from '../lib/recursive-delete'; import { verifyPartytownSetup } from '../lib/verify-partytown-setup'; import { validateTurboNextConfig } from '../lib/turbopack-warning'; import { BUILD_ID_FILE, BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, EXPORT_DETAIL, EXPORT_MARKER, IMAGES_MANIFEST, PAGES_MANIFEST, PHASE_PRODUCTION_BUILD, PRERENDER_MANIFEST, REACT_LOADABLE_MANIFEST, ROUTES_MANIFEST, SERVER_DIRECTORY, SERVER_FILES_MANIFEST, STATIC_STATUS_PAGES, MIDDLEWARE_MANIFEST, APP_PATHS_MANIFEST, APP_PATH_ROUTES_MANIFEST, APP_BUILD_MANIFEST, RSC_MODULE_TYPES, NEXT_FONT_MANIFEST, SUBRESOURCE_INTEGRITY_MANIFEST, MIDDLEWARE_BUILD_MANIFEST, MIDDLEWARE_REACT_LOADABLE_MANIFEST, SERVER_REFERENCE_MANIFEST, FUNCTIONS_CONFIG_MANIFEST, UNDERSCORE_NOT_FOUND_ROUTE_ENTRY, UNDERSCORE_NOT_FOUND_ROUTE, DYNAMIC_CSS_MANIFEST } from '../shared/lib/constants'; import { getSortedRoutes, isDynamicRoute, getSortedRouteObjects } from '../shared/lib/router/utils'; import loadConfig from '../server/config'; import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'; import { getPagePath } from '../server/require'; import * as ciEnvironment from '../server/ci-info'; import { turborepoTraceAccess, TurborepoAccessTraceResult, writeTurborepoAccessTraceResult } from './turborepo-access-trace'; import { eventBuildOptimize, eventCliSession, eventBuildFeatureUsage, eventNextPlugins, EVENT_BUILD_FEATURE_USAGE, eventPackageUsedInGetServerSideProps, eventBuildCompleted } from '../telemetry/events'; import { Telemetry } from '../telemetry/storage'; import { hadUnsupportedValue } from './analysis/get-page-static-info'; import { createPagesMapping, getStaticInfoIncludingLayouts, sortByPageExts } from './entries'; import { PAGE_TYPES } from '../lib/page-types'; 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 } from '../trace'; import { detectConflictingPaths, computeFromManifest, getJsPageSizeInKb, printCustomRoutes, printTreeView, copyTracedFiles, isReservedPage, isAppBuiltinNotFoundPage, collectRoutesUsingEdgeRuntime, collectMeta } from './utils'; import { writeBuildId } from './write-build-id'; import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'; import isError from '../lib/is-error'; import { isEdgeRuntime } from '../lib/is-edge-runtime'; import { recursiveCopy } from '../lib/recursive-copy'; import { recursiveReadDir } from '../lib/recursive-readdir'; import { loadBindings, lockfilePatchPromise, teardownTraceSubscriber, teardownHeapProfiler, createDefineEnv } from './swc'; import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'; import { getFilesInDir } from '../lib/get-files-in-dir'; import { eventSwcPlugins } from '../telemetry/events/swc-plugins'; import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'; import { ACTION_HEADER, NEXT_ROUTER_PREFETCH_HEADER, RSC_HEADER, RSC_CONTENT_TYPE_HEADER, NEXT_ROUTER_STATE_TREE_HEADER, NEXT_DID_POSTPONE_HEADER, NEXT_ROUTER_SEGMENT_PREFETCH_HEADER } from '../client/components/app-router-headers'; import { webpackBuild } from './webpack-build'; import { NextBuildContext } from './build-context'; import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'; import { isAppRouteRoute } from '../lib/is-app-route-route'; import { createClientRouterFilter } from '../lib/create-client-router-filter'; import { createValidFileMatcher } from '../server/lib/find-page-file'; import { startTypeChecking } from './type-check'; import { generateInterceptionRoutesRewrites } from '../lib/generate-interception-routes-rewrites'; import { buildDataRoute } from '../server/lib/router-utils/build-data-route'; import { collectBuildTraces } from './collect-build-traces'; import { formatManifest } from './manifests/formatter/format-manifest'; import { recordFrameworkVersion, updateBuildDiagnostics, recordFetchMetrics } from '../diagnostics/build-diagnostics'; import { getStartServerInfo, logStartInfo } from '../server/lib/app-info-log'; import { hasCustomExportOutput } from '../export/utils'; import { getTurbopackJsConfig, handleEntrypoints, handleRouteType, handlePagesErrorRoute, formatIssue, isRelevantWarning, isPersistentCachingEnabled } from '../server/dev/turbopack-utils'; import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader'; import { buildCustomRoute } from '../lib/build-custom-route'; import { createProgress } from './progress'; import { traceMemoryUsage } from '../lib/memory/trace'; import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils-server'; import uploadTrace from '../trace/upload-trace'; import { checkIsAppPPREnabled, checkIsRoutePPREnabled } from '../server/lib/experimental/ppr'; import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback'; import { RenderingMode } from './rendering-mode'; import { getParamKeys } from '../server/request/fallback-params'; import { formatNodeOptions, getParsedNodeOptionsWithoutInspect } from '../server/lib/utils'; /** * The headers that are allowed to be used when revalidating routes. Currently * this includes both headers used by the pages and app routers. */ const ALLOWED_HEADERS = [ 'host', MATCHED_PATH_HEADER, PRERENDER_REVALIDATE_HEADER, PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER, NEXT_CACHE_REVALIDATED_TAGS_HEADER, NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER ]; function pageToRoute(page) { const routeRegex = getNamedRouteRegex(page, true); return { page, regex: normalizeRouteRegex(routeRegex.re.source), routeKeys: routeRegex.routeKeys, namedRegex: routeRegex.namedRegex }; } function getCacheDir(distDir) { const cacheDir = path.join(distDir, 'cache'); if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) { const hasCache = existsSync(cacheDir); if (!hasCache) { // Intentionally not piping to stderr which is what `Log.warn` does in case people fail in CI when // stderr is detected. console.log(`${Log.prefixes.warn} No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache`); } } return cacheDir; } async function writeFileUtf8(filePath, content) { await fs.writeFile(filePath, content, 'utf-8'); } function readFileUtf8(filePath) { return fs.readFile(filePath, 'utf8'); } async function writeManifest(filePath, manifest) { await writeFileUtf8(filePath, formatManifest(manifest)); } async function readManifest(filePath) { return JSON.parse(await readFileUtf8(filePath)); } async function writePrerenderManifest(distDir, manifest) { await writeManifest(path.join(distDir, PRERENDER_MANIFEST), manifest); } async function writeClientSsgManifest(prerenderManifest, { buildId, distDir, locales }) { const ssgPages = new Set([ ...Object.entries(prerenderManifest.routes)// Filter out dynamic routes .filter(([, { srcRoute }])=>srcRoute == null).map(([route])=>normalizeLocalePath(route, locales).pathname), ...Object.keys(prerenderManifest.dynamicRoutes) ].sort()); const clientSsgManifestContent = `self.__SSG_MANIFEST=${devalue(ssgPages)};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`; await writeFileUtf8(path.join(distDir, CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js'), clientSsgManifestContent); } async function writeFunctionsConfigManifest(distDir, manifest) { await writeManifest(path.join(distDir, SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST), manifest); } async function writeRequiredServerFilesManifest(distDir, requiredServerFiles) { await writeManifest(path.join(distDir, SERVER_FILES_MANIFEST), requiredServerFiles); } async function writeImagesManifest(distDir, config) { var _config_images, _config_images1; const images = { ...config.images }; const { deviceSizes, imageSizes } = images; images.sizes = [ ...deviceSizes, ...imageSizes ]; // By default, remotePatterns will allow no remote images ([]) images.remotePatterns = ((config == null ? void 0 : (_config_images = config.images) == null ? void 0 : _config_images.remotePatterns) || []).map((p)=>({ // Modifying the manifest should also modify matchRemotePattern() protocol: p.protocol, hostname: makeRe(p.hostname).source, port: p.port, pathname: makeRe(p.pathname ?? '**', { dot: true }).source, search: p.search })); // By default, localPatterns will allow all local images (undefined) if (config == null ? void 0 : (_config_images1 = config.images) == null ? void 0 : _config_images1.localPatterns) { images.localPatterns = config.images.localPatterns.map((p)=>({ // Modifying the manifest should also modify matchLocalPattern() pathname: makeRe(p.pathname ?? '**', { dot: true }).source, search: p.search })); } await writeManifest(path.join(distDir, IMAGES_MANIFEST), { version: 1, images }); } const STANDALONE_DIRECTORY = 'standalone'; async function writeStandaloneDirectory(nextBuildSpan, distDir, pageKeys, denormalizedAppPages, outputFileTracingRoot, requiredServerFiles, middlewareManifest, hasInstrumentationHook, staticPages, loadedEnvFiles, appDir) { await nextBuildSpan.traceChild('write-standalone-directory').traceAsyncFn(async ()=>{ await copyTracedFiles(// requiredServerFiles.appDir Refers to the application directory, not App Router. requiredServerFiles.appDir, distDir, pageKeys.pages, denormalizedAppPages, outputFileTracingRoot, requiredServerFiles.config, middlewareManifest, hasInstrumentationHook, staticPages); for (const file of [ ...requiredServerFiles.files, path.join(requiredServerFiles.config.distDir, SERVER_FILES_MANIFEST), ...loadedEnvFiles.reduce((acc, envFile)=>{ if ([ '.env', '.env.production' ].includes(envFile.path)) { acc.push(envFile.path); } return acc; }, []) ]){ // requiredServerFiles.appDir Refers to the application directory, not App Router. const filePath = path.join(requiredServerFiles.appDir, file); const outputPath = path.join(distDir, STANDALONE_DIRECTORY, path.relative(outputFileTracingRoot, filePath)); await fs.mkdir(path.dirname(outputPath), { recursive: true }); await fs.copyFile(filePath, outputPath); } await recursiveCopy(path.join(distDir, SERVER_DIRECTORY, 'pages'), path.join(distDir, STANDALONE_DIRECTORY, path.relative(outputFileTracingRoot, distDir), SERVER_DIRECTORY, 'pages'), { overwrite: true }); if (appDir) { const originalServerApp = path.join(distDir, SERVER_DIRECTORY, 'app'); if (existsSync(originalServerApp)) { await recursiveCopy(originalServerApp, path.join(distDir, STANDALONE_DIRECTORY, path.relative(outputFileTracingRoot, distDir), SERVER_DIRECTORY, 'app'), { overwrite: true }); } } }); } function getNumberOfWorkers(config) { if (config.experimental.cpus && config.experimental.cpus !== defaultConfig.experimental.cpus) { return config.experimental.cpus; } if (config.experimental.memoryBasedWorkersCount) { return Math.max(Math.min(config.experimental.cpus || 1, Math.floor(os.freemem() / 1e9)), // enforce a minimum of 4 workers 4); } if (config.experimental.cpus) { return config.experimental.cpus; } // Fall back to 4 workers if a count is not specified return 4; } const staticWorkerPath = require.resolve('./worker'); const staticWorkerExposedMethods = [ 'hasCustomGetInitialProps', 'isPageStatic', 'getDefinedNamedExports', 'exportPages' ]; export function createStaticWorker(config, progress) { // Get the node options without inspect and also remove the // --max-old-space-size flag as it can cause memory issues. const nodeOptions = getParsedNodeOptionsWithoutInspect(); delete nodeOptions['max-old-space-size']; delete nodeOptions['max_old_space_size']; return new Worker(staticWorkerPath, { logger: Log, numWorkers: getNumberOfWorkers(config), onActivity: ()=>{ progress == null ? void 0 : progress.run(); }, onActivityAbort: ()=>{ progress == null ? void 0 : progress.clear(); }, forkOptions: { env: { ...process.env, NODE_OPTIONS: formatNodeOptions(nodeOptions) } }, enableWorkerThreads: config.experimental.workerThreads, exposedMethods: staticWorkerExposedMethods }); } async function writeFullyStaticExport(config, dir, enabledDirectories, configOutDir, nextBuildSpan) { const exportApp = require('../export').default; const pagesWorker = createStaticWorker(config); const appWorker = createStaticWorker(config); await exportApp(dir, { buildExport: false, nextConfig: config, enabledDirectories, silent: true, outdir: path.join(dir, configOutDir), numWorkers: getNumberOfWorkers(config) }, nextBuildSpan); pagesWorker.end(); appWorker.end(); } async function getBuildId(isGenerateMode, distDir, nextBuildSpan, config) { if (isGenerateMode) { return await fs.readFile(path.join(distDir, 'BUILD_ID'), 'utf8'); } return await nextBuildSpan.traceChild('generate-buildid').traceAsyncFn(()=>generateBuildId(config.generateBuildId, nanoid)); } const IS_TURBOPACK_BUILD = process.env.TURBOPACK && process.env.TURBOPACK_BUILD; export default async function build(dir, reactProductionProfiling = false, debugOutput = false, runLint = true, noMangling = false, appDirOnly = false, turboNextBuild = false, experimentalBuildMode, traceUploadUrl) { const isCompileMode = experimentalBuildMode === 'compile'; const isGenerateMode = experimentalBuildMode === 'generate'; let loadedConfig; try { const nextBuildSpan = trace('next-build', undefined, { buildMode: experimentalBuildMode, isTurboBuild: String(turboNextBuild), version: "15.1.7" }); NextBuildContext.nextBuildSpan = nextBuildSpan; NextBuildContext.dir = dir; NextBuildContext.appDirOnly = appDirOnly; NextBuildContext.reactProductionProfiling = reactProductionProfiling; NextBuildContext.noMangling = noMangling; await nextBuildSpan.traceAsyncFn(async ()=>{ var _mappedPages_404; // 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 turborepoAccessTraceResult = new TurborepoAccessTraceResult(); const config = await nextBuildSpan.traceChild('load-next-config').traceAsyncFn(()=>turborepoTraceAccess(()=>loadConfig(PHASE_PRODUCTION_BUILD, dir, { // Log for next.config loading process silent: false, reactProductionProfiling }), turborepoAccessTraceResult)); loadedConfig = config; process.env.NEXT_DEPLOYMENT_ID = config.deploymentId || ''; NextBuildContext.config = config; let configOutDir = 'out'; if (hasCustomExportOutput(config)) { configOutDir = config.distDir; config.distDir = '.next'; } const distDir = path.join(dir, config.distDir); setGlobal('phase', PHASE_PRODUCTION_BUILD); setGlobal('distDir', distDir); const buildId = await getBuildId(isGenerateMode, distDir, nextBuildSpan, config); NextBuildContext.buildId = buildId; const customRoutes = await nextBuildSpan.traceChild('load-custom-routes').traceAsyncFn(()=>loadCustomRoutes(config)); const { headers, rewrites, redirects } = customRoutes; const combinedRewrites = [ ...rewrites.beforeFiles, ...rewrites.afterFiles, ...rewrites.fallback ]; const hasRewrites = combinedRewrites.length > 0; NextBuildContext.originalRewrites = config._originalRewrites; NextBuildContext.originalRedirects = config._originalRedirects; const cacheDir = getCacheDir(distDir); const telemetry = new Telemetry({ distDir }); setGlobal('telemetry', telemetry); const publicDir = path.join(dir, 'public'); const { pagesDir, appDir } = findPagesDir(dir); NextBuildContext.pagesDir = pagesDir; NextBuildContext.appDir = appDir; const enabledDirectories = { app: typeof appDir === 'string', pages: typeof pagesDir === 'string' }; // Generate a random encryption key for this build. // This key is used to encrypt cross boundary values and can be used to generate hashes. const encryptionKey = await generateEncryptionKeyBase64({ isBuild: true, distDir }); NextBuildContext.encryptionKey = encryptionKey; const isSrcDir = path.relative(dir, pagesDir || appDir || '').startsWith('src'); const hasPublicDir = existsSync(publicDir); telemetry.record(eventCliSession(dir, config, { webpackVersion: 5, cliCommand: 'build', isSrcDir, hasNowJson: !!await findUp('now.json', { cwd: dir }), isCustomServer: null, turboFlag: false, pagesDir: !!pagesDir, appDir: !!appDir })); eventNextPlugins(path.resolve(dir)).then((events)=>telemetry.record(events)); eventSwcPlugins(path.resolve(dir), config).then((events)=>telemetry.record(events)); // Always log next version first then start rest jobs const { envInfo, expFeatureInfo } = await getStartServerInfo(dir, false); logStartInfo({ networkUrl: null, appUrl: null, envInfo, expFeatureInfo }); const ignoreESLint = Boolean(config.eslint.ignoreDuringBuilds); const shouldLint = !ignoreESLint && runLint; const typeCheckingOptions = { dir, appDir, pagesDir, runLint, shouldLint, ignoreESLint, telemetry, nextBuildSpan, config, cacheDir }; const distDirCreated = await nextBuildSpan.traceChild('create-dist-dir').traceAsyncFn(async ()=>{ try { await fs.mkdir(distDir, { recursive: true }); return true; } catch (err) { if (isError(err) && err.code === 'EPERM') { return false; } throw err; } }); if (!distDirCreated || !await isWriteable(distDir)) { throw new Error('> Build directory is not writeable. https://nextjs.org/docs/messages/build-dir-not-writeable'); } if (config.cleanDistDir && !isGenerateMode) { await recursiveDelete(distDir, /^cache/); } // For app directory, we run type checking after build. That's because // we dynamically generate types for each layout and page in the app // directory. if (!appDir && !isCompileMode) await startTypeChecking(typeCheckingOptions); if (appDir && 'exportPathMap' in config) { Log.error('The "exportPathMap" configuration cannot be used with the "app" directory. Please use generateStaticParams() instead.'); await telemetry.flush(); process.exit(1); } const buildLintEvent = { featureName: 'build-lint', invocationCount: shouldLint ? 1 : 0 }; telemetry.record({ eventName: EVENT_BUILD_FEATURE_USAGE, payload: buildLintEvent }); const validFileMatcher = createValidFileMatcher(config.pageExtensions, appDir); const providedPagePaths = JSON.parse(process.env.NEXT_PRIVATE_PAGE_PATHS || '[]'); let pagesPaths = Boolean(process.env.NEXT_PRIVATE_PAGE_PATHS) ? providedPagePaths : !appDirOnly && pagesDir ? await nextBuildSpan.traceChild('collect-pages').traceAsyncFn(()=>recursiveReadDir(pagesDir, { pathnameFilter: validFileMatcher.isPageFile })) : []; const middlewareDetectionRegExp = new RegExp(`^${MIDDLEWARE_FILENAME}\\.(?:${config.pageExtensions.join('|')})$`); const instrumentationHookDetectionRegExp = new RegExp(`^${INSTRUMENTATION_HOOK_FILENAME}\\.(?:${config.pageExtensions.join('|')})$`); const rootDir = path.join(pagesDir || appDir, '..'); const includes = [ middlewareDetectionRegExp, instrumentationHookDetectionRegExp ]; const rootPaths = Array.from(await getFilesInDir(rootDir)).filter((file)=>includes.some((include)=>include.test(file))).sort(sortByPageExts(config.pageExtensions)).map((file)=>path.join(rootDir, file).replace(dir, '')); const hasInstrumentationHook = rootPaths.some((p)=>p.includes(INSTRUMENTATION_HOOK_FILENAME)); const hasMiddlewareFile = rootPaths.some((p)=>p.includes(MIDDLEWARE_FILENAME)); NextBuildContext.hasInstrumentationHook = hasInstrumentationHook; const previewProps = { previewModeId: crypto.randomBytes(16).toString('hex'), previewModeSigningKey: crypto.randomBytes(32).toString('hex'), previewModeEncryptionKey: crypto.randomBytes(32).toString('hex') }; NextBuildContext.previewProps = previewProps; const mappedPages = await nextBuildSpan.traceChild('create-pages-mapping').traceAsyncFn(()=>createPagesMapping({ isDev: false, pageExtensions: config.pageExtensions, pagesType: PAGE_TYPES.PAGES, pagePaths: pagesPaths, pagesDir, appDir })); NextBuildContext.mappedPages = mappedPages; let mappedAppPages; let denormalizedAppPages; if (appDir) { const providedAppPaths = JSON.parse(process.env.NEXT_PRIVATE_APP_PATHS || '[]'); let appPaths = Boolean(process.env.NEXT_PRIVATE_APP_PATHS) ? providedAppPaths : await nextBuildSpan.traceChild('collect-app-paths').traceAsyncFn(()=>recursiveReadDir(appDir, { pathnameFilter: (absolutePath)=>validFileMatcher.isAppRouterPage(absolutePath) || // For now we only collect the root /not-found page in the app // directory as the 404 fallback validFileMatcher.isRootNotFound(absolutePath), ignorePartFilter: (part)=>part.startsWith('_') })); mappedAppPages = await nextBuildSpan.traceChild('create-app-mapping').traceAsyncFn(()=>createPagesMapping({ pagePaths: appPaths, isDev: false, pagesType: PAGE_TYPES.APP, pageExtensions: config.pageExtensions, pagesDir, appDir })); NextBuildContext.mappedAppPages = mappedAppPages; } const mappedRootPaths = await createPagesMapping({ isDev: false, pageExtensions: config.pageExtensions, pagePaths: rootPaths, pagesType: PAGE_TYPES.ROOT, pagesDir: pagesDir, appDir }); NextBuildContext.mappedRootPaths = mappedRootPaths; const pagesPageKeys = Object.keys(mappedPages); const conflictingAppPagePaths = []; const appPageKeys = new Set(); if (mappedAppPages) { denormalizedAppPages = Object.keys(mappedAppPages); for (const appKey of denormalizedAppPages){ const normalizedAppPageKey = normalizeAppPath(appKey); const pagePath = mappedPages[normalizedAppPageKey]; if (pagePath) { const appPath = mappedAppPages[appKey]; conflictingAppPagePaths.push([ pagePath.replace(/^private-next-pages/, 'pages'), appPath.replace(/^private-next-app-dir/, 'app') ]); } appPageKeys.add(normalizedAppPageKey); } } const appPaths = Array.from(appPageKeys); // Interception routes are modelled as beforeFiles rewrites rewrites.beforeFiles.push(...generateInterceptionRoutesRewrites(appPaths, config.basePath)); NextBuildContext.rewrites = rewrites; const totalAppPagesCount = appPaths.length; const pageKeys = { pages: pagesPageKeys, app: appPaths.length > 0 ? appPaths : undefined }; // Turbopack already handles conflicting app and page routes. if (!IS_TURBOPACK_BUILD) { const numConflictingAppPaths = conflictingAppPagePaths.length; if (mappedAppPages && numConflictingAppPaths > 0) { Log.error(`Conflicting app and page file${numConflictingAppPaths === 1 ? ' was' : 's were'} found, please remove the conflicting files to continue:`); for (const [pagePath, appPath] of conflictingAppPagePaths){ Log.error(` "${pagePath}" - "${appPath}"`); } await telemetry.flush(); process.exit(1); } } const conflictingPublicFiles = []; const hasPages404 = (_mappedPages_404 = mappedPages['/404']) == null ? void 0 : _mappedPages_404.startsWith(PAGES_DIR_ALIAS); const hasApp404 = !!(mappedAppPages == null ? void 0 : mappedAppPages[UNDERSCORE_NOT_FOUND_ROUTE_ENTRY]); const hasCustomErrorPage = mappedPages['/_error'].startsWith(PAGES_DIR_ALIAS); if (hasPublicDir) { const hasPublicUnderScoreNextDir = existsSync(path.join(publicDir, '_next')); if (hasPublicUnderScoreNextDir) { throw new Error(PUBLIC_DIR_MIDDLEWARE_CONFLICT); } } await nextBuildSpan.traceChild('public-dir-conflict-check').traceAsyncFn(async ()=>{ // Check if pages conflict with files in `public` // Only a page of public file can be served, not both. for(const page in mappedPages){ const hasPublicPageFile = await fileExists(path.join(publicDir, page === '/' ? '/index' : page), FileType.File); if (hasPublicPageFile) { conflictingPublicFiles.push(page); } } const numConflicting = conflictingPublicFiles.length; if (numConflicting) { throw new Error(`Conflicting public and page file${numConflicting === 1 ? ' was' : 's were'} found. https://nextjs.org/docs/messages/conflicting-public-file-page\n${conflictingPublicFiles.join('\n')}`); } }); const nestedReservedPages = pageKeys.pages.filter((page)=>{ return page.match(/\/(_app|_document|_error)$/) && path.dirname(page) !== '/'; }); if (nestedReservedPages.length) { Log.warn(`The following reserved Next.js pages were detected not directly under the pages directory:\n` + nestedReservedPages.join('\n') + `\nSee more info here: https://nextjs.org/docs/messages/nested-reserved-page\n`); } const restrictedRedirectPaths = [ '/_next' ].map((p)=>config.basePath ? `${config.basePath}${p}` : p); const isAppDynamicIOEnabled = Boolean(config.experimental.dynamicIO); const isAuthInterruptsEnabled = Boolean(config.experimental.authInterrupts); const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr); const routesManifestPath = path.join(distDir, ROUTES_MANIFEST); const routesManifest = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(()=>{ const sortedRoutes = getSortedRoutes([ ...pageKeys.pages, ...pageKeys.app ?? [] ]); const dynamicRoutes = []; const staticRoutes = []; for (const route of sortedRoutes){ if (isDynamicRoute(route)) { dynamicRoutes.push(pageToRoute(route)); } else if (!isReservedPage(route)) { staticRoutes.push(pageToRoute(route)); } } return { version: 3, pages404: true, caseSensitive: !!config.experimental.caseSensitiveRoutes, basePath: config.basePath, redirects: redirects.map((r)=>buildCustomRoute('redirect', r, restrictedRedirectPaths)), headers: headers.map((r)=>buildCustomRoute('header', r)), dynamicRoutes, staticRoutes, dataRoutes: [], i18n: config.i18n || undefined, rsc: { header: RSC_HEADER, // This vary header is used as a default. It is technically re-assigned in `base-server`, // and may include an additional Vary option for `Next-URL`. varyHeader: `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}, ${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}`, prefetchHeader: NEXT_ROUTER_PREFETCH_HEADER, didPostponeHeader: NEXT_DID_POSTPONE_HEADER, contentTypeHeader: RSC_CONTENT_TYPE_HEADER, suffix: RSC_SUFFIX, prefetchSuffix: RSC_PREFETCH_SUFFIX }, skipMiddlewareUrlNormalize: config.skipMiddlewareUrlNormalize, ppr: isAppPPREnabled ? { chain: { headers: { [NEXT_RESUME_HEADER]: '1' } } } : undefined }; }); if (rewrites.beforeFiles.length === 0 && rewrites.fallback.length === 0) { routesManifest.rewrites = rewrites.afterFiles.map((r)=>buildCustomRoute('rewrite', r)); } else { routesManifest.rewrites = { beforeFiles: rewrites.beforeFiles.map((r)=>buildCustomRoute('rewrite', r)), afterFiles: rewrites.afterFiles.map((r)=>buildCustomRoute('rewrite', r)), fallback: rewrites.fallback.map((r)=>buildCustomRoute('rewrite', r)) }; } let clientRouterFilters; if (config.experimental.clientRouterFilter) { const nonInternalRedirects = (config._originalRedirects || []).filter((r)=>!r.internal); clientRouterFilters = createClientRouterFilter([ ...appPaths ], config.experimental.clientRouterFilterRedirects ? nonInternalRedirects : [], config.experimental.clientRouterFilterAllowedRate); NextBuildContext.clientRouterFilters = clientRouterFilters; } // Ensure commonjs handling is used for files in the distDir (generally .next) // Files outside of the distDir can be "type": "module" await writeFileUtf8(path.join(distDir, 'package.json'), '{"type": "commonjs"}'); // These are written to distDir, so they need to come after creating and cleaning distDr. await recordFrameworkVersion("15.1.7"); await updateBuildDiagnostics({ buildStage: 'start' }); const outputFileTracingRoot = config.outputFileTracingRoot || dir; const pagesManifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST); async function turbopackBuild() { var _config_experimental, _config_experimental_turbo, _config_experimental1, _config_experimental_turbo1; if (!IS_TURBOPACK_BUILD) { throw new Error("next build doesn't support turbopack yet"); } await validateTurboNextConfig({ dir, isDev: false }); const startTime = process.hrtime(); const bindings = await loadBindings(config == null ? void 0 : (_config_experimental = config.experimental) == null ? void 0 : _config_experimental.useWasmBinary); const dev = false; // const supportedBrowsers = await getSupportedBrowsers(dir, dev) const supportedBrowsers = [ 'last 1 Chrome versions, last 1 Firefox versions, last 1 Safari versions, last 1 Edge versions' ]; const project = await bindings.turbo.createProject({ projectPath: dir, rootPath: ((_config_experimental1 = config.experimental) == null ? void 0 : (_config_experimental_turbo = _config_experimental1.turbo) == null ? void 0 : _config_experimental_turbo.root) || config.outputFileTracingRoot || dir, distDir, nextConfig: config, jsConfig: await getTurbopackJsConfig(dir, config), watch: { enable: false }, dev, env: process.env, defineEnv: createDefineEnv({ isTurbopack: true, clientRouterFilters, config, dev, distDir, fetchCacheKeyPrefix: config.experimental.fetchCacheKeyPrefix, hasRewrites, // Implemented separately in Turbopack, doesn't have to be passed here. middlewareMatchers: undefined }), buildId: NextBuildContext.buildId, encryptionKey: NextBuildContext.encryptionKey, previewProps: NextBuildContext.previewProps, browserslistQuery: supportedBrowsers.join(', ') }, { persistentCaching: isPersistentCachingEnabled(config), memoryLimit: (_config_experimental_turbo1 = config.experimental.turbo) == null ? void 0 : _config_experimental_turbo1.memoryLimit }); await fs.mkdir(path.join(distDir, 'server'), { recursive: true }); await fs.mkdir(path.join(distDir, 'static', buildId), { recursive: true }); await fs.writeFile(path.join(distDir, 'package.json'), JSON.stringify({ type: 'commonjs' }, null, 2)); // eslint-disable-next-line @typescript-eslint/no-unused-vars const entrypointsSubscription = project.entrypointsSubscribe(); const currentEntrypoints = { global: { app: undefined, document: undefined, error: undefined, middleware: undefined, instrumentation: undefined }, app: new Map(), page: new Map() }; const currentEntryIssues = new Map(); const manifestLoader = new TurbopackManifestLoader({ buildId, distDir, encryptionKey }); const entrypointsResult = await entrypointsSubscription.next(); if (entrypointsResult.done) { throw new Error('Turbopack did not return any entrypoints'); } entrypointsSubscription.return == null ? void 0 : entrypointsSubscription.return.call(entrypointsSubscription).catch(()=>{}); const entrypoints = entrypointsResult.value; const topLevelErrors = []; for (const issue of entrypoints.issues){ topLevelErrors.push({ message: formatIssue(issue) }); } if (topLevelErrors.length > 0) { throw new Error(`Turbopack build failed with ${topLevelErrors.length} issues:\n${topLevelErrors.map((e)=>e.message).join('\n')}`); } await handleEntrypoints({ entrypoints, currentEntrypoints, currentEntryIssues, manifestLoader, devRewrites: undefined, productionRewrites: customRoutes.rewrites, logErrors: false }); const progress = createProgress(currentEntrypoints.page.size + currentEntrypoints.app.size + 1, 'Building'); const promises = []; // Concurrency will start at INITIAL_CONCURRENCY and // slowly ramp up to CONCURRENCY by increasing the // concurrency by 1 every time a task is completed. const INITIAL_CONCURRENCY = 5; const CONCURRENCY = 10; const sema = new Sema(INITIAL_CONCURRENCY); let remainingRampup = CONCURRENCY - INITIAL_CONCURRENCY; const enqueue = (fn)=>{ promises.push((async ()=>{ await sema.acquire(); try { await fn(); } finally{ sema.release(); if (remainingRampup > 0) { remainingRampup--; sema.release(); } progress.run(); } })()); }; if (!appDirOnly) { for (const [page, route] of currentEntrypoints.page){ enqueue(()=>handleRouteType({ dev, page, pathname: page, route, currentEntryIssues, entrypoints: currentEntrypoints, manifestLoader, devRewrites: undefined, productionRewrites: customRoutes.rewrites, logErrors: false })); } } for (const [page, route] of currentEntrypoints.app){ enqueue(()=>handleRouteType({ page, dev: false, pathname: normalizeAppPath(page), route, currentEntryIssues, entrypoints: currentEntrypoints, manifestLoader, devRewrites: undefined, productionRewrites: customRoutes.rewrites, logErrors: false })); } enqueue(()=>handlePagesErrorRoute({ dev: false, currentEntryIssues, entrypoints: currentEntrypoints, manifestLoader, devRewrites: undefined, productionRewrites: customRoutes.rewrites, logErrors: false })); await Promise.all(promises); await manifestLoader.writeManifests({ devRewrites: undefined, productionRewrites: customRoutes.rewrites, entrypoints: currentEntrypoints }); const errors = []; const warnings = []; for (const [page, entryIssues] of currentEntryIssues){ for (const issue of entryIssues.values()){ if (issue.severity !== 'warning') { errors.push({ page, message: formatIssue(issue) }); } else { if (isRelevantWarning(issue)) { warnings.push({ page, message: formatIssue(issue) }); } } } } const shutdownPromise = project.shutdown(); if (warnings.length > 0) { Log.warn(`Turbopack build collected ${warnings.length} warnings:\n${warnings.map((e)=>{ return 'Page: ' + e.page + '\n' + e.message; }).join('\n')}`); } if (errors.length > 0) { throw new Error(`Turbopack build failed with ${errors.length} errors:\n${errors.map((e)=>{ return 'Page: ' + e.page + '\n' + e.message; }).join('\n')}`); } const time = process.hrtime(startTime); return { duration: time[0] + time[1] / 1e9, buildTraceContext: undefined, shutdownPromise }; } let buildTraceContext; let buildTracesPromise = undefined; // If there's has a custom webpack config and disable the build worker. // Otherwise respect the option if it's set. const useBuildWorker = config.experimental.webpackBuildWorker || config.experimental.webpackBuildWorker === undefined && !config.webpack; const runServerAndEdgeInParallel = config.experimental.parallelServerCompiles; const collectServerBuildTracesInParallel = config.experimental.parallelServerBuildTraces || config.experimental.parallelServerBuildTraces === undefined && isCompileMode; nextBuildSpan.setAttribute('has-custom-webpack-config', String(!!config.webpack)); nextBuildSpan.setAttribute('use-build-worker', String(useBuildWorker)); if (!useBuildWorker && (runServerAndEdgeInParallel || collectServerBuildTracesInParallel)) { throw new Error('The "parallelServerBuildTraces" and "parallelServerCompiles" options may only be used when build workers can be used. Read more: https://nextjs.org/docs/messages/parallel-build-without-worker'); } Log.info('Creating an optimized production build ...'); traceMemoryUsage('Starting build', nextBuildSpan); await updateBuildDiagnostics({ buildStage: 'compile', buildOptions: { useBuildWorker: String(useBuildWorker) } }); let shutdownPromise = Promise.resolve(); if (!isGenerateMode) { if (turboNextBuild) { const { duration: compilerDuration, shutdownPromise: p, ...rest } = await turbopackBuild(); shutdownPromise = p; traceMemoryUsage('Finished build', nextBuildSpan); buildTraceContext = rest.buildTraceContext; let durationString; if (compilerDuration > 120) { durationString = `${Math.round(compilerDuration / 6) / 10}min`; } else if (compilerDuration > 20) { durationString = `${Math.round(compilerDuration)}s`; } else if (compilerDuration > 2) { durationString = `${Math.round(compilerDuration * 10) / 10}s`; } else { durationString = `${Math.round(compilerDuration * 1000)}ms`; } Log.event(`Compiled successfully in ${durationString}`); telemetry.record(eventBuildCompleted(pagesPaths, { durationInSeconds: Math.round(compilerDuration), totalAppPagesCount })); } else { if (runServerAndEdgeInParallel || collectServerBuildTracesInParallel) { let durationInSeconds = 0; await updateBuildDiagnostics({ buildStage: 'compile-server' }); const serverBuildPromise = webpackBuild(useBuildWorker, [ 'server' ]).then((res)=>{ traceMemoryUsage('Finished server compilation', nextBuildSpan); buildTraceContext = res.buildTraceContext; durationInSeconds += res.duration; if (collectServerBuildTracesInParallel) { const buildTraceWorker = new Worker(require.resolve('./collect-build-traces'), { numWorkers: 1, exposedMethods: [ 'collectBuildTraces' ] }); buildTracesPromise = buildTraceWorker.collectBuildTraces({ dir, config, distDir, // Serialize Map as this is sent to the worker. edgeRuntimeRoutes: collectRoutesUsingEdgeRuntime(new Map()), staticPages: [], hasSsrAmpPages: false, buildTraceContext, outputFileTracingRoot }).catch((err)=>{ console.error(err); process.exit(1); });