-
Notifications
You must be signed in to change notification settings - Fork 34
/
esm.civet
78 lines (59 loc) · 2.06 KB
/
esm.civet
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
@file Civet ESM loader.
In Node 20.6.0+, use Civet's `register` to install this ESM loader:
@example
```bash
node --import @danielx/civet/register source.civet
```
In older Node, use `--loader` to install this ESM loader:
@example
```bash
node --loader @danielx/civet/esm source.civet
```
Previously depended on ts-node esm loader being downstream:
@example
```bash
node --loader ts-node/esm --loader @danielx/civet/esm source.civet
```
*/
{ readFileSync } from fs
{ pathToFileURL, fileURLToPath } from url
Civet from ./main.civet
{ compile, SourceMap } := Civet
baseURL := pathToFileURL(process.cwd() + '/').href
extensionsRegex := /\.civet$/
export function resolve(specifier: string, context: any, next: any)
{ parentURL = baseURL } := context
if extensionsRegex.test specifier
return
shortCircuit: true
format: "civet"
url: new URL(specifier, parentURL).href
// Let Node.js handle all other specifiers.
return next specifier, context
export async function load(url: string, context: any, next: any)
if context.format is "civet"
path := fileURLToPath url
source := readFileSync path, "utf8"
{code: tsSource, sourceMap} := await compile source,
filename: path
sourceMap: true
js: true
// NOTE: Append .tsx to URL so ts-node treats as TypeScript
transpiledUrl := url + ".tsx"
// NOTE: Assuming ts-node hook follows load hook
result := await next transpiledUrl,
// ts-node won't transpile unless this is module
// can't use commonjs since we don't rewrite imports
format: "module"
// NOTE: Setting the source in the context makes it available when ts-node uses defaultLoad
source: tsSource
// Remove .tsx extension for final URL
result.responseURL = (result.responseURL ?? transpiledUrl)
.replace /.tsx$/, ''
// parse source map from downstream (ts-node) result
// compose with civet source map
result.source = SourceMap.remap(result.source, sourceMap, url, result.responseURL)
return result
// Other URLs continue unchanged.
return next url, context