-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.mjs
299 lines (251 loc) · 7.91 KB
/
index.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { fileURLToPath as _fileURLToPath, pathToFileURL } from 'url'
import { dirname } from 'path'
import { realpathSync, promises as fsp } from 'fs'
import { createRequire, builtinModules } from 'module'
import { moduleResolve } from 'import-meta-resolve'
// CommonJS
export function createCommonJS (url) {
const __filename = fileURLToPath(url)
const __dirname = dirname(__filename)
// Lazy require
let _nativeRequire
const getNativeRequire = () => _nativeRequire || (_nativeRequire = createRequire(url))
function require (id) { return getNativeRequire()(id) }
require.resolve = (id, options) => getNativeRequire().resolve(id, options)
return {
__filename,
__dirname,
require
}
}
// Resolve
const DEFAULT_CONDITIONS_SET = new Set(['node', 'import'])
const BUILTIN_MODULES = new Set(builtinModules)
const DEFAULT_URL = pathToFileURL(process.cwd())
const DEFAULT_EXTENSIONS = ['.mjs', '.cjs', '.js', '.json']
const NOT_FOUND_ERRORS = new Set(['ERR_MODULE_NOT_FOUND', 'ERR_UNSUPPORTED_DIR_IMPORT', 'MODULE_NOT_FOUND'])
function _tryModuleResolve (id, url, conditions) {
try {
return moduleResolve(id, url, conditions)
} catch (err) {
if (!NOT_FOUND_ERRORS.has(err.code)) {
throw err
}
return null
}
}
function _resolve (id, opts = {}) {
// console.log('> resolve ', id, 'from', opts.url)
// Skip if already has a protocol
if (/(node|data|http|https):/.test(id)) {
return id
}
// Skip builtins
if (BUILTIN_MODULES.has(id)) {
return 'node:' + id
}
// Defaults
const conditionsSet = opts.conditions ? new Set(opts.conditions) : DEFAULT_CONDITIONS_SET
const url = opts.url ? normalizeid(opts.url) : DEFAULT_URL
// Try simple resolve
let resolved = _tryModuleResolve(id, url, conditionsSet)
// Try other extensions if not found
if (!resolved) {
for (const prefix of ['', '/index']) {
for (const ext of opts.extensions || DEFAULT_EXTENSIONS) {
resolved = _tryModuleResolve(id + prefix + ext, url, conditionsSet)
if (resolved) { break }
}
if (resolved) { break }
}
}
// Throw error if not found
if (!resolved) {
const err = new Error(`Cannot find module ${id} imported from ${url}`)
err.code = 'ERR_MODULE_NOT_FOUND'
throw err
}
// Resolve realPath and normalize slash
const realPath = realpathSync(fileURLToPath(resolved))
return pathToFileURL(realPath).toString()
}
export function resolveSync (id, opts) {
return _resolve(id, opts)
}
export function resolve (id, opts) {
return _pcall(resolveSync, id, opts)
}
export function resolvePathSync (id, opts) {
return fileURLToPath(resolveSync(id, opts))
}
export function resolvePath (id, opts) {
return _pcall(resolvePathSync, id, opts)
}
export function createResolve (defaults) {
return (id, url) => {
return resolve(id, { url, ...defaults })
}
}
export function interopDefault (sourceModule) {
if (!(sourceModule && 'default' in sourceModule)) {
return sourceModule
}
const newModule = sourceModule.default
for (const key in sourceModule) {
if (key === 'default') {
try {
if (!(key in newModule)) {
Object.defineProperty(newModule, key, {
enumerable: false,
configurable: false,
get () { return newModule }
})
}
} catch (_err) {}
} else {
try {
if (!(key in newModule)) {
Object.defineProperty(newModule, key, {
enumerable: true,
configurable: true,
get () { return sourceModule[key] }
})
}
} catch (_err) {}
}
}
return newModule
}
// Evaluate
// TODO: Migrate to new Regexes
const EVAL_ESM_IMPORT_RE = /(?<=import .* from ['"])([^'"]+)(?=['"])|(?<=export .* from ['"])([^'"]+)(?=['"])|(?<=import\s*['"])([^'"]+)(?=['"])|(?<=import\s*\(['"])([^'"]+)(?=['"]\))/g
export async function loadModule (id, opts = {}) {
const url = await resolve(id, opts)
const code = await loadURL(url)
return evalModule(code, { ...opts, url })
}
export async function evalModule (code, opts = {}) {
const transformed = await transformModule(code, opts)
const dataURL = toDataURL(transformed, opts)
return import(dataURL).catch((err) => {
err.stack = err.stack.replace(new RegExp(dataURL, 'g'), opts.url || '_mlly_eval_.mjs')
throw err
})
}
export async function transformModule (code, opts) {
// Convert JSON to module
if (opts.url && opts.url.endsWith('.json')) {
return 'export default ' + code
}
// Resolve relative imports
code = await resolveImports(code, opts)
// Rewrite import.meta.url
if (opts.url) {
code = code.replace(/import\.meta\.url/g, `'${opts.url}'`)
}
return code
}
export async function resolveImports (code, opts) {
const imports = Array.from(code.matchAll(EVAL_ESM_IMPORT_RE)).map(m => m[0])
if (!imports.length) {
return code
}
const uniqueImports = Array.from(new Set(imports))
const resolved = new Map()
await Promise.all(uniqueImports.map(async (id) => {
let url = await resolve(id, opts)
if (url.endsWith('.json')) {
const code = await loadURL(url)
url = toDataURL(await transformModule(code, { url }))
}
resolved.set(id, url)
}))
const re = new RegExp(uniqueImports.map(i => `(${i})`).join('|'), 'g')
return code.replace(re, id => resolved.get(id))
}
// Import analyzes
export const ESM_STATIC_IMPORT_RE = /^(?<=\s*)import\s*(["'\s]*(?<imports>[\w*${}\n\r\t, /]+)from\s*)?["']\s*(?<specifier>.*[@\w_-]+)\s*["'][^\n]*$/gm
export const DYNAMIC_IMPORT_RE = /import\s*\((?<expression>(?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gm
function _matchAll (regex, string, addition) {
const matches = []
for (const match of string.matchAll(regex)) {
matches.push({
...addition,
...match.groups,
code: match[0],
start: match.index,
end: match.index + match[0].length
})
}
return matches
}
export function findStaticImports (code) {
return _matchAll(ESM_STATIC_IMPORT_RE, code, { type: 'static' })
}
export function findDynamicImports (code) {
return _matchAll(DYNAMIC_IMPORT_RE, code, { type: 'dynamic' })
}
export function parseStaticImport (matched) {
const cleanedImports = (matched.imports || '')
.replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, '')
.replace(/\s+/g, ' ')
const namedImports = {}
for (const namedImport of cleanedImports.match(/\{([^}]*)\}/)?.[1]?.split(',') || []) {
const [, source = namedImport.trim(), importName = source] = namedImport.match(/^\s*([^\s]*) as ([^\s]*)\s*$/) || []
if (source) {
namedImports[source] = importName
}
}
const topLevelImports = cleanedImports.replace(/\{([^}]*)\}/, '')
const namespacedImport = topLevelImports.match(/\* as \s*([^\s]*)/)?.[1]
const defaultImport = topLevelImports.split(',').find(i => !i.match(/[*{}]/))?.trim() || undefined
return {
...matched,
defaultImport,
namespacedImport,
namedImports
}
}
// Utils
export function fileURLToPath (id) {
if (typeof id === 'string' && !id.startsWith('file://')) {
return normalizeSlash(id)
}
return normalizeSlash(_fileURLToPath(id))
}
export function normalizeid (id) {
if (typeof id !== 'string') {
id = id.toString()
}
if (/(node|data|http|https|file):/.test(id)) {
return id
}
if (BUILTIN_MODULES.has(id)) {
return 'node:' + id
}
return 'file://' + normalizeSlash(id)
}
export async function loadURL (url) {
const code = await fsp.readFile(fileURLToPath(url), 'utf-8')
return code
}
export function toDataURL (code) {
const base64 = Buffer.from(code).toString('base64')
return `data:text/javascript;base64,${base64}`
}
function normalizeSlash (str) {
return str.replace(/\\/g, '/')
}
function _pcall (fn, ...args) {
try {
return Promise.resolve(fn(...args)).catch(err => _perr(err))
} catch (err) {
return _perr(err)
}
}
function _perr (_err) {
const err = new Error(_err)
err.code = _err.code
Error.captureStackTrace(err, _pcall)
return Promise.reject(err)
}