2019-12-15 03:27:48 +00:00
|
|
|
import { URL } from 'url';
|
2017-09-03 11:20:06 +00:00
|
|
|
import path from 'path';
|
|
|
|
import process from 'process';
|
2019-02-25 21:27:55 +00:00
|
|
|
import { builtinModules } from 'module';
|
2017-09-03 11:20:06 +00:00
|
|
|
|
|
|
|
const JS_EXTENSIONS = new Set(['.js', '.mjs']);
|
|
|
|
|
2019-12-15 03:27:48 +00:00
|
|
|
const baseURL = new URL('file://');
|
2018-02-12 11:02:42 +00:00
|
|
|
baseURL.pathname = process.cwd() + '/';
|
|
|
|
|
2022-07-29 08:42:55 +00:00
|
|
|
export function resolve(specifier, { parentURL = baseURL }, next) {
|
2019-02-25 21:27:55 +00:00
|
|
|
if (builtinModules.includes(specifier)) {
|
2017-09-03 11:20:06 +00:00
|
|
|
return {
|
2022-05-04 15:51:12 +00:00
|
|
|
shortCircuit: true,
|
2020-09-28 04:24:44 +00:00
|
|
|
url: 'node:' + specifier
|
2017-09-03 11:20:06 +00:00
|
|
|
};
|
|
|
|
}
|
2019-09-27 03:20:09 +00:00
|
|
|
if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
|
2017-09-03 11:20:06 +00:00
|
|
|
// For node_modules support:
|
2022-07-29 08:42:55 +00:00
|
|
|
// return next(specifier);
|
2017-09-03 11:20:06 +00:00
|
|
|
throw new Error(
|
2019-09-27 03:20:09 +00:00
|
|
|
`imports must be URLs or begin with './', or '../'; '${specifier}' does not`);
|
2017-09-03 11:20:06 +00:00
|
|
|
}
|
2019-12-15 03:27:48 +00:00
|
|
|
const resolved = new URL(specifier, parentURL);
|
|
|
|
return {
|
2022-05-04 15:51:12 +00:00
|
|
|
shortCircuit: true,
|
2019-12-15 03:27:48 +00:00
|
|
|
url: resolved.href
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFormat(url, context, defaultGetFormat) {
|
2020-09-28 04:24:44 +00:00
|
|
|
if (url.startsWith('node:') && builtinModules.includes(url.slice(5))) {
|
2019-12-15 03:27:48 +00:00
|
|
|
return {
|
|
|
|
format: 'builtin'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { pathname } = new URL(url);
|
|
|
|
const ext = path.extname(pathname);
|
2017-09-03 11:20:06 +00:00
|
|
|
if (!JS_EXTENSIONS.has(ext)) {
|
|
|
|
throw new Error(
|
|
|
|
`Cannot load file with non-JavaScript file extension ${ext}.`);
|
|
|
|
}
|
|
|
|
return {
|
2018-08-28 15:28:46 +00:00
|
|
|
format: 'module'
|
2017-09-03 11:20:06 +00:00
|
|
|
};
|
|
|
|
}
|