Skip to content

Commit

Permalink
test: assert package can be loaded relative to data url
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jun 30, 2024
1 parent 143a5bd commit e0b581c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
29 changes: 21 additions & 8 deletions src/cjs/api/module-resolve-filename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,32 @@ export const createResolveFilename = (

request = interopCjsExports(request);

if (parent?.filename) {
const filePath = getOriginalFilePath(parent.filename);
if (filePath) {
parent.filename = filePath.split('?')[0];
}
}

// Strip query string
const requestAndQuery = request.split('?');
const searchParams = new URLSearchParams(requestAndQuery[1]);

// Inherit parent namespace if it exists
if (parent?.filename) {
const filePath = getOriginalFilePath(parent.filename);
if (filePath) {
const newFilename = filePath.split('?')[0];

/**
* Can't delete the old cache entry because there's an assertion
* https://github.com/nodejs/node/blob/v20.15.0/lib/internal/modules/esm/translators.js#L347
*/
// 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;
}

// Inherit parent namespace if it exists
const parentQuery = new URLSearchParams(parent.filename.split('?')[1]);
const parentNamespace = parentQuery.get('namespace');
if (parentNamespace) {
Expand Down
5 changes: 4 additions & 1 deletion tests/specs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const tsFiles = {
`,

cjs: {
node_modules: {
'pkg/index.js': 'module.exports = 1',
},
'exports-no.cts': `
// Supports decorators
const log = (target, key, descriptor) => descriptor;
Expand All @@ -36,7 +39,7 @@ const tsFiles = {
}
console.log("cts loaded" as string)
`,
'exports-yes.cts': 'module.exports = require("./reexport.cjs") as string',
'exports-yes.cts': 'module.exports = require("./reexport.cjs") as string; require("pkg");',
'esm-syntax.js': 'export const esmSyntax = "esm syntax"',
'reexport.cjs': `
exports.cjsReexport = "cjsReexport";
Expand Down

0 comments on commit e0b581c

Please sign in to comment.