-
-
Notifications
You must be signed in to change notification settings - Fork 36.7k
Expand file tree
/
Copy pathexample-loader.mjs
More file actions
66 lines (63 loc) Β· 1.59 KB
/
Copy pathexample-loader.mjs
File metadata and controls
66 lines (63 loc) Β· 1.59 KB
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
import { extname } from 'path';
import { default as Module } from 'module';
const JS_EXTENSIONS = new Set(['.js', '.mjs']);
const baseURL = new URL('file://');
baseURL.pathname = process.cwd() + '/';
/**
* @param {!(string)} specifier
* @param {{
* parentURL:!(URL|string|undefined),
* }} context
* @param {!(Function)} defaultResolve
* @returns {{
* url:!(string),
* }}
*/
export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
if (Module.builtinModules.includes(specifier)) {
return {
url: 'nodejs:' + specifier,
};
}
if (
/^\.{1,2}[/]/.test(specifier) !== true &&
!specifier.startsWith('file:')
) {
// For node_modules support:
// return defaultResolve(specifier, {parentURL}, defaultResolve);
throw new Error(
"Imports must either be URLs or begin with './' or '../'; " +
`'${specifier}' satisfied neither of these requirements.`
);
}
const resolved = new URL(specifier, parentURL);
return {
url: resolved.href,
};
}
/**
* @param {!(string)} url
// * param {!(Object)} context
// * param {!(Function)} defaultGetFormat
* @returns {{
* format:!(string),
* }}
*/
export function getFormat(url /* , context, defaultGetFormat */) {
if (url.startsWith('nodejs:') &&
Module.builtinModules.includes(url.slice(7))) {
return {
format: 'builtin',
};
}
const { pathname } = new URL(url);
const ext = extname(pathname);
if (!JS_EXTENSIONS.has(ext)) {
throw new Error(
`Cannot load file with non-JavaScript file extension ${ext}.`
);
}
return {
format: 'module',
};
}