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(es/module): Resolve .jsx imports fully #8936

Merged
merged 8 commits into from
May 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from "react";
var a = function(props) {
return /*#__PURE__*/ React.createElement("div", null);
};
a.propTypes = {};
export default a;
13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8935/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"module": {
"type": "es6",
"resolveFully": true
},
"jsc": {
"baseUrl": ".",
"parser": {
"syntax": "ecmascript",
"jsx": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function Button(props) {
return <button {...props} />;
}export function Button(props) {
return <button {...props} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function greet() {
return "Hello";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { greet } from "./greet";
export { Button } from "./button";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function Button(props) {
return /*#__PURE__*/ React.createElement("button", props);
}
export function Button(props) {
return /*#__PURE__*/ React.createElement("button", props);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function greet() {
return "Hello";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { greet } from "./greet.js";
export { Button } from "./button.js";
1 change: 1 addition & 0 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ fn tests(input_dir: PathBuf) {

if !entry.file_name().to_string_lossy().ends_with(".ts")
&& !entry.file_name().to_string_lossy().ends_with(".js")
&& !entry.file_name().to_string_lossy().ends_with(".jsx")
&& !entry.file_name().to_string_lossy().ends_with(".tsx")
{
continue;
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_transforms_module/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ where
false
};

let file_stem_matches = if let Some(stem) = target_path.file_stem() {
stem == orig_filename
} else {
false
};

if self.config.resolve_fully && is_resolved_as_js {
} else if orig_filename == "index" {
// Import: `./foo/index`
Expand All @@ -188,6 +194,8 @@ where
// Resolved: `./foo/index.js`

target_path.pop();
} else if is_resolved_as_non_js && self.config.resolve_fully && file_stem_matches {
target_path.set_extension("js");
} else if !is_resolved_as_js && !is_resolved_as_index && !is_exact {
target_path.set_file_name(orig_filename);
} else if is_resolved_as_non_js && is_exact {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import "./src/rel.decorator";
import "./src/rel.decorator.js";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import boo from "./src/utils/shared/foo/boo";
import boo from "./src/utils/shared/foo/boo.js";
console.log(boo());
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import "./src/rel.decorator";
import "./src/rel.decorator.js";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
async function main() {
const addFunction = (await import("./add")).default // This doesn't work
const addFunction = (await import("./add.js")).default // This doesn't work
;
console.log('2 + 3 =', addFunction(2, 3));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import addFunction from "./add";
import addFunction from "./add.js";
async function main() {
console.log('2 + 3 =', addFunction(2, 3));
}