Skip to content

Commit dc542e4

Browse files
Copilotsheremet-va
andcommitted
Support multiple Vite environments in module graph storage
Rewrite module storage logic to handle multiple vite.environments: - Store modules per project AND environment instead of per project only - Iterate through all project.vite.environments.[name].moduleGraph - Extract graph edges from each environment's module graph - When reading blobs, populate the correct environment's module graph - Update MergeReportModuleKeys type to include environmentName This properly supports the new Vite environments API where each environment (client, ssr, etc.) has its own module graph. Co-authored-by: sheremet-va <[email protected]>
1 parent 53cfde2 commit dc542e4

File tree

1 file changed

+51
-29
lines changed
  • packages/vitest/src/node/reporters

1 file changed

+51
-29
lines changed

packages/vitest/src/node/reporters/blob.ts

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,53 +54,70 @@ export class BlobReporter implements Reporter {
5454
: '.vitest-reports/blob.json'
5555
}
5656

57-
const modules = this.ctx.projects.map<MergeReportModuleKeys>(
57+
// Store modules organized by project and environment
58+
const modules = this.ctx.projects.flatMap<MergeReportModuleKeys>(
5859
(project) => {
59-
return [
60-
project.name,
61-
[...project.vite.moduleGraph.idToModuleMap.entries()].map<SerializedModuleNode | null>((mod) => {
60+
const environmentModules: MergeReportModuleKeys[] = []
61+
62+
// Iterate through all environments for this project
63+
for (const envName in project.vite.environments) {
64+
const env = project.vite.environments[envName]
65+
const envModules = [...env.moduleGraph.idToModuleMap.entries()].map<SerializedModuleNode | null>((mod) => {
6266
if (!mod[1].file) {
6367
return null
6468
}
6569
return [mod[0], mod[1].file, mod[1].url]
66-
}).filter(x => x != null),
67-
]
70+
}).filter(x => x != null)
71+
72+
if (envModules.length > 0) {
73+
environmentModules.push([
74+
project.name,
75+
envName,
76+
envModules,
77+
])
78+
}
79+
}
80+
81+
return environmentModules
6882
},
6983
)
7084

7185
// Build module ID to index map for efficient graph storage
7286
const moduleIdToIndex = new Map<string, number>()
7387
let globalModuleIndex = 0
7488

75-
modules.forEach(([_projectName, projectModules]) => {
89+
modules.forEach(([_projectName, _envName, projectModules]) => {
7690
projectModules.forEach((mod) => {
7791
moduleIdToIndex.set(mod[0], globalModuleIndex++)
7892
})
7993
})
8094

81-
// Extract graph relationships directly from module graph
95+
// Extract graph relationships directly from module graph for all environments
8296
const graphEdges: number[][] = []
8397
this.ctx.projects.forEach((project) => {
84-
project.vite.moduleGraph.idToModuleMap.forEach((moduleNode, _moduleId) => {
85-
if (!moduleNode.file) {
86-
return
87-
}
88-
const sourceIdx = moduleIdToIndex.get(moduleNode.id!)
89-
if (sourceIdx === undefined) {
90-
return
91-
}
92-
93-
// Store importedModules relationships
94-
moduleNode.importedModules.forEach((importedModule) => {
95-
if (!importedModule.id) {
98+
for (const envName in project.vite.environments) {
99+
const env = project.vite.environments[envName]
100+
env.moduleGraph.idToModuleMap.forEach((moduleNode, _moduleId) => {
101+
if (!moduleNode.file) {
96102
return
97103
}
98-
const targetIdx = moduleIdToIndex.get(importedModule.id)
99-
if (targetIdx !== undefined) {
100-
graphEdges.push([sourceIdx, targetIdx])
104+
const sourceIdx = moduleIdToIndex.get(moduleNode.id!)
105+
if (sourceIdx === undefined) {
106+
return
101107
}
108+
109+
// Store importedModules relationships
110+
moduleNode.importedModules.forEach((importedModule) => {
111+
if (!importedModule.id) {
112+
return
113+
}
114+
const targetIdx = moduleIdToIndex.get(importedModule.id)
115+
if (targetIdx !== undefined) {
116+
graphEdges.push([sourceIdx, targetIdx])
117+
}
118+
})
102119
})
103-
})
120+
}
104121
})
105122

106123
const report = [
@@ -185,29 +202,33 @@ export async function readBlobs(
185202
// Build a global module index to ID map
186203
const allModules: SerializedModuleNode[] = []
187204
blobs.forEach((blob) => {
188-
blob.moduleKeys.forEach(([_projectName, moduleIds]) => {
205+
blob.moduleKeys.forEach(([_projectName, _envName, moduleIds]) => {
189206
allModules.push(...moduleIds)
190207
})
191208
})
192209

193-
// Create module nodes in the module graph
210+
// Create module nodes in the module graph for each environment
194211
const moduleNodesById = new Map<string, any>()
195212
blobs.forEach((blob) => {
196-
blob.moduleKeys.forEach(([projectName, moduleIds]) => {
213+
blob.moduleKeys.forEach(([projectName, envName, moduleIds]) => {
197214
const project = projects[projectName]
198215
if (!project) {
199216
return
200217
}
218+
const env = project.vite.environments[envName]
219+
if (!env) {
220+
return
221+
}
201222
moduleIds.forEach(([moduleId, file, url]) => {
202-
const moduleNode = project.vite.moduleGraph.createFileOnlyEntry(file)
223+
const moduleNode = env.moduleGraph.createFileOnlyEntry(file)
203224
moduleNode.url = url
204225
moduleNode.id = moduleId
205226
moduleNode.transformResult = {
206227
// print error checks that transformResult is set
207228
code: ' ',
208229
map: null,
209230
}
210-
project.vite.moduleGraph.idToModuleMap.set(moduleId, moduleNode)
231+
env.moduleGraph.idToModuleMap.set(moduleId, moduleNode)
211232
moduleNodesById.set(moduleId, moduleNode)
212233
})
213234
})
@@ -277,5 +298,6 @@ type SerializedModuleNode = [
277298

278299
type MergeReportModuleKeys = [
279300
projectName: string,
301+
environmentName: string,
280302
modules: SerializedModuleNode[],
281303
]

0 commit comments

Comments
 (0)