This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
loader.mjs
253 lines (213 loc) · 5.83 KB
/
loader.mjs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { URL, fileURLToPath, pathToFileURL } from 'url'
import fs from 'fs'
import { dirname } from 'path'
import { build, transformSync } from 'esbuild'
import semver from 'semver'
const isWindows = process.platform === 'win32'
const httpRegex = /^https?:\/\//
const extensionsRegex = /\.m?(tsx?|json)$/
async function esbuildResolve(id, dir) {
let result
await build({
stdin: {
contents: `import ${JSON.stringify(id)}`,
resolveDir: dir,
},
write: false,
bundle: true,
treeShaking: false,
ignoreAnnotations: true,
platform: 'node',
plugins: [{
name: 'resolve',
setup({ onLoad }) {
onLoad({ filter: /.*/ }, (args) => {
result = args.path
return { contents: '' }
})
},
}],
})
return result
}
function esbuildTransformSync(rawSource, filename, url, format) {
const {
code: js,
warnings,
map: jsSourceMap,
} = transformSync(rawSource.toString(), {
sourcefile: filename,
sourcemap: 'both',
loader: new URL(url).pathname.match(extensionsRegex)[1],
target: `node${process.versions.node}`,
format: format === 'module' ? 'esm' : 'cjs',
})
if (warnings && warnings.length > 0) {
for (const warning of warnings) {
console.warn(warning.location)
console.warn(warning.text)
}
}
return { js, jsSourceMap }
}
function getTsCompatSpecifier(parentURL, specifier) {
let tsSpecifier
let search
if (specifier.startsWith('./') || specifier.startsWith('../')) {
// Relative import
const url = new URL(specifier, parentURL)
tsSpecifier = fileURLToPath(url).replace(/\.tsx?$/, '')
search = url.search
}
else {
// Bare import
tsSpecifier = specifier
search = ''
}
return {
tsSpecifier,
search,
}
}
function isValidURL(s) {
try {
return !!new URL(s)
}
catch (e) {
if (e instanceof TypeError)
return false
throw e
}
}
async function resolveBase(specifier, context, defaultResolve) {
const {
parentURL,
} = context
if (httpRegex.test(specifier) || httpRegex.test(parentURL)) {
return {
url: new URL(specifier, parentURL).href,
format: 'module',
}
}
let url
// According to Node's algorithm, we first check if it is a valid URL.
// When the module is the entry point, node will provides a file URL to it.
if (isValidURL(specifier)) {
url = new URL(specifier)
}
else {
// Try to resolve the module according to typescript's algorithm,
// and construct a valid url.
const parsed = getTsCompatSpecifier(parentURL, specifier)
const path = await esbuildResolve(parsed.tsSpecifier, dirname(fileURLToPath(parentURL)))
if (path) {
url = pathToFileURL(path)
url.search = parsed.search
}
}
if (url) {
// If the resolved file is typescript
if (extensionsRegex.test(url.pathname)) {
return {
url: url.href,
format: 'module',
}
}
// Else, for other types, use default resolve with the valid path
return defaultResolve(url.href, context, defaultResolve)
}
return defaultResolve(specifier, context, defaultResolve)
}
async function loadBase(url, context, defaultLoad) {
if (httpRegex.test(url)) {
return {
format: 'module',
source: await fetchNetworkModule(url),
}
}
if (extensionsRegex.test(new URL(url).pathname)) {
const { format } = context
let filename = url
if (!isWindows) filename = fileURLToPath(url)
const rawSource = fs.readFileSync(new URL(url), { encoding: 'utf8' })
const { js } = esbuildTransformSync(rawSource, filename, url, format)
return {
format: 'module',
source: js,
}
}
// Let Node.js handle all other format / sources.
return defaultLoad(url, context, defaultLoad)
}
function getFormatBase(url, context, defaultGetFormat) {
if (httpRegex.test(url)) {
return {
format: 'module',
}
}
if (extensionsRegex.test(new URL(url).pathname)) {
return {
format: 'module',
}
}
// Let Node.js handle all other URLs.
return defaultGetFormat(url, context, defaultGetFormat)
}
async function transformSourceBase(source, context, defaultTransformSource) {
const { url, format } = context
if (httpRegex.test(url)) {
return {
format: 'module',
source: await fetchNetworkModule(url),
}
}
if (extensionsRegex.test(new URL(url).pathname)) {
let filename = url
if (!isWindows) filename = fileURLToPath(url)
const { js } = esbuildTransformSync(source, filename, url, format)
return {
source: js,
}
}
// Let Node.js handle all other sources.
return defaultTransformSource(source, context, defaultTransformSource)
}
async function getSourceBase(url, context, defaultGetSource) {
if (httpRegex.test(url)) {
return {
source: await fetchNetworkModule(url),
}
}
return defaultGetSource(url, context, defaultGetSource)
}
const networkModuleCache = new Map()
function fetchNetworkModule(url) {
if (!networkModuleCache.has(url)) {
const promise = (async () => {
const _fetch = (typeof fetch != 'undefined')
? fetch
: (await import('node-fetch')).default
return await _fetch(url).then(r => r.text())
})()
networkModuleCache.set(url, promise)
}
return networkModuleCache.get(url)
}
// New hook starting from Node v16.12.0
// See: https://github.com/nodejs/node/pull/37468
const _resolve = resolveBase
let _load, _getFormat, _transformSource, _getSource
if (semver.satisfies(process.versions.node, '>=16.12.0')) {
_load = loadBase
}
else {
_getFormat = getFormatBase
_transformSource = transformSourceBase
_getSource = getSourceBase
}
export const resolve = _resolve
export const load = _load
export const getFormat = _getFormat
export const transformSource = _transformSource
export const getSource = _getSource
export { networkModuleCache }