Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ pub struct TempModule {
impl TempModule {
pub fn transform_from(module: OwnedOrRef<BoxModule>) -> OwnedOrRef<BoxModule> {
let m = module.as_ref();
OwnedOrRef::Owned(Box::new(Self {
OwnedOrRef::Owned(BoxModule::new(Box::new(Self {
id: m.identifier(),
build_info: m.build_info().clone(),
build_meta: m.build_meta().clone(),
dependencies: m.get_dependencies().to_vec(),
// clean all of blocks
blocks: vec![],
}))
})))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rspack_error::Result;

use super::{TaskContext, build::BuildTask, lazy::ProcessUnlazyDependenciesTask};
use crate::{
BoxDependency, Module, ModuleIdentifier, ModuleProfile,
BoxDependency, BoxModule, ModuleIdentifier, ModuleProfile,
compilation::make::ForwardedIdSet,
module_graph::{ModuleGraph, ModuleGraphModule},
utils::task_loop::{Task, TaskResult, TaskType},
Expand All @@ -11,7 +11,7 @@ use crate::{
#[derive(Debug)]
pub struct AddTask {
pub original_module_identifier: Option<ModuleIdentifier>,
pub module: Box<dyn Module>,
pub module: BoxModule,
pub module_graph_module: Box<ModuleGraphModule>,
pub dependencies: Vec<BoxDependency>,
pub current_profile: Option<ModuleProfile>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use super::{
TaskContext, lazy::ProcessUnlazyDependenciesTask, process_dependencies::ProcessDependenciesTask,
};
use crate::{
AsyncDependenciesBlock, BoxDependency, BuildContext, BuildResult, CompilationId, CompilerId,
CompilerOptions, DependencyParents, Module, ModuleProfile, ResolverFactory, SharedPluginDriver,
AsyncDependenciesBlock, BoxDependency, BoxModule, BuildContext, BuildResult, CompilationId,
CompilerId, CompilerOptions, DependencyParents, ModuleProfile, ResolverFactory,
SharedPluginDriver,
compilation::make::{ForwardedIdSet, HasLazyDependencies, LazyDependencies},
utils::{
ResourceId,
Expand All @@ -20,7 +21,7 @@ use crate::{
pub struct BuildTask {
pub compiler_id: CompilerId,
pub compilation_id: CompilationId,
pub module: Box<dyn Module>,
pub module: BoxModule,
pub current_profile: Option<ModuleProfile>,
pub resolver_factory: Arc<ResolverFactory>,
pub compiler_options: Arc<CompilerOptions>,
Expand Down Expand Up @@ -88,7 +89,7 @@ impl Task<TaskContext> for BuildTask {

#[derive(Debug)]
struct BuildResultTask {
pub module: Box<dyn Module>,
pub module: BoxModule,
pub build_result: Box<BuildResult>,
pub plugin_driver: SharedPluginDriver,
pub current_profile: Option<ModuleProfile>,
Expand Down
10 changes: 4 additions & 6 deletions crates/rspack_core/src/context_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ impl ContextModuleFactory {
context_options: dependency.options().clone(),
type_prefix: dependency.type_prefix(),
};
let module = Box::new(ContextModule::new(
self.resolve_dependencies.clone(),
options.clone(),
));
let module = ContextModule::new(self.resolve_dependencies.clone(), options.clone()).boxed();
(module, Some(options))
}
Ok(ResolveResult::Ignored) => {
Expand Down Expand Up @@ -383,9 +380,10 @@ impl ContextModuleFactory {
let module = ContextModule::new(
after_resolve_data.resolve_dependencies,
context_module_options.clone(),
);
)
.boxed();

Ok(Some(ModuleFactoryResult::new_with_module(Box::new(module))))
Ok(Some(ModuleFactoryResult::new_with_module(module)))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ fn get_outgoing_async_modules(
helper(
compilation,
mg,
&**mg
.module_by_identifier(&module)
.expect("should have module"),
mg.module_by_identifier(&module)
.expect("should have module")
.as_ref(),
set,
visited,
);
Expand Down Expand Up @@ -547,7 +547,7 @@ pub fn import_statement(
);

if phase.is_defer() && !target_module.build_meta().has_top_level_await {
let async_deps = get_outgoing_async_modules(compilation, &**target_module);
let async_deps = get_outgoing_async_modules(compilation, target_module.as_ref());
let import_content = format!(
"/* deferred ESM import */{opt_declaration}{import_var} = {};\n",
get_property_accessed_deferred_module(exports_type, &module_id_expr, async_deps)
Expand Down
80 changes: 70 additions & 10 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use async_trait::async_trait;
use json::JsonValue;
use rspack_cacheable::{
cacheable, cacheable_dyn,
with::{AsOption, AsPreset, AsVec},
with::{AsInner, AsInnerConverter, AsOption, AsPreset, AsVec},
};
use rspack_collections::{Identifiable, Identifier, IdentifierMap, IdentifierSet};
use rspack_error::{Diagnosable, Result};
Expand Down Expand Up @@ -522,22 +522,82 @@ pub fn module_update_hash(
}

pub trait ModuleExt {
fn boxed(self) -> Box<dyn Module>;
fn boxed(self) -> BoxModule;
}

impl<T: Module> ModuleExt for T {
fn boxed(self) -> Box<dyn Module> {
Box::new(self)
fn boxed(self) -> BoxModule {
BoxModule(Box::new(self))
}
}

pub type BoxModule = Box<dyn Module>;
/// A newtype wrapper around `Box<dyn Module>` for improved type safety.
#[cacheable(with=AsInner)]
#[repr(transparent)]
pub struct BoxModule(Box<dyn Module>);

impl Identifiable for Box<dyn Module> {
impl BoxModule {
/// Create a new BoxModule from a boxed Module trait object.
pub fn new(module: Box<dyn Module>) -> Self {
BoxModule(module)
}
}

impl AsInnerConverter for BoxModule {
type Inner = Box<dyn Module>;

fn to_inner(&self) -> &Self::Inner {
&self.0
}

fn from_inner(data: Self::Inner) -> Self {
BoxModule(data)
}
}

impl std::ops::Deref for BoxModule {
type Target = Box<dyn Module>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::ops::DerefMut for BoxModule {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl From<Box<dyn Module>> for BoxModule {
fn from(inner: Box<dyn Module>) -> Self {
BoxModule(inner)
}
}

impl AsRef<dyn Module> for BoxModule {
fn as_ref(&self) -> &dyn Module {
self.0.as_ref()
}
}

impl AsMut<dyn Module> for BoxModule {
fn as_mut(&mut self) -> &mut dyn Module {
self.0.as_mut()
}
}

impl Debug for BoxModule {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl Identifiable for BoxModule {
/// Uniquely identify a module. If two modules share the same module identifier, then they are considered as the same module.
/// e.g `javascript/auto|<absolute-path>/index.js` and `javascript/auto|<absolute-path>/index.js` are considered as the same.
fn identifier(&self) -> Identifier {
self.as_ref().identifier()
self.0.as_ref().identifier()
}
}

Expand Down Expand Up @@ -636,7 +696,7 @@ mod test {
use rspack_sources::BoxSource;
use rspack_util::source_map::{ModuleSourceMapConfig, SourceMapKind};

use super::Module;
use super::{BoxModule, Module};
use crate::{
AsyncDependenciesBlockIdentifier, BuildContext, BuildResult, CodeGenerationResult, Compilation,
ConcatenationScope, Context, DependenciesBlock, DependencyId, ModuleExt, ModuleGraph,
Expand Down Expand Up @@ -776,8 +836,8 @@ mod test {

#[test]
fn should_downcast_successfully() {
let a: Box<dyn Module> = ExternalModule(String::from("a")).boxed();
let b: Box<dyn Module> = RawModule(String::from("a")).boxed();
let a: BoxModule = ExternalModule(String::from("a")).boxed();
let b: BoxModule = RawModule(String::from("a")).boxed();

assert!(a.downcast_ref::<ExternalModule>().is_some());
assert!(b.downcast_ref::<RawModule>().is_some());
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/self_module_factory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rspack_error::Result;

use crate::{ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, SelfModule};
use crate::{ModuleExt, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, SelfModule};

#[derive(Debug)]
pub struct SelfModuleFactory;
Expand All @@ -11,8 +11,8 @@ impl ModuleFactory for SelfModuleFactory {
let issuer = data
.issuer_identifier
.expect("self module must have issuer");
Ok(ModuleFactoryResult::new_with_module(Box::new(
SelfModule::new(issuer),
)))
Ok(ModuleFactoryResult::new_with_module(
SelfModule::new(issuer).boxed(),
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ async fn build_module(
module.set_source_map_kind(SourceMapKind::SimpleSourceMap);
}
if self.cheap {
module.set_source_map_kind(*module.get_source_map_kind() | SourceMapKind::Cheap)
let current_kind = *module.get_source_map_kind();
module.set_source_map_kind(current_kind | SourceMapKind::Cheap)
}
Ok(())
}
Expand All @@ -58,7 +59,8 @@ async fn runtime_module(
module.set_source_map_kind(SourceMapKind::SimpleSourceMap);
}
if self.cheap {
module.set_source_map_kind(*module.get_source_map_kind() | SourceMapKind::Cheap)
let current_kind = *module.get_source_map_kind();
module.set_source_map_kind(current_kind | SourceMapKind::Cheap)
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_dll/src/dll_entry/dll_module_factory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use rspack_core::{ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult};
use rspack_core::{ModuleExt, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult};
use rspack_error::Result;

use super::{dll_entry_dependency::DllEntryDependency, dll_module::DllModule};
Expand All @@ -16,7 +16,7 @@ impl ModuleFactory for DllModuleFactory {
.expect("unreachable");

Ok(ModuleFactoryResult {
module: Some(Box::new(DllModule::new(dll_entry_dependency))),
module: Some(DllModule::new(dll_entry_dependency).boxed()),
})
}
}
43 changes: 25 additions & 18 deletions crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rspack_core::{
BoxModule, Compilation, CompilationParams, CompilerCompilation, Context, DependencyType,
LibIdentOptions, ModuleFactoryCreateData, NormalModuleCreateData, NormalModuleFactoryFactorize,
NormalModuleFactoryModule, Plugin,
LibIdentOptions, ModuleExt, ModuleFactoryCreateData, NormalModuleCreateData,
NormalModuleFactoryFactorize, NormalModuleFactoryModule, Plugin,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
Expand Down Expand Up @@ -87,26 +87,32 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result<Option<B
);

if let Some(resolved) = self.options.content.get(&inner_request) {
return Ok(Some(Box::new(DelegatedModule::new(
self.options.source.clone(),
resolved.clone(),
self.options.r#type.clone(),
inner_request,
Some(request.to_owned()),
))));
return Ok(Some(
DelegatedModule::new(
self.options.source.clone(),
resolved.clone(),
self.options.r#type.clone(),
inner_request,
Some(request.to_owned()),
)
.boxed(),
));
}

for extension in self.options.extensions.iter() {
let request_plus_ext = format!("{inner_request}{extension}");

if let Some(resolved) = self.options.content.get(&request_plus_ext) {
return Ok(Some(Box::new(DelegatedModule::new(
self.options.source.clone(),
resolved.clone(),
self.options.r#type.clone(),
request_plus_ext,
format!("{request}{extension}").into(),
))));
return Ok(Some(
DelegatedModule::new(
self.options.source.clone(),
resolved.clone(),
self.options.r#type.clone(),
request_plus_ext,
format!("{request}{extension}").into(),
)
.boxed(),
));
}
}
}
Expand All @@ -132,13 +138,14 @@ async fn nmf_module(
context: &self.options.compilation_context,
});

*module = Box::new(DelegatedModule::new(
*module = DelegatedModule::new(
self.options.source.clone(),
resolved.clone(),
self.options.r#type.clone(),
request.to_string(),
original_request.map(|request| request.to_string()),
));
)
.boxed();
};

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_plugin_extract_css/src/css_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use rspack_collections::{Identifiable, Identifier};
use rspack_core::{
AsyncDependenciesBlockIdentifier, BuildContext, BuildInfo, BuildMeta, BuildResult,
CodeGenerationResult, Compilation, CompilerOptions, ConcatenationScope, DependenciesBlock,
DependencyId, FactoryMeta, Module, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult,
ModuleGraph, ModuleLayer, RuntimeSpec, SourceType, impl_module_meta_info, impl_source_map_config,
module_update_hash, rspack_sources::BoxSource,
DependencyId, FactoryMeta, Module, ModuleExt, ModuleFactory, ModuleFactoryCreateData,
ModuleFactoryResult, ModuleGraph, ModuleLayer, RuntimeSpec, SourceType, impl_module_meta_info,
impl_source_map_config, module_update_hash, rspack_sources::BoxSource,
};
use rspack_error::{Result, impl_empty_diagnosable_trait};
use rspack_hash::{RspackHash, RspackHashDigest};
Expand Down Expand Up @@ -239,9 +239,9 @@ impl ModuleFactory for CssModuleFactory {
.downcast_ref::<CssDependency>()
.expect("unreachable");

Ok(ModuleFactoryResult::new_with_module(Box::new(
CssModule::new(css_dep.clone()),
)))
Ok(ModuleFactoryResult::new_with_module(
CssModule::new(css_dep.clone()).boxed(),
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ impl Dependency for ESMExportImportedSpecifierDependency {
let ids = self.get_ids(module_graph);
if let Some(should_error) = self
.export_presence_mode
.get_effective_export_presence(&**module)
.get_effective_export_presence(module.as_ref())
{
let mut diagnostics = Vec::new();
// don't need to check the import specifier is existed or not when name is None (export *)
Expand Down
Loading
Loading