| 94 | // asks for the same one thousands of times, each a realpath |
| 95 | const resolved = new Map<string, string | undefined>() |
| 96 | const resolve = (specifier: string, importer: string): string | undefined => { |
| 97 | if (!isAbsolute(importer)) return |
| 98 | let result = resolver.resolveFileSync(importer, specifier) |
| 99 | // a tsconfig the resolver can't load fails every resolution |
| 100 | if (result.error && /tsconfig/i.test(result.error)) { |
| 101 | resolverWithoutTsconfig ??= resolver.cloneWithOptions(resolveOptions) |
| 102 | result = resolverWithoutTsconfig.resolveFileSync(importer, specifier) |
| 103 | } |
| 104 | // e.g. `@app/schemas` from tests/user.test.ts, where tsconfig.json |
| 105 | // includes only src: TypeScript applies its `paths` to no file outside |
| 106 | // `include`, but esbuild, Bun and a bundler's alias for the same |
| 107 | // names apply them to every file under it, so the nearest's are tried |
| 108 | if (!result.path && !opts?.tsconfig && !isRelativeOrAbsolute(specifier)) { |
| 109 | const tsconfig = tsconfigIn(dirname(importer)) |
| 110 | if (tsconfig) { |
| 111 | try { |
| 112 | // a resolver of its own, as a clone shares what the first found |
| 113 | const outside = (resolversByTsconfig[tsconfig] ??= |
| 114 | new ResolverFactory({ |
| 115 | ...resolveOptions, |
| 116 | tsconfig: { configFile: tsconfig, references: "auto" } |
| 117 | })).resolveFileSync(importer, specifier) |
| 118 | if (outside.path) result = outside |
| 119 | } catch { |
| 120 | // one it can't load, which resolves nothing |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | // it keeps an importer's 8.3 names, which another importer may not have |
| 125 | return result.path && realPathOf(result.path) |
| 126 | } |
| 127 | return { |
| 128 | resolve: (specifier, importer) => { |
| 129 | const key = `${importer}\0${specifier}` |