Skip to content

Commit

Permalink
fix(es/module): Resolve .jsx imports fully (#8936)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8935
  • Loading branch information
kdy1 authored May 8, 2024
1 parent 6021b47 commit c536d2a
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 5 deletions.
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} />;
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8935/input/src/greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function greet() {
return "Hello";
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8935/input/src/index.js
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);
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8935/output/src/greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function greet() {
return "Hello";
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8935/output/src/index.js
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));
}

0 comments on commit c536d2a

Please sign in to comment.