-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloader.mjs
More file actions
27 lines (24 loc) · 777 Bytes
/
loader.mjs
File metadata and controls
27 lines (24 loc) · 777 Bytes
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
26
27
import { registerHooks, stripTypeScriptTypes } from 'node:module';
const tsRegex = /^file:.*(?<!\.d)\.m?ts$/;
// Intercept .ts / .mts files (skipping .d.ts files) and
// transpile to JS, returning ES module
registerHooks({
load(url, context, nextLoad) {
if (tsRegex.test(url)) {
return {
format: 'module',
source: stripTypeScriptTypes(
/** @type {import('node:module').ModuleSource} */ (
// eslint-disable-next-line @typescript-eslint/no-base-to-string -- ModuleSource returns useful information from .toString()
nextLoad(url).source
).toString(),
{
mode: 'transform',
sourceUrl: url,
},
),
};
}
return nextLoad(url, context);
},
});