Skip to content

Commit

Permalink
Fix module_resolution: "nodenext" with mjs or cjs (vercel#71635)
Browse files Browse the repository at this point in the history
A regression from vercel/turborepo#8748

Closes PACK-3312

Previously, it treated imports to `something.mjs` as importing
`something.mts` with a fallback to `something.js`.

But it should obviously instead fallback to the original extension
`mjs`, which is also what tsc does:

```
Found 'package.json' at 'input/package.json'.
======== Resolving module './src/foo.mjs' from 'input/index.js'. ========
Explicitly specified module resolution kind: 'NodeNext'.
Resolving in CJS mode with conditions 'require', 'types', 'node'.
Loading module as file / folder, candidate module location 'input/src/foo.mjs', target file types: TypeScript, JavaScript, Declaration.
File name 'input/src/foo.mjs' has a '.mjs' extension - stripping it.
File 'input/src/foo.mts' does not exist.
File 'input/src/foo.d.mts' does not exist.
File 'input/src/foo.mjs' does not exist.
File 'input/src/foo.mjs.ts' does not exist.
File 'input/src/foo.mjs.tsx' does not exist.
File 'input/src/foo.mjs.d.ts' does not exist.
File 'input/src/foo.mjs.js' does not exist.
File 'input/src/foo.mjs.jsx' does not exist.
Directory 'input/src/foo.mjs' does not exist, skipping all lookups in it.
======== Module name './src/foo.mjs' was not resolved. ========
```
  • Loading branch information
mischnic authored and stipsan committed Oct 22, 2024
1 parent abfd54b commit be1b4e9
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 33 deletions.
40 changes: 20 additions & 20 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,41 +2006,41 @@ async fn resolve_relative_request(

if options_value.enable_typescript_with_output_extension {
new_path.replace_final_constants(&|c: &RcStr| -> Option<Pattern> {
let result = match c.rsplit_once(".") {
Some((base, "js")) => Some((
let (base, replacement) = match c.rsplit_once(".") {
Some((base, "js")) => (
base,
vec![
Pattern::Constant(".ts".into()),
Pattern::Constant(".tsx".into()),
Pattern::Constant(".js".into()),
],
)),
Some((base, "mjs")) => Some((
),
Some((base, "mjs")) => (
base,
vec![
Pattern::Constant(".mts".into()),
Pattern::Constant(".js".into()),
Pattern::Constant(".mjs".into()),
],
)),
Some((base, "cjs")) => Some((
),
Some((base, "cjs")) => (
base,
vec![
Pattern::Constant(".cts".into()),
Pattern::Constant(".js".into()),
Pattern::Constant(".cjs".into()),
],
)),
_ => None,
};
result.map(|(base, replacement)| {
if base.is_empty() {
Pattern::Alternatives(replacement)
} else {
Pattern::Concatenation(vec![
Pattern::Constant(base.into()),
Pattern::Alternatives(replacement),
])
),
_ => {
return None;
}
})
};
if base.is_empty() {
Some(Pattern::Alternatives(replacement))
} else {
Some(Pattern::Concatenation(vec![
Pattern::Constant(base.into()),
Pattern::Alternatives(replacement),
]))
}
});
new_path.normalize();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import foo from "./src/foo.js";
import bar from "./src/bar.js";
import fooEsm from "./src/foo-esm.mjs";
import fooCjs from "./src/foo-cjs.cjs";
import fileTsx from "./src/file-tsx.js";
import fileMts from "./src/file-mts.mjs";
import fileCts from "./src/file-cts.cjs";
import fileMjs from "./src/file-mjs.mjs";
import fileCjs from "./src/file-cjs.cjs";

it("should correctly resolve explicit extensions with nodenext", () => {
expect(foo).toBe("foo");
expect(bar).toBe("bar");
expect(fooEsm).toBe("fooEsm");
expect(fooCjs).toBe("fooCjs");
expect(foo).toBe("foo.ts");
expect(fileTsx).toBe("file-tsx");
expect(fileMts).toBe("file-mts");
expect(fileCts).toBe("file-cts");
expect(fileMjs).toBe("file-mjs");
expect(fileCjs).toBe("file-cjs");
});

// import fooButton from "foo/button";

// it("should correctly resolve explicit extensions with nodenext", () => {
// expect(fooButton).toBe("button");
// });

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'file-cjs'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "file-cts";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'file-mjs'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "file-mts";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "file-tsx";

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default "foo";
export default "foo.ts";
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
"moduleResolution": "NodeNext",
"allowJs": true,
"outDir": "dist"
},
"include": ["index.js"],
}

0 comments on commit be1b4e9

Please sign in to comment.