@@ -10,7 +10,12 @@ const {
1010 StringPrototypeStartsWith,
1111} = primordials ;
1212
13- const { Module, resolveForCJSWithHooks, clearCJSResolutionCaches } = require ( 'internal/modules/cjs/loader' ) ;
13+ const {
14+ Module,
15+ resolveForCJSWithHooks,
16+ clearCJSResolutionCaches,
17+ relativeResolveCache,
18+ } = require ( 'internal/modules/cjs/loader' ) ;
1419const { fileURLToPath, isURL, URLParse, pathToFileURL } = require ( 'internal/url' ) ;
1520const { emitExperimentalWarning, kEmptyObject, isWindows } = require ( 'internal/util' ) ;
1621const { validateObject, validateOneOf, validateString } = require ( 'internal/validators' ) ;
@@ -56,23 +61,6 @@ function normalizeClearCacheParent(parentURL) {
5661 return { __proto__ : null , parentURL : url . href , parentPath } ;
5762}
5863
59- /**
60- * Parse a specifier as a URL when possible.
61- * @param {string|URL } specifier
62- * @returns {URL|null }
63- */
64- function getURLFromClearCacheSpecifier ( specifier ) {
65- if ( isURL ( specifier ) ) {
66- return specifier ;
67- }
68-
69- if ( typeof specifier !== 'string' || path . isAbsolute ( specifier ) ) {
70- return null ;
71- }
72-
73- return URLParse ( specifier ) ?? null ;
74- }
75-
7664/**
7765 * Create a synthetic parent module for CJS resolution.
7866 * @param {string } parentPath
@@ -88,10 +76,9 @@ function createParentModuleForClearCache(parentPath) {
8876/**
8977 * Resolve a cache filename for CommonJS.
9078 * Always goes through resolveForCJSWithHooks so that registered hooks
91- * are respected. For file: URLs, search/hash are stripped before resolving
92- * since CJS operates on file paths. For non-file URLs, the specifier is
93- * passed as-is to let hooks handle it.
94- * @param {string|URL } specifier
79+ * are respected. CJS operates on file paths and bare specifiers; URL
80+ * objects are not valid require() arguments so they are not supported.
81+ * @param {string } specifier
9582 * @param {string|undefined } parentPath
9683 * @returns {string|null }
9784 */
@@ -100,26 +87,9 @@ function resolveClearCacheFilename(specifier, parentPath) {
10087 return null ;
10188 }
10289
103- const parsedURL = getURLFromClearCacheSpecifier ( specifier ) ;
104- let request = specifier ;
105- if ( parsedURL ) {
106- if ( parsedURL . protocol === 'file:' ) {
107- // Strip search/hash - CJS operates on file paths.
108- if ( parsedURL . search !== '' || parsedURL . hash !== '' ) {
109- parsedURL . search = '' ;
110- parsedURL . hash = '' ;
111- }
112- request = fileURLToPath ( parsedURL ) ;
113- } else {
114- // Non-file URLs (e.g. virtual://) - pass the href as-is
115- // so that registered hooks can resolve them.
116- request = parsedURL . href ;
117- }
118- }
119-
12090 const parent = parentPath ? createParentModuleForClearCache ( parentPath ) : null ;
12191 try {
122- const { filename, format } = resolveForCJSWithHooks ( request , parent , false , false ) ;
92+ const { filename, format } = resolveForCJSWithHooks ( specifier , parent , false , false ) ;
12393 if ( format === 'builtin' ) {
12494 return null ;
12595 }
@@ -314,10 +284,13 @@ function clearCache(specifier, options) {
314284 }
315285
316286 // CJS has relativeResolveCache and Module._pathCache that map
317- // specifiers to filenames. Clear entries pointing to the resolved file.
318- if ( resolvedFilename ) {
319- clearCJSResolutionCaches ( resolvedFilename ) ;
287+ // specifiers to filenames. Only clear the exact entry for this request.
288+ if ( resolver === 'require' ) {
289+ const request = isSpecifierURL ? specifier . href : specifier ;
290+ delete relativeResolveCache [ `${ parentPath } \x00${ request } ` ] ;
291+ }
320292
293+ if ( resolvedFilename ) {
321294 // Clear package.json caches for the resolved module's package so that
322295 // updated exports/imports conditions are picked up on re-resolution.
323296 const { getNearestParentPackageJSON, clearPackageJSONCache } =
0 commit comments