Skip to content

Commit

Permalink
fix: source-map-devtool-plugin file context option (#8888)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind authored Dec 30, 2024
1 parent 99fc4cd commit aeb444f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
24 changes: 12 additions & 12 deletions crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl SourceMapDevToolPlugin {

let futures = mapped_sources
.into_iter()
.map(|(filename, source, source_map)| {
.map(|(source_filename, source, source_map)| {
async {
let source_map_json = match source_map {
Some(map) => Some(map.to_json().into_diagnostic()?),
Expand All @@ -376,21 +376,21 @@ impl SourceMapDevToolPlugin {

let mut asset = compilation
.assets()
.get(&filename)
.get(&source_filename)
.unwrap_or_else(|| {
panic!(
"expected to find filename '{}' in compilation.assets, but it was not present",
&filename
&source_filename
)
})
.clone();
let Some(source_map_json) = source_map_json else {
return Ok(MappedAsset {
asset: (filename, asset),
asset: (source_filename, asset),
source_map: None,
});
};
let css_extension_detected = CSS_EXTENSION_DETECT_REGEXP.is_match(&filename);
let css_extension_detected = CSS_EXTENSION_DETECT_REGEXP.is_match(&source_filename);
let current_source_mapping_url_comment = match &self.source_mapping_url_comment {
Some(SourceMappingUrlComment::String(s)) => {
let s = if css_extension_detected {
Expand All @@ -405,14 +405,14 @@ impl SourceMapDevToolPlugin {
};

if let Some(source_map_filename_config) = &self.source_map_filename {
let chunk = file_to_chunk.get(&filename);
let chunk = file_to_chunk.get(&source_filename);
let filename = match &self.file_context {
Some(file_context) => Cow::Owned(
relative(Path::new(file_context), Path::new(&filename))
relative(Path::new(file_context), Path::new(&source_filename))
.to_string_lossy()
.to_string(),
),
None => Cow::Borrowed(&filename),
None => Cow::Borrowed(&source_filename),
};

let mut hasher = RspackHash::from(&compilation.options.output);
Expand Down Expand Up @@ -464,7 +464,7 @@ impl SourceMapDevToolPlugin {
let data = data.url(&source_map_url);
let current_source_mapping_url_comment = match &current_source_mapping_url_comment {
SourceMappingUrlCommentRef::String(s) => compilation
.get_asset_path(&FilenameTemplate::from(s.to_string()), data)
.get_asset_path(&FilenameTemplate::from(s.as_ref()), data)
.always_ok(),
SourceMappingUrlCommentRef::Fn(f) => {
let comment = f(data).await?;
Expand All @@ -490,7 +490,7 @@ impl SourceMapDevToolPlugin {
asset.source = Some(source.clone());
}
let mut source_map_asset_info = AssetInfo::default().with_development(Some(true));
if let Some(asset) = compilation.assets().get(filename.as_ref()) {
if let Some(asset) = compilation.assets().get(&source_filename) {
// set source map asset version to be the same as the target asset
source_map_asset_info.version = asset.info.version.clone();
}
Expand All @@ -499,7 +499,7 @@ impl SourceMapDevToolPlugin {
source_map_asset_info,
);
Ok(MappedAsset {
asset: (filename.to_string(), asset),
asset: (source_filename, asset),
source_map: Some((source_map_filename, source_map_asset)),
})
} else {
Expand Down Expand Up @@ -531,7 +531,7 @@ impl SourceMapDevToolPlugin {
.boxed(),
);
Ok(MappedAsset {
asset: (filename, asset),
asset: (source_filename, asset),
source_map: None,
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
it("should generate correct relative sourceMappingURL when fileContext is passed", function () {
var fs = require("fs"),
path = require("path");
var source = fs.readFileSync(path.join(__dirname, "main.js"), "utf-8");
expect(source).toMatch("//# sourceMappingURL=http://localhost:50505/sourcemaps/../main.js.map");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const rspack = require("@rspack/core");

/** @type {import("@rspack/core").Configuration} */
module.exports = {
node: {
__dirname: false,
__filename: false
},
output: {
filename: "[name].js"
},
plugins: [
new rspack.SourceMapDevToolPlugin({
filename: "sourcemaps/[file].map",
fileContext: 'assets',
publicPath: 'http://localhost:50505/'
})
]
};

1 comment on commit aeb444f

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on aeb444f Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-12-30 437c370) Current Change
10000_big_production-mode_disable-minimize + exec 37.5 s ± 658 ms 38.2 s ± 576 ms +1.83 %
10000_development-mode + exec 1.89 s ± 22 ms 1.82 s ± 27 ms -3.57 %
10000_development-mode_hmr + exec 685 ms ± 15 ms 673 ms ± 7.4 ms -1.72 %
10000_production-mode + exec 2.49 s ± 46 ms 2.47 s ± 83 ms -0.67 %
arco-pro_development-mode + exec 1.75 s ± 76 ms 1.76 s ± 92 ms +0.63 %
arco-pro_development-mode_hmr + exec 378 ms ± 3.8 ms 377 ms ± 1.2 ms -0.25 %
arco-pro_production-mode + exec 3.64 s ± 75 ms 3.57 s ± 121 ms -2.08 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.64 s ± 76 ms 3.62 s ± 70 ms -0.80 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.63 s ± 80 ms 3.55 s ± 150 ms -2.21 %
threejs_development-mode_10x + exec 1.52 s ± 18 ms 1.49 s ± 22 ms -2.14 %
threejs_development-mode_10x_hmr + exec 790 ms ± 8.7 ms 761 ms ± 9.4 ms -3.74 %
threejs_production-mode_10x + exec 5.34 s ± 72 ms 5.36 s ± 87 ms +0.47 %
10000_big_production-mode_disable-minimize + rss memory 9458 MiB ± 133 MiB 9560 MiB ± 147 MiB +1.08 %
10000_development-mode + rss memory 650 MiB ± 8.03 MiB 691 MiB ± 28.7 MiB +6.23 %
10000_development-mode_hmr + rss memory 1421 MiB ± 196 MiB 1467 MiB ± 482 MiB +3.28 %
10000_production-mode + rss memory 618 MiB ± 21.6 MiB 678 MiB ± 30.4 MiB +9.86 %
arco-pro_development-mode + rss memory 592 MiB ± 24.8 MiB 597 MiB ± 44.4 MiB +0.70 %
arco-pro_development-mode_hmr + rss memory 636 MiB ± 47.2 MiB 652 MiB ± 77.7 MiB +2.48 %
arco-pro_production-mode + rss memory 725 MiB ± 44.8 MiB 737 MiB ± 84.4 MiB +1.65 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 770 MiB ± 58.4 MiB 767 MiB ± 25.9 MiB -0.33 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 750 MiB ± 36.5 MiB 754 MiB ± 57.5 MiB +0.59 %
threejs_development-mode_10x + rss memory 624 MiB ± 24 MiB 653 MiB ± 36.2 MiB +4.56 %
threejs_development-mode_10x_hmr + rss memory 1173 MiB ± 179 MiB 1181 MiB ± 248 MiB +0.74 %
threejs_production-mode_10x + rss memory 895 MiB ± 49.3 MiB 925 MiB ± 59.8 MiB +3.41 %

Please sign in to comment.