Skip to content

Commit

Permalink
bug/issue 1161 gracefully handle bare specifiers when resolve custom …
Browse files Browse the repository at this point in the history
…loader URLs (#1162)

* gracefully handle bare specifiers when resolve custom loader URLs

* handle bare specifiers from relative paths
  • Loading branch information
thescientist13 authored Oct 13, 2023
1 parent 4c84cab commit eb5b77f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/cli/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,21 @@ async function getCustomLoaderResponse(url, body = '', checkOnly = false) {
// https://nodejs.org/docs/latest-v18.x/api/esm.html#resolvespecifier-context-nextresolve
export async function resolve(specifier, context, defaultResolve) {
const { parentURL } = context;
const { shouldHandle } = await getCustomLoaderResponse(new URL(specifier), null, true);
const url = specifier.startsWith('file://')
? new URL(specifier)
: specifier.startsWith('.')
? new URL(specifier, parentURL)
: undefined;

if (shouldHandle) {
return {
url: new URL(specifier, parentURL).href,
shortCircuit: true
};
if (url) {
const { shouldHandle } = await getCustomLoaderResponse(url, null, true);

if (shouldHandle) {
return {
url: url.href,
shortCircuit: true
};
}
}

return defaultResolve(specifier, context, defaultResolve);
Expand All @@ -61,11 +69,11 @@ export async function resolve(specifier, context, defaultResolve) {
// https://nodejs.org/docs/latest-v18.x/api/esm.html#loadurl-context-nextload
export async function load(source, context, defaultLoad) {
const extension = source.split('.').pop();
const url = new URL('', `${source}?type=${extension}`);
const url = new URL(`${source}?type=${extension}`);
const { shouldHandle } = await getCustomLoaderResponse(url, null, true);

if (shouldHandle) {
const contents = await fs.readFile(new URL(source), 'utf-8');
const contents = await fs.readFile(url, 'utf-8');
const { response } = await getCustomLoaderResponse(url, contents);
const body = await response.text();

Expand Down

0 comments on commit eb5b77f

Please sign in to comment.