Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions test/fixtures/es-module-loaders/example-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
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 }, defaultResolve) {
/**
* @param {string} specifier
* @param {{
* parentURL:(string|undefined),
* conditions:Array<string>,
* }} context
* @param {Function} defaultResolve
* @returns {{
* url:string,
* }}
*/
export function resolve(specifier, context, defaultResolve) {
const { parentURL = baseURL } = context;
if (builtinModules.includes(specifier)) {
return {
url: 'nodejs:' + specifier
};
}
if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
if (!(specifier.startsWith('file:') || /^\.{1,2}[/]/.test(specifier))) {
// For node_modules support:
// return defaultResolve(specifier, {parentURL}, defaultResolve);
throw new Error(
`imports must be URLs or begin with './', or '../'; '${specifier}' does not`);
"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
};
}

export function getFormat(url, context, defaultGetFormat) {
/**
* @param {string} url
* // param {Object} context
* // param {Function} defaultGetFormat
* @returns {{
* format:string,
* }}
*/
export function getFormat(url /* , context, defaultGetFormat */) {
if (url.startsWith('nodejs:') && builtinModules.includes(url.slice(7))) {
return {
format: 'builtin'
Expand All @@ -36,7 +56,7 @@ export function getFormat(url, context, defaultGetFormat) {
const ext = path.extname(pathname);
if (!JS_EXTENSIONS.has(ext)) {
throw new Error(
`Cannot load file with non-JavaScript file extension ${ext}.`);
`Cannot load file with non-JavaScript file extension of '${ext}'.`);
}
return {
format: 'module'
Expand Down