Skip to content

Commit 519c54c

Browse files
authored
feat: support enabling builtin loaders in compiler builder (#9262)
* feat: support enabling builtin loaders in compiler builder * chore: lint
1 parent 01374bb commit 519c54c

File tree

22 files changed

+611
-2
lines changed

22 files changed

+611
-2
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ jobs:
328328
329329
- name: Run test
330330
# reason for excluding https://github.com/napi-rs/napi-rs/issues/2200
331-
run: cargo test --workspace --exclude rspack_node -- --nocapture
331+
run: cargo test --workspace --exclude rspack_node --exclude rspack -- --nocapture
332+
333+
- name: Run rspack test
334+
run: cargo test --package rspack --all-features -- --nocapture
332335

333336
rust_test_miri:
334337
name: Rust test miri

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rspack/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ name = "rspack"
66
repository = "https://github.com/web-infra-dev/rspack"
77
version = "0.2.0"
88

9+
[features]
10+
full = ["loaders"]
11+
12+
loader_lightningcss = ["rspack_loader_lightningcss"]
13+
loader_preact_refresh = ["rspack_loader_preact_refresh"]
14+
loader_react_refresh = ["rspack_loader_react_refresh"]
15+
loader_swc = ["rspack_loader_swc"]
16+
loaders = ["loader_lightningcss", "loader_preact_refresh", "loader_react_refresh", "loader_swc"]
17+
918
[dependencies]
1019
bitflags = { workspace = true }
1120
enum-tag = { workspace = true }
@@ -20,6 +29,7 @@ rspack_regex = { workspace = true }
2029
rustc-hash = { workspace = true }
2130
serde_json = { workspace = true }
2231

32+
# Plugins
2333
rspack_plugin_asset = { workspace = true }
2434
rspack_plugin_css = { workspace = true }
2535
rspack_plugin_devtool = { workspace = true }
@@ -40,6 +50,12 @@ rspack_plugin_swc_js_minimizer = { workspace = true }
4050
rspack_plugin_wasm = { workspace = true }
4151
rspack_plugin_worker = { workspace = true }
4252

53+
# Loaders
54+
rspack_loader_lightningcss = { workspace = true, optional = true }
55+
rspack_loader_preact_refresh = { workspace = true, optional = true }
56+
rspack_loader_react_refresh = { workspace = true, optional = true }
57+
rspack_loader_swc = { workspace = true, optional = true }
58+
4359
[dev-dependencies]
4460
insta = { workspace = true, features = ["filters"] }
4561
tokio = { workspace = true }

crates/rspack/src/builder/mod.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ impl CompilerBuilder {
431431
}
432432

433433
/// Build [`Compiler`] from options and plugins.
434+
#[must_use]
434435
pub fn build(&mut self) -> Compiler {
435436
let mut builder_context = BuilderContext::default();
436437
let compiler_options = self.options_builder.build(&mut builder_context);
@@ -455,6 +456,44 @@ impl CompilerBuilder {
455456
}
456457
}
457458

459+
#[cfg(feature = "loader_lightningcss")]
460+
impl CompilerBuilder {
461+
/// Enable support for builtin:lightningcss-loader.
462+
pub fn enable_loader_lightningcss(&mut self) -> &mut Self {
463+
self.plugin(Box::new(
464+
rspack_loader_lightningcss::LightningcssLoaderPlugin::new(),
465+
))
466+
}
467+
}
468+
469+
#[cfg(feature = "loader_swc")]
470+
impl CompilerBuilder {
471+
/// Enable support for builtin:swc-loader.
472+
pub fn enable_loader_swc(&mut self) -> &mut Self {
473+
self.plugin(Box::new(rspack_loader_swc::SwcLoaderPlugin::new()))
474+
}
475+
}
476+
477+
#[cfg(feature = "loader_react_refresh")]
478+
impl CompilerBuilder {
479+
/// Enable support for builtin:react-refresh-loader.
480+
pub fn enable_loader_react_refresh(&mut self) -> &mut Self {
481+
self.plugin(Box::new(
482+
rspack_loader_react_refresh::ReactRefreshLoaderPlugin::new(),
483+
))
484+
}
485+
}
486+
487+
#[cfg(feature = "loader_preact_refresh")]
488+
impl CompilerBuilder {
489+
/// Enable support for builtin:preact-refresh-loader.
490+
pub fn enable_loader_preact_refresh(&mut self) -> &mut Self {
491+
self.plugin(Box::new(
492+
rspack_loader_preact_refresh::PreactRefreshLoaderPlugin::new(),
493+
))
494+
}
495+
}
496+
458497
impl Builder for CompilerOptions {
459498
type Item = CompilerOptionsBuilder;
460499

@@ -838,6 +877,7 @@ impl CompilerOptionsBuilder {
838877
///
839878
/// [`BuilderContext`]: crate::builder::BuilderContext
840879
/// [`CompilerOptions`]: rspack_core::options::CompilerOptions
880+
#[must_use]
841881
pub fn build(&mut self, builder_context: &mut BuilderContext) -> CompilerOptions {
842882
let name = self.name.take();
843883
let context = f!(self.context.take(), || {
@@ -1480,6 +1520,7 @@ impl NodeOptionBuilder {
14801520
/// Build [`NodeOption`] from options.
14811521
///
14821522
/// [`NodeOption`]: rspack_core::options::NodeOption
1523+
#[must_use]
14831524
fn build(
14841525
&mut self,
14851526
target_properties: &TargetProperties,
@@ -1606,6 +1647,7 @@ impl ModuleOptionsBuilder {
16061647
/// Normally, you don't need to call this function, it's used internally by [`CompilerBuilder::build`].
16071648
///
16081649
/// [`ModuleOptions`]: rspack_core::options::ModuleOptions
1650+
#[must_use]
16091651
fn build(
16101652
&mut self,
16111653
_builder_context: &mut BuilderContext,
@@ -2566,6 +2608,7 @@ impl OutputOptionsBuilder {
25662608
///
25672609
/// [`OutputOptions`]: rspack_core::options::OutputOptions
25682610
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
2611+
#[must_use]
25692612
fn build(
25702613
&mut self,
25712614
builder_context: &mut BuilderContext,
@@ -3322,6 +3365,7 @@ impl OptimizationOptionsBuilder {
33223365
/// Build [`Optimization`] from options.
33233366
///
33243367
/// [`Optimization`]: rspack_core::options::Optimization
3368+
#[must_use]
33253369
fn build(
33263370
&mut self,
33273371
builder_context: &mut BuilderContext,
@@ -3669,6 +3713,7 @@ impl ExperimentsBuilder {
36693713
/// Build [`Experiments`] from options.
36703714
///
36713715
/// [`Experiments`]: rspack_core::options::Experiments
3716+
#[must_use]
36723717
fn build(
36733718
&mut self,
36743719
_builder_context: &mut BuilderContext,
@@ -3759,7 +3804,7 @@ mod test {
37593804

37603805
#[test]
37613806
fn mutable_builder_into_owned_builder() {
3762-
CompilerOptions::builder()
3807+
let _ = CompilerOptions::builder()
37633808
.optimization(OptimizationOptionsBuilder::default().node_env("development".to_string()))
37643809
.output(OutputOptionsBuilder::default().charset(true))
37653810
.experiments(ExperimentsBuilder::default().future_defaults(true))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
.a {
3+
color: blue
4+
}
5+
6+
.b {
7+
color: red
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./index.css";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function Foo() {
2+
return <div>Foo</div>;
3+
}
4+
5+
eval(Foo);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function Foo() {
2+
return <div>Foo</div>;
3+
}
4+
5+
eval(Foo);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function Foo() {
2+
return <div>Foo</div>;
3+
}
4+
5+
eval(Foo);

0 commit comments

Comments
 (0)