Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: entrypoint.getRuntimeChunk #5914

Merged
merged 1 commit into from Mar 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/node_binding/binding.d.ts
Expand Up @@ -121,6 +121,8 @@ export function __chunk_inner_has_runtime(jsChunkUkey: number, compilation: JsCo

export function __chunk_inner_is_only_initial(jsChunkUkey: number, compilation: JsCompilation): boolean

export function __entrypoint_inner_get_runtime_chunk(ukey: number, compilation: JsCompilation): JsChunk

export interface AfterResolveData {
request: string
context: string
Expand Down Expand Up @@ -287,6 +289,7 @@ export interface JsChunkAssetArgs {

export interface JsChunkGroup {
__inner_parents: Array<number>
__inner_ukey: number
chunks: Array<JsChunk>
index?: number
name?: string
Expand Down
12 changes: 12 additions & 0 deletions crates/rspack_binding_values/src/chunk_group.rs
Expand Up @@ -7,6 +7,8 @@ use crate::{JsChunk, JsCompilation};
pub struct JsChunkGroup {
#[napi(js_name = "__inner_parents")]
pub inner_parents: Vec<u32>,
#[napi(js_name = "__inner_ukey")]
pub inner_ukey: u32,
pub chunks: Vec<JsChunk>,
pub index: Option<u32>,
pub name: Option<String>,
Expand All @@ -29,6 +31,7 @@ impl JsChunkGroup {
.iter()
.map(|ukey| ukey.as_usize() as u32)
.collect(),
inner_ukey: cg.ukey.as_usize() as u32,
name: cg.name().map(|name| name.to_string()),
}
}
Expand All @@ -45,3 +48,12 @@ pub fn get_chunk_group(ukey: u32, compilation: &JsCompilation) -> JsChunkGroup {
let cg = chunk_group(ukey, compilation);
JsChunkGroup::from_chunk_group(cg, compilation)
}

#[napi(js_name = "__entrypoint_inner_get_runtime_chunk")]
pub fn get_runtime_chunk(ukey: u32, compilation: &JsCompilation) -> JsChunk {
let compilation = &compilation.inner;
let entrypoint = chunk_group(ukey, compilation);
let chunk_ukey = entrypoint.get_runtime_chunk(&compilation.chunk_group_by_ukey);
let chunk = compilation.chunk_by_ukey.expect_get(&chunk_ukey);
JsChunk::from(chunk)
}
4 changes: 3 additions & 1 deletion crates/rspack_core/src/chunk.rs
Expand Up @@ -276,7 +276,9 @@ impl Chunk {
.groups
.iter()
.filter_map(|ukey| get_chunk_group_from_ukey(ukey, chunk_group_by_ukey))
.any(|group| group.kind.is_entrypoint() && group.get_runtime_chunk() == self.ukey)
.any(|group| {
group.kind.is_entrypoint() && group.get_runtime_chunk(chunk_group_by_ukey) == self.ukey
})
}

pub fn has_async_chunks(&self, chunk_group_by_ukey: &ChunkGroupByUkey) -> bool {
Expand Down
14 changes: 10 additions & 4 deletions crates/rspack_core/src/chunk_group.rs
Expand Up @@ -106,11 +106,17 @@ impl ChunkGroup {
self.runtime_chunk = Some(chunk_ukey);
}

pub fn get_runtime_chunk(&self) -> ChunkUkey {
pub fn get_runtime_chunk(&self, chunk_group_by_ukey: &ChunkGroupByUkey) -> ChunkUkey {
match self.kind {
ChunkGroupKind::Entrypoint { .. } => self
.runtime_chunk
.expect("EntryPoint runtime chunk not set"),
ChunkGroupKind::Entrypoint { .. } => self.runtime_chunk.unwrap_or_else(|| {
for parent in self.parents_iterable() {
let parent = chunk_group_by_ukey.expect_get(parent);
if matches!(parent.kind, ChunkGroupKind::Entrypoint { .. }) {
return parent.get_runtime_chunk(chunk_group_by_ukey);
}
}
panic!("Entrypoint should set_runtime_chunk at build_chunk_graph before get_runtime_chunk")
}),
ChunkGroupKind::Normal { .. } => {
unreachable!("Normal chunk group doesn't have runtime chunk")
}
Expand Down
9 changes: 6 additions & 3 deletions crates/rspack_core/src/compiler/compilation.rs
Expand Up @@ -897,7 +897,10 @@ impl Compilation {
.or(entrypoint.name().map(|n| n.to_string()));
if let (Some(runtime), Some(chunk)) = (
runtime,
get_chunk_from_ukey(&entrypoint.get_runtime_chunk(), chunk_by_ukey),
get_chunk_from_ukey(
&entrypoint.get_runtime_chunk(chunk_group_by_ukey),
chunk_by_ukey,
),
) {
chunk_graph.set_runtime_id(runtime, chunk.id.clone());
}
Expand All @@ -923,11 +926,11 @@ impl Compilation {
pub fn get_chunk_graph_entries(&self) -> HashSet<ChunkUkey> {
let entries = self.entrypoints.values().map(|entrypoint_ukey| {
let entrypoint = self.chunk_group_by_ukey.expect_get(entrypoint_ukey);
entrypoint.get_runtime_chunk()
entrypoint.get_runtime_chunk(&self.chunk_group_by_ukey)
});
let async_entries = self.async_entrypoints.iter().map(|entrypoint_ukey| {
let entrypoint = self.chunk_group_by_ukey.expect_get(entrypoint_ukey);
entrypoint.get_runtime_chunk()
entrypoint.get_runtime_chunk(&self.chunk_group_by_ukey)
});
HashSet::from_iter(entries.chain(async_entries))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_runtime/src/helpers.rs
Expand Up @@ -28,7 +28,7 @@ pub fn update_hash_for_entry_startup(
}

if let Some(runtime_chunk) = get_chunk_group_from_ukey(entry, &compilation.chunk_group_by_ukey)
.map(|e| e.get_runtime_chunk())
.map(|e| e.get_runtime_chunk(&compilation.chunk_group_by_ukey))
{
for chunk_ukey in get_all_chunks(
entry,
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn get_runtime_chunk_output_name(args: &RenderChunkArgs) -> Result<String> {
let runtime_chunk = args
.compilation
.chunk_by_ukey
.expect_get(&entry_point.get_runtime_chunk());
.expect_get(&entry_point.get_runtime_chunk(&args.compilation.chunk_group_by_ukey));

Ok(get_chunk_output_name(runtime_chunk, args.compilation))
}
Expand All @@ -153,7 +153,7 @@ pub fn generate_entry_startup(
}

if let Some(runtime_chunk) = get_chunk_group_from_ukey(entry, &compilation.chunk_group_by_ukey)
.map(|e| e.get_runtime_chunk())
.map(|e| e.get_runtime_chunk(&compilation.chunk_group_by_ukey))
{
let chunks = get_all_chunks(
entry,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_runtime/src/module_chunk_format.rs
Expand Up @@ -136,7 +136,7 @@ impl Plugin for ModuleChunkFormatPlugin {
let runtime_chunk = compilation
.chunk_group_by_ukey
.expect_get(entry)
.get_runtime_chunk();
.get_runtime_chunk(&compilation.chunk_group_by_ukey);
let chunks = get_all_chunks(
entry,
&runtime_chunk,
Expand Down
10 changes: 9 additions & 1 deletion packages/rspack/src/ChunkGroup.ts
Expand Up @@ -12,7 +12,7 @@ export class ChunkGroup {
return new ChunkGroup(chunk, compilation);
}

private constructor(inner: JsChunkGroup, compilation: JsCompilation) {
protected constructor(inner: JsChunkGroup, compilation: JsCompilation) {
this.#inner = inner;
this.#inner_compilation = compilation;
}
Expand Down Expand Up @@ -46,4 +46,12 @@ export class ChunkGroup {
get name(): string | undefined {
return this.#inner.name;
}

__internal_inner_ukey() {
return this.#inner.__inner_ukey;
}

__internal_inner_compilation() {
return this.#inner_compilation;
}
}
5 changes: 3 additions & 2 deletions packages/rspack/src/Compilation.ts
Expand Up @@ -56,6 +56,7 @@ import { memoizeValue } from "./util/memoize";
import { Chunk } from "./Chunk";
import { CodeGenerationResult } from "./Module";
import { ChunkGraph } from "./ChunkGraph";
import { Entrypoint } from "./Entrypoint";

export type AssetInfo = Partial<JsAssetInfo> & Record<string, any>;
export type Assets = Record<string, Source>;
Expand Down Expand Up @@ -310,11 +311,11 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
/**
* Get a map of all entrypoints.
*/
get entrypoints(): ReadonlyMap<string, ChunkGroup> {
get entrypoints(): ReadonlyMap<string, Entrypoint> {
return new Map(
Object.entries(this.#inner.entrypoints).map(([n, e]) => [
n,
ChunkGroup.__from_binding(e, this.#inner)
Entrypoint.__from_binding(e, this.#inner)
])
);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/rspack/src/Entrypoint.ts
@@ -0,0 +1,26 @@
import {
__entrypoint_inner_get_runtime_chunk,
type JsChunkGroup,
type JsCompilation
} from "@rspack/binding";
import { ChunkGroup } from "./ChunkGroup";
import { Chunk } from "./Chunk";

export class Entrypoint extends ChunkGroup {
static __from_binding(chunk: JsChunkGroup, compilation: JsCompilation) {
return new Entrypoint(chunk, compilation);
}

protected constructor(inner: JsChunkGroup, compilation: JsCompilation) {
super(inner, compilation);
}

getRuntimeChunk(): Chunk | null {
const c = __entrypoint_inner_get_runtime_chunk(
this.__internal_inner_ukey(),
this.__internal_inner_compilation()
);
if (c) return Chunk.__from_binding(c, this.__internal_inner_compilation());
return null;
}
}
Empty file.
Empty file.
@@ -0,0 +1,3 @@
module.exports = {
noTests: true
}
@@ -0,0 +1,37 @@
const table = {
['main1']: 'main1',
['main2']: 'main2-runtime'
}

function plugin(compiler) {
compiler.hooks.compilation.tap("plugin", compilation => {
compilation.hooks.processAssets.tap("plugin", () => {
for (let [name, entrypoint] of compilation.entrypoints.entries()) {
const runtimeChunk = entrypoint.getRuntimeChunk();
expect(runtimeChunk.name).toBe(table[name])
}
});
});
}

const common = {
output: {
filename: "[name].js",
},
plugins: [plugin]
}

module.exports = [{
...common,
entry: {
main1: "./entry1.js",
},
}, {
...common,
entry: {
main2: {
import: "./entry2.js",
runtime: "main2-runtime"
}
},
}];