|
| 1 | +import type { NextConfigComplete } from '../../server/config-shared' |
| 2 | +import type { __ApiPreviewProps } from '../../server/api-utils' |
| 3 | + |
| 4 | +import { trace, setGlobal } from '../../trace' |
| 5 | +import * as Log from '../output/log' |
| 6 | +import * as path from 'node:path' |
| 7 | +import loadConfig from '../../server/config' |
| 8 | +import { PHASE_ANALYZE } from '../../shared/lib/constants' |
| 9 | +import { turbopackAnalyze, type AnalyzeContext } from '../turbopack-analyze' |
| 10 | +import { durationToString } from '../duration-to-string' |
| 11 | +import { cp, writeFile } from 'node:fs/promises' |
| 12 | +import { |
| 13 | + collectAppFiles, |
| 14 | + collectPagesFiles, |
| 15 | + createPagesMapping, |
| 16 | + processLayoutRoutes, |
| 17 | +} from '../entries' |
| 18 | +import { createValidFileMatcher } from '../../server/lib/find-page-file' |
| 19 | +import { findPagesDir } from '../../lib/find-pages-dir' |
| 20 | +import { PAGE_TYPES } from '../../lib/page-types' |
| 21 | +import { pageToRoute } from '../utils' |
| 22 | +import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' |
| 23 | + |
| 24 | +export default async function analyze( |
| 25 | + dir: string, |
| 26 | + reactProductionProfiling = false, |
| 27 | + noMangling = false, |
| 28 | + appDirOnly = false, |
| 29 | + experimentalBuildMode: 'default' | 'compile' | 'generate' | 'generate-env' |
| 30 | +): Promise<void> { |
| 31 | + console.log( |
| 32 | + 'Analyze args dir:', |
| 33 | + dir, |
| 34 | + 'reactProductionProfiling:', |
| 35 | + reactProductionProfiling, |
| 36 | + 'noMangling:', |
| 37 | + noMangling, |
| 38 | + 'appDirOnly:', |
| 39 | + appDirOnly, |
| 40 | + 'experimentalBuildMode:', |
| 41 | + experimentalBuildMode |
| 42 | + ) |
| 43 | + // const analyzeStartTime = Date.now() |
| 44 | + // const telemetry = traceGlobals.get('telemetry') as Telemetry |
| 45 | + try { |
| 46 | + const nextAnalyzeSpan = trace('next-build', undefined, { |
| 47 | + buildMode: experimentalBuildMode, |
| 48 | + version: process.env.__NEXT_VERSION as string, |
| 49 | + }) |
| 50 | + |
| 51 | + await nextAnalyzeSpan.traceAsyncFn(async () => { |
| 52 | + const config: NextConfigComplete = await nextAnalyzeSpan |
| 53 | + .traceChild('load-next-config') |
| 54 | + .traceAsyncFn(() => |
| 55 | + loadConfig(PHASE_ANALYZE, dir, { |
| 56 | + // Log for next.config loading process |
| 57 | + silent: false, |
| 58 | + reactProductionProfiling, |
| 59 | + }) |
| 60 | + ) |
| 61 | + |
| 62 | + // Reading the config can modify environment variables that influence the bundler selection. |
| 63 | + nextAnalyzeSpan.setAttribute('bundler', 'turbopack') |
| 64 | + |
| 65 | + process.env.NEXT_DEPLOYMENT_ID = config.deploymentId || '' |
| 66 | + |
| 67 | + const distDir = path.join(dir, '.next') |
| 68 | + setGlobal('phase', PHASE_ANALYZE) |
| 69 | + setGlobal('distDir', distDir) |
| 70 | + // await nextAnalyzeSpan |
| 71 | + // .traceChild('telemetry-flush') |
| 72 | + // .traceAsyncFn(() => telemetry.flush()) |
| 73 | + |
| 74 | + Log.info('Creating an optimized production build ...') |
| 75 | + |
| 76 | + const analyzeContext: AnalyzeContext = { |
| 77 | + config, |
| 78 | + dir, |
| 79 | + distDir, |
| 80 | + noMangling, |
| 81 | + appDirOnly, |
| 82 | + } |
| 83 | + |
| 84 | + console.log('analyzeContext:', analyzeContext) |
| 85 | + |
| 86 | + let shutdownPromise = Promise.resolve() |
| 87 | + const { duration: analyzeDuration, shutdownPromise: p } = |
| 88 | + await turbopackAnalyze(analyzeContext) |
| 89 | + shutdownPromise = p |
| 90 | + |
| 91 | + const durationString = durationToString(analyzeDuration) |
| 92 | + Log.event(`Compiled successfully in ${durationString}`) |
| 93 | + |
| 94 | + // telemetry.record( |
| 95 | + // eventBuildCompleted(pagesPaths, { |
| 96 | + // bundler: 'turbopack', |
| 97 | + // durationInSeconds: Math.round(compilerDuration), |
| 98 | + // totalAppPagesCount, |
| 99 | + // }) |
| 100 | + // ) |
| 101 | + |
| 102 | + await shutdownPromise |
| 103 | + |
| 104 | + await cp( |
| 105 | + path.join(__dirname, '../../bundle-analyzer'), |
| 106 | + path.join(dir, '.next/diagnostics/analyze'), |
| 107 | + { recursive: true } |
| 108 | + ) |
| 109 | + |
| 110 | + const routes = await collectRoutes(dir, appDirOnly, config) |
| 111 | + |
| 112 | + // Write an index of routes for the route picker |
| 113 | + await writeFile( |
| 114 | + path.join(dir, '.next/diagnostics/analyze/data/routes.json'), |
| 115 | + JSON.stringify( |
| 116 | + routes.appRoutes |
| 117 | + .concat(routes.layoutRoutes) |
| 118 | + .concat(routes.pagesRoutes ?? []) |
| 119 | + .filter((r) => !r.includes('/node_modules/')), |
| 120 | + null, |
| 121 | + 2 |
| 122 | + ) |
| 123 | + ) |
| 124 | + }) |
| 125 | + } catch (e) { |
| 126 | + // const telemetry: Telemetry | undefined = traceGlobals.get('telemetry') |
| 127 | + // if (telemetry) { |
| 128 | + // telemetry.record( |
| 129 | + // eventBuildFailed({ |
| 130 | + // bundler: 'turbopack', |
| 131 | + // errorCode: getErrorCodeForTelemetry(e), |
| 132 | + // durationInSeconds: Math.floor((Date.now() - analyzeStartTime) / 1000), |
| 133 | + // }) |
| 134 | + // ) |
| 135 | + // } |
| 136 | + throw e |
| 137 | + } finally { |
| 138 | + // await flushAllTraces() |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +async function collectRoutes( |
| 143 | + dir: string, |
| 144 | + appDirOnly: boolean, |
| 145 | + config: NextConfigComplete |
| 146 | +): Promise<{ |
| 147 | + appRoutes: string[] |
| 148 | + layoutRoutes: string[] |
| 149 | + pagesRoutes: string[] | null |
| 150 | +}> { |
| 151 | + const { pagesDir, appDir } = findPagesDir(dir) |
| 152 | + |
| 153 | + const validFileMatcher = createValidFileMatcher(config.pageExtensions, appDir) |
| 154 | + |
| 155 | + const { appPaths, layoutPaths } = await collectAppFiles(dir, validFileMatcher) |
| 156 | + |
| 157 | + const pagesPaths = pagesDir |
| 158 | + ? await collectPagesFiles(pagesDir, validFileMatcher) |
| 159 | + : null |
| 160 | + |
| 161 | + const appMapping = await createPagesMapping({ |
| 162 | + pagePaths: appPaths, |
| 163 | + isDev: false, |
| 164 | + pagesType: PAGE_TYPES.APP, |
| 165 | + pageExtensions: config.pageExtensions, |
| 166 | + pagesDir, |
| 167 | + appDir, |
| 168 | + appDirOnly, |
| 169 | + }) |
| 170 | + |
| 171 | + console.log('appMapping:', appMapping) |
| 172 | + |
| 173 | + const layoutsMapping = await createPagesMapping({ |
| 174 | + pagePaths: layoutPaths, |
| 175 | + isDev: false, |
| 176 | + pagesType: PAGE_TYPES.APP, |
| 177 | + pageExtensions: config.pageExtensions, |
| 178 | + pagesDir, |
| 179 | + appDir, |
| 180 | + appDirOnly, |
| 181 | + }) |
| 182 | + |
| 183 | + const pagesMapping = pagesPaths |
| 184 | + ? await createPagesMapping({ |
| 185 | + pagePaths: pagesPaths, |
| 186 | + isDev: false, |
| 187 | + pagesType: PAGE_TYPES.PAGES, |
| 188 | + pageExtensions: config.pageExtensions, |
| 189 | + pagesDir, |
| 190 | + appDir, |
| 191 | + appDirOnly, |
| 192 | + }) |
| 193 | + : null |
| 194 | + |
| 195 | + return { |
| 196 | + appRoutes: Object.keys(appMapping) |
| 197 | + .map(pageToRouteName) |
| 198 | + .map(normalizeAppPath), |
| 199 | + layoutRoutes: processLayoutRoutes(layoutsMapping, dir, false).map( |
| 200 | + (r) => r.route |
| 201 | + ), |
| 202 | + pagesRoutes: pagesMapping |
| 203 | + ? Object.keys(pagesMapping).map(pageToRouteName) |
| 204 | + : null, |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +function pageToRouteName(page: string): string { |
| 209 | + console.log('pageToRouteName page:', page, 'route:', pageToRoute(page)) |
| 210 | + return pageToRoute(page).page |
| 211 | +} |
0 commit comments