Skip to content

Commit

Permalink
refactor: remove mut options in plugin apply (#7958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Sep 24, 2024
1 parent e833f20 commit 7cddc17
Show file tree
Hide file tree
Showing 96 changed files with 210 additions and 447 deletions.
1 change: 1 addition & 0 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 crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin {
fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
_options: &CompilerOptions,
) -> rspack_error::Result<()> {
ctx
.context
Expand Down
5 changes: 1 addition & 4 deletions crates/rspack_binding_options/src/options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use napi_derive::napi;
use rspack_core::{
CacheOptions, CompilerOptions, Context, Experiments, Incremental, ModuleOptions, OutputOptions,
References, Target,
References,
};

mod raw_builtins;
Expand Down Expand Up @@ -69,7 +69,6 @@ impl TryFrom<RawOptions> for CompilerOptions {
let resolve_loader = value.resolve_loader.try_into()?;
let mode = value.mode.unwrap_or_default().into();
let module: ModuleOptions = value.module.try_into()?;
let target = Target::new(&value.target)?;
let cache = value.cache.into();
let experiments = Experiments {
incremental: match value.experiments.incremental {
Expand Down Expand Up @@ -101,7 +100,6 @@ impl TryFrom<RawOptions> for CompilerOptions {
context,
mode,
module,
target,
output,
resolve,
resolve_loader,
Expand All @@ -111,7 +109,6 @@ impl TryFrom<RawOptions> for CompilerOptions {
snapshot,
optimization,
node,
dev_server: Default::default(),
profile: value.profile,
bail: value.bail,
__references: value.__references,
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_binding_options/src/plugins/js_loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ impl Plugin for JsLoaderRspackPlugin {
"rspack.JsLoaderRspackPlugin"
}

fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.normal_module_factory_hooks
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ impl Compiler {
input_filesystem.clone(),
))
});
let (plugin_driver, options) = PluginDriver::new(options, plugins, resolver_factory.clone());
let options = Arc::new(options);
let plugin_driver = PluginDriver::new(options.clone(), plugins, resolver_factory.clone());
let old_cache = Arc::new(OldCache::new(options.clone()));
let unaffected_modules_cache = Arc::new(UnaffectedModulesCache::default());
let module_executor = ModuleExecutor::default();
Expand Down
7 changes: 2 additions & 5 deletions crates/rspack_core/src/options/compiler_options.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use super::Incremental;
use crate::{
CacheOptions, Context, DevServerOptions, Experiments, Mode, ModuleOptions, NodeOption,
Optimization, OutputOptions, Resolve, SnapshotOptions, StatsOptions, Target,
CacheOptions, Context, Experiments, Mode, ModuleOptions, NodeOption, Optimization, OutputOptions,
Resolve, SnapshotOptions, StatsOptions,
};

#[derive(Debug)]
pub struct CompilerOptions {
pub context: Context,
pub dev_server: DevServerOptions,
pub output: OutputOptions,
// TODO(swc-loader): target should not exist on compiler options
pub target: Target,
pub mode: Mode,
pub resolve: Resolve,
pub resolve_loader: Resolve,
Expand Down
4 changes: 0 additions & 4 deletions crates/rspack_core/src/options/dev_server.rs

This file was deleted.

4 changes: 0 additions & 4 deletions crates/rspack_core/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ mod entry;
pub use entry::*;
mod optimizations;
pub use optimizations::*;
mod dev_server;
pub use dev_server::*;
mod output;
pub use output::*;
mod target;
pub use target::*;
mod resolve;
pub use resolve::*;
mod mode;
Expand Down
61 changes: 0 additions & 61 deletions crates/rspack_core/src/options/target.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/rspack_core/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait Plugin: fmt::Debug + Send + Sync {
fn apply(
&self,
_ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
_options: &CompilerOptions,
) -> Result<()> {
Ok(())
}
Expand Down
40 changes: 16 additions & 24 deletions crates/rspack_core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ pub struct PluginDriver {

impl PluginDriver {
pub fn new(
mut options: CompilerOptions,
options: Arc<CompilerOptions>,
plugins: Vec<Box<dyn Plugin>>,
resolver_factory: Arc<ResolverFactory>,
) -> (Arc<Self>, Arc<CompilerOptions>) {
) -> Arc<Self> {
let mut compiler_hooks = Default::default();
let mut compilation_hooks = Default::default();
let mut normal_module_factory_hooks = Default::default();
Expand All @@ -53,31 +53,23 @@ impl PluginDriver {
};
for plugin in &plugins {
plugin
.apply(
PluginContext::with_context(&mut apply_context),
&mut options,
)
.apply(PluginContext::with_context(&mut apply_context), &options)
.expect("TODO:");
}

let options = Arc::new(options);

(
Arc::new(Self {
options: options.clone(),
plugins,
resolver_factory,
registered_parser_and_generator_builder,
diagnostics: Arc::new(Mutex::new(vec![])),
compiler_hooks,
compilation_hooks,
normal_module_factory_hooks,
context_module_factory_hooks,
normal_module_hooks,
concatenated_module_hooks,
}),
options,
)
Arc::new(Self {
options: options.clone(),
plugins,
resolver_factory,
registered_parser_and_generator_builder,
diagnostics: Arc::new(Mutex::new(vec![])),
compiler_hooks,
compilation_hooks,
normal_module_factory_hooks,
context_module_factory_hooks,
normal_module_hooks,
concatenated_module_hooks,
})
}

pub fn take_diagnostic(&self) -> Vec<Diagnostic> {
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_ids/src/deterministic_chunk_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error:
}

impl Plugin for DeterministicChunkIdsPlugin {
fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.compilation_hooks
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_ids/src/deterministic_module_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ fn module_ids(&self, compilation: &mut Compilation) -> Result<()> {
}

impl Plugin for DeterministicModuleIdsPlugin {
fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.compilation_hooks
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_ids/src/named_chunk_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Plugin for NamedChunkIdsPlugin {
fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
_options: &CompilerOptions,
) -> rspack_error::Result<()> {
ctx
.context
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_ids/src/named_module_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ impl Plugin for NamedModuleIdsPlugin {
"NamedModuleIdsPlugin"
}

fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.compilation_hooks
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_ids/src/natural_chunk_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Plugin for NaturalChunkIdsPlugin {
fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
_options: &CompilerOptions,
) -> rspack_error::Result<()> {
ctx
.context
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_ids/src/natural_module_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ impl Plugin for NaturalModuleIdsPlugin {
"NaturalModuleIdsPlugin"
}

fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.compilation_hooks
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ impl Plugin for AssetPlugin {
fn apply(
&self,
ctx: rspack_core::PluginContext<&mut rspack_core::ApplyContext>,
_options: &mut CompilerOptions,
_options: &CompilerOptions,
) -> Result<()> {
ctx
.context
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_banner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Plugin for BannerPlugin {
fn apply(
&self,
ctx: rspack_core::PluginContext<&mut rspack_core::ApplyContext>,
_options: &mut rspack_core::CompilerOptions,
_options: &rspack_core::CompilerOptions,
) -> Result<()> {
ctx
.context
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_plugin_context_replacement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ impl Plugin for ContextReplacementPlugin {
"rspack.ContextReplacementPlugin"
}

fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.context_module_factory_hooks
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_copy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ impl Plugin for CopyRspackPlugin {
fn apply(
&self,
ctx: rspack_core::PluginContext<&mut rspack_core::ApplyContext>,
_options: &mut rspack_core::CompilerOptions,
_options: &rspack_core::CompilerOptions,
) -> Result<()> {
ctx
.context
Expand Down
5 changes: 0 additions & 5 deletions crates/rspack_plugin_css/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,6 @@ impl ParserAndGenerator for CssParserAndGenerator {
left,
right,
)?
} else if generate_context.compilation.options.dev_server.hot {
format!(
"module.hot.accept();\n{}{}module.exports = {{}}{};\n",
ns_obj, left, right
)
} else {
format!("{}{}module.exports = {{}}{};\n", ns_obj, left, right)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl Plugin for CssPlugin {
fn apply(
&self,
ctx: rspack_core::PluginContext<&mut rspack_core::ApplyContext>,
_options: &mut CompilerOptions,
_options: &CompilerOptions,
) -> Result<()> {
ctx
.context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ impl Plugin for EvalDevToolModulePlugin {
EVAL_DEV_TOOL_MODULE_PLUGIN_NAME
}

fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.compiler_hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ impl Plugin for EvalSourceMapDevToolPlugin {
EVAL_SOURCE_MAP_DEV_TOOL_PLUGIN_NAME
}

fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> {
ctx
.context
.compiler_hooks
Expand Down
Loading

2 comments on commit 7cddc17

@rspack-bot
Copy link

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 (2024-09-24 ce775cc) Current Change
10000_development-mode + exec 2.22 s ± 47 ms 2.28 s ± 46 ms +2.29 %
10000_development-mode_hmr + exec 687 ms ± 7.8 ms 709 ms ± 26 ms +3.28 %
10000_production-mode + exec 2.8 s ± 38 ms 2.91 s ± 40 ms +3.87 %
arco-pro_development-mode + exec 1.84 s ± 83 ms 1.85 s ± 65 ms +0.99 %
arco-pro_development-mode_hmr + exec 434 ms ± 1.9 ms 438 ms ± 2.2 ms +0.84 %
arco-pro_production-mode + exec 3.25 s ± 61 ms 3.33 s ± 31 ms +2.54 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.32 s ± 67 ms 3.37 s ± 67 ms +1.77 %
threejs_development-mode_10x + exec 1.68 s ± 21 ms 1.7 s ± 12 ms +0.84 %
threejs_development-mode_10x_hmr + exec 781 ms ± 7.9 ms 797 ms ± 12 ms +1.98 %
threejs_production-mode_10x + exec 5.11 s ± 31 ms 5.27 s ± 23 ms +3.01 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

Please sign in to comment.