-
Notifications
You must be signed in to change notification settings - Fork 46
/
nodeLoader.mjs
50 lines (44 loc) · 1.52 KB
/
nodeLoader.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
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import callLoader from './lib/callLoader.js'
import yamlLoader from './lib/yamlLoader.js'
import eliminatorLoader from './lib/eliminatorLoader.js'
import { isStartupjsPluginEcosystemFile } from './lib/utils.js'
export function resolve (specifier, context, nextResolve) {
const { parentURL = null } = context
// Handling YAML files
if (specifier.endsWith('.yaml')) {
return {
shortCircuit: true,
url: new URL(specifier, parentURL).href
}
}
// Let Node.js handle all other specifiers.
return nextResolve(specifier, context, nextResolve)
}
export async function load (url, context, nextLoad) {
// If it's a YAML file, read it, parse it and return it as JavaScript source code
if (/\.yaml$/.test(url)) {
const filePath = fileURLToPath(url)
let source = await readFile(filePath, 'utf8')
source = callLoader(yamlLoader, source, filePath)
return {
format: 'module',
shortCircuit: true,
source
}
}
// process code elimination of other envs for *.plugin.js and startupjs.config.js
if (isStartupjsPluginEcosystemFile(url)) {
const filePath = fileURLToPath(url)
let source = await readFile(filePath, 'utf8')
source = callLoader(eliminatorLoader, source, filePath, { envs: ['features', 'isomorphic', 'server'] })
return {
format: 'module',
shortCircuit: true,
source
}
}
// Let Node.js handle all other URLs.
return nextLoad(url, context, nextLoad)
}