|
1 | | -import { lstatSync } from 'node:fs' |
2 | 1 | import path from 'node:path' |
| 2 | +import { tsFileCache } from './scope' |
| 3 | +import type { ResolverFactory } from 'oxc-resolver' |
| 4 | +import type { ModuleNode, Plugin } from 'vite' |
3 | 5 |
|
4 | | -export type ResolveTSFileIdImpl = ( |
5 | | - id: string, |
6 | | - importer: string, |
7 | | -) => Promise<string | undefined> | string | undefined |
| 6 | +let typesResolver: ResolverFactory |
8 | 7 |
|
9 | | -export const resolveTSFileId: ResolveTSFileIdImpl = (id, importer) => { |
10 | | - return resolveTSFileIdImpl(id, importer) |
| 8 | +const referencedFiles = new Map<string /* file */, Set<string /* importer */>>() |
| 9 | + |
| 10 | +function collectReferencedFile(importer: string, file: string) { |
| 11 | + if (!importer) return |
| 12 | + if (!referencedFiles.has(file)) { |
| 13 | + referencedFiles.set(file, new Set([importer])) |
| 14 | + } else { |
| 15 | + referencedFiles.get(file)!.add(importer) |
| 16 | + } |
11 | 17 | } |
12 | 18 |
|
13 | | -/** |
14 | | - * @limitation don't node_modules and JavaScript file |
15 | | - */ |
16 | | -export const resolveTSFileIdNode: ResolveTSFileIdImpl = ( |
| 19 | +const resolveCache = new Map< |
| 20 | + string /* importer */, |
| 21 | + Map<string /* id */, string /* result */> |
| 22 | +>() |
| 23 | + |
| 24 | +export async function resolveDts( |
17 | 25 | id: string, |
18 | 26 | importer: string, |
19 | | -) => { |
20 | | - return ( |
21 | | - tryResolve(id, importer) || |
22 | | - tryResolve(`${id}.ts`, importer) || |
23 | | - tryResolve(`${id}.d.ts`, importer) || |
24 | | - tryResolve(`${id}/index`, importer) || |
25 | | - tryResolve(`${id}/index.ts`, importer) || |
26 | | - tryResolve(`${id}/index.d.ts`, importer) |
| 27 | +): Promise<string | undefined> { |
| 28 | + const cached = resolveCache.get(importer)?.get(id) |
| 29 | + if (cached) return cached |
| 30 | + |
| 31 | + if (!typesResolver) { |
| 32 | + const { ResolverFactory } = await import('oxc-resolver') |
| 33 | + typesResolver = new ResolverFactory({ |
| 34 | + mainFields: ['types'], |
| 35 | + conditionNames: ['types', 'import'], |
| 36 | + extensions: ['.d.ts', '.ts'], |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + const { error, path: resolved } = await typesResolver.async( |
| 41 | + path.dirname(importer), |
| 42 | + id, |
27 | 43 | ) |
28 | | -} |
29 | | -function tryResolve(id: string, importer: string) { |
30 | | - const filePath = path.resolve(importer, '..', id) |
31 | | - try { |
32 | | - const stat = lstatSync(filePath) |
33 | | - if (stat.isFile()) return filePath |
34 | | - } catch { |
35 | | - return |
| 44 | + if (error || !resolved) return |
| 45 | + |
| 46 | + collectReferencedFile(importer, resolved) |
| 47 | + if (resolveCache.has(importer)) { |
| 48 | + resolveCache.get(importer)!.set(id, resolved) |
| 49 | + } else { |
| 50 | + resolveCache.set(importer, new Map([[id, resolved]])) |
36 | 51 | } |
| 52 | + return resolved |
37 | 53 | } |
38 | 54 |
|
39 | | -let resolveTSFileIdImpl: ResolveTSFileIdImpl = resolveTSFileIdNode |
| 55 | +export const resolveDtsHMR: NonNullable<Plugin['handleHotUpdate']> = ({ |
| 56 | + file, |
| 57 | + server, |
| 58 | + modules, |
| 59 | +}) => { |
| 60 | + const cache = new Map<string, Set<ModuleNode>>() |
| 61 | + if (tsFileCache[file]) delete tsFileCache[file] |
| 62 | + |
| 63 | + const affected = getAffectedModules(file) |
| 64 | + return [...modules, ...affected] |
40 | 65 |
|
41 | | -export function setResolveTSFileIdImpl(impl: ResolveTSFileIdImpl): void { |
42 | | - resolveTSFileIdImpl = impl |
| 66 | + function getAffectedModules(file: string): Set<ModuleNode> { |
| 67 | + if (cache.has(file)) return cache.get(file)! |
| 68 | + if (!referencedFiles.has(file)) return new Set([]) |
| 69 | + |
| 70 | + const modules = new Set<ModuleNode>([]) |
| 71 | + cache.set(file, modules) |
| 72 | + for (const importer of referencedFiles.get(file)!) { |
| 73 | + const mods = server.moduleGraph.getModulesByFile(importer) |
| 74 | + if (mods) mods.forEach((m) => modules.add(m)) |
| 75 | + |
| 76 | + getAffectedModules(importer).forEach((m) => modules.add(m)) |
| 77 | + } |
| 78 | + return modules |
| 79 | + } |
43 | 80 | } |
0 commit comments