Skip to content

Commit

Permalink
fix(node): Include "node" condition during CJS re-export analysis (#2…
Browse files Browse the repository at this point in the history
…5785)

Fixes #25777.

We were missing the "node" condition, so we were resolving to the wrong
conditional export, causing our analysis to be incorrect.
  • Loading branch information
nathanwhit authored Sep 21, 2024
1 parent 4b02210 commit 9be8dce
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/node_resolver/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer, TNodeResolverEnv: NodeResolverEnv>
&referrer,
// FIXME(bartlomieju): check if these conditions are okay, probably
// should be `deno-require`, because `deno` is already used in `esm_resolver.rs`
&["deno", "require", "default"],
&["deno", "node", "require", "default"],
NodeResolutionMode::Execution,
);
let reexport_specifier = match result {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
hello: "bad"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
hello: "from node"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
hello: "default export"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@denotest/conditional-exports",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"node": "./good.cjs",
"require": "./bad.js",
"default": "./index.js"
},
"./*": "./*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Object.defineProperty(exports, '__esModule', { value: true });

const pkg = require("@denotest/conditional-exports-node");

Object.keys(pkg).forEach(function (k) {
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = pkg[k];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/package.cjs.js')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./index.js";
11 changes: 11 additions & 0 deletions tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@denotest/mjs-reexport-cjs",
"version": "1.0.0",
"dependencies": {
"@denotest/conditional-exports-node": "*"
},
"exports": {
"node": "./index.mjs",
"default": "./index.js"
}
}
13 changes: 13 additions & 0 deletions tests/specs/node/cjs_reexport_node_condition/__test__.jsonc
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.ts",
"output": "from node\n"
}
]
}
3 changes: 3 additions & 0 deletions tests/specs/node/cjs_reexport_node_condition/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nodeModulesDir": "manual"
}
3 changes: 3 additions & 0 deletions tests/specs/node/cjs_reexport_node_condition/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { hello } from "@denotest/mjs-reexport-cjs";

console.log(hello);
5 changes: 5 additions & 0 deletions tests/specs/node/cjs_reexport_node_condition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@denotest/mjs-reexport-cjs": "*"
}
}

0 comments on commit 9be8dce

Please sign in to comment.