Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): resolve exports even if parent module filename isn't present #26553

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ext/node/ops/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,16 @@ where
return Ok(None);
};

let referrer = Url::from_file_path(parent_path).unwrap();
let referrer = if parent_path.is_empty() {
None
} else {
Some(Url::from_file_path(parent_path).unwrap())
};
let r = node_resolver.package_exports_resolve(
&pkg.path,
&format!(".{expansion}"),
exports,
Some(&referrer),
referrer.as_ref(),
NodeModuleKind::Cjs,
REQUIRE_CONDITIONS,
NodeResolutionMode::Execution,
Expand Down
6 changes: 1 addition & 5 deletions ext/node/polyfills/01_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,13 @@ function resolveExports(
return;
}

if (!parentPath) {
return false;
}

return op_require_resolve_exports(
usesLocalNodeModulesDir,
modulesPath,
request,
name,
expansion,
parentPath,
parentPath ?? "",
) ?? false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@denotest/cjs-multiple-exports",
"version": "1.0.0",
"exports": {
".": "./src/index.js",
"./add": "./src/add.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function add(a, b) {
return a + b;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
hello: "world"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"tempDir": true,
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"args": "run -A main.cjs",
"output": "3\n"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require("node:path");
const Module = require("node:module");
function requireFromString(code, filename) {
const paths = Module._nodeModulePaths((0, path.dirname)(filename));
const m = new Module(filename, module.parent);
m.paths = paths;
m._compile(code, filename);
return m.exports;
}

const code = `
const add = require("@denotest/cjs-multiple-exports/add");

console.log(add(1, 2));
`;
requireFromString(code, "fake.js");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@denotest/cjs-multiple-exports": "1.0.0"
}
}