node/test/fixtures/es-module-loaders/example-loader.mjs
Jacob Smith 447635b440
test: refactor ESM tests to improve performance
PR-URL: https://github.com/nodejs/node/pull/43784
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-07-29 10:42:55 +02:00

47 lines
1.2 KiB
JavaScript

import { URL } from 'url';
import path from 'path';
import process from 'process';
import { builtinModules } from 'module';
const JS_EXTENSIONS = new Set(['.js', '.mjs']);
const baseURL = new URL('file://');
baseURL.pathname = process.cwd() + '/';
export function resolve(specifier, { parentURL = baseURL }, next) {
if (builtinModules.includes(specifier)) {
return {
shortCircuit: true,
url: 'node:' + specifier
};
}
if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
// For node_modules support:
// return next(specifier);
throw new Error(
`imports must be URLs or begin with './', or '../'; '${specifier}' does not`);
}
const resolved = new URL(specifier, parentURL);
return {
shortCircuit: true,
url: resolved.href
};
}
export function getFormat(url, context, defaultGetFormat) {
if (url.startsWith('node:') && builtinModules.includes(url.slice(5))) {
return {
format: 'builtin'
};
}
const { pathname } = new URL(url);
const ext = path.extname(pathname);
if (!JS_EXTENSIONS.has(ext)) {
throw new Error(
`Cannot load file with non-JavaScript file extension ${ext}.`);
}
return {
format: 'module'
};
}