From 23ebe7e310e41771ca51db054779af968b6bc4fd Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 30 Jun 2024 12:27:00 +0900 Subject: [PATCH] refactor: preserve resolveFilename args accurately --- src/cjs/api/module-resolve-filename.ts | 19 +++++++++---------- src/esm/api/register.ts | 8 ++++++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/cjs/api/module-resolve-filename.ts b/src/cjs/api/module-resolve-filename.ts index f95582f38..50424c25b 100644 --- a/src/cjs/api/module-resolve-filename.ts +++ b/src/cjs/api/module-resolve-filename.ts @@ -160,11 +160,10 @@ export const createResolveFilename = ( ): ResolveFilename => ( request, parent, - isMain, - options, + ...restOfArgs ) => { if (state.enabled === false) { - return nextResolve(request, parent, isMain, options); + return nextResolve(request, parent, ...restOfArgs); } request = interopCjsExports(request); @@ -176,7 +175,8 @@ export const createResolveFilename = ( if (parent?.filename) { const filePath = getOriginalFilePath(parent.filename); if (filePath) { - const newFilename = filePath.split('?')[0]; + const pathAndQuery = filePath.split('?'); + const newFilename = pathAndQuery[0]; /** * Can't delete the old cache entry because there's an assertion @@ -204,7 +204,7 @@ export const createResolveFilename = ( // If request namespace doesnt match the namespace, ignore if ((searchParams.get('namespace') ?? undefined) !== namespace) { - return nextResolve(request, parent, isMain, options); + return nextResolve(request, parent, ...restOfArgs); } let _nextResolve = nextResolve; @@ -222,8 +222,7 @@ export const createResolveFilename = ( const resolve: SimpleResolve = request_ => _nextResolve( request_, parent, - isMain, - options, + ...restOfArgs, ); let resolved = resolveRequest(requestAndQuery[0], parent, resolve); @@ -232,9 +231,9 @@ export const createResolveFilename = ( if ( path.isAbsolute(resolved) - // These two have native loaders which don't support queries - && !resolved.endsWith('.json') - && !resolved.endsWith('.node') + // These two have native loaders which don't support queries + && !resolved.endsWith('.json') + && !resolved.endsWith('.node') ) { resolved += urlSearchParamsStringify(searchParams); } diff --git a/src/esm/api/register.ts b/src/esm/api/register.ts index 89c9f0c68..a6294ea8e 100644 --- a/src/esm/api/register.ts +++ b/src/esm/api/register.ts @@ -43,8 +43,12 @@ export const register: Register = ( if (!cjsInteropApplied) { const { _resolveFilename } = module; module._resolveFilename = ( - request, _parent, _isMain, _options, - ) => _resolveFilename(interopCjsExports(request), _parent, _isMain, _options); + request, + ...restOfArgs + ) => _resolveFilename( + interopCjsExports(request), + ...restOfArgs, + ); cjsInteropApplied = true; }