-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
loader.ts
180 lines (149 loc) · 4.75 KB
/
loader.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { existsSync, promises as fs } from 'fs';
import { fileURLToPath, URL } from 'url';
import * as tsm from './utils.js';
import type { Config, Extension, Options } from 'tsm/config';
type TSM = typeof import('./utils.d');
let config: Config;
let esbuild: typeof import('esbuild');
let env = (tsm as TSM).$defaults('esm');
let setup = env.file && import('file:///' + env.file);
type Promisable<T> = Promise<T> | T;
type Source = string | SharedArrayBuffer | Uint8Array;
type Format = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm';
type Resolve = (
ident: string,
context: {
conditions: string[];
parentURL?: string;
},
fallback: Resolve
) => Promisable<{
url: string;
shortCircuit: boolean;
format?: Format;
}>;
type Inspect = (
url: string,
context: object,
fallback: Inspect
) => Promisable<{ format: Format }>;
type Transform = (
source: Source,
context: Record<'url' | 'format', string>,
fallback: Transform
) => Promisable<{ source: Source }>;
type LoadResult = Promisable<{
format: Format;
shortCircuit: boolean;
source: Source;
}>
type Load = (
url: string,
context: { format?: Format },
fallback: (url: string, context: { format?: Format }) => LoadResult
) => LoadResult;
async function toConfig(): Promise<Config> {
let mod = await setup;
mod = mod && mod.default || mod;
return (tsm as TSM).$finalize(env, mod);
}
const EXTN = /\.\w+(?=\?|$)/;
const isTS = /\.[mc]?tsx?(?=\?|$)/;
async function toOptions(uri: string): Promise<Options|void> {
config = config || await toConfig();
let [extn] = EXTN.exec(uri) || [];
return config[extn as `.${string}`];
}
function check(fileurl: string): string | void {
let tmp = fileURLToPath(fileurl);
if (existsSync(tmp)) return fileurl;
}
/**
* extension aliases; runs after checking for extn on disk
* @example `import('./foo.mjs')` but only `foo.mts` exists
*/
const MAPs: Record<Extension, Extension[]> = {
'.js': ['.ts', '.tsx', '.jsx'],
'.jsx': ['.tsx'],
'.mjs': ['.mts'],
'.cjs': ['.cts'],
};
const root = new URL('file:///' + process.cwd() + '/');
export const resolve: Resolve = async function (ident, context, fallback) {
// ignore "prefix:" and non-relative identifiers
if (/^\w+\:?/.test(ident)) return fallback(ident, context, fallback);
let target = new URL(ident, context.parentURL || root);
let ext: Extension, path: string | void, arr: Extension[];
let match: RegExpExecArray | null, i=0, base: string;
// source ident includes extension
if (match = EXTN.exec(target.href)) {
ext = match[0] as Extension;
if (!context.parentURL || isTS.test(ext)) {
return { url: target.href, shortCircuit: true };
}
// target ident exists
if (path = check(target.href)) {
return { url: path, shortCircuit: true };
}
// target is virtual alias
if (arr = MAPs[ext]) {
base = target.href.substring(0, match.index);
for (; i < arr.length; i++) {
if (path = check(base + arr[i])) {
i = match.index + ext.length;
return {
shortCircuit: true,
url: i > target.href.length
// handle target `?args` trailer
? base + target.href.substring(i)
: path
};
}
}
}
// return original behavior, let it error
return fallback(ident, context, fallback);
}
config = config || await toConfig();
for (ext in config) {
path = check(target.href + ext);
if (path) return { url: path, shortCircuit: true };
}
return fallback(ident, context, fallback);
}
export const load: Load = async function (uri, context, fallback) {
// note: inline `getFormat`
let options = await toOptions(uri);
if (options == null) return fallback(uri, context);
let format: Format = options.format === 'cjs' ? 'commonjs' : 'module';
// TODO: decode SAB/U8 correctly
let path = fileURLToPath(uri);
let source = await fs.readFile(path);
// note: inline `transformSource`
esbuild = esbuild || await import('esbuild');
let result = await esbuild.transform(source.toString(), {
...options,
sourcefile: path,
format: format === 'module' ? 'esm' : 'cjs',
});
return { format, source: result.code, shortCircuit: true };
}
/** @deprecated */
export const getFormat: Inspect = async function (uri, context, fallback) {
let options = await toOptions(uri);
if (options == null) return fallback(uri, context, fallback);
return { format: options.format === 'cjs' ? 'commonjs' : 'module' };
}
/** @deprecated */
export const transformSource: Transform = async function (source, context, xform) {
let options = await toOptions(context.url);
if (options == null) return xform(source, context, xform);
// TODO: decode SAB/U8 correctly
esbuild = esbuild || await import('esbuild');
let result = await esbuild.transform(source.toString(), {
...options,
sourcefile: context.url,
format: context.format === 'module' ? 'esm' : 'cjs',
});
return { source: result.code };
}