From 23c5ddb512bf15d76263ef74b5bc28e1aac42a3a Mon Sep 17 00:00:00 2001 From: Hana Date: Wed, 6 Dec 2023 16:09:27 +0800 Subject: [PATCH] feat: diagnosable modules --- .../src/options/raw_module/js_loader.rs | 4 +++- crates/rspack_core/src/context_module.rs | 5 ++++- crates/rspack_core/src/external_module.rs | 6 ++++- crates/rspack_core/src/missing_module.rs | 4 +++- crates/rspack_core/src/module.rs | 6 ++--- crates/rspack_core/src/normal_module.rs | 22 +++++++++++++++++-- crates/rspack_core/src/raw_module.rs | 4 +++- crates/rspack_core/src/runtime_module.rs | 4 +++- crates/rspack_error/src/diagnostic.rs | 8 +++++++ .../src/container/container_entry_module.rs | 4 +++- .../src/container/remote_module.rs | 4 +++- .../src/sharing/consume_shared_module.rs | 4 +++- .../src/sharing/provide_shared_module.rs | 4 +++- .../src/lazy_compilation.rs | 4 +++- 14 files changed, 67 insertions(+), 16 deletions(-) diff --git a/crates/rspack_binding_options/src/options/raw_module/js_loader.rs b/crates/rspack_binding_options/src/options/raw_module/js_loader.rs index 294e4dbb20e4..de85b2fa66bc 100644 --- a/crates/rspack_binding_options/src/options/raw_module/js_loader.rs +++ b/crates/rspack_binding_options/src/options/raw_module/js_loader.rs @@ -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, @@ -463,7 +465,7 @@ impl napi::bindgen_prelude::FromNapiValue for JsLoaderResult { })?; let source_map_: Option = obj.get("sourceMap")?; let additional_data_: Option = obj.get("additionalData")?; - // eagerly clone this field since `External` might be dropped. + // change: eagerly clone this field since `External` might be dropped. let additional_data_external_: External = obj .get("additionalDataExternal")? .map(|v: External| External::new(v.clone())) diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index 9d77f072c362..2e08eb522d4c 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -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}; @@ -659,6 +660,8 @@ impl Module for ContextModule { } } +impl Diagnosable for ContextModule {} + impl Identifiable for ContextModule { fn identifier(&self) -> Identifier { self.identifier diff --git a/crates/rspack_core/src/external_module.rs b/crates/rspack_core/src/external_module.rs index 1922afb6fdce..6b3252bb47d4 100644 --- a/crates/rspack_core/src/external_module.rs +++ b/crates/rspack_core/src/external_module.rs @@ -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; @@ -432,6 +434,8 @@ impl Module for ExternalModule { } } +impl Diagnosable for ExternalModule {} + impl Hash for ExternalModule { fn hash(&self, state: &mut H) { "__rspack_internal__ExternalModule".hash(state); diff --git a/crates/rspack_core/src/missing_module.rs b/crates/rspack_core/src/missing_module.rs index d1b89ee611e9..607e8e05dcf6 100644 --- a/crates/rspack_core/src/missing_module.rs +++ b/crates/rspack_core/src/missing_module.rs @@ -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; @@ -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 diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index 88bbbfb7623c..f8abc4dab673 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -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; @@ -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; @@ -179,7 +179,7 @@ pub trait Module: blocks: Vec::new(), analyze_result: Default::default(), } - .with_empty_diagnostic(), + .with_diagnostic(self.take_diagnostics()), ) } diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index 3e4fe98969fd..e17d9a5c2a59 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -4,7 +4,7 @@ use std::{ hash::{BuildHasherDefault, Hash}, sync::{ atomic::{AtomicUsize, Ordering}, - Arc, + Arc, Mutex, }, }; @@ -12,7 +12,8 @@ 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; @@ -120,6 +121,7 @@ pub struct NormalModule { #[allow(unused)] debug_id: usize, cached_source_sizes: DashMap>, + diagnostics: Mutex>, code_generation_dependencies: Option>>, presentational_dependencies: Option>>, @@ -207,6 +209,7 @@ impl NormalModule { options, cached_source_sizes: DashMap::default(), + diagnostics: Mutex::new(Default::default()), code_generation_dependencies: None, presentational_dependencies: None, } @@ -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?; @@ -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) { + self.diagnostics.lock().unwrap().append(&mut diagnostics); + } + + fn take_diagnostics(&self) -> Vec { + self.diagnostics.lock().unwrap().drain(..).collect() + } +} + impl PartialEq for NormalModule { fn eq(&self, other: &Self) -> bool { self.identifier() == other.identifier() diff --git a/crates/rspack_core/src/raw_module.rs b/crates/rspack_core/src/raw_module.rs index a9ecff5a5a4d..7ea70ccad4fe 100644 --- a/crates/rspack_core/src/raw_module.rs +++ b/crates/rspack_core/src/raw_module.rs @@ -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}; @@ -126,6 +126,8 @@ impl Module for RawModule { } } +impl Diagnosable for RawModule {} + impl Hash for RawModule { fn hash(&self, state: &mut H) { "__rspack_internal__RawModule".hash(state); diff --git a/crates/rspack_core/src/runtime_module.rs b/crates/rspack_core/src/runtime_module.rs index bd6a09f7ea9a..5811bada5b1c 100644 --- a/crates/rspack_core/src/runtime_module.rs +++ b/crates/rspack_core/src/runtime_module.rs @@ -42,7 +42,7 @@ impl 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() @@ -116,5 +116,7 @@ macro_rules! impl_runtime_module { Ok(result) } } + + impl rspack_error::Diagnosable for $ident {} }; } diff --git a/crates/rspack_error/src/diagnostic.rs b/crates/rspack_error/src/diagnostic.rs index c2f660f1f6ae..d71efbacaaf1 100644 --- a/crates/rspack_error/src/diagnostic.rs +++ b/crates/rspack_error/src/diagnostic.rs @@ -106,6 +106,14 @@ impl Diagnostic { } } +pub trait Diagnosable { + fn add_diagnostic(&self, _diagnostic: Diagnostic) {} + fn add_diagnostics(&self, _diagnostics: Vec) {} + fn take_diagnostics(&self) -> Vec { + vec![] + } +} + pub fn errors_to_diagnostics(errs: Vec) -> Vec { errs.into_iter().map(Diagnostic::from).collect() } diff --git a/crates/rspack_plugin_mf/src/container/container_entry_module.rs b/crates/rspack_plugin_mf/src/container/container_entry_module.rs index 72a715c2202a..afa665e9568b 100644 --- a/crates/rspack_plugin_mf/src/container/container_entry_module.rs +++ b/crates/rspack_plugin_mf/src/container/container_entry_module.rs @@ -10,7 +10,7 @@ use rspack_core::{ LibIdentOptions, Module, ModuleDependency, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, }; -use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; +use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; use rspack_hash::RspackHash; use rspack_identifier::{Identifiable, Identifier}; @@ -256,6 +256,8 @@ var init = (shareScope, initScope) => {{ } } +impl Diagnosable for ContainerEntryModule {} + impl Hash for ContainerEntryModule { fn hash(&self, state: &mut H) { "__rspack_internal__ContainerEntryModule".hash(state); diff --git a/crates/rspack_plugin_mf/src/container/remote_module.rs b/crates/rspack_plugin_mf/src/container/remote_module.rs index 84263155732f..a19ba92a14d2 100644 --- a/crates/rspack_plugin_mf/src/container/remote_module.rs +++ b/crates/rspack_plugin_mf/src/container/remote_module.rs @@ -8,7 +8,7 @@ use rspack_core::{ CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions, Module, ModuleIdentifier, ModuleType, RuntimeSpec, SourceType, }; -use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; +use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; use rspack_hash::RspackHash; use rspack_identifier::{Identifiable, Identifier}; @@ -175,6 +175,8 @@ impl Module for RemoteModule { } } +impl Diagnosable for RemoteModule {} + impl Hash for RemoteModule { fn hash(&self, state: &mut H) { "__rspack_internal__RemoteModule".hash(state); diff --git a/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs b/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs index ae8753151d09..32ac3a9a0d22 100644 --- a/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs +++ b/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs @@ -7,7 +7,7 @@ use rspack_core::{ CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions, Module, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, }; -use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; +use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; use rspack_hash::RspackHash; use rspack_identifier::{Identifiable, Identifier}; @@ -223,6 +223,8 @@ impl Module for ConsumeSharedModule { } } +impl Diagnosable for ConsumeSharedModule {} + impl Hash for ConsumeSharedModule { fn hash(&self, state: &mut H) { "__rspack_internal__ConsumeSharedModule".hash(state); diff --git a/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs b/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs index 2a7683601359..020471ca6119 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs @@ -7,7 +7,7 @@ use rspack_core::{ CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions, Module, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, }; -use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; +use rspack_error::{Diagnosable, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray}; use rspack_hash::RspackHash; use rspack_identifier::{Identifiable, Identifier}; @@ -188,6 +188,8 @@ impl Module for ProvideSharedModule { } } +impl Diagnosable for ProvideSharedModule {} + impl Hash for ProvideSharedModule { fn hash(&self, state: &mut H) { "__rspack_internal__ProvideSharedModule".hash(state); diff --git a/crates/rspack_plugin_runtime/src/lazy_compilation.rs b/crates/rspack_plugin_runtime/src/lazy_compilation.rs index 0ff04a89dc08..567ce0601002 100644 --- a/crates/rspack_plugin_runtime/src/lazy_compilation.rs +++ b/crates/rspack_plugin_runtime/src/lazy_compilation.rs @@ -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)] @@ -91,6 +91,8 @@ impl Identifiable for LazyCompilationProxyModule { } } +impl Diagnosable for LazyCompilationProxyModule {} + impl Hash for LazyCompilationProxyModule { fn hash(&self, state: &mut H) { "__rspack_internal__LazyCompilationProxyModule".hash(state);