use std::borrow::Cow;
use async_trait::async_trait;
use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{Result, impl_empty_diagnosable_trait};
use rspack_hash::RspackHashDigest;
use rspack_macros::impl_source_map_config;
use rspack_sources::BoxSource;
use rspack_util::source_map::SourceMapKind;
use crate::{
BoxModule, BuildContext, BuildInfo, BuildMeta, ChunkUkey, CodeGenerationResultBuilder,
Compilation, Context, DependenciesBlock, DependenciesBlockData, FactoryMetaStore, FreezeLock,
LibIdentOptions, Module, ModuleCodeGenerationContext, ModuleGraph, ModuleIdentifier, ModuleType,
RuntimeSpec, SourceType, impl_module_meta_info,
};
#[impl_source_map_config]
#[cacheable]
#[derive(Debug)]
pub struct SelfModule {
identifier: ModuleIdentifier,
readable_identifier: String,
dependencies_block: DependenciesBlockData,
factory_meta: FactoryMetaStore,
build_info: FreezeLock<BuildInfo>,
build_meta: FreezeLock<BuildMeta>,
}
impl SelfModule {
pub fn new(module_identifier: ModuleIdentifier) -> Self {
let identifier = format!("self {module_identifier}");
Self {
identifier: ModuleIdentifier::from(identifier.as_str()),
readable_identifier: identifier,
dependencies_block: Default::default(),
factory_meta: Default::default(),
build_info: BuildInfo {
strict: true,
..Default::default()
}
.into(),
build_meta: Default::default(),
source_map_kind: SourceMapKind::empty(),
}
}
}
impl Identifiable for SelfModule {
fn identifier(&self) -> Identifier {
self.identifier
}
}
impl DependenciesBlock for SelfModule {
fn dependencies_block(&self) -> &DependenciesBlockData {
&self.dependencies_block
}
fn dependencies_block_mut(&mut self) -> &mut DependenciesBlockData {
&mut self.dependencies_block
}
}
#[cacheable_dyn]
#[async_trait]
impl Module for SelfModule {
impl_module_meta_info!();
fn size(&self, _source_type: Option<&SourceType>, _compilation: Option<&Compilation>) -> f64 {
self.identifier.len() as f64
}
fn module_type(&self) -> &ModuleType {
&ModuleType::Fallback
}
fn source_types(&self, _module_graph: &ModuleGraph) -> &[SourceType] {
&[SourceType::JavaScript]
}
fn source(&self) -> Option<&BoxSource> {
None
}
fn readable_identifier(&self, _context: &Context) -> Cow<'_, str> {
self.readable_identifier.as_str().into()
}
fn lib_ident(&self, _options: LibIdentOptions) -> Option<Cow<'_, str>> {
None
}
fn chunk_condition(&self, _chunk: &ChunkUkey, _compilation: &Compilation) -> Option<bool> {
None
}
async fn code_generation(
&self,
_code_generation_context: &mut ModuleCodeGenerationContext,
) -> Result<CodeGenerationResultBuilder> {
Ok(CodeGenerationResultBuilder::default())
}
async fn get_runtime_hash(
&self,
compilation: &Compilation,
_runtime: Option<&RuntimeSpec>,
) -> Result<RspackHashDigest> {
Ok(RspackHashDigest::new(
&[],
&compilation.options.output.hash_digest,
))
}
async fn build(
self: Box<Self>,
_build_context: BuildContext,
_compilation: Option<&Compilation>,
) -> Result<BoxModule> {
Ok(BoxModule::new(self))
}
}
impl_empty_diagnosable_trait!(SelfModule);