From fe785afa38bea33d796d55896a78b9d64927dae5 Mon Sep 17 00:00:00 2001 From: Cong-Cong Pan Date: Thu, 1 Feb 2024 15:38:21 +0800 Subject: [PATCH] feat: support custom source root (#5522) * 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 2684030adcd8f351225cc1f2ef446e3941814673. * feat: upgrade rspack_sources * chore: upgrade rspack_sources * chore: refactor code --- Cargo.lock | 6 ++---- Cargo.toml | 2 +- crates/rspack_plugin_devtool/src/lib.rs | 17 +++++++++------ .../index.js | 8 +++++++ .../webpack.config.js | 21 +++++++++++++++++++ 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/index.js create mode 100644 packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/webpack.config.js diff --git a/Cargo.lock b/Cargo.lock index 3272c566bc4b..9c849d38a6e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3150,15 +3150,13 @@ dependencies = [ [[package]] name = "rspack_sources" -version = "0.2.7" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a741e3d5c1b73996bad4beeefb8aa0bbffad40ba4f0c4b09080265c11bd7bd" +checksum = "6f09931bc4ed54e5a04b59e5a9cb9a13a4a42c479431aeff7abe7cb35b6b3169" dependencies = [ "dashmap", "dyn-clone", "memchr", - "once_cell", - "parking_lot", "rustc-hash", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index b68053d19a2e..3bf08612ddd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/rspack_plugin_devtool/src/lib.rs b/crates/rspack_plugin_devtool/src/lib.rs index 4c33341a8949..2ff2cab58dd2 100644 --- a/crates/rspack_plugin_devtool/src/lib.rs +++ b/crates/rspack_plugin_devtool/src/lib.rs @@ -136,6 +136,7 @@ pub struct SourceMapDevToolPlugin { no_sources: bool, public_path: Option, module: bool, + source_root: Option, #[derivative(Debug = "ignore")] test: Option, } @@ -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, } } @@ -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; } @@ -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; } @@ -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) @@ -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(); diff --git a/packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/index.js b/packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/index.js new file mode 100644 index 000000000000..04cf8f18918d --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/index.js @@ -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") + "/" + ); +}); diff --git a/packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/webpack.config.js b/packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/webpack.config.js new file mode 100644 index 000000000000..cbb08b698c6e --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/source-map-dev-tool-plugin-source-root/webpack.config.js @@ -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") + "/" + }) + ] +};