Skip to content

Commit

Permalink
feat: support custom source root (#5522)
Browse files Browse the repository at this point in the history
* fix: test jobs should not run in fork repos

* feat: support source root

* chore: test SourceMapDevToolPlugin sourceRoot option

* Revert "fix: test jobs should not run in fork repos"

This reverts commit 2684030.

* feat: upgrade rspack_sources

* chore: upgrade rspack_sources

* chore: refactor code
  • Loading branch information
SyMind authored Feb 1, 2024
1 parent 4de09a0 commit fe785af
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ quote = { version = "1.0.35" }
rayon = { version = "1.8.1" }
regex = { version = "1.10.3" }
rkyv = { version = "0.7.43" }
rspack_sources = { version = "=0.2.7" }
rspack_sources = { version = "=0.2.9" }
rustc-hash = { version = "1.1.0" }
schemars = { version = "0.8.16" }
serde = { version = "1.0.196" }
Expand Down
17 changes: 11 additions & 6 deletions crates/rspack_plugin_devtool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub struct SourceMapDevToolPlugin {
no_sources: bool,
public_path: Option<String>,
module: bool,
source_root: Option<String>,
#[derivative(Debug = "ignore")]
test: Option<TestFn>,
}
Expand Down Expand Up @@ -178,6 +179,7 @@ impl SourceMapDevToolPlugin {
no_sources: options.no_sources,
public_path: options.public_path,
module: options.module,
source_root: options.source_root,
test: options.test,
}
}
Expand Down Expand Up @@ -301,7 +303,7 @@ impl Plugin for SourceMapDevToolPlugin {
let mut has_name = used_names_set.contains(&source_name);
if !has_name {
used_names_set.insert(source_name.clone());
*source = source_name;
*source = Cow::from(source_name);
continue;
}

Expand All @@ -323,7 +325,7 @@ impl Plugin for SourceMapDevToolPlugin {
has_name = used_names_set.contains(&source_name);
if !has_name {
used_names_set.insert(source_name.clone());
*source = source_name;
*source = Cow::from(source_name);
continue;
}

Expand All @@ -333,13 +335,16 @@ impl Plugin for SourceMapDevToolPlugin {
has_name = used_names_set.contains(&source_name);
}
used_names_set.insert(source_name.clone());
*source = source_name;
*source = Cow::from(source_name);
}
if self.no_sources {
for content in source_map.sources_content_mut() {
*content = String::default();
*content = Cow::from(String::default());
}
}
if let Some(source_root) = &self.source_root {
source_map.set_source_root(Some(source_root.clone()));
}
let mut source_map_buffer = Vec::new();
source_map
.to_writer(&mut source_map_buffer)
Expand Down Expand Up @@ -791,11 +796,11 @@ impl EvalSourceMapDevToolPlugin {
for source in map.sources_mut() {
let resource_path = normalize_custom_filename(source);
let resource_path = contextify(&compilation.options.context, resource_path);
*source = resource_path;
*source = Cow::from(resource_path);
}
if self.no_sources {
for content in map.sources_content_mut() {
*content = String::default();
*content = Cow::from(String::default());
}
}
let mut map_buffer = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it("should generate the correct sourceRoot in SourceMap", function () {
const fs = require("fs");
const path = require("path");
const source = JSON.parse(fs.readFileSync(__filename + ".map", "utf-8"));
expect(source.sourceRoot).toContain(
path.resolve(__dirname, "../folder") + "/"
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require("path");
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: "[file].map",
sourceRoot: path.join(__dirname, "folder") + "/"
})
]
};

0 comments on commit fe785af

Please sign in to comment.