Skip to content

Commit 43d41d2

Browse files
authored
fix: source map sources in node modules should convert to absolute path (#11501)
1 parent e447f57 commit 43d41d2

File tree

8 files changed

+79
-3
lines changed

8 files changed

+79
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rspack_loader_swc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ rspack_workspace = { workspace = true }
3535
rustc-hash = { workspace = true }
3636
serde = { workspace = true, features = ["derive"] }
3737
serde_json = { workspace = true }
38+
sugar_path = { workspace = true }
3839
swc = { workspace = true, features = ["manual-tokio-runtime"] }
3940
swc_config = { workspace = true }
4041
swc_core = { workspace = true, features = ["base", "ecma_ast", "common", "ecma_preset_env", "ecma_helpers_inline"] }

crates/rspack_loader_swc/src/lib.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod options;
33
mod plugin;
44
mod transformer;
55

6-
use std::default::Default;
6+
use std::{default::Default, path::Path};
77

88
use options::SwcCompilerOptionsWithAdditional;
99
pub use options::SwcLoaderJsOptions;
@@ -14,6 +14,7 @@ use rspack_error::{Diagnostic, Result, miette};
1414
use rspack_javascript_compiler::{JavaScriptCompiler, TransformOutput};
1515
use rspack_loader_runner::{Identifier, Loader, LoaderContext};
1616
pub use rspack_workspace::rspack_swc_core_version;
17+
use sugar_path::SugarPath;
1718
use swc_config::{merge::Merge, types::MergingOption};
1819
use swc_core::{
1920
base::config::{InputSourceMap, TransformConfig},
@@ -95,7 +96,7 @@ impl SwcLoader {
9596
};
9697

9798
let javascript_compiler = JavaScriptCompiler::new();
98-
let filename = FileName::Real(resource_path.into_std_path_buf());
99+
let filename = FileName::Real(resource_path.clone().into_std_path_buf());
99100

100101
let source = content.into_string_lossy();
101102
let is_typescript =
@@ -104,7 +105,7 @@ impl SwcLoader {
104105

105106
let TransformOutput {
106107
code,
107-
map,
108+
mut map,
108109
diagnostics,
109110
} = javascript_compiler.transform(
110111
source,
@@ -140,6 +141,29 @@ impl SwcLoader {
140141
);
141142
}
142143

144+
// When compiling target modules, SWC retrieves the source map via sourceMapUrl.
145+
// The sources paths in the source map are relative to the target module. We need to resolve these paths
146+
// to absolute paths using the resource path to avoid incorrect project path references.
147+
if let (Some(map), Some(resource_dir)) = (map.as_mut(), resource_path.parent()) {
148+
map.set_sources(
149+
map
150+
.sources()
151+
.iter()
152+
.map(|source| {
153+
let source_path = Path::new(source);
154+
if source_path.is_relative() {
155+
source_path
156+
.absolutize_with(resource_dir.as_std_path())
157+
.to_string_lossy()
158+
.into_owned()
159+
} else {
160+
source.to_string()
161+
}
162+
})
163+
.collect::<Vec<_>>(),
164+
);
165+
}
166+
143167
loader_context.finish_with((code, map));
144168

145169
Ok(())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import sayHello from "lib-with-source-map"
2+
sayHello("SyMind");

packages/rspack-test-tools/tests/configCases/source-map/source-map-resolve-vendor-path/node_modules/lib-with-source-map/index.js

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

packages/rspack-test-tools/tests/configCases/source-map/source-map-resolve-vendor-path/node_modules/lib-with-source-map/index.js.map

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

packages/rspack-test-tools/tests/configCases/source-map/source-map-resolve-vendor-path/node_modules/lib-with-source-map/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
devtool: "source-map",
4+
module: {
5+
rules: [
6+
{
7+
test: /\.(jsx?|tsx?)$/,
8+
use: [
9+
{
10+
loader: "builtin:swc-loader"
11+
}
12+
]
13+
}
14+
]
15+
},
16+
plugins: [
17+
{
18+
apply(compiler) {
19+
compiler.hooks.afterEmit.tap("PLUGIN", compilation => {
20+
const sourceMap = JSON.parse(compilation.assets["bundle0.js.map"].source());
21+
expect(sourceMap.sources).toEqual(expect.arrayContaining([
22+
'webpack:///./node_modules/lib-with-source-map/main.js',
23+
'webpack:///./index.js'
24+
]));
25+
});
26+
}
27+
}
28+
]
29+
};

0 commit comments

Comments
 (0)