Skip to content

Commit

Permalink
refactor: chunk graph binding API (#8648)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind authored Dec 11, 2024
1 parent 2338d6b commit 532a15d
Show file tree
Hide file tree
Showing 23 changed files with 1,569 additions and 1,214 deletions.
129 changes: 60 additions & 69 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ export declare class EntryOptionsDto {
}
export type EntryOptionsDTO = EntryOptionsDto

export declare class JsChunk {
get name(): string | undefined
get id(): string | undefined
get ids(): Array<string>
get idNameHints(): Array<string>
get filenameTemplate(): string | undefined
get cssFilenameTemplate(): string | undefined
get files(): Array<string>
get runtime(): Array<string>
get hash(): string | undefined
get contentHash(): Record<string, string>
get renderedHash(): string | undefined
get chunkReason(): string | undefined
get auxiliaryFiles(): Array<string>
isOnlyInitial(): boolean
canBeInitial(): boolean
hasRuntime(): boolean
getAllAsyncChunks(): JsChunk[]
getAllInitialChunks(): JsChunk[]
getAllReferencedChunks(): JsChunk[]
groups(): JsChunkGroup[]
}

export declare class JsChunkGraph {
getChunkModules(chunk: JsChunk): JsModule[]
getChunkEntryModules(chunk: JsChunk): JsModule[]
getChunkEntryDependentChunksIterable(chunk: JsChunk): JsChunk[]
getChunkModulesIterableBySourceType(chunk: JsChunk, sourceType: string): JsModule[]
getModuleChunks(module: JsModule): JsChunk[]
}

export declare class JsChunkGroup {
get chunks(): JsChunk[]
get index(): number | undefined
get name(): string | undefined
get origins(): Array<JsChunkGroupOrigin>
isInitial(): boolean
getParents(): JsChunkGroup[]
getRuntimeChunk(): JsChunk
getFiles(): Array<string>
}

export declare class JsCompilation {
updateAsset(filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: JsAssetInfo | ((assetInfo: JsAssetInfo) => JsAssetInfo)): void
getAssets(): Readonly<JsAsset>[]
Expand All @@ -54,11 +96,11 @@ export declare class JsCompilation {
get modules(): Array<JsModule>
get builtModules(): Array<JsModule>
getOptimizationBailout(): Array<JsStatsOptimizationBailout>
getChunks(): Array<JsChunk>
getChunks(): JsChunk[]
getNamedChunkKeys(): Array<string>
getNamedChunk(name: string): JsChunk | null
getNamedChunk(name: string): JsChunk
getNamedChunkGroupKeys(): Array<string>
getNamedChunkGroup(name: string): JsChunkGroup | null
getNamedChunkGroup(name: string): JsChunkGroup
setAssetSource(name: string, source: JsCompatSource): void
deleteAssetSource(name: string): void
getAssetFilenames(): Array<string>
Expand All @@ -68,7 +110,7 @@ export declare class JsCompilation {
deleteAsset(filename: string): void
renameAsset(filename: string, newName: string): void
get entrypoints(): Record<string, JsChunkGroup>
get chunkGroups(): Array<JsChunkGroup>
get chunkGroups(): JsChunkGroup[]
get hash(): string | null
dependencies(): JsDependencies
pushDiagnostic(diagnostic: JsRspackDiagnostic): void
Expand All @@ -94,8 +136,9 @@ export declare class JsCompilation {
rebuildModule(moduleIdentifiers: Array<string>, f: any): void
importModule(request: string, layer: string | undefined | null, publicPath: JsFilename | undefined | null, baseUri: string | undefined | null, originalModule: string | undefined | null, originalModuleContext: string | undefined | null, callback: any): void
get entries(): JsEntries
addRuntimeModule(chunkUkey: number, runtimeModule: JsAddingRuntimeModule): void
addRuntimeModule(chunk: JsChunk, runtimeModule: JsAddingRuntimeModule): void
get moduleGraph(): JsModuleGraph
get chunkGraph(): JsChunkGraph
}

export declare class JsContextModuleFactoryAfterResolveData {
Expand Down Expand Up @@ -219,30 +262,6 @@ export declare class Rspack {
rebuild(changed_files: string[], removed_files: string[], callback: (err: null | Error) => void): void
}

export declare function __chunk_graph_inner_get_chunk_entry_dependent_chunks_iterable(jsChunkUkey: number, jsCompilation: JsCompilation): Array<JsChunk>

export declare function __chunk_graph_inner_get_chunk_entry_modules(jsChunkUkey: number, jsCompilation: JsCompilation): JsModule[]

export declare function __chunk_graph_inner_get_chunk_modules(jsChunkUkey: number, jsCompilation: JsCompilation): JsModule[]

export declare function __chunk_graph_inner_get_chunk_modules_iterable_by_source_type(jsChunkUkey: number, sourceType: string, jsCompilation: JsCompilation): JsModule[]

export declare function __chunk_group_inner_get_chunk_group(ukey: number, jsCompilation: JsCompilation): JsChunkGroup

export declare function __chunk_inner_can_be_initial(jsChunkUkey: number, jsCompilation: JsCompilation): boolean

export declare function __chunk_inner_get_all_async_chunks(jsChunkUkey: number, jsCompilation: JsCompilation): Array<JsChunk>

export declare function __chunk_inner_get_all_initial_chunks(jsChunkUkey: number, jsCompilation: JsCompilation): Array<JsChunk>

export declare function __chunk_inner_get_all_referenced_chunks(jsChunkUkey: number, jsCompilation: JsCompilation): Array<JsChunk>

export declare function __chunk_inner_has_runtime(jsChunkUkey: number, jsCompilation: JsCompilation): boolean

export declare function __chunk_inner_is_only_initial(jsChunkUkey: number, jsCompilation: JsCompilation): boolean

export declare function __entrypoint_inner_get_runtime_chunk(ukey: number, jsCompilation: JsCompilation): JsChunk

export interface BuiltinPlugin {
name: BuiltinPluginName
options: unknown
Expand Down Expand Up @@ -444,6 +463,12 @@ export interface JsAssetInfoRelated {
sourceMap?: string
}

export interface JsBannerContentFnCtx {
hash: string
chunk: JsChunk
filename: string
}

export interface JsBeforeAssetTagGenerationData {
assets: JsHtmlPluginAssets
outputName: string
Expand Down Expand Up @@ -485,44 +510,22 @@ export interface JsCacheGroupTestCtx {
module: JsModule
}

export interface JsChunk {
__inner_ukey: number
__inner_groups: Array<number>
name?: string
id?: string
ids: Array<string>
idNameHints: Array<string>
filenameTemplate?: string
cssFilenameTemplate?: string
files: Array<string>
runtime: Array<string>
hash?: string
contentHash: Record<string, string>
renderedHash?: string
chunkReason?: string
auxiliaryFiles: Array<string>
}

export interface JsChunkAssetArgs {
chunk: JsChunk
filename: string
}

export interface JsChunkGroup {
__inner_parents: Array<number>
__inner_ukey: number
chunks: Array<JsChunk>
index?: number
name?: string
isInitial: boolean
origins: Array<JsChunkGroupOrigin>
}

export interface JsChunkGroupOrigin {
module?: JsModule | undefined
request?: string
}

export interface JsChunkOptionNameCtx {
module: JsModule
chunks: JsChunk[]
cacheGroupKey: string
}

/**
* File clean options
*
Expand Down Expand Up @@ -1138,12 +1141,6 @@ export interface RawAssetResourceGeneratorOptions {
publicPath?: "auto" | JsFilename
}

export interface RawBannerContentFnCtx {
hash: string
chunk: JsChunk
filename: string
}

export interface RawBannerPluginOptions {
banner: string | ((...args: any[]) => any)
entryOnly?: boolean
Expand Down Expand Up @@ -1197,12 +1194,6 @@ export interface RawCacheOptions {
version: string
}

export interface RawChunkOptionNameCtx {
module: JsModule
chunks: Array<JsChunk>
cacheGroupKey: string
}

export interface RawConsumeOptions {
key: string
import?: string
Expand Down
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
42 changes: 18 additions & 24 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use rspack_binding_values::{
JsAfterEmitData, JsAfterResolveData, JsAfterResolveOutput, JsAfterTemplateExecutionData,
JsAlterAssetTagGroupsData, JsAlterAssetTagsData, JsAssetEmittedArgs,
JsBeforeAssetTagGenerationData, JsBeforeEmitData, JsBeforeResolveArgs, JsBeforeResolveOutput,
JsChunk, JsChunkAssetArgs, JsCompilationWrapper, JsContextModuleFactoryAfterResolveDataWrapper,
JsContextModuleFactoryAfterResolveResult, JsContextModuleFactoryBeforeResolveDataWrapper,
JsContextModuleFactoryBeforeResolveResult, JsCreateData, JsExecuteModuleArg, JsFactorizeArgs,
JsFactorizeOutput, JsModuleWrapper, JsNormalModuleFactoryCreateModuleArgs, JsResolveArgs,
JsResolveForSchemeArgs, JsResolveForSchemeOutput, JsResolveOutput, JsRuntimeGlobals,
JsRuntimeModule, JsRuntimeModuleArg, JsRuntimeRequirementInTreeArg,
JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned,
JsChunkAssetArgs, JsChunkWrapper, JsCompilationWrapper,
JsContextModuleFactoryAfterResolveDataWrapper, JsContextModuleFactoryAfterResolveResult,
JsContextModuleFactoryBeforeResolveDataWrapper, JsContextModuleFactoryBeforeResolveResult,
JsCreateData, JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, JsModuleWrapper,
JsNormalModuleFactoryCreateModuleArgs, JsResolveArgs, JsResolveForSchemeArgs,
JsResolveForSchemeOutput, JsResolveOutput, JsRuntimeGlobals, JsRuntimeModule, JsRuntimeModuleArg,
JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned,
};
use rspack_collections::IdentifierSet;
use rspack_core::{
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 @@ -1191,9 +1191,8 @@ impl CompilationAdditionalTreeRuntimeRequirements
chunk_ukey: &ChunkUkey,
runtime_requirements: &mut RuntimeGlobals,
) -> rspack_error::Result<()> {
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let arg = JsAdditionalTreeRuntimeRequirementsArg {
chunk: JsChunk::from(chunk, 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,9 +1217,8 @@ impl CompilationRuntimeRequirementInTree for CompilationRuntimeRequirementInTree
_runtime_requirements: &RuntimeGlobals,
runtime_requirements_mut: &mut RuntimeGlobals,
) -> rspack_error::Result<Option<()>> {
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let arg = JsRuntimeRequirementInTreeArg {
chunk: JsChunk::from(chunk, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
runtime_requirements: JsRuntimeGlobals::from(*all_runtime_requirements),
};
let result = self.function.blocking_call_with_sync(arg)?;
Expand All @@ -1245,12 +1243,11 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
&self,
compilation: &mut Compilation,
m: &ModuleIdentifier,
c: &ChunkUkey,
chunk_ukey: &ChunkUkey,
) -> rspack_error::Result<()> {
let Some(module) = compilation.runtime_modules.get(m) else {
return Ok(());
};
let chunk = compilation.chunk_by_ukey.expect_get(c);
let arg = JsRuntimeModuleArg {
module: JsRuntimeModule {
source: Some(
Expand All @@ -1267,7 +1264,7 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
.cow_replace("webpack/runtime/", "")
.into_owned(),
},
chunk: JsChunk::from(chunk, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
};
if let Some(module) = self.function.call_with_sync(arg).await?
&& let Some(source) = module.source
Expand All @@ -1294,10 +1291,9 @@ impl CompilationChunkHash for CompilationChunkHashTap {
chunk_ukey: &ChunkUkey,
hasher: &mut RspackHash,
) -> rspack_error::Result<()> {
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let result = self
.function
.call_with_sync(JsChunk::from(chunk, compilation))
.call_with_sync(JsChunkWrapper::new(*chunk_ukey, compilation))
.await?;
result.hash(hasher);
Ok(())
Expand All @@ -1316,11 +1312,10 @@ impl CompilationChunkAsset for CompilationChunkAssetTap {
chunk_ukey: &ChunkUkey,
file: &str,
) -> rspack_error::Result<()> {
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
self
.function
.call_with_sync(JsChunkAssetArgs {
chunk: JsChunk::from(chunk, compilation),
chunk: JsChunkWrapper::new(*chunk_ukey, compilation),
filename: file.to_string(),
})
.await
Expand Down Expand Up @@ -1662,10 +1657,9 @@ impl JavascriptModulesChunkHash for JavascriptModulesChunkHashTap {
chunk_ukey: &ChunkUkey,
hasher: &mut RspackHash,
) -> rspack_error::Result<()> {
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let result = self
.function
.call_with_sync(JsChunk::from(chunk, 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,29 +1,31 @@
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)]
pub struct RawBannerContentFnCtx {
#[napi(object, object_from_js = false)]
pub struct JsBannerContentFnCtx {
pub hash: String,
pub chunk: JsChunk,
#[napi(ts_type = "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(),
chunk: JsChunk::from(value.chunk, value.compilation),
chunk: JsChunkWrapper::new(value.chunk.ukey(), value.compilation),
filename: value.filename.to_string(),
}
}
}

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

impl TryFrom<RawBannerContentWrapper> for BannerContent {
Expand Down
Loading

2 comments on commit 532a15d

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rsdoctor ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-12-11 6a5b766) Current Change
10000_big_production-mode_disable-minimize + exec 37.6 s ± 447 ms 37.9 s ± 720 ms +1.02 %
10000_development-mode + exec 1.79 s ± 22 ms 1.78 s ± 26 ms -0.85 %
10000_development-mode_hmr + exec 659 ms ± 21 ms 653 ms ± 23 ms -0.92 %
10000_production-mode + exec 2.33 s ± 29 ms 2.37 s ± 55 ms +1.68 %
arco-pro_development-mode + exec 1.76 s ± 82 ms 1.72 s ± 55 ms -2.12 %
arco-pro_development-mode_hmr + exec 424 ms ± 1.1 ms 425 ms ± 1.4 ms +0.27 %
arco-pro_production-mode + exec 3.13 s ± 79 ms 3.12 s ± 57 ms -0.41 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.15 s ± 56 ms 3.15 s ± 83 ms +0.21 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.11 s ± 78 ms 3.13 s ± 71 ms +0.90 %
threejs_development-mode_10x + exec 1.61 s ± 18 ms 1.62 s ± 19 ms +0.23 %
threejs_development-mode_10x_hmr + exec 785 ms ± 21 ms 790 ms ± 3.3 ms +0.59 %
threejs_production-mode_10x + exec 4.87 s ± 23 ms 4.87 s ± 31 ms -0.04 %
10000_big_production-mode_disable-minimize + rss memory 9699 MiB ± 36.6 MiB 9737 MiB ± 42.2 MiB +0.40 %
10000_development-mode + rss memory 836 MiB ± 66.1 MiB 812 MiB ± 22.9 MiB -2.80 %
10000_development-mode_hmr + rss memory 1925 MiB ± 184 MiB 2007 MiB ± 155 MiB +4.28 %
10000_production-mode + rss memory 712 MiB ± 54.4 MiB 742 MiB ± 38.9 MiB +4.31 %
arco-pro_development-mode + rss memory 721 MiB ± 27.3 MiB 730 MiB ± 37.1 MiB +1.22 %
arco-pro_development-mode_hmr + rss memory 927 MiB ± 73 MiB 931 MiB ± 96.8 MiB +0.35 %
arco-pro_production-mode + rss memory 829 MiB ± 65.1 MiB 827 MiB ± 45.9 MiB -0.18 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 801 MiB ± 26.3 MiB 808 MiB ± 56 MiB +0.92 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 832 MiB ± 51.2 MiB 826 MiB ± 60.9 MiB -0.72 %
threejs_development-mode_10x + rss memory 768 MiB ± 34.8 MiB 773 MiB ± 19.9 MiB +0.65 %
threejs_development-mode_10x_hmr + rss memory 1585 MiB ± 286 MiB 1703 MiB ± 239 MiB +7.40 %
threejs_production-mode_10x + rss memory 1104 MiB ± 60.4 MiB 1114 MiB ± 81.3 MiB +0.90 %

Please sign in to comment.