Skip to content

Commit

Permalink
refactor: diagnosable modules (#4924)
Browse files Browse the repository at this point in the history
* feat: diagnosable modules

* save

* improve

* improve
  • Loading branch information
h-a-n-a authored and ahabhgk committed Jan 2, 2024
1 parent 002c671 commit df2ed5f
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ impl napi::bindgen_prelude::TypeName for JsLoaderResult {
napi::ValueType::Object
}
}

// Manually convert
impl napi::bindgen_prelude::FromNapiValue for JsLoaderResult {
unsafe fn from_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
Expand Down Expand Up @@ -463,7 +465,7 @@ impl napi::bindgen_prelude::FromNapiValue for JsLoaderResult {
})?;
let source_map_: Option<Buffer> = obj.get("sourceMap")?;
let additional_data_: Option<Buffer> = obj.get("additionalData")?;
// eagerly clone this field since `External<T>` might be dropped.
// change: eagerly clone this field since `External<T>` might be dropped.
let additional_data_external_: External<AdditionalData> = obj
.get("additionalDataExternal")?
.map(|v: External<AdditionalData>| External::new(v.clone()))
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/compiler/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use rspack_error::{Diagnostic, Result};
use rspack_error::{Diagnostic, IntoTWithDiagnosticArray, Result};

use crate::{
cache::Cache, BoxDependency, BuildContext, BuildResult, Compilation, CompilerContext,
Expand Down Expand Up @@ -257,7 +257,7 @@ impl WorkerTask for BuildTask {
compiler_context: CompilerContext {
options: compiler_options.clone(),
resolver_factory: resolver_factory.clone(),
module: Some(module.identifier()),
module: module.identifier(),
module_context: module.as_normal_module().and_then(|m| m.get_context()),
},
plugin_driver: plugin_driver.clone(),
Expand All @@ -270,7 +270,7 @@ impl WorkerTask for BuildTask {
.await
.unwrap_or_else(|e| panic!("Run succeed_module hook failed: {}", e));

result.map(|t| (t, module))
result.map(|t| (t.with_diagnostic(module.clone_diagnostics()), module))
})
.await
{
Expand Down
33 changes: 12 additions & 21 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use std::{
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use rspack_error::{
miette::IntoDiagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
};
use rspack_error::{impl_empty_diagnosable_trait, miette::IntoDiagnostic, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_regex::RspackRegex;
Expand Down Expand Up @@ -582,10 +580,7 @@ impl Module for ContextModule {
Some(Cow::Owned(id))
}

async fn build(
&mut self,
build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, build_context: BuildContext<'_>) -> Result<BuildResult> {
self.resolve_dependencies(build_context)
}

Expand Down Expand Up @@ -654,6 +649,8 @@ impl Module for ContextModule {
}
}

impl_empty_diagnosable_trait!(ContextModule);

impl Identifiable for ContextModule {
fn identifier(&self) -> Identifier {
self.identifier
Expand Down Expand Up @@ -746,10 +743,7 @@ impl ContextModule {
Ok(())
}

fn resolve_dependencies(
&self,
build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
fn resolve_dependencies(&self, build_context: BuildContext<'_>) -> Result<BuildResult> {
tracing::trace!("resolving context module path {}", self.options.resource);

let resolver = &self.resolve_factory.get(ResolveOptionsWithDependencyType {
Expand Down Expand Up @@ -835,16 +829,13 @@ impl ContextModule {
..Default::default()
};

Ok(
BuildResult {
build_info,
build_meta: BuildMeta::default(),
dependencies,
blocks,
analyze_result: Default::default(),
}
.with_diagnostic(vec![]),
)
Ok(BuildResult {
build_info,
build_meta: BuildMeta::default(),
dependencies,
blocks,
analyze_result: Default::default(),
})
}
}

Expand Down
11 changes: 5 additions & 6 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::hash::Hash;
use std::iter;

use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{impl_empty_diagnosable_trait, internal_error, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rustc_hash::FxHashMap as HashMap;
Expand Down Expand Up @@ -345,10 +345,7 @@ impl Module for ExternalModule {
42.0
}

async fn build(
&mut self,
build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, build_context: BuildContext<'_>) -> Result<BuildResult> {
let mut hasher = RspackHash::from(&build_context.compiler_options.output);
self.update_hash(&mut hasher);

Expand Down Expand Up @@ -376,7 +373,7 @@ impl Module for ExternalModule {
}
_ => build_result.build_meta.exports_type = BuildMetaExportsType::Dynamic,
}
Ok(build_result.with_empty_diagnostic())
Ok(build_result)
}

fn code_generation(
Expand Down Expand Up @@ -432,6 +429,8 @@ impl Module for ExternalModule {
}
}

impl_empty_diagnosable_trait!(ExternalModule);

impl Hash for ExternalModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__ExternalModule".hash(state);
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/loader/loader_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{CompilerOptions, Context, ModuleIdentifier, ResolverFactory};
pub struct CompilerContext {
pub options: Arc<CompilerOptions>,
pub resolver_factory: Arc<ResolverFactory>,
pub module: Option<ModuleIdentifier>, // current module
pub module: ModuleIdentifier, // current module
pub module_context: Option<Box<Context>>, // current module context
}

Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/missing_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::hash::Hash;

use rspack_error::Result;
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
use serde_json::json;
Expand Down Expand Up @@ -104,6 +104,8 @@ impl Identifiable for MissingModule {
}
}

impl_empty_diagnosable_trait!(MissingModule);

impl PartialEq for MissingModule {
fn eq(&self, other: &Self) -> bool {
self.identifier == other.identifier
Expand Down
35 changes: 14 additions & 21 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{any::Any, borrow::Cow, fmt::Debug};

use async_trait::async_trait;
use json::JsonValue;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, Result};
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;
Expand Down Expand Up @@ -141,7 +141,7 @@ pub type ModuleIdentifier = Identifier;

#[async_trait]
pub trait Module:
Debug + Send + Sync + AsAny + DynHash + DynEq + Identifiable + DependenciesBlock
Debug + Send + Sync + AsAny + DynHash + DynEq + Identifiable + DependenciesBlock + Diagnosable
{
/// Defines what kind of module this is.
fn module_type(&self) -> &ModuleType;
Expand All @@ -161,10 +161,7 @@ pub trait Module:

/// The actual build of the module, which will be called by the `Compilation`.
/// Build can also returns the dependencies of the module, which will be used by the `Compilation` to build the dependency graph.
async fn build(
&mut self,
build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, build_context: BuildContext<'_>) -> Result<BuildResult> {
let mut hasher = RspackHash::from(&build_context.compiler_options.output);
self.update_hash(&mut hasher);

Expand All @@ -173,16 +170,13 @@ pub trait Module:
..Default::default()
};

Ok(
BuildResult {
build_info,
build_meta: Default::default(),
dependencies: Vec::new(),
blocks: Vec::new(),
analyze_result: Default::default(),
}
.with_empty_diagnostic(),
)
Ok(BuildResult {
build_info,
build_meta: Default::default(),
dependencies: Vec::new(),
blocks: Vec::new(),
analyze_result: Default::default(),
})
}

/// The actual code generation of the module, which will be called by the `Compilation`.
Expand Down Expand Up @@ -341,7 +335,7 @@ mod test {
use std::borrow::Cow;
use std::hash::Hash;

use rspack_error::{Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, Result};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;

Expand Down Expand Up @@ -389,6 +383,8 @@ mod test {
}
}

impl Diagnosable for $ident {}

impl DependenciesBlock for $ident {
fn add_block_id(&mut self, _: AsyncDependenciesBlockIdentifier) {
unreachable!()
Expand Down Expand Up @@ -429,10 +425,7 @@ mod test {
(stringify!($ident).to_owned() + self.0).into()
}

async fn build(
&mut self,
_build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, _build_context: BuildContext<'_>) -> Result<BuildResult> {
unreachable!()
}

Expand Down
9 changes: 4 additions & 5 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl ModuleGraph {
mod test {
use std::borrow::Cow;

use rspack_error::{Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, Result};
use rspack_identifier::Identifiable;
use rspack_sources::Source;

Expand All @@ -793,6 +793,8 @@ mod test {
}
}

impl Diagnosable for $ident {}

impl DependenciesBlock for $ident {
fn add_block_id(&mut self, _: AsyncDependenciesBlockIdentifier) {
unreachable!()
Expand Down Expand Up @@ -833,10 +835,7 @@ mod test {
unreachable!()
}

async fn build(
&mut self,
_build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, _build_context: BuildContext<'_>) -> Result<BuildResult> {
unreachable!()
}

Expand Down
Loading

0 comments on commit df2ed5f

Please sign in to comment.