Skip to content

Commit af82e29

Browse files
committed
feat: remove empty unneeded runtime chunk
1 parent 2cc31b5 commit af82e29

File tree

8 files changed

+22
-37
lines changed

8 files changed

+22
-37
lines changed

crates/rspack_plugin_esm_library/src/plugin.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub static CONCATENATED_MODULES_MAP: LazyLock<
3737
pub static LINKS: LazyLock<RwLock<FxHashMap<u32, UkeyMap<ChunkUkey, ChunkLinkContext>>>> =
3838
LazyLock::new(Default::default);
3939

40+
pub static RSPACK_ESM_RUNTIME_CHUNK: &str = "RSPACK_ESM_RUNTIME";
41+
4042
#[plugin]
4143
#[derive(Debug, Default)]
4244
pub struct EsmLibraryPlugin {}
@@ -355,12 +357,25 @@ static RSPACK_ESM_CHUNK_PLACEHOLDER_RE: LazyLock<Regex> =
355357
#[plugin_hook(CompilationProcessAssets for EsmLibraryPlugin, stage = Compilation::PROCESS_ASSETS_STAGE_AFTER_OPTIMIZE_HASH)]
356358
async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
357359
let mut replaced = vec![];
360+
let mut removed = vec![];
358361

359362
for (asset_name, asset) in compilation.assets() {
360363
if asset.get_info().javascript_module.unwrap_or_default() {
361364
let Some(source) = asset.get_source() else {
362365
continue;
363366
};
367+
368+
if asset
369+
.get_info()
370+
.extras
371+
.contains_key(RSPACK_ESM_RUNTIME_CHUNK)
372+
&& source.source().trim().is_empty()
373+
{
374+
// remove empty runtime chunk
375+
removed.push(asset_name.to_string());
376+
continue;
377+
}
378+
364379
let mut replace_source = ReplaceSource::new(source.clone());
365380
let output_path = compilation.options.output.path.as_std_path();
366381
let mut self_path = output_path.join(asset_name);
@@ -425,6 +440,9 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
425440
.expect("should have asset")
426441
.set_source(Some(Arc::new(replace_source)));
427442
}
443+
for remove_name in removed {
444+
compilation.assets_mut().remove(&remove_name);
445+
}
428446

429447
Ok(())
430448
}

crates/rspack_plugin_esm_library/src/render.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use swc_core::common::sync::Lazy;
2626

2727
use crate::{
2828
chunk_link::{ChunkLinkContext, Ref},
29-
plugin::{CONCATENATED_MODULES_MAP, LINKS},
29+
plugin::{CONCATENATED_MODULES_MAP, LINKS, RSPACK_ESM_RUNTIME_CHUNK},
3030
runtime::RegisterModuleRuntime,
3131
};
3232

@@ -186,6 +186,9 @@ impl EsmLibraryPlugin {
186186

187187
// render webpack runtime
188188
if chunk.has_runtime(&compilation.chunk_group_by_ukey) {
189+
asset_info
190+
.extras
191+
.insert(RSPACK_ESM_RUNTIME_CHUNK.into(), "true".into());
189192
// render chunk needs to render *all* runtimes in the whole tree
190193
let tree_runtime_requirements =
191194
ChunkGraph::get_tree_runtime_requirements(compilation, chunk_ukey);

tests/rspack-test/esmOutputCases/deconflict/deconflit-local/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,4 @@ it('should have deconflicted symbol', () => {
1616
})
1717

1818

19-
```
20-
21-
```mjs title=runtime.mjs
22-
23-
24-
2519
```

tests/rspack-test/esmOutputCases/deconflict/other-chunk-local/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,4 @@ const value = foo_value
3333

3434
export { value };
3535

36-
```
37-
38-
```mjs title=runtime.mjs
39-
40-
41-
4236
```

tests/rspack-test/esmOutputCases/dynamic-import/import-self/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ it('should have access to the value from the same file', async () => {
1212
})
1313

1414

15-
```
16-
17-
```mjs title=runtime.mjs
18-
19-
20-
2115
```
2216

2317
```mjs title=value_js.mjs

tests/rspack-test/esmOutputCases/externals/externals-import-render/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,4 @@ it('should compile', () => {
1616
})
1717

1818

19-
```
20-
21-
```mjs title=runtime.mjs
22-
23-
24-
2519
```

tests/rspack-test/esmOutputCases/re-exports/deep-re-exports-esm/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,4 @@ it('should re-export esm correctly', async () => {
2424

2525
export { lib2_lib2 as lib2, lib3_lib3 as lib3, lib_lib as lib };
2626

27-
```
28-
29-
```mjs title=runtime.mjs
30-
31-
32-
3327
```

tests/rspack-test/esmOutputCases/re-exports/re-export-external/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,4 @@ it('should compile and import success', async () => {
3838
export { external_fs_readFile as r, external_fs_readFile as readFile, fs_0 as fs, named, starExports };
3939
export * from "fs";
4040

41-
```
42-
43-
```mjs title=runtime.mjs
44-
45-
46-
4741
```

0 commit comments

Comments
 (0)