Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 9, 2024
1 parent cac16e1 commit b3509cd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 46 deletions.
9 changes: 8 additions & 1 deletion crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,14 @@ impl Rspack {
}

fn cleanup_last_compilation(&self, compilation: &Compilation) {
JsCompilationWrapper::cleanup_last_compilation(compilation.id());
let compilation_id = compilation.id();

JsCompilationWrapper::cleanup_last_compilation(compilation_id);
JsModuleWrapper::cleanup_last_compilation(compilation_id);
JsChunkWrapper::cleanup_last_compilation(compilation_id);
JsChunkGroupWrapper::cleanup_last_compilation(compilation_id);
JsDependencyWrapper::cleanup_last_compilation(compilation_id);
JsDependenciesBlockWrapper::cleanup_last_compilation(compilation_id);
}
}

Expand Down
23 changes: 11 additions & 12 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rspack_binding_values::{
JsAfterEmitData, JsAfterResolveData, JsAfterResolveOutput, JsAfterTemplateExecutionData,
JsAlterAssetTagGroupsData, JsAlterAssetTagsData, JsAssetEmittedArgs,
JsBeforeAssetTagGenerationData, JsBeforeEmitData, JsBeforeResolveArgs, JsBeforeResolveOutput,
JsChunk, JsChunkAssetArgs, JsChunkWrapper, JsCompilationWrapper,
JsChunkAssetArgs, JsChunkWrapper, JsCompilationWrapper,
JsContextModuleFactoryAfterResolveDataWrapper, JsContextModuleFactoryAfterResolveResult,
JsContextModuleFactoryBeforeResolveDataWrapper, JsContextModuleFactoryBeforeResolveResult,
JsCreateData, JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, JsModuleWrapper,
Expand Down Expand Up @@ -492,7 +492,7 @@ pub struct RegisterJsTaps {
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsChunk) => Buffer); stage: number; }>"
)]
pub register_compilation_chunk_hash_taps: RegisterFunction<JsChunk, Buffer>,
pub register_compilation_chunk_hash_taps: RegisterFunction<JsChunkWrapper, Buffer>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsChunkAssetArgs) => void); stage: number; }>"
)]
Expand Down Expand Up @@ -558,7 +558,7 @@ pub struct RegisterJsTaps {
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsChunk) => Buffer); stage: number; }>"
)]
pub register_javascript_modules_chunk_hash_taps: RegisterFunction<JsChunk, Buffer>,
pub register_javascript_modules_chunk_hash_taps: RegisterFunction<JsChunkWrapper, Buffer>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsBeforeAssetTagGenerationData) => JsBeforeAssetTagGenerationData); stage: number; }>"
)]
Expand Down Expand Up @@ -756,7 +756,7 @@ define_register!(
);
define_register!(
RegisterCompilationChunkHashTaps,
tap = CompilationChunkHashTap<JsChunk, Buffer> @ CompilationChunkHashHook,
tap = CompilationChunkHashTap<JsChunkWrapper, Buffer> @ CompilationChunkHashHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::CompilationChunkHash,
Expand Down Expand Up @@ -874,7 +874,7 @@ define_register!(
/* JavascriptModules Hooks */
define_register!(
RegisterJavascriptModulesChunkHashTaps,
tap = JavascriptModulesChunkHashTap<JsChunk, Buffer> @ JavascriptModulesChunkHashHook,
tap = JavascriptModulesChunkHashTap<JsChunkWrapper, Buffer> @ JavascriptModulesChunkHashHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::JavascriptModulesChunkHash,
Expand Down Expand Up @@ -1192,7 +1192,7 @@ impl CompilationAdditionalTreeRuntimeRequirements
runtime_requirements: &mut RuntimeGlobals,
) -> rspack_error::Result<()> {
let arg = JsAdditionalTreeRuntimeRequirementsArg {
chunk: JsChunkWrapper::new(chunk_ukey, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
runtime_requirements: JsRuntimeGlobals::from(*runtime_requirements),
};
let result = self.function.call_with_sync(arg).await?;
Expand All @@ -1218,7 +1218,7 @@ impl CompilationRuntimeRequirementInTree for CompilationRuntimeRequirementInTree
runtime_requirements_mut: &mut RuntimeGlobals,
) -> rspack_error::Result<Option<()>> {
let arg = JsRuntimeRequirementInTreeArg {
chunk: JsChunkWrapper::new(chunk_ukey, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
runtime_requirements: JsRuntimeGlobals::from(*all_runtime_requirements),
};
let result = self.function.blocking_call_with_sync(arg)?;
Expand Down Expand Up @@ -1248,7 +1248,6 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
let Some(module) = compilation.runtime_modules.get(m) else {
return Ok(());
};
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let arg = JsRuntimeModuleArg {
module: JsRuntimeModule {
source: Some(
Expand All @@ -1265,7 +1264,7 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
.cow_replace("webpack/runtime/", "")
.into_owned(),
},
chunk: JsChunkWrapper::new(chunk_ukey, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
};
if let Some(module) = self.function.call_with_sync(arg).await?
&& let Some(source) = module.source
Expand Down Expand Up @@ -1294,7 +1293,7 @@ impl CompilationChunkHash for CompilationChunkHashTap {
) -> rspack_error::Result<()> {
let result = self
.function
.call_with_sync(JsChunkWrapper::new(chunk_ukey, compilation))
.call_with_sync(JsChunkWrapper::new(*chunk_ukey, compilation))
.await?;
result.hash(hasher);
Ok(())
Expand All @@ -1316,7 +1315,7 @@ impl CompilationChunkAsset for CompilationChunkAssetTap {
self
.function
.call_with_sync(JsChunkAssetArgs {
chunk: JsChunkWrapper::new(chunk_ukey, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
filename: file.to_string(),
})
.await
Expand Down Expand Up @@ -1660,7 +1659,7 @@ impl JavascriptModulesChunkHash for JavascriptModulesChunkHashTap {
) -> rspack_error::Result<()> {
let result = self
.function
.call_with_sync(JsChunkWrapper::new(chunk_ukey, compilation))
.call_with_sync(JsChunkWrapper::new(*chunk_ukey, compilation))
.await?;
result.hash(hasher);
Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use derivative::Derivative;
use napi::Either;
use napi_derive::napi;
use rspack_binding_values::{into_asset_conditions, JsChunk, RawAssetConditions};
use rspack_binding_values::{into_asset_conditions, JsChunkWrapper, RawAssetConditions};
use rspack_collections::DatabaseItem;
use rspack_error::Result;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_banner::{BannerContent, BannerContentFnCtx, BannerPluginOptions};

#[napi(object, object_from_js = false)]
pub struct RawBannerContentFnCtx {
pub struct JsBannerContentFnCtx {
pub hash: String,
#[napi(js_name = "JsChunk")]
pub chunk: JsChunkWrapper,
pub filename: String,
}

impl<'a> From<BannerContentFnCtx<'a>> for RawBannerContentFnCtx {
impl<'a> From<BannerContentFnCtx<'a>> for JsBannerContentFnCtx {
fn from(value: BannerContentFnCtx) -> Self {
Self {
hash: value.hash.to_string(),
Expand All @@ -24,7 +25,7 @@ impl<'a> From<BannerContentFnCtx<'a>> for RawBannerContentFnCtx {
}
}

type RawBannerContent = Either<String, ThreadsafeFunction<RawBannerContentFnCtx, String>>;
type RawBannerContent = Either<String, ThreadsafeFunction<JsBannerContentFnCtx, String>>;
struct RawBannerContentWrapper(RawBannerContent);

impl TryFrom<RawBannerContentWrapper> for BannerContent {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;

use napi::{bindgen_prelude::Either3, JsString};
use rspack_binding_values::JsChunk;
use rspack_binding_values::JsChunkWrapper;
use rspack_collections::DatabaseItem;
use rspack_napi::string::JsStringExt;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_regex::RspackRegex;

pub type Chunks = Either3<RspackRegex, JsString, ThreadsafeFunction<JsChunk, bool>>;
pub type Chunks = Either3<RspackRegex, JsString, ThreadsafeFunction<JsChunkWrapper, bool>>;

pub fn create_chunks_filter(raw: Chunks) -> rspack_plugin_split_chunks::ChunkFilter {
use pollster::block_on;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ use std::sync::Arc;

use napi::bindgen_prelude::Either3;
use napi_derive::napi;
use rspack_binding_values::{JsChunk, JsModuleWrapper};
use rspack_binding_values::{JsChunkWrapper, JsModuleWrapper};
use rspack_collections::DatabaseItem;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_split_chunks::{ChunkNameGetter, ChunkNameGetterFnCtx};

pub(super) type RawChunkOptionName =
Either3<String, bool, ThreadsafeFunction<RawChunkOptionNameCtx, Option<String>>>;
Either3<String, bool, ThreadsafeFunction<JsChunkOptionNameCtx, Option<String>>>;

#[inline]
pub(super) fn default_chunk_option_name() -> ChunkNameGetter {
ChunkNameGetter::Disabled
}

#[napi(object, object_from_js = false)]
pub struct RawChunkOptionNameCtx {
pub struct JsChunkOptionNameCtx {
#[napi(ts_type = "JsModule")]
pub module: JsModuleWrapper,
pub chunks: Vec<JsChunk>,
#[napi(ts_type = "JsChunk[]")]
pub chunks: Vec<JsChunkWrapper>,
pub cache_group_key: String,
}

impl<'a> From<ChunkNameGetterFnCtx<'a>> for RawChunkOptionNameCtx {
impl<'a> From<ChunkNameGetterFnCtx<'a>> for JsChunkOptionNameCtx {
fn from(value: ChunkNameGetterFnCtx<'a>) -> Self {
RawChunkOptionNameCtx {
JsChunkOptionNameCtx {
module: JsModuleWrapper::new(
value.module,
value.compilation.id(),
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_binding_values/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rspack_napi::OneShotRef;
#[napi]
pub struct JsChunk {
pub(crate) chunk_ukey: ChunkUkey,
compilation_id: CompilationId,
compilation: NonNull<Compilation>,
}

Expand Down Expand Up @@ -197,6 +196,8 @@ pub struct JsChunkWrapper {
compilation: NonNull<Compilation>,
}

unsafe impl Send for JsChunkWrapper {}

impl JsChunkWrapper {
pub fn new(chunk_ukey: ChunkUkey, compilation: &Compilation) -> Self {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
Expand Down Expand Up @@ -240,7 +241,6 @@ impl ToNapiValue for JsChunkWrapper {
std::collections::hash_map::Entry::Vacant(entry) => {
let js_module = JsChunk {
chunk_ukey: val.chunk_ukey,
compilation_id: val.compilation_id,
compilation: val.compilation,
};
let r = entry.insert(OneShotRef::new(env, js_module)?);
Expand Down
36 changes: 18 additions & 18 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use rspack_plugin_runtime::RuntimeModuleFromJs;
use super::{JsFilename, PathWithInfo};
use crate::utils::callbackify;
use crate::JsAddingRuntimeModule;
use crate::JsChunkGroupWrapper;
use crate::JsChunkWrapper;
use crate::JsCompatSource;
use crate::JsModuleGraph;
use crate::JsModuleWrapper;
use crate::JsStatsOptimizationBailout;
use crate::LocalJsFilename;
use crate::ToJsCompatSource;
use crate::{JsAsset, JsAssetInfo, JsChunkGroup, JsPathData, JsStats};
use crate::{JsAsset, JsAssetInfo, JsPathData, JsStats};
use crate::{JsRspackDiagnostic, JsRspackError};

#[napi]
Expand Down Expand Up @@ -258,15 +259,15 @@ impl JsCompilation {
)
}

#[napi]
pub fn get_named_chunk_group(&self, name: String) -> Result<Option<JsChunkGroup>> {
#[napi(ts_return_type = "JsChunkGroup")]
pub fn get_named_chunk_group(&self, name: String) -> Result<Option<JsChunkGroupWrapper>> {
let compilation = self.as_ref()?;
Ok(compilation.named_chunk_groups.get(&name).and_then(|c| {
Ok(
compilation
.chunk_group_by_ukey
.get(c)
.map(|cg| JsChunkGroup::from_chunk_group(cg, compilation))
}))
.named_chunk_groups
.get(&name)
.map(|ukey| JsChunkGroupWrapper::new(*ukey, compilation)),
)
}

#[napi]
Expand Down Expand Up @@ -371,8 +372,8 @@ impl JsCompilation {
Ok(())
}

#[napi(getter)]
pub fn entrypoints(&self) -> Result<HashMap<String, JsChunkGroup>> {
#[napi(getter, ts_return_type = "Record<string, JsChunkGroup>")]
pub fn entrypoints(&self) -> Result<HashMap<&String, JsChunkGroupWrapper>> {
let compilation = self.as_ref()?;

Ok(
Expand All @@ -381,24 +382,24 @@ impl JsCompilation {
.iter()
.map(|(n, _)| {
(
n.clone(),
JsChunkGroup::from_chunk_group(compilation.entrypoint_by_name(n), compilation),
n,
JsChunkGroupWrapper::new(compilation.entrypoint_by_name(n).ukey, compilation),
)
})
.collect(),
)
}

#[napi(getter)]
pub fn chunk_groups(&self) -> Result<Vec<JsChunkGroup>> {
#[napi(getter, ts_return_type = "JsChunkGroup")]
pub fn chunk_groups(&self) -> Result<Vec<JsChunkGroupWrapper>> {
let compilation = self.as_ref()?;

Ok(
compilation
.chunk_group_by_ukey
.values()
.map(|cg| JsChunkGroup::from_chunk_group(cg, compilation))
.collect::<Vec<JsChunkGroup>>(),
.keys()
.map(|ukey| JsChunkGroupWrapper::new(*ukey, compilation))
.collect::<Vec<JsChunkGroupWrapper>>(),
)
}

Expand Down Expand Up @@ -740,7 +741,6 @@ impl JsCompilationWrapper {
let mut refs = ref_cell.borrow_mut();
refs.remove(&compilation_id);
});
JsModuleWrapper::cleanup_last_compilation(compilation_id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hash::FxHashMap as HashMap;

use super::JsCompatSourceOwned;
use crate::{
JsChunk, JsChunkWrapper, JsCodegenerationResults, JsCompatSource, JsDependenciesBlockWrapper,
JsChunkWrapper, JsCodegenerationResults, JsCompatSource, JsDependenciesBlockWrapper,
JsDependencyWrapper, ToJsCompatSource,
};

Expand Down

0 comments on commit b3509cd

Please sign in to comment.