Skip to content

Commit

Permalink
feat: Add support for debug IDs (#8943)
Browse files Browse the repository at this point in the history
* feat: Add support for debug IDs
* chore: upgrade rspack-sources to 0.4.3

---------

Co-authored-by: Gengkun <[email protected]>
Co-authored-by: Cong-Cong <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2025
1 parent 4fdaf68 commit c06787b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 13 deletions.
4 changes: 2 additions & 2 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 @@ -66,7 +66,7 @@ rayon = { version = "1.10.0" }
regex = { version = "1.11.1" }
ropey = "1.6.1"
rspack_resolver = { features = ["package_json_raw_json_api"], version = "0.5.0" }
rspack_sources = { version = "0.4.1" }
rspack_sources = { version = "0.4.3" }
rustc-hash = { version = "2.1.0" }
serde = { version = "1.0.217" }
serde_json = { version = "1.0.134" }
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,7 @@ export interface RawSourceMapDevToolPluginOptions {
test?: string | RegExp | (string | RegExp)[]
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
debugIds?: boolean
}

export interface RawSplitChunkSizes {
Expand Down
1 change: 1 addition & 0 deletions crates/rspack/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ impl CompilerOptionsBuilder {
test: None,
include: None,
exclude: None,
debug_ids: false,
};

if eval_wrapped {
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/raw_options/raw_devtool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub struct RawSourceMapDevToolPluginOptions {
pub include: Option<RawAssetConditions>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub exclude: Option<RawAssetConditions>,
pub debug_ids: Option<bool>,
}

impl From<RawSourceMapDevToolPluginOptions> for SourceMapDevToolPluginOptions {
Expand Down Expand Up @@ -153,6 +154,7 @@ impl From<RawSourceMapDevToolPluginOptions> for SourceMapDevToolPluginOptions {
test: opts.test.map(into_asset_conditions),
include: opts.include.map(into_asset_conditions),
exclude: opts.exclude.map(into_asset_conditions),
debug_ids: opts.debug_ids.unwrap_or(false),
}
}
}
Expand Down
62 changes: 53 additions & 9 deletions crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ pub struct SourceMapDevToolPluginOptions {
pub test: Option<AssetConditions>,
pub include: Option<AssetConditions>,
pub exclude: Option<AssetConditions>,
pub debug_ids: bool,
}

fn create_debug_id(filename: &str, source: &[u8]) -> String {
// We need two 64 bit hashes to give us the 128 bits required for a uuid
// The first 64 bit hash is built from the source
let mut hasher = RspackHash::new(&rspack_hash::HashFunction::Xxhash64);
hasher.write(source);
let hash1 = hasher.finish().to_le_bytes();

// The second 64 bit hash is built from the filename and the source hash
let mut hasher = RspackHash::new(&rspack_hash::HashFunction::Xxhash64);
hasher.write(filename.as_bytes());
hasher.write(&hash1);
let hash2 = hasher.finish().to_le_bytes();

let mut bytes = [hash1, hash2].concat();

// Build the uuid from the 16 bytes
let mut uuid = String::with_capacity(36);
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;

for (i, byte) in bytes.iter().enumerate() {
if i == 4 || i == 6 || i == 8 || i == 10 {
uuid.push('-');
}
uuid.push_str(&format!("{byte:02x}"));
}
uuid
}

enum SourceMappingUrlComment {
Expand Down Expand Up @@ -119,6 +149,7 @@ pub struct SourceMapDevToolPlugin {
test: Option<AssetConditions>,
include: Option<AssetConditions>,
exclude: Option<AssetConditions>,
debug_ids: bool,
mapped_assets_cache: MappedAssetsCache,
}

Expand Down Expand Up @@ -183,6 +214,7 @@ impl SourceMapDevToolPlugin {
options.test,
options.include,
options.exclude,
options.debug_ids,
MappedAssetsCache::new(),
)
}
Expand Down Expand Up @@ -369,9 +401,17 @@ impl SourceMapDevToolPlugin {
.into_iter()
.map(|(source_filename, source, source_map)| {
async {
let source_map_json = match source_map {
Some(map) => Some(map.to_json().into_diagnostic()?),
None => None,
let (source_map_json, debug_id) = match source_map {
Some(mut map) => {
let debug_id = self.debug_ids.then(|| {
let debug_id = create_debug_id(&source_filename, &source.buffer());
map.set_debug_id(Some(debug_id.clone()));
debug_id
});

(Some(map.to_json().into_diagnostic()?), debug_id)
}
None => (None, None),
};

let mut asset = compilation
Expand Down Expand Up @@ -473,15 +513,19 @@ impl SourceMapDevToolPlugin {
.always_ok()
}
};
let current_source_mapping_url_comment = current_source_mapping_url_comment
.cow_replace("[url]", &source_map_url)
.into_owned();

let debug_id_comment = debug_id
.map(|id| format!("\n//# debugId={id}"))
.unwrap_or_default();

asset.source = Some(
ConcatSource::new([
source.clone(),
RawStringSource::from(
current_source_mapping_url_comment
.cow_replace("[url]", &source_map_url)
.into_owned(),
)
.boxed(),
RawStringSource::from(debug_id_comment).boxed(),
RawStringSource::from(current_source_mapping_url_comment).boxed(),
])
.boxed(),
);
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/rspackOptionsApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class RspackOptionsApply {
const cheap = options.devtool.includes("cheap");
const moduleMaps = options.devtool.includes("module");
const noSources = options.devtool.includes("nosources");
const debugIds = options.devtool.includes("debugids");
const Plugin = evalWrapped
? EvalSourceMapDevToolPlugin
: SourceMapDevToolPlugin;
Expand All @@ -200,7 +201,8 @@ export class RspackOptionsApply {
module: moduleMaps ? true : !cheap,
columns: !cheap,
noSources: noSources,
namespace: options.output.devtoolNamespace
namespace: options.output.devtoolNamespace,
debugIds: debugIds
}).apply(compiler);
} else if (options.devtool.includes("eval")) {
new EvalDevToolModulePlugin({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require("fs");

it("source should include debug id that matches debugId key in sourcemap", function() {
const source = fs.readFileSync(__filename, "utf-8");
const sourceMap = fs.readFileSync(__filename + ".map", "utf-8");
const map = JSON.parse(sourceMap);
expect(map.debugId).toBeDefined();
expect(
/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/i.test(map.debugId)
).toBe(true);
expect(source).toContain(`//# debugId=${map.debugId}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
devtool: "source-map-debugids"
};

2 comments on commit c06787b

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on c06787b Jan 20, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ❌ failure
rsbuild ✅ success
rsdoctor ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on c06787b Jan 20, 2025

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 (2025-01-20 1d58294) Current Change
10000_big_production-mode_disable-minimize + exec 37.8 s ± 901 ms 37.6 s ± 853 ms -0.40 %
10000_development-mode + exec 1.82 s ± 16 ms 1.82 s ± 40 ms -0.43 %
10000_development-mode_hmr + exec 680 ms ± 6.2 ms 684 ms ± 17 ms +0.62 %
10000_production-mode + exec 2.39 s ± 24 ms 2.36 s ± 63 ms -1.23 %
10000_production-mode_persistent-cold + exec 2.57 s ± 130 ms 2.5 s ± 64 ms -2.78 %
10000_production-mode_persistent-hot + exec 1.76 s ± 31 ms 1.73 s ± 81 ms -1.94 %
arco-pro_development-mode + exec 1.76 s ± 87 ms 1.75 s ± 158 ms -0.55 %
arco-pro_development-mode_hmr + exec 388 ms ± 3.7 ms 388 ms ± 5.6 ms +0.08 %
arco-pro_production-mode + exec 3.74 s ± 79 ms 3.68 s ± 156 ms -1.40 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.79 s ± 53 ms 3.74 s ± 252 ms -1.36 %
arco-pro_production-mode_persistent-cold + exec 3.89 s ± 129 ms 3.78 s ± 94 ms -2.81 %
arco-pro_production-mode_persistent-hot + exec 2.46 s ± 63 ms 2.49 s ± 225 ms +1.12 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.78 s ± 165 ms 3.62 s ± 204 ms -4.13 %
large-dyn-imports_development-mode + exec 2.09 s ± 55 ms 2.07 s ± 27 ms -0.98 %
large-dyn-imports_production-mode + exec 2.15 s ± 48 ms 2.13 s ± 72 ms -0.89 %
threejs_development-mode_10x + exec 1.61 s ± 14 ms 1.52 s ± 26 ms -5.62 %
threejs_development-mode_10x_hmr + exec 770 ms ± 22 ms 787 ms ± 28 ms +2.17 %
threejs_production-mode_10x + exec 5.55 s ± 241 ms 5.35 s ± 181 ms -3.56 %
threejs_production-mode_10x_persistent-cold + exec 5.62 s ± 323 ms 5.39 s ± 295 ms -4.23 %
threejs_production-mode_10x_persistent-hot + exec 4.74 s ± 234 ms 4.57 s ± 186 ms -3.60 %
10000_big_production-mode_disable-minimize + rss memory 8691 MiB ± 16 MiB 8697 MiB ± 78.1 MiB +0.07 %
10000_development-mode + rss memory 647 MiB ± 15.8 MiB 664 MiB ± 31.1 MiB +2.72 %
10000_development-mode_hmr + rss memory 1240 MiB ± 178 MiB 1215 MiB ± 286 MiB -2.02 %
10000_production-mode + rss memory 628 MiB ± 22.8 MiB 665 MiB ± 38.9 MiB +5.81 %
10000_production-mode_persistent-cold + rss memory 746 MiB ± 13.1 MiB 758 MiB ± 22.6 MiB +1.57 %
10000_production-mode_persistent-hot + rss memory 730 MiB ± 23.5 MiB 751 MiB ± 26.7 MiB +2.85 %
arco-pro_development-mode + rss memory 567 MiB ± 20.5 MiB 581 MiB ± 34.1 MiB +2.45 %
arco-pro_development-mode_hmr + rss memory 638 MiB ± 59.9 MiB 678 MiB ± 63.5 MiB +6.23 %
arco-pro_production-mode + rss memory 728 MiB ± 42 MiB 744 MiB ± 36.9 MiB +2.09 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 730 MiB ± 14.1 MiB 747 MiB ± 24.1 MiB +2.31 %
arco-pro_production-mode_persistent-cold + rss memory 863 MiB ± 51.8 MiB 850 MiB ± 17.3 MiB -1.46 %
arco-pro_production-mode_persistent-hot + rss memory 711 MiB ± 24.1 MiB 731 MiB ± 23.5 MiB +2.83 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 727 MiB ± 14.4 MiB 739 MiB ± 23 MiB +1.65 %
large-dyn-imports_development-mode + rss memory 645 MiB ± 4.63 MiB 662 MiB ± 7.07 MiB +2.68 %
large-dyn-imports_production-mode + rss memory 528 MiB ± 6.7 MiB 545 MiB ± 3.71 MiB +3.31 %
threejs_development-mode_10x + rss memory 527 MiB ± 27.9 MiB 552 MiB ± 25.9 MiB +4.62 %
threejs_development-mode_10x_hmr + rss memory 1128 MiB ± 58.7 MiB 1173 MiB ± 91.3 MiB +3.99 %
threejs_production-mode_10x + rss memory 818 MiB ± 29.1 MiB 862 MiB ± 54.8 MiB +5.39 %
threejs_production-mode_10x_persistent-cold + rss memory 933 MiB ± 25.3 MiB 935 MiB ± 96.8 MiB +0.18 %
threejs_production-mode_10x_persistent-hot + rss memory 863 MiB ± 57.2 MiB 870 MiB ± 58.4 MiB +0.72 %

Please sign in to comment.