Skip to content

Commit ea351b5

Browse files
zalishchukSyMind
andauthored
fix(source-maps): avoid loader request in lightningcss source maps (#13830)
* fix: avoid loader request in lightningcss source maps * fix: preserve absolute source paths in lightningcss loader maps --------- Co-authored-by: Cong-Cong Pan <dacongsama@live.com>
1 parent ecc70b3 commit ea351b5

7 files changed

Lines changed: 66 additions & 5 deletions

File tree

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_lightningcss/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ version.workspace = true
99

1010
[dependencies]
1111
async-trait = { workspace = true }
12+
cow-utils = { workspace = true }
1213
derive_more = { workspace = true, features = ["debug"] }
1314
lightningcss = { workspace = true, features = ["sourcemap", "into_owned"] }
1415
parcel_sourcemap = { workspace = true }

crates/rspack_loader_lightningcss/src/lib.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
};
55

66
use config::Config;
7+
use cow_utils::CowUtils;
78
use derive_more::Debug;
89
pub use lightningcss;
910
use lightningcss::{
@@ -175,11 +176,9 @@ impl LightningCssLoader {
175176
})
176177
.to_rspack_result()?;
177178

178-
let module_request = loader_context.context.module.request();
179-
180179
let mut parcel_source_map = if loader_context.context.source_map_kind.enabled() {
181180
let mut sm = parcel_sourcemap::SourceMap::new(&loader_context.context.options.context);
182-
sm.add_source(module_request);
181+
sm.add_source(&filename);
183182
sm.set_source_content(0, &content_str).to_rspack_result()?;
184183
Some(sm)
185184
} else {
@@ -221,12 +220,34 @@ impl LightningCssLoader {
221220
}),
222221
}
223222
}));
223+
224+
let mut posix_context = loader_context
225+
.context
226+
.options
227+
.context
228+
.cow_replace("\\", "/");
229+
if !posix_context.ends_with('/') {
230+
posix_context.to_mut().push('/');
231+
}
232+
let posix_context = posix_context.into_owned();
233+
224234
let rspack_source_map = SourceMap::new(
225235
mappings,
236+
// Parcel stores sources relative to project_root, while Rspack source maps
237+
// use absolute module paths for downstream loader/plugin handling.
226238
parcel_source_map
227239
.get_sources()
228240
.iter()
229-
.map(ToString::to_string)
241+
.map(|source| {
242+
if source.starts_with('/') || source.contains(':') {
243+
source.clone()
244+
} else {
245+
let mut absolute_source = String::with_capacity(posix_context.len() + source.len());
246+
absolute_source.push_str(&posix_context);
247+
absolute_source.push_str(source);
248+
absolute_source
249+
}
250+
})
230251
.collect::<Vec<_>>(),
231252
parcel_source_map
232253
.get_sources_content()
@@ -240,9 +261,10 @@ impl LightningCssLoader {
240261
.collect::<Vec<_>>(),
241262
);
242263

264+
let posix_name = filename.cow_replace("\\", "/");
243265
let source_map_source = SourceMapSource::new(SourceMapSourceOptions {
244266
value: content.code.clone(),
245-
name: module_request.to_string(),
267+
name: posix_name,
246268
source_map: rspack_source_map,
247269
original_source: None,
248270
inner_source_map: loader_context.take_source_map(),
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const path = require('node:path');
2+
3+
module.exports = function (code, map) {
4+
const expectedSource = path
5+
.resolve(__dirname, 'index.css')
6+
.replace(/\\/g, '/');
7+
8+
expect(map).toBeTruthy();
9+
expect(map.sources).toEqual([expectedSource]);
10+
11+
this.callback(null, code, map);
12+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.source-map-probe {
2+
color: red;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.css';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
mode: 'development',
4+
target: 'web',
5+
devtool: 'source-map',
6+
module: {
7+
generator: {
8+
'css/auto': {
9+
exportsOnly: false,
10+
},
11+
},
12+
rules: [
13+
{
14+
test: /\.css$/,
15+
use: ['./assert-source-map-loader.js', 'builtin:lightningcss-loader'],
16+
sideEffects: true,
17+
type: 'css/auto',
18+
},
19+
],
20+
},
21+
};

0 commit comments

Comments
 (0)