| 4 | import { getTestFileEnvironment } from './environments' |
| 5 | |
| 6 | export async function getModuleGraph( |
| 7 | ctx: Vitest, |
| 8 | projectName: string, |
| 9 | testFilePath: string, |
| 10 | browser = false, |
| 11 | ): Promise<ModuleGraphData> { |
| 12 | const graph: Record<string, string[]> = {} |
| 13 | const externalized = new Set<string>() |
| 14 | const inlined = new Set<string>() |
| 15 | |
| 16 | const project = ctx.getProjectByName(projectName) |
| 17 | |
| 18 | const environment = project.config.experimental.viteModuleRunner === false |
| 19 | ? project.vite.environments.__vitest__ |
| 20 | : getTestFileEnvironment(project, testFilePath, browser) |
| 21 | |
| 22 | if (!environment) { |
| 23 | throw new Error(`Cannot find environment for ${testFilePath}`) |
| 24 | } |
| 25 | const seen = new Map<EnvironmentModuleNode, string>() |
| 26 | |
| 27 | function get(mod?: EnvironmentModuleNode) { |
| 28 | if (!mod || !mod.id) { |
| 29 | return |
| 30 | } |
| 31 | if ( |
| 32 | mod.id === '\0vitest/browser' |
| 33 | // the export helper is injected in all vue files |
| 34 | // so the module graph becomes too bouncy |
| 35 | || mod.id.includes('plugin-vue:export-helper') |
| 36 | ) { |
| 37 | return |
| 38 | } |
| 39 | if (seen.has(mod)) { |
| 40 | return seen.get(mod) |
| 41 | } |
| 42 | const id = clearId(mod.id) |
| 43 | seen.set(mod, id) |
| 44 | if (id.startsWith('__vite-browser-external:')) { |
| 45 | const external = id.slice('__vite-browser-external:'.length) |
| 46 | externalized.add(external) |
| 47 | return external |
| 48 | } |
| 49 | const external = project._resolver.wasExternalized(id) |
| 50 | if (typeof external === 'string') { |
| 51 | externalized.add(external) |
| 52 | return external |
| 53 | } |
| 54 | if (browser && mod.file?.includes(project.browser!.vite.config.cacheDir)) { |
| 55 | externalized.add(mod.id) |
| 56 | return id |
| 57 | } |
| 58 | inlined.add(id) |
| 59 | const mods = Array.from(mod.importedModules).filter( |
| 60 | i => i.id && !i.id.includes('/vitest/dist/'), |
| 61 | ) |
| 62 | graph[id] = mods.map(m => get(m)).filter( |
| 63 | Boolean, |