rspack_plugin_entry/
lib.rs

1use std::sync::LazyLock;
2
3use rspack_core::{
4  BoxDependency, Compilation, CompilationParams, CompilerCompilation, CompilerMake, Context,
5  DependencyType, EntryDependency, EntryOptions, Plugin,
6};
7use rspack_error::Result;
8use rspack_hook::{plugin, plugin_hook};
9
10type LazyDependency = LazyLock<BoxDependency, Box<dyn FnOnce() -> BoxDependency + Send>>;
11
12#[plugin]
13#[derive(Debug)]
14pub struct EntryPlugin {
15  // Need "cache" the dependency to tell incremental that this entry dependency is not changed
16  // so it can be reused and skip the module make
17  dependency: LazyDependency,
18  options: EntryOptions,
19}
20
21impl EntryPlugin {
22  pub fn new(context: Context, entry_request: String, options: EntryOptions) -> Self {
23    let layer = options.layer.clone();
24    let name = options.name.is_none();
25    let dependency: LazyDependency = LazyLock::new(Box::new(move || {
26      Box::new(EntryDependency::new(entry_request, context, layer, name))
27    }));
28
29    Self::new_inner(dependency, options)
30  }
31}
32
33#[plugin_hook(CompilerCompilation for EntryPlugin)]
34async fn compilation(
35  &self,
36  compilation: &mut Compilation,
37  params: &mut CompilationParams,
38) -> Result<()> {
39  compilation.set_dependency_factory(DependencyType::Entry, params.normal_module_factory.clone());
40  Ok(())
41}
42
43#[plugin_hook(CompilerMake for EntryPlugin)]
44async fn make(&self, compilation: &mut Compilation) -> Result<()> {
45  let this = &self.inner;
46  compilation
47    .add_entry(this.dependency.clone(), this.options.clone())
48    .await?;
49  Ok(())
50}
51
52impl Plugin for EntryPlugin {
53  fn apply(&self, ctx: &mut rspack_core::ApplyContext<'_>) -> Result<()> {
54    ctx.compiler_hooks.compilation.tap(compilation::new(self));
55    ctx.compiler_hooks.make.tap(make::new(self));
56    Ok(())
57  }
58}