| 60 | /^\.{0,2}\//.test(specifier) || isAbsolute(specifier) |
| 61 | |
| 62 | export const createNodeHost = (opts?: NodeHostOptions): PrecompileHost => { |
| 63 | const resolveOptions: NapiResolveOptions = { |
| 64 | conditionNames: opts?.conditions ?? nodeConditions, |
| 65 | extensions: [...scriptExtensions, ".json"], |
| 66 | extensionAlias: { |
| 67 | ".js": [".ts", ".tsx", ".js"], |
| 68 | ".jsx": [".tsx", ".jsx"], |
| 69 | ".mjs": [".mts", ".mjs"], |
| 70 | ".cjs": [".cts", ".cjs"] |
| 71 | } |
| 72 | } |
| 73 | const resolver = new ResolverFactory({ |
| 74 | ...resolveOptions, |
| 75 | tsconfig: |
| 76 | opts?.tsconfig ? |
| 77 | { configFile: opts.tsconfig, references: "auto" } |
| 78 | : "auto" |
| 79 | }) |
| 80 | let resolverWithoutTsconfig: ResolverFactory | undefined |
| 81 | const tsconfigsByDir: Record<string, string | undefined> = {} |
| 82 | const tsconfigIn = (dir: string): string | undefined => { |
| 83 | if (dir in tsconfigsByDir) return tsconfigsByDir[dir] |
| 84 | const tsconfig = join(dir, "tsconfig.json") |
| 85 | return (tsconfigsByDir[dir] = |
| 86 | existsSync(tsconfig) ? tsconfig |
| 87 | : dir === dirname(dir) ? undefined |
| 88 | : tsconfigIn(dirname(dir))) |
| 89 | } |
| 90 | // each tsconfig's resolver for a file its `include` leaves out |
| 91 | const resolversByTsconfig: Record<string, ResolverFactory> = {} |
| 92 | // the resolver keeps what it found of a file, there or not, for as long as |
| 93 | // it lives, so an answer is kept as long: following a program's validators |
| 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 { |