Skip to content

Commit

Permalink
revert basic implementation of resolveSync; use defaultResolve directly
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Dec 30, 2024
1 parent 10fa02d commit 1619459
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ class Hooks {
};
}

resolveSync(originalSpecifier, parentURL, importAttributes) {
return defaultResolve(originalSpecifier, parentURL, importAttributes);
resolveSync(_originalSpecifier, _parentURL, _importAttributes) {
throw new ERR_METHOD_NOT_IMPLEMENTED('resolveSync()');
}

/**
Expand Down
13 changes: 9 additions & 4 deletions lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ function getPackageJSONURL(specifier, base) {
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
}

const pjsonImportAttributes = { __proto__: null, type: 'json' };
let cascadedLoader;
/** @type {import('./esm/resolve.js').defaultResolve} */
let defaultResolve;
/**
* @param {URL['href'] | string | URL} specifier The location for which to get the "root" package.json
* @param {URL['href'] | string | URL} [base] The location of the current module (ex file://tmp/foo.js).
Expand Down Expand Up @@ -296,10 +296,15 @@ function findPackageJSON(specifier, base = 'data:') {
}

let resolvedTarget;
cascadedLoader ??= require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
defaultResolve ??= require('internal/modules/esm/resolve').defaultResolve;

try {
resolvedTarget = cascadedLoader.resolveSync(specifier, `${parentURL}`, pjsonImportAttributes).url;
// TODO(@JakobJingleheimer): Detect whether findPackageJSON is being used within a loader
// (possibly piggyback on `allowImportMetaResolve`)
// - When inside, use the default resolve
// - (I think it's impossible to use the chain because of re-entry & a deadlock from atomics).
// - When outside, use cascadedLoader.resolveSync (not implemented yet, but the pieces exist).
resolvedTarget = defaultResolve(specifier, { parentURL: `${parentURL}` }).url;
} catch (err) {
if (err.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
resolvedTarget = err.url;
Expand Down

0 comments on commit 1619459

Please sign in to comment.