rspack_core 0.100.1

rspack core
Documentation
use async_trait::async_trait;
use rspack_error::Result;

use super::build_module_graph_pass;
use crate::{
  Compilation,
  cache::Cache,
  compilation::{
    finish_make::finish_make_pass, finish_module_graph::finish_module_graph_pass,
    make::make_hook_pass, pass::PassExt,
  },
  logger::Logger,
};

/// Composite pass for the entire Build Module Graph phase.
///
/// This phase includes multiple sub-passes:
/// - make hook
/// - build module graph
/// - finish make
/// - finish module graph
pub struct BuildModuleGraphPhasePass;

#[async_trait]
impl PassExt for BuildModuleGraphPhasePass {
  fn name(&self) -> &'static str {
    "build module graph"
  }

  async fn before_pass(&self, compilation: &mut Compilation, cache: &mut dyn Cache) {
    cache.before_build_module_graph(compilation).await;
  }

  async fn run_pass(&self, _compilation: &mut Compilation) -> Result<()> {
    // This method is not used; we override run_pass_with_cache instead
    unreachable!("BuildModuleGraphPhasePass uses run_pass_with_cache")
  }

  async fn run_pass_with_cache(
    &self,
    compilation: &mut Compilation,
    _cache: &mut dyn Cache,
  ) -> Result<()> {
    let plugin_driver = compilation.plugin_driver.clone();
    let logger = compilation.get_logger("rspack.Compiler");
    // align with webpack, make hook include build_module_graph phase in webpack
    let start = logger.time("make hook");
    make_hook_pass(compilation, plugin_driver.clone()).await?;
    build_module_graph_pass(compilation).await?;
    logger.time_end(start);

    finish_make_pass(compilation, plugin_driver).await?;
    finish_module_graph_pass(compilation).await?;

    use crate::incremental::IncrementalPasses;
    if compilation
      .incremental
      .passes_enabled(IncrementalPasses::BUILD_MODULE_GRAPH)
    {
      compilation
        .build_module_graph_artifact
        .module_graph
        .checkpoint();
    }
    Ok(())
  }

  async fn after_pass(&self, compilation: &mut Compilation, cache: &mut dyn Cache) {
    cache.after_build_module_graph(compilation).await;
  }
}