Skip to content

Commit b2457f7

Browse files
committed
test(benchmark): add basic sourcemap benchmark
1 parent 389db82 commit b2457f7

3 files changed

Lines changed: 79 additions & 24 deletions

File tree

crates/rspack/src/builder/builder_context.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,27 @@ impl BuilderContext {
159159

160160
// DevTool plugins
161161
BuiltinPluginOptions::SourceMapDevToolPlugin(options) => {
162+
plugins.push(
163+
rspack_plugin_devtool::SourceMapDevToolModuleOptionsPlugin::new(
164+
rspack_plugin_devtool::SourceMapDevToolModuleOptionsPluginOptions {
165+
module: options.module,
166+
cheap: !options.columns,
167+
},
168+
)
169+
.boxed(),
170+
);
162171
plugins.push(rspack_plugin_devtool::SourceMapDevToolPlugin::new(options).boxed());
163172
}
164173
BuiltinPluginOptions::EvalSourceMapDevToolPlugin(options) => {
174+
plugins.push(
175+
rspack_plugin_devtool::SourceMapDevToolModuleOptionsPlugin::new(
176+
rspack_plugin_devtool::SourceMapDevToolModuleOptionsPluginOptions {
177+
module: options.module,
178+
cheap: !options.columns,
179+
},
180+
)
181+
.boxed(),
182+
);
165183
plugins.push(rspack_plugin_devtool::EvalSourceMapDevToolPlugin::new(options).boxed());
166184
}
167185
BuiltinPluginOptions::EvalDevToolModulePlugin(options) => {

crates/rspack/tests/basic.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rspack::builder::Builder as _;
1+
use rspack::builder::{Builder as _, Devtool};
22
use rspack_core::Compiler;
33
use rspack_paths::Utf8Path;
44

@@ -17,3 +17,24 @@ async fn basic() {
1717
let asset = &compiler.compilation.assets().get("main.js").unwrap();
1818
assert_eq!(asset.source.as_ref().unwrap().source(), "console.log(123);");
1919
}
20+
21+
#[tokio::test(flavor = "multi_thread")]
22+
async fn basic_sourcemap() {
23+
let mut compiler = Compiler::builder()
24+
.context(Utf8Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/basic"))
25+
.entry("main", "./src/index.js")
26+
.devtool(Devtool::SourceMap)
27+
.build();
28+
29+
compiler.build().await.unwrap();
30+
31+
let errors: Vec<_> = compiler.compilation.get_errors().collect();
32+
assert!(errors.is_empty());
33+
34+
let asset = &compiler.compilation.assets().get("main.js").unwrap();
35+
assert_eq!(
36+
asset.source.as_ref().unwrap().source(),
37+
"console.log(123);\n//# sourceMappingURL=main.js.map"
38+
);
39+
assert!(compiler.compilation.assets().get("main.js.map").is_some());
40+
}

tasks/benchmark/benches/basic.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use std::sync::Arc;
55

6-
use rspack::builder::Builder as _;
6+
use rspack::builder::{Builder as _, Devtool};
77
use rspack_benchmark::{criterion_group, criterion_main, Criterion};
88
use rspack_core::Compiler;
99
use rspack_fs::{MemoryFileSystem, ReadableFileSystem, WritableFileSystem};
@@ -12,15 +12,23 @@ use tokio::runtime::Builder;
1212
trait FileSystem: ReadableFileSystem + WritableFileSystem + Send + Sync {}
1313
impl<T: ReadableFileSystem + WritableFileSystem + Send + Sync> FileSystem for T {}
1414

15-
async fn basic(fs: Arc<dyn FileSystem>) {
16-
let mut compiler = Compiler::builder()
15+
async fn basic(fs: Arc<dyn FileSystem>, sm: bool) {
16+
let mut builder = Compiler::builder();
17+
18+
builder
1719
.context("/")
1820
.entry("main", "./src/index.js")
1921
.input_filesystem(fs.clone())
20-
.output_filesystem(fs.clone())
21-
.build();
22+
.output_filesystem(fs.clone());
23+
24+
if sm {
25+
builder.devtool(Devtool::SourceMap);
26+
}
27+
28+
let mut compiler = builder.build();
2229

2330
compiler.run().await.unwrap();
31+
2432
assert!(compiler
2533
.compilation
2634
.get_errors()
@@ -29,28 +37,36 @@ async fn basic(fs: Arc<dyn FileSystem>) {
2937
}
3038

3139
pub fn criterion_benchmark(c: &mut Criterion) {
40+
let rt = Builder::new_multi_thread().build().unwrap();
41+
42+
let fs = MemoryFileSystem::default();
43+
rt.block_on(async {
44+
fs.create_dir_all("/src".into()).await.unwrap();
45+
fs.write(
46+
"/src/index.js".into(),
47+
br#"if(process.env.NODE_ENV === "production") {
48+
console.log(123);
49+
} else {
50+
console.log(456);
51+
}
52+
"#,
53+
)
54+
.await
55+
.unwrap();
56+
});
57+
let fs = Arc::new(fs);
58+
3259
c.bench_function("basic", |b| {
33-
let rt = Builder::new_multi_thread().build().unwrap();
34-
35-
let fs = MemoryFileSystem::default();
36-
rt.block_on(async {
37-
fs.create_dir_all("/src".into()).await.unwrap();
38-
fs.write(
39-
"/src/index.js".into(),
40-
br#"if(process.env.NODE_ENV === "production") {
41-
console.log(123);
42-
} else {
43-
console.log(456);
44-
}
45-
"#,
46-
)
47-
.await
48-
.unwrap();
60+
b.to_async(&rt).iter(|| {
61+
let fs = fs.clone();
62+
basic(fs, false)
4963
});
50-
let fs = Arc::new(fs);
64+
});
65+
66+
c.bench_function("basic_sourcemap", |b| {
5167
b.to_async(&rt).iter(|| {
5268
let fs = fs.clone();
53-
basic(fs)
69+
basic(fs, true)
5470
});
5571
});
5672
}

0 commit comments

Comments
 (0)