Skip to content

Commit

Permalink
Bug/issue 1042 update NodeJS custom loaders implementation (#1043)
Browse files Browse the repository at this point in the history
* update experimental custom loaders implementation to align with latest API design

* update documentation for recommended nodejs version for custom imports

* update nvmrc for latest node 16.19

* update documentation for recommended nodejs version for custom imports

* explicitely forward to custom resolve in test loader wrapper

* revert minimum nodejs version bumps
  • Loading branch information
thescientist13 authored Jan 19, 2023
1 parent a5a4403 commit ed9077a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ function getCustomLoaderPlugins(url, body, headers) {

// https://nodejs.org/docs/latest-v16.x/api/esm.html#resolvespecifier-context-nextresolve
export function resolve(specifier, context, defaultResolve) {
const { baseURL } = context;
const { parentURL } = context;

if (getCustomLoaderPlugins(specifier).length > 0) {
return {
url: new URL(specifier, baseURL).href,
url: new URL(specifier, parentURL).href,
shortCircuit: true
};
}
Expand Down
12 changes: 11 additions & 1 deletion test/test-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://github.com/nodejs/node/discussions/41711
import fs from 'fs';
import path from 'path';
import { load as experimentalLoad } from '../packages/cli/src/loader.js';
import { load as experimentalLoad, resolve as experimentalResolve } from '../packages/cli/src/loader.js';

export async function load(url, context, defaultLoad) {
const ext = path.extname(url);
Expand All @@ -14,6 +14,16 @@ export async function load(url, context, defaultLoad) {
}
}

export function resolve(specifier, context, defaultResolve) {
const ext = path.extname(specifier);

if (ext === '') {
return experimentalResolve(specifier, context, defaultResolve);
} else {
return defaultResolve(specifier, context, defaultResolve);
}
}

async function loadBin(url, context, defaultLoad) {
const dirs = path.dirname(url.replace(/[A-Z]:\//g, '')).split('/');
const parentDir = dirs.at(-1);
Expand Down

0 comments on commit ed9077a

Please sign in to comment.