Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom source root #5522

Merged
merged 10 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") + "/"
})
]
};
Loading