| import type { AnalyzeData, ModuleIndex, ModulesData } from './analyze-data' |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function computeActiveEntries( |
| modulesData: ModulesData, |
| analyzeData: AnalyzeData |
| ): ModuleIndex[] { |
| const potentialEntryDependents = [ |
| 'next/dist/esm/build/templates/pages.js', |
| 'next/dist/esm/build/templates/pages-api.js', |
| 'next/dist/esm/build/templates/pages-edge-api.js', |
| 'next/dist/esm/build/templates/edge-ssr.js', |
| 'next/dist/esm/build/templates/app-route.js', |
| 'next/dist/esm/build/templates/edge-app-route.js', |
| 'next/dist/esm/build/templates/app-page.js', |
| 'next/dist/esm/build/templates/edge-ssr-app.js', |
| 'next/dist/esm/build/templates/middleware.js', |
| '[next]/entry/page-loader.ts', |
| ] |
| const potentialEntries = [ |
| 'next/dist/client/app-next-turbopack.js', |
| 'next/dist/client/next-turbopack.js', |
| ] |
|
|
| const activeEntries = new Set<ModuleIndex>() |
|
|
| for ( |
| let moduleIndex = 0; |
| moduleIndex < modulesData.moduleCount(); |
| moduleIndex++ |
| ) { |
| const ident = modulesData.module(moduleIndex)?.ident |
| if (ident == null) { |
| continue |
| } |
|
|
| if ( |
| potentialEntryDependents.some((entryIdent) => ident.includes(entryIdent)) |
| ) { |
| const dependencies = modulesData.moduleDependencies(moduleIndex) |
| for (const dep of dependencies) { |
| const path = modulesData.module(dep)!.path |
| if (path.includes('next/dist/')) { |
| continue |
| } |
| const source = analyzeData.getSourceIndexFromPath(path) |
| if (source !== undefined) { |
| activeEntries.add(dep) |
| } |
| } |
| } |
| if (potentialEntries.some((entryIdent) => ident.includes(entryIdent))) { |
| activeEntries.add(moduleIndex) |
| } |
| } |
|
|
| return Array.from(activeEntries) |
| } |
|
|
| |
| |
| |
| |
| |
| export function computeModuleDepthMap( |
| modulesData: ModulesData, |
| activeEntries: ModuleIndex[] |
| ): Map<ModuleIndex, number> { |
| const depthMap = new Map<ModuleIndex, number>() |
| const delayedModules = new Array<{ depth: number; queue: ModuleIndex[] }>() |
|
|
| |
| for (const moduleIndex of activeEntries) { |
| depthMap.set(moduleIndex, 0) |
| } |
|
|
| |
| |
| |
| |
| let i = 0 |
| for (const [moduleIndex, depth] of depthMap) { |
| const newDepth = depth + 1 |
| |
| const dependencies = modulesData.moduleDependencies(moduleIndex) |
| for (const depIndex of dependencies) { |
| if (!depthMap.has(depIndex)) { |
| depthMap.set(depIndex, newDepth) |
| } |
| } |
|
|
| |
| const asyncDependencies = modulesData.asyncModuleDependencies(moduleIndex) |
| for (const depIndex of asyncDependencies) { |
| if (!depthMap.has(depIndex)) { |
| const newDepth = depth + 1000 |
| |
| |
| |
| |
| let delayedQueue = delayedModules.find((dq) => dq.depth === newDepth) |
| if (!delayedQueue) { |
| delayedQueue = { depth: newDepth, queue: [] } |
| delayedModules.push(delayedQueue) |
| |
| delayedModules.sort((a, b) => b.depth - a.depth) |
| } |
| delayedQueue.queue.push(depIndex) |
| } |
| } |
|
|
| i++ |
|
|
| |
| |
| |
| while ( |
| delayedModules.length > 0 && |
| (i === depthMap.size || |
| newDepth === delayedModules[delayedModules.length - 1].depth) |
| ) { |
| const { depth, queue } = delayedModules.pop()! |
| for (const depIndex of queue) { |
| if (!depthMap.has(depIndex)) { |
| depthMap.set(depIndex, depth) |
| } |
| } |
| } |
| } |
|
|
| if (delayedModules.length > 0) { |
| throw new Error( |
| 'Internal error: delayed modules remain after BFS processing' |
| ) |
| } |
|
|
| return depthMap |
| } |
|
|