Skip to content

Commit 549825a

Browse files
authored
fix: default resolve roots (#6340)
1 parent 57ed00b commit 549825a

File tree

11 files changed

+73
-61
lines changed

11 files changed

+73
-61
lines changed

Cargo.lock

Lines changed: 2 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rspack_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ json = { workspace = true }
2525
mime_guess = { workspace = true }
2626
num-bigint = "0.4.4"
2727
once_cell = { workspace = true }
28-
oxc_resolver = { version = "1.6.7", features = ["package_json_raw_json_api"] }
28+
oxc_resolver = { version = "1.7.0", features = ["package_json_raw_json_api"] }
2929
paste = { workspace = true }
3030
petgraph = { version = "0.6.4", features = ["serde-1"] }
3131
rayon = { workspace = true }

crates/rspack_core/src/resolver/resolver_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ fn to_oxc_resolver_options(
281281
restrictions,
282282
roots,
283283
builtin_modules: false,
284+
imports_fields: vec![vec!["imports".to_string()]],
284285
}
285286
}
286287

packages/rspack-test-tools/tests/__snapshots__/Defaults.unittest.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ Object {
477477
"modules": Array [
478478
"node_modules",
479479
],
480+
"roots": Array [
481+
"<cwd>",
482+
],
480483
},
481484
"resolveLoader": Object {
482485
"conditionNames": Array [

packages/rspack-test-tools/tests/defaultsCases/cache/non-root.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,33 @@ module.exports = {
99
cwd: path.resolve(__dirname, "../../../../rspack/tests/fixtures"),
1010
diff: e =>
1111
e.toMatchInlineSnapshot(`
12-
- Expected
13-
+ Received
12+
- Expected
13+
+ Received
1414
15-
@@ ... @@
16-
- "cache": false,
17-
- "context": "<cwd>",
18-
+ "cache": Object {
19-
+ "type": "filesystem",
20-
+ },
21-
+ "context": "<cwd>/tests/fixtures",
22-
@@ ... @@
23-
- "chunkLoadingGlobal": "webpackChunk_rspack_core",
24-
+ "chunkLoadingGlobal": "webpackChunk",
25-
@@ ... @@
26-
- "devtoolNamespace": "@rspack/core",
27-
+ "devtoolNamespace": "",
28-
@@ ... @@
29-
- "hotUpdateGlobal": "webpackHotUpdate_rspack_core",
30-
+ "hotUpdateGlobal": "webpackHotUpdate",
31-
@@ ... @@
32-
- "path": "<cwd>/dist",
33-
+ "path": "<cwd>/tests/fixtures/dist",
34-
@@ ... @@
35-
- "uniqueName": "@rspack/core",
36-
+ "uniqueName": "",
37-
`)
15+
@@ ... @@
16+
- "cache": false,
17+
- "context": "<cwd>",
18+
+ "cache": Object {
19+
+ "type": "filesystem",
20+
+ },
21+
+ "context": "<cwd>/tests/fixtures",
22+
@@ ... @@
23+
- "chunkLoadingGlobal": "webpackChunk_rspack_core",
24+
+ "chunkLoadingGlobal": "webpackChunk",
25+
@@ ... @@
26+
- "devtoolNamespace": "@rspack/core",
27+
+ "devtoolNamespace": "",
28+
@@ ... @@
29+
- "hotUpdateGlobal": "webpackHotUpdate_rspack_core",
30+
+ "hotUpdateGlobal": "webpackHotUpdate",
31+
@@ ... @@
32+
- "path": "<cwd>/dist",
33+
+ "path": "<cwd>/tests/fixtures/dist",
34+
@@ ... @@
35+
- "uniqueName": "@rspack/core",
36+
+ "uniqueName": "",
37+
@@ ... @@
38+
- "<cwd>",
39+
+ "<cwd>/tests/fixtures",
40+
`)
3841
};

packages/rspack-test-tools/tests/defaultsCases/config/browserslist.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ module.exports = {
66
}),
77
diff: e =>
88
e.toMatchInlineSnapshot(`
9-
- Expected
10-
+ Received
9+
- Expected
10+
+ Received
1111
12-
@@ ... @@
13-
- "context": "<cwd>",
14-
+ "context": "<cwd>/tests/fixtures/browserslist",
15-
@@ ... @@
16-
- "chunkLoadingGlobal": "webpackChunk_rspack_core",
17-
+ "chunkLoadingGlobal": "webpackChunkbrowserslist_test",
18-
@@ ... @@
19-
- "devtoolNamespace": "@rspack/core",
20-
+ "devtoolNamespace": "browserslist-test",
21-
@@ ... @@
22-
- "hotUpdateGlobal": "webpackHotUpdate_rspack_core",
23-
+ "hotUpdateGlobal": "webpackHotUpdatebrowserslist_test",
24-
@@ ... @@
25-
- "uniqueName": "@rspack/core",
26-
+ "uniqueName": "browserslist-test",
27-
`)
12+
@@ ... @@
13+
- "context": "<cwd>",
14+
+ "context": "<cwd>/tests/fixtures/browserslist",
15+
@@ ... @@
16+
- "chunkLoadingGlobal": "webpackChunk_rspack_core",
17+
+ "chunkLoadingGlobal": "webpackChunkbrowserslist_test",
18+
@@ ... @@
19+
- "devtoolNamespace": "@rspack/core",
20+
+ "devtoolNamespace": "browserslist-test",
21+
@@ ... @@
22+
- "hotUpdateGlobal": "webpackHotUpdate_rspack_core",
23+
+ "hotUpdateGlobal": "webpackHotUpdatebrowserslist_test",
24+
@@ ... @@
25+
- "uniqueName": "@rspack/core",
26+
+ "uniqueName": "browserslist-test",
27+
@@ ... @@
28+
- "<cwd>",
29+
+ "<cwd>/tests/fixtures/browserslist",
30+
`)
2831
};

packages/rspack/src/config/defaults.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const applyRspackOptionsDefaults = (
128128

129129
options.resolve = cleverMerge(
130130
getResolveDefaults({
131+
context: options.context!,
131132
targetProperties,
132133
mode: options.mode,
133134
css: options.experiments.css!
@@ -876,10 +877,12 @@ const getResolveLoaderDefaults = () => {
876877
// The values are aligned with webpack
877878
// https://github.com/webpack/webpack/blob/b9fb99c63ca433b24233e0bbc9ce336b47872c08/lib/config/defaults.js#L1431
878879
const getResolveDefaults = ({
880+
context,
879881
targetProperties,
880882
mode,
881883
css
882884
}: {
885+
context: string;
883886
targetProperties: any;
884887
mode?: Mode;
885888
css: boolean;
@@ -927,6 +930,7 @@ const getResolveDefaults = ({
927930
extensions: [],
928931
aliasFields: [],
929932
exportsFields: ["exports"],
933+
roots: [context],
930934
mainFields: ["main"],
931935
byDependency: {
932936
wasm: esmDeps(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'a'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "b"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import a from "/dir/a";
2+
3+
it("should support roots", async () => {
4+
expect(a).toBe("a");
5+
const b = "b"; // keep /dir2/ as a context module
6+
const m = await import("/dir2/" + b);
7+
expect(m.default).toBe("b")
8+
})

0 commit comments

Comments
 (0)