Skip to content

Commit

Permalink
refactor(cjs): always inherit query from parent (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Jun 30, 2024
1 parent aa3f2a1 commit c6d5203
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/cjs/api/module-resolve-filename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ export const createResolveFilename = (

if (parent?.filename) {
const filePath = getOriginalFilePath(parent.filename);
let query: string | undefined;
if (filePath) {
const pathAndQuery = filePath.split('?');
const newFilename = pathAndQuery[0];
query = pathAndQuery[1];

/**
* Can't delete the old cache entry because there's an assertion
Expand All @@ -185,17 +187,19 @@ export const createResolveFilename = (
// delete Module._cache[parent.filename];

parent.filename = newFilename;
// @ts-expect-error - private property
parent.path = path.dirname(newFilename);
// https://github.com/nodejs/node/blob/v20.15.0/lib/internal/modules/esm/translators.js#L383
// @ts-expect-error - private property
parent.paths = Module._nodeModulePaths(parent.path);

Module._cache[newFilename] = parent as NodeModule;
}

if (!query) {
query = parent.filename.split('?')[1];
}

// Inherit parent namespace if it exists
const parentQuery = new URLSearchParams(parent.filename.split('?')[1]);
const parentQuery = new URLSearchParams(query);
const parentNamespace = parentQuery.get('namespace');
if (parentNamespace) {
searchParams.append('namespace', parentNamespace);
Expand Down Expand Up @@ -238,5 +242,24 @@ export const createResolveFilename = (
resolved += urlSearchParamsStringify(searchParams);
}

// Only the CJS lexer doesn't pass in the rest of the arguments
if (restOfArgs.length === 0) {
/**
* If this is called by the CJS lexer, the resolved path is directly passed into
* readFile to parse the exports
*
* Detect it via stack and return a valid path
*
* https://github.com/nodejs/node/blob/v20.15.0/lib/internal/modules/esm/translators.js#L415
*/
// eslint-disable-next-line unicorn/error-message
const stack = new Error().stack!.split('\n').slice(1);
const cjsPrepareCall = 'at cjsPreparseModuleExports (node:internal';
const isFromCjsLexer = stack[1].includes(cjsPrepareCall) || stack[2].includes(cjsPrepareCall);
if (isFromCjsLexer) {
resolved = resolved.split('?')[0];
}
}

return resolved;
};

0 comments on commit c6d5203

Please sign in to comment.