-
Notifications
You must be signed in to change notification settings - Fork 1
/
ts-node-loader.js
25 lines (21 loc) · 995 Bytes
/
ts-node-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// This loader integrates `ts-node` loader with `tsconfig-paths` ability to
// resolve paths with Typescript path mappings, thanks to that, this module
// can be loaded directly from Typescript sources using `ts-node`:
//
// `node --experimental-specifier-resolution=node --loader ./ts-node-loader.js ./src/main.ts`
//
// Source: https://github.com/TypeStrong/ts-node/discussions/1450
//
import { pathToFileURL } from 'url';
import { resolve as resolveTs, getFormat, transformSource, load } from 'ts-node/esm';
import * as tsConfigPaths from 'tsconfig-paths'
export { getFormat, transformSource, load };
const { absoluteBaseUrl, paths } = tsConfigPaths.loadConfig()
const matchPath = tsConfigPaths.createMatchPath(absoluteBaseUrl, paths)
export function resolve(specifier, context, defaultResolver) {
const mappedSpecifier = matchPath(specifier);
if (mappedSpecifier) {
specifier = pathToFileURL(mappedSpecifier) + '.ts';
}
return resolveTs(specifier, context, defaultResolver);
}