Unverified Commit e9a3ba93 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: fix import chain by determining depth locally per route (#86350)

### What?

fix the import chain showing only modules relevant to the current route

fix module graph being merged by path. It's keyed by identifier now.

also add more info to import chain, e. g. layer info. and more details in tooltip.
parent e393401b
...@@ -15,6 +15,7 @@ import { Input } from '@/components/ui/input' ...@@ -15,6 +15,7 @@ import { Input } from '@/components/ui/input'
import { Skeleton, TreemapSkeleton } from '@/components/ui/skeleton' import { Skeleton, TreemapSkeleton } from '@/components/ui/skeleton'
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
import { AnalyzeData, ModulesData } from '@/lib/analyze-data' import { AnalyzeData, ModulesData } from '@/lib/analyze-data'
import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph'
import { SpecialModule } from '@/lib/types' import { SpecialModule } from '@/lib/types'
import { getSpecialModuleType, fetchStrict } from '@/lib/utils' import { getSpecialModuleType, fetchStrict } from '@/lib/utils'
...@@ -29,7 +30,9 @@ function formatBytes(bytes: number): string { ...@@ -29,7 +30,9 @@ function formatBytes(bytes: number): string {
export default function Home() { export default function Home() {
const [selectedRoute, setSelectedRoute] = useState<string | null>(null) const [selectedRoute, setSelectedRoute] = useState<string | null>(null)
const [environmentFilter, setEnvironmentFilter] = useState<string>('client') const [environmentFilter, setEnvironmentFilter] = useState<
'client' | 'server'
>('client')
const [typeFilter, setTypeFilter] = useState<string[]>(['js', 'css', 'json']) const [typeFilter, setTypeFilter] = useState<string[]>(['js', 'css', 'json'])
const [selectedSourceIndex, setSelectedSourceIndex] = useState<number | null>( const [selectedSourceIndex, setSelectedSourceIndex] = useState<number | null>(
null null
...@@ -100,6 +103,14 @@ export default function Home() { ...@@ -100,6 +103,14 @@ export default function Home() {
return () => window.removeEventListener('keydown', handleKeyDown) return () => window.removeEventListener('keydown', handleKeyDown)
}, []) }, [])
// Compute module depth map from active entries
const moduleDepthMap = useMemo(() => {
if (!modulesData || !analyzeData) return new Map()
const activeEntries = computeActiveEntries(modulesData, analyzeData)
return computeModuleDepthMap(modulesData, activeEntries)
}, [modulesData, analyzeData])
const filterSource = useMemo(() => { const filterSource = useMemo(() => {
if (!analyzeData) return undefined if (!analyzeData) return undefined
...@@ -172,7 +183,7 @@ export default function Home() { ...@@ -172,7 +183,7 @@ export default function Home() {
className="mr-4" className="mr-4"
value={environmentFilter} value={environmentFilter}
onValueChange={(value) => { onValueChange={(value) => {
if (value) setEnvironmentFilter(value) if (value) setEnvironmentFilter(value as 'client' | 'server')
}} }}
size="sm" size="sm"
> >
...@@ -332,7 +343,8 @@ export default function Home() { ...@@ -332,7 +343,8 @@ export default function Home() {
startFileId={selectedSourceIndex} startFileId={selectedSourceIndex}
analyzeData={analyzeData} analyzeData={analyzeData}
modulesData={modulesData} modulesData={modulesData}
filterSource={filterSource} depthMap={moduleDepthMap}
environmentFilter={environmentFilter}
/> />
)} )}
{(() => { {(() => {
......
// Type definitions matching the Rust structures from analyze.rs // Type definitions matching the Rust structures from analyze.rs
// Type aliases for better readability
export type ModuleIndex = number
export type SourceIndex = number
export interface AnalyzeModule { export interface AnalyzeModule {
ident: string
path: string path: string
depth: number
} }
export interface AnalyzeSource { export interface AnalyzeSource {
...@@ -53,7 +57,7 @@ interface ModulesDataHeader { ...@@ -53,7 +57,7 @@ interface ModulesDataHeader {
export class ModulesData { export class ModulesData {
private modulesHeader: ModulesDataHeader private modulesHeader: ModulesDataHeader
private modulesBinaryData: DataView private modulesBinaryData: DataView
private pathToModuleIndex: Map<string, number> private pathToModuleIndex: Map<string, ModuleIndex[]>
constructor(modulesArrayBuffer: ArrayBuffer) { constructor(modulesArrayBuffer: ArrayBuffer) {
// Parse modules.data // Parse modules.data
...@@ -76,11 +80,16 @@ export class ModulesData { ...@@ -76,11 +80,16 @@ export class ModulesData {
this.pathToModuleIndex = new Map() this.pathToModuleIndex = new Map()
for (let i = 0; i < this.modulesHeader.modules.length; i++) { for (let i = 0; i < this.modulesHeader.modules.length; i++) {
const module = this.modulesHeader.modules[i] const module = this.modulesHeader.modules[i]
this.pathToModuleIndex.set(module.path, i) const existing = this.pathToModuleIndex.get(module.path)
if (existing) {
existing.push(i)
} else {
this.pathToModuleIndex.set(module.path, [i])
}
} }
} }
module(index: number): AnalyzeModule | undefined { module(index: ModuleIndex): AnalyzeModule | undefined {
return this.modulesHeader.modules[index] return this.modulesHeader.modules[index]
} }
...@@ -88,15 +97,15 @@ export class ModulesData { ...@@ -88,15 +97,15 @@ export class ModulesData {
return this.modulesHeader.modules.length return this.modulesHeader.modules.length
} }
getModuleIndexFromPath(path: string): number | undefined { getModuleIndiciesFromPath(path: string): ModuleIndex[] {
return this.pathToModuleIndex.get(path) return this.pathToModuleIndex.get(path) ?? []
} }
// Read edges data for a specific index only // Read edges data for a specific index only
private readEdgesDataAtIndex( private readEdgesDataAtIndex(
reference: EdgesDataReference, reference: EdgesDataReference,
index: number index: ModuleIndex
): number[] { ): ModuleIndex[] {
const { offset, length } = reference const { offset, length } = reference
if (length === 0) { if (length === 0) {
...@@ -143,28 +152,28 @@ export class ModulesData { ...@@ -143,28 +152,28 @@ export class ModulesData {
return edges return edges
} }
moduleDependents(index: number): number[] { moduleDependents(index: ModuleIndex): ModuleIndex[] {
return this.readEdgesDataAtIndex( return this.readEdgesDataAtIndex(
this.modulesHeader.module_dependents, this.modulesHeader.module_dependents,
index index
) )
} }
asyncModuleDependents(index: number): number[] { asyncModuleDependents(index: ModuleIndex): ModuleIndex[] {
return this.readEdgesDataAtIndex( return this.readEdgesDataAtIndex(
this.modulesHeader.async_module_dependents, this.modulesHeader.async_module_dependents,
index index
) )
} }
moduleDependencies(index: number): number[] { moduleDependencies(index: ModuleIndex): ModuleIndex[] {
return this.readEdgesDataAtIndex( return this.readEdgesDataAtIndex(
this.modulesHeader.module_dependencies, this.modulesHeader.module_dependencies,
index index
) )
} }
asyncModuleDependencies(index: number): number[] { asyncModuleDependencies(index: ModuleIndex): ModuleIndex[] {
return this.readEdgesDataAtIndex( return this.readEdgesDataAtIndex(
this.modulesHeader.async_module_dependencies, this.modulesHeader.async_module_dependencies,
index index
...@@ -182,6 +191,7 @@ export class ModulesData { ...@@ -182,6 +191,7 @@ export class ModulesData {
export class AnalyzeData { export class AnalyzeData {
private analyzeHeader: AnalyzeDataHeader private analyzeHeader: AnalyzeDataHeader
private analyzeBinaryData: DataView private analyzeBinaryData: DataView
private pathToSourceIndex: Map<string, SourceIndex>
constructor(analyzeArrayBuffer: ArrayBuffer) { constructor(analyzeArrayBuffer: ArrayBuffer) {
// Parse analyze.data // Parse analyze.data
...@@ -199,11 +209,18 @@ export class AnalyzeData { ...@@ -199,11 +209,18 @@ export class AnalyzeData {
analyzeArrayBuffer, analyzeArrayBuffer,
analyzeBinaryOffset analyzeBinaryOffset
) )
// Build pathToSourceIndex map
this.pathToSourceIndex = new Map()
for (let i = 0; i < this.analyzeHeader.sources.length; i++) {
const fullPath = this.getFullSourcePath(i)
this.pathToSourceIndex.set(fullPath, i)
}
} }
// Accessor methods for header data // Accessor methods for header data
source(index: number): AnalyzeSource | undefined { source(index: SourceIndex): AnalyzeSource | undefined {
return this.analyzeHeader.sources[index] return this.analyzeHeader.sources[index]
} }
...@@ -211,6 +228,10 @@ export class AnalyzeData { ...@@ -211,6 +228,10 @@ export class AnalyzeData {
return this.analyzeHeader.sources.length return this.analyzeHeader.sources.length
} }
getSourceIndexFromPath(path: string): SourceIndex | undefined {
return this.pathToSourceIndex.get(path)
}
chunkPart(index: number): AnalyzeChunkPart | undefined { chunkPart(index: number): AnalyzeChunkPart | undefined {
return this.analyzeHeader.chunk_parts[index] return this.analyzeHeader.chunk_parts[index]
} }
...@@ -227,7 +248,7 @@ export class AnalyzeData { ...@@ -227,7 +248,7 @@ export class AnalyzeData {
return this.analyzeHeader.output_files.length return this.analyzeHeader.output_files.length
} }
sourceRoots(): number[] { sourceRoots(): SourceIndex[] {
return this.analyzeHeader.source_roots return this.analyzeHeader.source_roots
} }
...@@ -236,8 +257,8 @@ export class AnalyzeData { ...@@ -236,8 +257,8 @@ export class AnalyzeData {
// Read edges data for a specific index only // Read edges data for a specific index only
private readEdgesDataAtIndex( private readEdgesDataAtIndex(
reference: EdgesDataReference, reference: EdgesDataReference,
index: number index: SourceIndex
): number[] { ): SourceIndex[] {
const { offset, length } = reference const { offset, length } = reference
if (length === 0) { if (length === 0) {
...@@ -291,19 +312,19 @@ export class AnalyzeData { ...@@ -291,19 +312,19 @@ export class AnalyzeData {
) )
} }
sourceChunkParts(index: number): number[] { sourceChunkParts(index: SourceIndex): number[] {
return this.readEdgesDataAtIndex( return this.readEdgesDataAtIndex(
this.analyzeHeader.source_chunk_parts, this.analyzeHeader.source_chunk_parts,
index index
) )
} }
sourceChildren(index: number): number[] { sourceChildren(index: SourceIndex): SourceIndex[] {
return this.readEdgesDataAtIndex(this.analyzeHeader.source_children, index) return this.readEdgesDataAtIndex(this.analyzeHeader.source_children, index)
} }
// Utility method to get the full path of a source by walking up the parent chain // Utility method to get the full path of a source by walking up the parent chain
getFullSourcePath(index: number): string { getFullSourcePath(index: SourceIndex): string {
const source = this.source(index) const source = this.source(index)
if (!source) return '' if (!source) return ''
...@@ -315,7 +336,7 @@ export class AnalyzeData { ...@@ -315,7 +336,7 @@ export class AnalyzeData {
return parentPath + source.path return parentPath + source.path
} }
getSourceOutputSize(index: number): number { getSourceOutputSize(index: SourceIndex): number {
const chunkParts = this.sourceChunkParts(index) const chunkParts = this.sourceChunkParts(index)
let totalSize = 0 let totalSize = 0
for (const chunkPartIndex of chunkParts) { for (const chunkPartIndex of chunkParts) {
...@@ -327,7 +348,7 @@ export class AnalyzeData { ...@@ -327,7 +348,7 @@ export class AnalyzeData {
return totalSize return totalSize
} }
sourceChunks(index: number): string[] { sourceChunks(index: SourceIndex): string[] {
const chunkParts = this.sourceChunkParts(index) const chunkParts = this.sourceChunkParts(index)
const uniqueChunks = new Set<string>() const uniqueChunks = new Set<string>()
...@@ -344,7 +365,7 @@ export class AnalyzeData { ...@@ -344,7 +365,7 @@ export class AnalyzeData {
return Array.from(uniqueChunks).sort() return Array.from(uniqueChunks).sort()
} }
getSourceFlags(index: number): { getSourceFlags(index: SourceIndex): {
client: boolean client: boolean
server: boolean server: boolean
traced: boolean traced: boolean
...@@ -388,14 +409,14 @@ export class AnalyzeData { ...@@ -388,14 +409,14 @@ export class AnalyzeData {
return { client, server, traced, js, css, json, asset } return { client, server, traced, js, css, json, asset }
} }
isPolyfillModule(index: number): boolean { isPolyfillModule(index: SourceIndex): boolean {
const fullSourcePath = this.getFullSourcePath(index) const fullSourcePath = this.getFullSourcePath(index)
return fullSourcePath.endsWith( return fullSourcePath.endsWith(
'node_modules/next/dist/build/polyfills/polyfill-module.js' 'node_modules/next/dist/build/polyfills/polyfill-module.js'
) )
} }
isPolyfillNoModule(index: number): boolean { isPolyfillNoModule(index: SourceIndex): boolean {
const fullSourcePath = this.getFullSourcePath(index) const fullSourcePath = this.getFullSourcePath(index)
return fullSourcePath.endsWith( return fullSourcePath.endsWith(
'node_modules/next/dist/build/polyfills/polyfill-nomodule.js' 'node_modules/next/dist/build/polyfills/polyfill-nomodule.js'
......
import type { AnalyzeData, ModuleIndex, ModulesData } from './analyze-data'
/**
* Compute active entries from the current route's sources.
*
* It's a heuristic approach that looks for known entry module idents
* and traces their dependencies to find active modules.
*
* I don't like it as it has too much assumptions about next.js internals.
* It would be better if the source map contains idents instead of only paths.
*/
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 (
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)
}
/**
* Compute module depth from active entries using BFS
* Returns a Map from ModuleIndex to depth
* Unreachable modules will not have an entry in the map
*/
export function computeModuleDepthMap(
modulesData: ModulesData,
activeEntries: ModuleIndex[]
): Map<ModuleIndex, number> {
const depthMap = new Map<ModuleIndex, number>()
const delayedModules = new Array<{ depth: number; queue: ModuleIndex[] }>()
// Initialize queue with active entries
for (const moduleIndex of activeEntries) {
depthMap.set(moduleIndex, 0)
}
// BFS to compute depth
// We need to insert new entries into the depth map in monotonic increasing order of depth
// so that we always process shallower modules before deeper ones
// This is important to avoid visiting modules multiple times and needing to decrease their depth
let i = 0
for (const [moduleIndex, depth] of depthMap) {
const newDepth = depth + 1
// Process regular dependencies
const dependencies = modulesData.moduleDependencies(moduleIndex)
for (const depIndex of dependencies) {
if (!depthMap.has(depIndex)) {
depthMap.set(depIndex, newDepth)
}
}
// Process async dependencies with higher depth penalty
const asyncDependencies = modulesData.asyncModuleDependencies(moduleIndex)
for (const depIndex of asyncDependencies) {
if (!depthMap.has(depIndex)) {
const newDepth = depth + 1000
// We can't directly insert async dependencies into the depth map
// because they might be processed before their parent module
// leading to incorrect depth assignment.
// Instead, we queue them to be processed later.
let delayedQueue = delayedModules.find((dq) => dq.depth === newDepth)
if (!delayedQueue) {
delayedQueue = { depth: newDepth, queue: [] }
delayedModules.push(delayedQueue)
// Keep delayed queues sorted by depth descending
delayedModules.sort((a, b) => b.depth - a.depth)
}
delayedQueue.queue.push(depIndex)
}
}
i++
// Check if we need to process the next delayed queue to insert its items into the depth map
// This happens when we reach the end of the current queue
// or the next delayed queue has the same depth so its items need to be processed now
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
}
import type { AnalyzeData } from './analyze-data' import type { AnalyzeData, SourceIndex } from './analyze-data'
import { layoutTreemap } from './layout-treemap' import { layoutTreemap } from './layout-treemap'
import { SpecialModule } from './types' import { SpecialModule } from './types'
import { getSpecialModuleType } from './utils' import { getSpecialModuleType } from './utils'
...@@ -29,7 +29,7 @@ export interface LayoutNode extends LayoutNodeInfo { ...@@ -29,7 +29,7 @@ export interface LayoutNode extends LayoutNodeInfo {
css?: boolean css?: boolean
json?: boolean json?: boolean
asset?: boolean asset?: boolean
sourceIndex?: number // Track which source this node represents sourceIndex?: SourceIndex // Track which source this node represents
} }
interface SourceMetadata { interface SourceMetadata {
...@@ -39,7 +39,7 @@ interface SourceMetadata { ...@@ -39,7 +39,7 @@ interface SourceMetadata {
function precomputeSourceMetadata( function precomputeSourceMetadata(
analyzeData: AnalyzeData, analyzeData: AnalyzeData,
filterSource?: (sourceIndex: number) => boolean filterSource?: (sourceIndex: SourceIndex) => boolean
): SourceMetadata[] { ): SourceMetadata[] {
const sourceCount = analyzeData.sourceCount() const sourceCount = analyzeData.sourceCount()
const metadata: SourceMetadata[] = new Array(sourceCount) const metadata: SourceMetadata[] = new Array(sourceCount)
...@@ -65,7 +65,7 @@ function precomputeSourceMetadata( ...@@ -65,7 +65,7 @@ function precomputeSourceMetadata(
} }
// Top-down pass: aggregate child sizes and filtered status for directories // Top-down pass: aggregate child sizes and filtered status for directories
function processDirectory(idx: number) { function processDirectory(idx: SourceIndex) {
const children = analyzeData.sourceChildren(idx) const children = analyzeData.sourceChildren(idx)
if (children.length === 0) return // Already processed as leaf if (children.length === 0) return // Already processed as leaf
...@@ -97,11 +97,11 @@ function precomputeSourceMetadata( ...@@ -97,11 +97,11 @@ function precomputeSourceMetadata(
// Internal function that uses precomputed metadata // Internal function that uses precomputed metadata
function computeTreemapLayoutFromAnalyzeInternal( function computeTreemapLayoutFromAnalyzeInternal(
analyzeData: AnalyzeData, analyzeData: AnalyzeData,
sourceIndex: number, sourceIndex: SourceIndex,
foldedPath: string, foldedPath: string,
rect: LayoutRect, rect: LayoutRect,
metadata: SourceMetadata[], metadata: SourceMetadata[],
filterSource?: (sourceIndex: number) => boolean filterSource?: (sourceIndex: SourceIndex) => boolean
): LayoutNode { ): LayoutNode {
const source = analyzeData.source(sourceIndex) const source = analyzeData.source(sourceIndex)
if (!source) { if (!source) {
...@@ -164,7 +164,7 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -164,7 +164,7 @@ function computeTreemapLayoutFromAnalyzeInternal(
if (isCollapsed) { if (isCollapsed) {
// Count all descendant files // Count all descendant files
function countDescendants(idx: number): number { function countDescendants(idx: SourceIndex): number {
const children = analyzeData.sourceChildren(idx) const children = analyzeData.sourceChildren(idx)
if (children.length === 0) return 1 if (children.length === 0) return 1
return children.reduce( return children.reduce(
...@@ -253,9 +253,9 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -253,9 +253,9 @@ function computeTreemapLayoutFromAnalyzeInternal(
// Public function that precomputes metadata and calls internal function // Public function that precomputes metadata and calls internal function
export function computeTreemapLayoutFromAnalyze( export function computeTreemapLayoutFromAnalyze(
analyzeData: AnalyzeData, analyzeData: AnalyzeData,
sourceIndex: number, sourceIndex: SourceIndex,
rect: LayoutRect, rect: LayoutRect,
filterSource?: (sourceIndex: number) => boolean filterSource?: (sourceIndex: SourceIndex) => boolean
): LayoutNode { ): LayoutNode {
// Precompute metadata once for entire tree // Precompute metadata once for entire tree
const metadata = precomputeSourceMetadata(analyzeData, filterSource) const metadata = precomputeSourceMetadata(analyzeData, filterSource)
......
...@@ -2,7 +2,7 @@ import { type ClassValue, clsx } from 'clsx' ...@@ -2,7 +2,7 @@ import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { SpecialModule } from './types' import { SpecialModule } from './types'
import { NetworkError } from './errors' import { NetworkError } from './errors'
import { AnalyzeData } from './analyze-data' import { AnalyzeData, SourceIndex } from './analyze-data'
export function cn(...inputs: ClassValue[]) { export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)) return twMerge(clsx(inputs))
...@@ -29,7 +29,7 @@ export async function jsonFetcher<T>(url: string): Promise<T> { ...@@ -29,7 +29,7 @@ export async function jsonFetcher<T>(url: string): Promise<T> {
export function getSpecialModuleType( export function getSpecialModuleType(
analyzeData: AnalyzeData | undefined, analyzeData: AnalyzeData | undefined,
sourceIndex: number | null sourceIndex: SourceIndex | null
): SpecialModule | null { ): SpecialModule | null {
if (!analyzeData || sourceIndex == null) return null if (!analyzeData || sourceIndex == null) return null
...@@ -42,3 +42,19 @@ export function getSpecialModuleType( ...@@ -42,3 +42,19 @@ export function getSpecialModuleType(
return null return null
} }
let IDENT_ATTRIBUTES_REGEXP =
/^(.+?)(?: \{(.*)\})?(?: \[(.*)\])?(?: \((.*?)\))?(?: <(.*?)>)?$/
export function splitIdent(ident: string): {
fullPath: string
templateArgs: string
layer: string
moduleType: string
treeShaking: string
} {
let [match, fullPath, templateArgs, layer, moduleType, treeShaking] =
IDENT_ATTRIBUTES_REGEXP.exec(ident) || ['']
ident = ident.substring(0, ident.length - match.length)
return { fullPath, templateArgs, layer, moduleType, treeShaking }
}
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
"class-variance-authority": "^0.7.1", "class-variance-authority": "^0.7.1",
"clsx": "^2.1.1", "clsx": "^2.1.1",
"cmdk": "1.0.4", "cmdk": "1.0.4",
"lucide-react": "^0.454.0", "lucide-react": "^0.554.0",
"next": "16.0.1", "next": "16.0.1",
"next-themes": "^0.4.6", "next-themes": "^0.4.6",
"polished": "^4.3.1", "polished": "^4.3.1",
......
...@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap; ...@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc, FxIndexSet, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
trace::TraceRawVcs, trace::TraceRawVcs,
}; };
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -72,8 +72,8 @@ pub struct AnalyzeSource { ...@@ -72,8 +72,8 @@ pub struct AnalyzeSource {
#[derive(Serialize)] #[derive(Serialize)]
pub struct AnalyzeModule { pub struct AnalyzeModule {
pub ident: RcStr,
pub path: RcStr, pub path: RcStr,
pub depth: u32,
} }
#[derive(Serialize)] #[derive(Serialize)]
...@@ -283,18 +283,23 @@ impl ModulesDataBuilder { ...@@ -283,18 +283,23 @@ impl ModulesDataBuilder {
} }
} }
fn ensure_module(&mut self, path: &str) -> (&mut AnalyzeModuleBuilder, u32) { fn get_module(&mut self, ident: &str) -> (&mut AnalyzeModuleBuilder, u32) {
if let Some(&index) = self.module_index_map.get(path) { if let Some(&index) = self.module_index_map.get(ident) {
return (&mut self.modules[index as usize], index);
}
panic!("Module with ident `{}` not found", ident);
}
fn ensure_module(&mut self, ident: &str, path: &str) -> (&mut AnalyzeModuleBuilder, u32) {
if let Some(&index) = self.module_index_map.get(ident) {
return (&mut self.modules[index as usize], index); return (&mut self.modules[index as usize], index);
} }
let index = self.modules.len() as u32; let index = self.modules.len() as u32;
let ident = RcStr::from(ident);
let path = RcStr::from(path); let path = RcStr::from(path);
self.module_index_map.insert(path.clone(), index); self.module_index_map.insert(ident.clone(), index);
self.modules.push(AnalyzeModuleBuilder { self.modules.push(AnalyzeModuleBuilder {
module: AnalyzeModule { module: AnalyzeModule { ident, path },
path,
depth: u32::MAX,
},
dependencies: FxIndexSet::default(), dependencies: FxIndexSet::default(),
async_dependencies: FxIndexSet::default(), async_dependencies: FxIndexSet::default(),
dependents: FxIndexSet::default(), dependents: FxIndexSet::default(),
...@@ -420,11 +425,14 @@ pub async fn analyze_output_assets(output_assets: Vc<OutputAssets>) -> Result<Vc ...@@ -420,11 +425,14 @@ pub async fn analyze_output_assets(output_assets: Vc<OutputAssets>) -> Result<Vc
pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc<FileContent>> { pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc<FileContent>> {
let mut builder = ModulesDataBuilder::new(); let mut builder = ModulesDataBuilder::new();
let mut all_modules = FxIndexSet::default();
let mut all_edges = FxIndexSet::default(); let mut all_edges = FxIndexSet::default();
let mut all_async_edges = FxIndexSet::default(); let mut all_async_edges = FxIndexSet::default();
for &module_graph in module_graphs.await? { for &module_graph in module_graphs.await? {
let module_graph = module_graph.read_graphs().await?; let module_graph = module_graph.read_graphs().await?;
module_graph.traverse_all_edges_unordered(|(parent_node, reference), node| { module_graph.traverse_all_edges_unordered(|(parent_node, reference), node| {
all_modules.insert(parent_node);
all_modules.insert(node);
match reference.chunking_type { match reference.chunking_type {
ChunkingType::Async => { ChunkingType::Async => {
all_async_edges.insert((parent_node, node)); all_async_edges.insert((parent_node, node));
...@@ -442,9 +450,24 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc ...@@ -442,9 +450,24 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc
if from == to { if from == to {
return Ok(None); return Ok(None);
} }
let from_path = from.ident().path().to_string().owned().await?; let from_ident = from.ident().to_string().owned().await?;
let to_path = to.ident().path().to_string().owned().await?; let to_ident = to.ident().to_string().owned().await?;
Ok(Some((from_path, to_path))) Ok(Some((from_ident, to_ident)))
}
let all_modules = all_modules
.iter()
.copied()
.map(async |module| {
let ident = module.ident().to_string().owned().await?;
let path = module.ident().path().to_string().owned().await?;
Ok((ident, path))
})
.try_join()
.await?;
for (ident, path) in all_modules {
builder.ensure_module(&ident, &path);
} }
let all_edges = all_edges let all_edges = all_edges
...@@ -459,9 +482,9 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc ...@@ -459,9 +482,9 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc
.map(mapper) .map(mapper)
.try_flat_join() .try_flat_join()
.await?; .await?;
for (from_path, to_path) in all_edges { for (from_ident, to_ident) in all_edges {
let from_index = builder.ensure_module(&from_path).1; let from_index = builder.get_module(&from_ident).1;
let to_index = builder.ensure_module(&to_path).1; let to_index = builder.get_module(&to_ident).1;
if from_index == to_index { if from_index == to_index {
continue; continue;
} }
...@@ -472,9 +495,9 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc ...@@ -472,9 +495,9 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc
.dependents .dependents
.insert(from_index); .insert(from_index);
} }
for (from_path, to_path) in all_async_edges { for (from_ident, to_ident) in all_async_edges {
let from_index = builder.ensure_module(&from_path).1; let from_index = builder.get_module(&from_ident).1;
let to_index = builder.ensure_module(&to_path).1; let to_index = builder.get_module(&to_ident).1;
if from_index == to_index { if from_index == to_index {
continue; continue;
} }
...@@ -486,56 +509,6 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc ...@@ -486,56 +509,6 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc
.insert(from_index); .insert(from_index);
} }
// Compute depth using BFS from modules without incoming edges
let mut queue = std::collections::VecDeque::new();
// Find modules without incoming edges and set their depth to 0
for (index, module) in builder.modules.iter_mut().enumerate() {
if module.dependents.is_empty() && module.async_dependents.is_empty() {
module.module.depth = 0;
queue.push_back(index as u32);
}
}
// Process queue and propagate depth
while let Some(current_index) = queue.pop_front() {
let current_depth = builder.modules[current_index as usize].module.depth;
// Collect dependencies to avoid borrow conflicts
let dependencies: Vec<u32> = builder.modules[current_index as usize]
.dependencies
.iter()
.copied()
.collect();
// Update dependencies
let new_depth = current_depth + 1;
for &dep_index in &dependencies {
let dep_module = &mut builder.modules[dep_index as usize];
if new_depth < dep_module.module.depth {
dep_module.module.depth = new_depth;
queue.push_back(dep_index);
}
}
// Collect async dependencies to avoid borrow conflicts
let async_dependencies: Vec<u32> = builder.modules[current_index as usize]
.async_dependencies
.iter()
.copied()
.collect();
// Update async dependencies
let new_depth = current_depth + 1000;
for &dep_index in &async_dependencies {
let dep_module = &mut builder.modules[dep_index as usize];
if new_depth < dep_module.module.depth {
dep_module.module.depth = new_depth;
queue.push_back(dep_index);
}
}
}
let rope = builder.build(); let rope = builder.build();
Ok(FileContent::Content(File::from(rope)).cell()) Ok(FileContent::Content(File::from(rope)).cell())
} }
......
...@@ -643,8 +643,8 @@ importers: ...@@ -643,8 +643,8 @@ importers:
specifier: 1.0.4 specifier: 1.0.4
version: 1.0.4(@types/react-dom@19.2.1(@types/react@19.2.2))(@types/react@19.2.2)(react-dom@19.3.0-canary-8ac5f4eb-20251119(react@19.3.0-canary-8ac5f4eb-20251119))(react@19.3.0-canary-8ac5f4eb-20251119) version: 1.0.4(@types/react-dom@19.2.1(@types/react@19.2.2))(@types/react@19.2.2)(react-dom@19.3.0-canary-8ac5f4eb-20251119(react@19.3.0-canary-8ac5f4eb-20251119))(react@19.3.0-canary-8ac5f4eb-20251119)
lucide-react: lucide-react:
specifier: ^0.454.0 specifier: ^0.554.0
version: 0.454.0(react@19.3.0-canary-8ac5f4eb-20251119) version: 0.554.0(react@19.3.0-canary-8ac5f4eb-20251119)
next: next:
specifier: 16.0.1 specifier: 16.0.1
version: 16.0.1(@babel/core@7.26.10)(@opentelemetry/api@1.6.0)(@playwright/test@1.51.1)(babel-plugin-macros@3.1.0)(babel-plugin-react-compiler@0.0.0-experimental-3fde738-20250918)(react-dom@19.3.0-canary-8ac5f4eb-20251119(react@19.3.0-canary-8ac5f4eb-20251119))(react@19.3.0-canary-8ac5f4eb-20251119)(sass@1.77.8) version: 16.0.1(@babel/core@7.26.10)(@opentelemetry/api@1.6.0)(@playwright/test@1.51.1)(babel-plugin-macros@3.1.0)(babel-plugin-react-compiler@0.0.0-experimental-3fde738-20250918)(react-dom@19.3.0-canary-8ac5f4eb-20251119(react@19.3.0-canary-8ac5f4eb-20251119))(react@19.3.0-canary-8ac5f4eb-20251119)(sass@1.77.8)
...@@ -12820,8 +12820,8 @@ packages: ...@@ -12820,8 +12820,8 @@ packages:
peerDependencies: peerDependencies:
react: 19.3.0-canary-8ac5f4eb-20251119 react: 19.3.0-canary-8ac5f4eb-20251119
lucide-react@0.454.0: lucide-react@0.554.0:
resolution: {integrity: sha512-hw7zMDwykCLnEzgncEEjHeA6+45aeEzRYuKHuyRSOPkhko+J3ySGjGIzu+mmMfDFG1vazHepMaYFYHbTFAZAAQ==} resolution: {integrity: sha512-St+z29uthEJVx0Is7ellNkgTEhaeSoA42I7JjOCBCrc5X6LYMGSv0P/2uS5HDLTExP5tpiqRD2PyUEOS6s9UXA==}
peerDependencies: peerDependencies:
react: 19.3.0-canary-8ac5f4eb-20251119 react: 19.3.0-canary-8ac5f4eb-20251119
...@@ -31817,7 +31817,7 @@ snapshots: ...@@ -31817,7 +31817,7 @@ snapshots:
dependencies: dependencies:
react: 19.3.0-canary-fb2177c1-20251114 react: 19.3.0-canary-fb2177c1-20251114
lucide-react@0.454.0(react@19.3.0-canary-8ac5f4eb-20251119): lucide-react@0.554.0(react@19.3.0-canary-8ac5f4eb-20251119):
dependencies: dependencies:
react: 19.3.0-canary-8ac5f4eb-20251119 react: 19.3.0-canary-8ac5f4eb-20251119
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