Skip to content

Commit 34cdf14

Browse files
authored
fix: do not expose build() for level-2 options builder (#9188)
1 parent 3fb1b39 commit 34cdf14

File tree

3 files changed

+207
-21
lines changed

3 files changed

+207
-21
lines changed

crates/rspack/src/builder/mod.rs

Lines changed: 201 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,26 @@ impl CompilerBuilder {
232232

233233
/// Set options for module configuration.
234234
///
235+
/// Both are accepted:
236+
/// - [`ModuleOptionsBuilder`]
237+
/// - [`ModuleOptions`]
238+
///
239+
/// # Examples
240+
///
241+
/// ```rust
242+
/// use rspack::builder::{Builder as _, ModuleOptionsBuilder};
243+
/// use rspack_core::{Compiler, ModuleOptions};
244+
///
245+
/// // Using builder without calling `build()`
246+
/// let compiler = Compiler::builder().module(ModuleOptionsBuilder::default().rules(vec![]));
247+
///
248+
/// // `ModuleOptions::builder` equals to `ModuleOptionsBuilder::default()`
249+
/// let compiler = Compiler::builder().module(ModuleOptions::builder().rules(vec![]));
250+
///
251+
/// // Directly passing `ModuleOptions`
252+
/// let compiler = Compiler::builder().module(ModuleOptions::default());
253+
/// ```
254+
///
235255
/// See [`CompilerOptionsBuilder::module`] for more details.
236256
pub fn module<V>(&mut self, module: V) -> &mut Self
237257
where
@@ -243,6 +263,26 @@ impl CompilerBuilder {
243263

244264
/// Set options for output.
245265
///
266+
/// Both are accepted:
267+
/// - [`OutputOptionsBuilder`]
268+
/// - [`OutputOptions`]
269+
///
270+
/// # Examples
271+
///
272+
/// ```rust
273+
/// use rspack::builder::{Builder as _, OutputOptionsBuilder};
274+
/// use rspack_core::{Compiler, OutputOptions};
275+
///
276+
/// // Using builder without calling `build()`
277+
/// let compiler = Compiler::builder().output(OutputOptionsBuilder::default().path("/dist"));
278+
///
279+
/// // `OutputOptions::builder` equals to `OutputOptionsBuilder::default()`
280+
/// let compiler = Compiler::builder().output(OutputOptions::builder().path("/dist"));
281+
///
282+
/// // Or directly passing `OutputOptions`
283+
/// // let compiler = Compiler::builder().output(OutputOptions { ... });
284+
/// ```
285+
///
246286
/// See [`CompilerOptionsBuilder::output`] for more details.
247287
pub fn output<V>(&mut self, output: V) -> &mut Self
248288
where
@@ -254,6 +294,28 @@ impl CompilerBuilder {
254294

255295
/// Set options for optimization.
256296
///
297+
/// Both are accepted:
298+
/// - [`OptimizationOptionsBuilder`]
299+
/// - [`Optimization`]
300+
///
301+
/// # Examples
302+
///
303+
/// ```rust
304+
/// use rspack::builder::{Builder as _, OptimizationOptionsBuilder};
305+
/// use rspack_core::{Compiler, Optimization};
306+
///
307+
/// // Using builder without calling `build()`
308+
/// let compiler = Compiler::builder()
309+
/// .optimization(OptimizationOptionsBuilder::default().remove_available_modules(true));
310+
///
311+
/// // `Optimization::builder` equals to `OptimizationOptionsBuilder::default()`
312+
/// let compiler =
313+
/// Compiler::builder().optimization(Optimization::builder().remove_available_modules(true));
314+
///
315+
/// // Or directly passing `Optimization`
316+
/// // let compiler = Compiler::builder().optimization(Optimization { ... });
317+
/// ```
318+
///
257319
/// See [`CompilerOptionsBuilder::optimization`] for more details.
258320
pub fn optimization<V>(&mut self, optimization: V) -> &mut Self
259321
where
@@ -265,6 +327,17 @@ impl CompilerBuilder {
265327

266328
/// Set options for Node.js environment.
267329
///
330+
/// # Examples
331+
///
332+
/// ```rust
333+
/// use rspack::builder::{Builder as _, NodeOptionBuilder};
334+
/// use rspack_core::{Compiler, NodeGlobalOption, NodeOption};
335+
///
336+
/// // Using builder without calling `build()`
337+
/// let compiler =
338+
/// Compiler::builder().node(NodeOptionBuilder::default().global(NodeGlobalOption::True));
339+
/// ```
340+
///
268341
/// See [`CompilerOptionsBuilder::node`] for more details.
269342
pub fn node<V>(&mut self, node: V) -> &mut Self
270343
where
@@ -292,6 +365,29 @@ impl CompilerBuilder {
292365

293366
/// Set options for experiments.
294367
///
368+
/// Both are accepted:
369+
/// - [`ExperimentsBuilder`]
370+
/// - [`Experiments`]
371+
///
372+
/// # Examples
373+
///
374+
/// ```rust
375+
/// use rspack::builder::{Builder as _, ExperimentsBuilder};
376+
/// use rspack_core::incremental::IncrementalPasses;
377+
/// use rspack_core::{Compiler, Experiments};
378+
///
379+
/// // Using builder without calling `build()`
380+
/// let compiler = Compiler::builder()
381+
/// .experiments(ExperimentsBuilder::default().incremental(IncrementalPasses::empty()));
382+
///
383+
/// // `Experiments::builder` equals to `ExperimentsBuilder::default()`
384+
/// let compiler =
385+
/// Compiler::builder().experiments(Experiments::builder().incremental(IncrementalPasses::empty()));
386+
///
387+
/// // Or directly passing `Experiments`
388+
/// // let compiler = Compiler::builder().experiments(Experiments { ... });
389+
/// ```
390+
///
295391
/// See [`CompilerOptionsBuilder::experiments`] for more details.
296392
pub fn experiments<V>(&mut self, experiments: V) -> &mut Self
297393
where
@@ -580,6 +676,26 @@ impl CompilerOptionsBuilder {
580676
}
581677

582678
/// Set options for module configuration.
679+
///
680+
/// Both are accepted:
681+
/// - [`ModuleOptionsBuilder`]
682+
/// - [`ModuleOptions`]
683+
///
684+
/// # Examples
685+
///
686+
/// ```rust
687+
/// use rspack::builder::{Builder as _, ModuleOptionsBuilder};
688+
/// use rspack_core::{Compiler, ModuleOptions};
689+
///
690+
/// // Using builder without calling `build()`
691+
/// let compiler = Compiler::builder().module(ModuleOptionsBuilder::default().rules(vec![]));
692+
///
693+
/// // `ModuleOptions::builder` equals to `ModuleOptionsBuilder::default()`
694+
/// let compiler = Compiler::builder().module(ModuleOptions::builder().rules(vec![]));
695+
///
696+
/// // Directly passing `ModuleOptions`
697+
/// let compiler = Compiler::builder().module(ModuleOptions::default());
698+
/// ```
583699
pub fn module<V>(&mut self, module: V) -> &mut Self
584700
where
585701
V: Into<ModuleOptionsBuilder>,
@@ -595,6 +711,26 @@ impl CompilerOptionsBuilder {
595711
}
596712

597713
/// Set options for output.
714+
///
715+
/// Both are accepted:
716+
/// - [`OutputOptionsBuilder`]
717+
/// - [`OutputOptions`]
718+
///
719+
/// # Examples
720+
///
721+
/// ```rust
722+
/// use rspack::builder::{Builder as _, OutputOptionsBuilder};
723+
/// use rspack_core::{Compiler, OutputOptions};
724+
///
725+
/// // Using builder without calling `build()`
726+
/// let compiler = Compiler::builder().output(OutputOptionsBuilder::default().path("/dist"));
727+
///
728+
/// // `OutputOptions::builder` equals to `OutputOptionsBuilder::default()`
729+
/// let compiler = Compiler::builder().output(OutputOptions::builder().path("/dist"));
730+
///
731+
/// // Or directly passing `OutputOptions`
732+
/// // let compiler = Compiler::builder().output(OutputOptions { ... });
733+
/// ```
598734
pub fn output<V>(&mut self, output: V) -> &mut Self
599735
where
600736
V: Into<OutputOptionsBuilder>,
@@ -603,7 +739,29 @@ impl CompilerOptionsBuilder {
603739
self
604740
}
605741

606-
/// Set options for optimization.
742+
/// Set options for optimization.
743+
///
744+
/// Both are accepted:
745+
/// - [`OptimizationOptionsBuilder`]
746+
/// - [`Optimization`]
747+
///
748+
/// # Examples
749+
///
750+
/// ```rust
751+
/// use rspack::builder::{Builder as _, OptimizationOptionsBuilder};
752+
/// use rspack_core::{Compiler, Optimization};
753+
///
754+
/// // Using builder without calling `build()`
755+
/// let compiler = Compiler::builder()
756+
/// .optimization(OptimizationOptionsBuilder::default().remove_available_modules(true));
757+
///
758+
/// // `Optimization::builder` equals to `OptimizationOptionsBuilder::default()`
759+
/// let compiler =
760+
/// Compiler::builder().optimization(Optimization::builder().remove_available_modules(true));
761+
///
762+
/// // Or directly passing `Optimization`
763+
/// // let compiler = Compiler::builder().optimization(Optimization { ... });
764+
/// ```
607765
pub fn optimization<V>(&mut self, optimization: V) -> &mut Self
608766
where
609767
V: Into<OptimizationOptionsBuilder>,
@@ -613,6 +771,18 @@ impl CompilerOptionsBuilder {
613771
}
614772

615773
/// Set options for Node.js environment.
774+
///
775+
///
776+
/// # Examples
777+
///
778+
/// ```rust
779+
/// use rspack::builder::{Builder as _, NodeOptionBuilder};
780+
/// use rspack_core::{Compiler, NodeGlobalOption, NodeOption};
781+
///
782+
/// // Using builder without calling `build()`
783+
/// let compiler =
784+
/// Compiler::builder().node(NodeOptionBuilder::default().global(NodeGlobalOption::True));
785+
/// ```
616786
pub fn node<V>(&mut self, node: V) -> &mut Self
617787
where
618788
V: Into<NodeOptionBuilder>,
@@ -628,6 +798,29 @@ impl CompilerOptionsBuilder {
628798
}
629799

630800
/// Set options for experiments.
801+
///
802+
/// Both are accepted:
803+
/// - [`ExperimentsBuilder`]
804+
/// - [`Experiments`]
805+
///
806+
/// # Examples
807+
///
808+
/// ```rust
809+
/// use rspack::builder::{Builder as _, ExperimentsBuilder};
810+
/// use rspack_core::incremental::IncrementalPasses;
811+
/// use rspack_core::{Compiler, Experiments};
812+
///
813+
/// // Using builder without calling `build()`
814+
/// let compiler = Compiler::builder()
815+
/// .experiments(ExperimentsBuilder::default().incremental(IncrementalPasses::empty()));
816+
///
817+
/// // `Experiments::builder` equals to `ExperimentsBuilder::default()`
818+
/// let compiler =
819+
/// Compiler::builder().experiments(Experiments::builder().incremental(IncrementalPasses::empty()));
820+
///
821+
/// // Or directly passing `Experiments`
822+
/// // let compiler = Compiler::builder().experiments(Experiments { ... });
823+
/// ```
631824
pub fn experiments<V>(&mut self, experiments: V) -> &mut Self
632825
where
633826
V: Into<ExperimentsBuilder>,
@@ -1276,7 +1469,7 @@ impl NodeOptionBuilder {
12761469
/// Build [`NodeOption`] from options.
12771470
///
12781471
/// [`NodeOption`]: rspack_core::options::NodeOption
1279-
pub fn build(
1472+
fn build(
12801473
&mut self,
12811474
target_properties: &TargetProperties,
12821475
output_module: bool,
@@ -1399,8 +1592,10 @@ impl ModuleOptionsBuilder {
13991592

14001593
/// Build [`ModuleOptions`] from options.
14011594
///
1595+
/// Normally, you don't need to call this function, it's used internally by [`CompilerBuilder::build`].
1596+
///
14021597
/// [`ModuleOptions`]: rspack_core::options::ModuleOptions
1403-
pub fn build(
1598+
fn build(
14041599
&mut self,
14051600
_builder_context: &mut BuilderContext,
14061601
async_web_assembly: bool,
@@ -2360,7 +2555,7 @@ impl OutputOptionsBuilder {
23602555
///
23612556
/// [`OutputOptions`]: rspack_core::options::OutputOptions
23622557
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
2363-
pub fn build(
2558+
fn build(
23642559
&mut self,
23652560
builder_context: &mut BuilderContext,
23662561
context: &Context,
@@ -3090,7 +3285,7 @@ impl OptimizationOptionsBuilder {
30903285
/// Build [`Optimization`] from options.
30913286
///
30923287
/// [`Optimization`]: rspack_core::options::Optimization
3093-
pub fn build(
3288+
fn build(
30943289
&mut self,
30953290
builder_context: &mut BuilderContext,
30963291
development: bool,
@@ -3432,7 +3627,7 @@ impl ExperimentsBuilder {
34323627
/// Build [`Experiments`] from options.
34333628
///
34343629
/// [`Experiments`]: rspack_core::options::Experiments
3435-
pub fn build(
3630+
fn build(
34363631
&mut self,
34373632
_builder_context: &mut BuilderContext,
34383633
development: bool,

crates/rspack/src/builder/target.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct TargetProperties {
3636
pub async_function: Option<bool>,
3737
}
3838

39+
#[allow(unused)]
3940
impl TargetProperties {
4041
pub fn web(&self) -> bool {
4142
self.web.unwrap_or(false)

tasks/benchmark/benches/build_chunk_graph.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
use std::sync::Arc;
33

44
use criterion::criterion_group;
5-
use rspack::builder::{
6-
Builder as _, BuilderContext, ExperimentsBuilder, OptimizationOptionsBuilder,
7-
};
5+
use rspack::builder::Builder as _;
86
use rspack_benchmark::Criterion;
97
use rspack_core::{
10-
build_chunk_graph, fast_set, incremental::IncrementalPasses, Compilation, Compiler,
8+
build_chunk_graph, fast_set, incremental::IncrementalPasses, Compilation, Compiler, Experiments,
9+
Optimization,
1110
};
1211
use rspack_fs::{MemoryFileSystem, WritableFileSystem};
1312
use tokio::runtime::Builder;
@@ -109,22 +108,13 @@ pub fn build_chunk_graph_benchmark(c: &mut Criterion) {
109108
serde_json::from_str::<Vec<Vec<usize>>>(include_str!("build_chunk_graph/random_table.json"))
110109
.expect("should not fail to parse random table json");
111110

112-
let mut builder_context = BuilderContext::default();
113111
let mut compiler = Compiler::builder()
114112
.context("/")
115113
.entry("main", "/src/dynamic-0.js")
116114
.input_filesystem(fs.clone())
117115
.output_filesystem(fs.clone())
118-
.optimization(
119-
OptimizationOptionsBuilder::default()
120-
.remove_available_modules(true)
121-
.build(&mut builder_context, true, false, false),
122-
)
123-
.experiments(
124-
ExperimentsBuilder::default()
125-
.incremental(IncrementalPasses::empty())
126-
.build(&mut builder_context, true, false),
127-
)
116+
.optimization(Optimization::builder().remove_available_modules(true))
117+
.experiments(Experiments::builder().incremental(IncrementalPasses::empty()))
128118
.build();
129119

130120
fast_set(

0 commit comments

Comments
 (0)