Skip to content

Commit

Permalink
refactor(blob): use createFileOnlyEntry to create module nodes (#6400)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 26, 2024
1 parent 92d098c commit 9f78a91
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class Vitest {
throw new Error('Cannot merge reports when `--reporter=blob` is used. Remove blob reporter from the config first.')
}

const { files, errors, coverages } = await readBlobs(this.config.mergeReports, this.projects)
const { files, errors, coverages } = await readBlobs(this.version, this.config.mergeReports, this.projects)

await this.report('onInit', this)
await this.report('onPathsCollected', files.flatMap(f => f.filepath))
Expand Down
57 changes: 31 additions & 26 deletions packages/vitest/src/node/reporters/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { parse, stringify } from 'flatted'
import { dirname, resolve } from 'pathe'
import { cleanUrl } from 'vite-node/utils'
import type { File } from '@vitest/runner'
import { getOutputFile } from '../../utils/config-helpers'
import type { WorkspaceProject } from '../workspace'
Expand Down Expand Up @@ -43,11 +42,16 @@ export class BlobReporter implements Reporter {
: '.vitest-reports/blob.json'
}

const moduleKeys = this.ctx.projects.map<MergeReportModuleKeys>(
const modules = this.ctx.projects.map<MergeReportModuleKeys>(
(project) => {
return [
project.getName(),
[...project.server.moduleGraph.idToModuleMap.keys()],
[...project.server.moduleGraph.idToModuleMap.entries()].map<SerializedModuleNode | null>((mod) => {
if (!mod[1].file) {
return null
}
return [mod[0], mod[1].file, mod[1].url]
}).filter(x => x != null),
]
},
)
Expand All @@ -56,7 +60,7 @@ export class BlobReporter implements Reporter {
this.ctx.version,
files,
errors,
moduleKeys,
modules,
coverage,
] satisfies MergeReport)

Expand All @@ -73,6 +77,7 @@ export class BlobReporter implements Reporter {
}

export async function readBlobs(
currentVersion: string,
blobsDirectory: string,
projectsArray: WorkspaceProject[],
) {
Expand Down Expand Up @@ -113,6 +118,12 @@ export async function readBlobs(
)
}

if (!versions.has(currentVersion)) {
throw new Error(
`the blobs in "${blobsDirectory}" were generated by a different version of Vitest. Expected v${currentVersion}, but received v${blobs[0].version}`,
)
}

// fake module graph - it is used to check if module is imported, but we don't use values inside
const projects = Object.fromEntries(
projectsArray.map(p => [p.getName(), p]),
Expand All @@ -124,26 +135,11 @@ export async function readBlobs(
if (!project) {
return
}
moduleIds.forEach((moduleId) => {
project.server.moduleGraph.idToModuleMap.set(moduleId, {
id: moduleId,
url: moduleId,
file: cleanUrl(moduleId),
ssrTransformResult: null,
transformResult: null,
importedBindings: null,
importedModules: new Set(),
importers: new Set(),
type: 'js',
clientImportedModules: new Set(),
ssrError: null,
ssrImportedModules: new Set(),
ssrModule: null,
acceptedHmrDeps: new Set(),
acceptedHmrExports: null,
lastHMRTimestamp: 0,
lastInvalidationTimestamp: 0,
})
moduleIds.forEach(([moduleId, file, url]) => {
const moduleNode = project.server.moduleGraph.createFileOnlyEntry(file)
moduleNode.url = url
moduleNode.id = moduleId
project.server.moduleGraph.idToModuleMap.set(moduleId, moduleNode)
})
})
})
Expand All @@ -169,8 +165,17 @@ type MergeReport = [
vitestVersion: string,
files: File[],
errors: unknown[],
moduleKeys: MergeReportModuleKeys[],
modules: MergeReportModuleKeys[],
coverage: unknown,
]

type MergeReportModuleKeys = [projectName: string, moduleIds: string[]]
type SerializedModuleNode = [
id: string,
file: string,
url: string,
]

type MergeReportModuleKeys = [
projectName: string,
modules: SerializedModuleNode[],
]

0 comments on commit 9f78a91

Please sign in to comment.