Skip to content

Commit

Permalink
fix(es/module): Fix jsc.paths with resolveFully (#8784)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8782
  • Loading branch information
kdy1 authored Mar 28, 2024
1 parent 5a619eb commit 4961bb0
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 19 deletions.
3 changes: 2 additions & 1 deletion crates/swc/tests/fixture/issues-7xxx/7829/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"module": {
"type": "es6"
"type": "es6",
"resolveFully": true
},
"jsc": {
"baseUrl": ".",
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7829/1/output/1.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { fn } from "./libs/pkg/src/index.ts";
import { fn } from "./libs/pkg/src/index.js";
console.log(fn);
3 changes: 2 additions & 1 deletion crates/swc/tests/fixture/issues-7xxx/7829/2/input/.swcrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"module": {
"type": "es6"
"type": "es6",
"resolveFully": true
},
"jsc": {
"baseUrl": ".",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
const _a = require("./packages/a/src/index.ts");
const _a = require("./packages/a/src/index.js");
console.log(`${(0, _a.displayA)()}`);
23 changes: 23 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8782/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"isModule": true,
"jsc": {
"target": "es2022",
"keepClassNames": true,
"loose": false,
"baseUrl": ".",
"paths": {
"@foo/math": ["libs/math/index.js"],
"@foo/math/*": ["libs/math/*"],
"@foo/common": ["libs/common/main.js"]
},
"parser": {
"syntax": "typescript"
}
},
"minify": false,
"module": {
"type": "es6",
"resolveFully": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const commonStuff = 'common string';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { models } from "../../projects/api/models.js";

console.log(models);

export { foo } from "./src/foo.js";

export const sum = function(a, b) {
return a + b;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// import { commonStuff } from '../../common/index.js';
import { commonStuff } from '@foo/common';
console.log(commonStuff)

export const foo = 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sum } from "@foo/math";
// import { foo } from "@foo/math/src/foo";
import { foo } from "@foo/math/src/foo.js";
console.log(foo);

console.log(sum(1, 2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const models = {
foo: 'foo'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const commonStuff = 'common string';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { models } from "../../projects/api/models.js";
console.log(models);
export { foo } from "./src/foo.js";
export const sum = function(a, b) {
return a + b;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// import { commonStuff } from '../../common/index.js';
import { commonStuff } from "../../common/main.js";
console.log(commonStuff);
export const foo = 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { sum } from "../../libs/math/index.js";
// import { foo } from "@foo/math/src/foo";
import { foo } from "../../libs/math/src/foo.js";
console.log(foo);
console.log(sum(1, 2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const models = {
foo: 'foo'
};
19 changes: 7 additions & 12 deletions crates/swc_ecma_loader/src/resolvers/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,26 +289,21 @@ where
let slug = to[0]
.split([std::path::MAIN_SEPARATOR, '/'])
.last()
.map(|v| v.into());
.filter(|&slug| slug != "index.ts" && slug != "index.tsx")
.map(|v| v.split_once('.').map(|v| v.0).unwrap_or(v))
.map(From::from);

if tp.is_absolute() {
return Ok(Resolution {
filename: FileName::Real(tp.into()),
slug,
});
}

if let Ok(res) = self.resolve(&self.base_url_filename, &format!("./{}", &to[0]))
if let Ok(res) = self
.invoke_inner_resolver(&self.base_url_filename, &format!("./{}", &to[0]))
{
return Ok(Resolution {
slug: match &res.filename {
FileName::Real(p) => p
.file_stem()
.filter(|&s| s != "index")
.map(|v| v.to_string_lossy().into()),
_ => None,
},
..res
});
return Ok(Resolution { slug, ..res });
}

return Ok(Resolution {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_loader/tests/tsc_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn exact() {
resolved,
Resolution {
filename: FileName::Custom("success".into()),
slug: None
slug: Some("jquery".into())
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_module/tests/path_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn paths_resolver(base_dir: &Path, rules: Vec<(String, Vec<String>)>) -> JscPath
),
swc_ecma_transforms_module::path::Config {
base_dir: Some(base_dir),
resolve_fully: false,
resolve_fully: true,
},
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import o from ".";
import o from "./index.js";
export default function bar() {
console.log(o);
}
1 change: 1 addition & 0 deletions node-swc/__tests__/transform/issue_4730_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ it("should work", async () => {
},
module: {
type: "commonjs",
resolveFully: true,
},
});
expect(code).toMatchInlineSnapshot(`
Expand Down

0 comments on commit 4961bb0

Please sign in to comment.