Skip to content

Commit

Permalink
feat: diagnosable modules
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Dec 6, 2023
1 parent a5b1bed commit 106514c
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 16 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
5 changes: 4 additions & 1 deletion crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use rspack_error::{
internal_error, miette::IntoDiagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
internal_error, miette::IntoDiagnostic, Diagnosable, IntoTWithDiagnosticArray, Result,
TWithDiagnosticArray,
};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
Expand Down Expand Up @@ -659,6 +660,8 @@ impl Module for ContextModule {
}
}

impl Diagnosable for ContextModule {}

impl Identifiable for ContextModule {
fn identifier(&self) -> Identifier {
self.identifier
Expand Down
6 changes: 5 additions & 1 deletion crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::borrow::Cow;
use std::hash::Hash;
use std::iter;

use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{
internal_error, Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rustc_hash::FxHashMap as HashMap;
Expand Down Expand Up @@ -434,6 +436,8 @@ impl Module for ExternalModule {
}
}

impl Diagnosable for ExternalModule {}

impl Hash for ExternalModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__ExternalModule".hash(state);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, hash::Hash};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
Expand Down Expand Up @@ -255,6 +255,8 @@ var init = (shareScope, initScope) => {{
}
}

impl Diagnosable for ContainerEntryModule {}

impl Hash for ContainerEntryModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__ContainerEntryModule".hash(state);
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/mf/container/remote_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 async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
Expand Down Expand Up @@ -167,6 +167,8 @@ impl Module for RemoteModule {
}
}

impl Diagnosable for RemoteModule {}

impl Hash for RemoteModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__RemoteModule".hash(state);
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/mf/sharing/consume_shared_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, hash::Hash};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;
Expand Down Expand Up @@ -227,6 +227,8 @@ impl Module for ConsumeSharedModule {
}
}

impl Diagnosable for ConsumeSharedModule {}

impl Hash for ConsumeSharedModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__ConsumeSharedModule".hash(state);
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/mf/sharing/provide_shared_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, hash::Hash};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;
Expand Down Expand Up @@ -189,6 +189,8 @@ impl Module for ProvideSharedModule {
}
}

impl Diagnosable for ProvideSharedModule {}

impl Hash for ProvideSharedModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__ProvideSharedModule".hash(state);
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::{Diagnosable, 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 Diagnosable for MissingModule {}

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

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;
Expand Down Expand Up @@ -139,7 +139,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 Down Expand Up @@ -179,7 +179,7 @@ pub trait Module:
blocks: Vec::new(),
analyze_result: Default::default(),
}
.with_empty_diagnostic(),
.with_diagnostic(self.take_diagnostics()),
)
}

Expand Down
22 changes: 20 additions & 2 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ use std::{
hash::{BuildHasherDefault, Hash},
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
Arc, Mutex,
},
};

use bitflags::bitflags;
use dashmap::DashMap;
use derivative::Derivative;
use rspack_error::{
internal_error, Diagnostic, IntoTWithDiagnosticArray, Result, Severity, TWithDiagnosticArray,
internal_error, Diagnosable, Diagnostic, IntoTWithDiagnosticArray, Result, Severity,
TWithDiagnosticArray,
};
use rspack_hash::RspackHash;
use rspack_identifier::Identifiable;
Expand Down Expand Up @@ -120,6 +121,7 @@ pub struct NormalModule {
#[allow(unused)]
debug_id: usize,
cached_source_sizes: DashMap<SourceType, f64, BuildHasherDefault<FxHasher>>,
diagnostics: Mutex<Vec<Diagnostic>>,

code_generation_dependencies: Option<Vec<Box<dyn ModuleDependency>>>,
presentational_dependencies: Option<Vec<Box<dyn DependencyTemplate>>>,
Expand Down Expand Up @@ -207,6 +209,7 @@ impl NormalModule {

options,
cached_source_sizes: DashMap::default(),
diagnostics: Mutex::new(Default::default()),
code_generation_dependencies: None,
presentational_dependencies: None,
}
Expand Down Expand Up @@ -345,6 +348,7 @@ impl Module for NormalModule {
let mut build_info = BuildInfo::default();
let mut build_meta = BuildMeta::default();
let mut diagnostics = Vec::new();
panic!("ooo");

build_context.plugin_driver.before_loaders(self).await?;

Expand Down Expand Up @@ -581,6 +585,20 @@ impl Module for NormalModule {
}
}

impl Diagnosable for NormalModule {
fn add_diagnostic(&self, diagnostic: Diagnostic) {
self.diagnostics.lock().unwrap().push(diagnostic);
}

fn add_diagnostics(&self, mut diagnostics: Vec<Diagnostic>) {
self.diagnostics.lock().unwrap().append(&mut diagnostics);
}

fn take_diagnostics(&self) -> Vec<Diagnostic> {
self.diagnostics.lock().unwrap().drain(..).collect()
}
}

impl PartialEq for NormalModule {
fn eq(&self, other: &Self) -> bool {
self.identifier() == other.identifier()
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/raw_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::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::Identifiable;
use rspack_sources::{BoxSource, RawSource, Source, SourceExt};
Expand Down Expand Up @@ -126,6 +126,8 @@ impl Module for RawModule {
}
}

impl Diagnosable for RawModule {}

impl Hash for RawModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__RawModule".hash(state);
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T: RuntimeModule + 'static> RuntimeModuleExt for T {

#[macro_export]
macro_rules! impl_runtime_module {
($ident: ident) => {
($ident:ident) => {
impl rspack_identifier::Identifiable for $ident {
fn identifier(&self) -> rspack_identifier::Identifier {
self.name()
Expand Down Expand Up @@ -116,5 +116,7 @@ macro_rules! impl_runtime_module {
Ok(result)
}
}

impl rspack_error::Diagnosable for $ident {}
};
}
8 changes: 8 additions & 0 deletions crates/rspack_error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ impl Diagnostic {
}
}

pub trait Diagnosable {
fn add_diagnostic(&self, _diagnostic: Diagnostic) {}
fn add_diagnostics(&self, _diagnostics: Vec<Diagnostic>) {}
fn take_diagnostics(&self) -> Vec<Diagnostic> {
vec![]
}
}

pub fn errors_to_diagnostics(errs: Vec<Error>) -> Vec<Diagnostic> {
errs.into_iter().map(Diagnostic).collect()
}
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_plugin_runtime/src/lazy_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rspack_core::{
PluginNormalModuleFactoryCreateModuleHookOutput, RuntimeGlobals, RuntimeSpec, SourceType,
};
use rspack_core::{CodeGenerationResult, Context, ModuleIdentifier};
use rspack_error::Result;
use rspack_error::{Diagnosable, Result};
use rspack_identifier::Identifiable;

#[derive(Debug)]
Expand Down Expand Up @@ -91,6 +91,8 @@ impl Identifiable for LazyCompilationProxyModule {
}
}

impl Diagnosable for LazyCompilationProxyModule {}

impl Hash for LazyCompilationProxyModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
"__rspack_internal__LazyCompilationProxyModule".hash(state);
Expand Down

0 comments on commit 106514c

Please sign in to comment.