UNPKG

@nothing-but/node-resolve-ts

Version:

Resolve TypeScript files in Node.js using ESM loader hooks.

23 lines (18 loc) 584 B
import path from 'node:path' /** @type {import('node:module').ResolveHook} */ export async function resolve(specifier, ctx, next) { let ext = path.extname(specifier) switch (ext) { case '.js': case '.jsx': try { return await next(specifier, ctx) } catch { switch (ext) { case '.js': return next(specifier.substring(0, specifier.length-3) + '.ts', ctx) case '.jsx': return next(specifier.substring(0, specifier.length-4) + '.tsx', ctx) } } } return next(specifier, ctx) }