Skip to content
Open
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
1 change: 1 addition & 0 deletions crates/node_binding/napi-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export declare class ChunkGraph {
getModuleId(module: Module): string | number | null
_getModuleHash(module: Module, runtime: string | string[] | undefined): string | null
getBlockChunkGroup(jsBlock: AsyncDependenciesBlock): ChunkGroup | null
getChunkGroupBlocks(chunkGroup: ChunkGroup): AsyncDependenciesBlock[]
}

export declare class ChunkGroup {
Expand Down
29 changes: 27 additions & 2 deletions crates/rspack_binding_api/src/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use napi_derive::napi;
use rspack_core::{Compilation, ModuleId, SourceType};

use crate::{
async_dependency_block::AsyncDependenciesBlock,
async_dependency_block::{AsyncDependenciesBlock, AsyncDependenciesBlockWrapper},
chunk::{Chunk, ChunkWrapper},
chunk_group::ChunkGroupWrapper,
chunk_group::{ChunkGroup, ChunkGroupWrapper},
module::{ModuleObject, ModuleObjectRef},
runtime::JsRuntimeSpec,
};
Expand Down Expand Up @@ -210,4 +210,29 @@ impl ChunkGraph {
.map(|chunk_group| ChunkGroupWrapper::new(chunk_group.ukey, compilation)),
)
}

#[napi(
ts_args_type = "chunkGroup: ChunkGroup",
ts_return_type = "AsyncDependenciesBlock[]"
)]
pub fn get_chunk_group_blocks(
&self,
chunk_group: &ChunkGroup,
) -> napi::Result<Vec<AsyncDependenciesBlockWrapper>> {
let compilation = self.as_ref()?;
let module_graph = compilation.get_module_graph();

Ok(
compilation
.chunk_graph
.get_chunk_group_blocks(chunk_group.chunk_group_ukey)
.into_iter()
.filter_map(|block_id| {
module_graph
.block_by_id(&block_id)
.map(|block| AsyncDependenciesBlockWrapper::new(block, compilation))
})
.collect(),
)
}
}
2 changes: 1 addition & 1 deletion crates/rspack_binding_api/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{

#[napi]
pub struct ChunkGroup {
chunk_group_ukey: rspack_core::ChunkGroupUkey,
pub(crate) chunk_group_ukey: rspack_core::ChunkGroupUkey,
compilation: NonNull<Compilation>,
}

Expand Down
17 changes: 17 additions & 0 deletions crates/rspack_core/src/chunk_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ impl ChunkGraph {
let cgm = self.expect_chunk_graph_module(*module_id);
!cgm.entry_in_chunks.is_empty()
}

pub fn get_chunk_group_blocks(
&self,
chunk_group_ukey: ChunkGroupUkey,
) -> Vec<AsyncDependenciesBlockIdentifier> {
self
.block_to_chunk_group_ukey
.iter()
.filter_map(|(block_id, group_ukey)| {
if *group_ukey == chunk_group_ukey {
Some(*block_id)
} else {
None
}
})
.collect()
}
}
static INDENT: &str = " ";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import(/* webpackChunkName: "foo-blocks" */ "./foo");
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Plugin {
apply(compiler) {
compiler.hooks.compilation.tap("Test", compilation => {
compilation.hooks.processAssets.tap("Test", () => {
const entry = compilation.entries.get("main");
const entryDependency = entry.dependencies[0];
const entryModule = compilation.moduleGraph.getModule(entryDependency);
const block = entryModule.blocks[0];
const chunkGroup = compilation.chunkGraph.getBlockChunkGroup(block);

expect(chunkGroup.name).toBe("foo-blocks");

const blocks = compilation.chunkGraph.getChunkGroupBlocks(chunkGroup);
expect(blocks.length).toBe(1);
const blockFromGroup = blocks[0];
expect(blockFromGroup).toBe(block);

const blockDependencies = block.dependencies;
expect(blockDependencies.length).toBe(1);
expect(blockDependencies[0].request).toBe("./foo");

const groupBlockDependencies = blockFromGroup.dependencies;
expect(groupBlockDependencies.length).toBe(1);
expect(groupBlockDependencies[0]).toBe(blockDependencies[0]);
});
});
}
}

/** @type {import("@rspack/core").Configuration} */
module.exports = {
target: "web",
node: false,
entry: {
main: "./index.js"
},
output: {
filename: "[name].js"
},
optimization: {
sideEffects: false
},
plugins: [new Plugin()]
};
Loading