Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4790d46

Browse files
authored
fix: chunk modules should include empty module (#1583)
<!-- Thank you for contributing! --> ### Description <!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
1 parent c166094 commit 4790d46

5 files changed

Lines changed: 19 additions & 10 deletions

File tree

crates/rolldown/src/utils/chunk/render_chunk.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ pub async fn render_chunk(
4545
.par_iter()
4646
.copied()
4747
.filter_map(|id| graph.module_table.modules[id].as_ecma())
48-
.filter_map(|m| {
49-
render_ecma_module(m, &graph.ast_table[m.idx], m.resource_id.as_ref(), options)
50-
.map(|rendered| (m.idx, &m.resource_id, rendered))
48+
.map(|m| {
49+
(
50+
m.idx,
51+
&m.resource_id,
52+
render_ecma_module(m, &graph.ast_table[m.idx], m.resource_id.as_ref(), options),
53+
)
5154
})
5255
.collect::<Vec<_>>()
5356
.into_iter()
@@ -60,8 +63,7 @@ pub async fn render_chunk(
6063
if *id == graph.runtime.id() && matches!(options.format, OutputFormat::Cjs) =>
6164
{
6265
let maybe_runtime_module = rendered_iter.next();
63-
if let Some((_, _module_resource_id, module_render_output)) = maybe_runtime_module {
64-
let emitted_sources = module_render_output;
66+
if let Some((_, _module_resource_id, Some(emitted_sources))) = maybe_runtime_module {
6567
for source in emitted_sources {
6668
concat_source.add_source(source);
6769
}
@@ -89,9 +91,10 @@ pub async fn render_chunk(
8991
}
9092

9193
rendered_iter.for_each(|(_id, module_resource_id, module_render_output)| {
92-
let emitted_sources = module_render_output;
93-
for source in emitted_sources {
94-
concat_source.add_source(source);
94+
if let Some(emitted_sources) = module_render_output {
95+
for source in emitted_sources {
96+
concat_source.add_source(source);
97+
}
9598
}
9699

97100
// FIXME: NAPI-RS used CStr under the hood, so it can't handle null byte in the string.

packages/rolldown/src/binding.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ export declare function transform(filename: string, sourceText: string, options?
304304

305305
export interface TransformOptions {
306306
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
307+
/** Force jsx parsing, */
308+
jsx?: boolean
307309
typescript?: TypeScriptBindingOptions
308310
react?: ReactBindingOptions
309311
es2015?: Es2015BindingOptions

packages/rolldown/tests/fixtures/plugin/write-bundle/_config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import type { RolldownOutputChunk } from '../../../../src'
44
import { defineTest } from '@tests'
55

66
const entry = path.join(__dirname, './main.js')
7+
const foo = path.join(__dirname, './foo.js')
78

89
const writeBundleFn = vi.fn()
910

1011
export default defineTest({
1112
config: {
1213
input: entry,
14+
treeshake: false,
1315
plugins: [
1416
{
1517
name: 'test-plugin',
@@ -25,8 +27,9 @@ export default defineTest({
2527
expect(chunk.facadeModuleId).toBe(entry)
2628
expect(chunk.exports.length).toBe(0)
2729
expect(chunk.imports).length(0)
28-
expect(chunk.moduleIds).toStrictEqual([entry])
29-
expect(Object.keys(chunk.modules).length).toBe(1)
30+
// The `foo.js` should be include `modules/moduleIds` even it is empty.
31+
expect(chunk.moduleIds).toStrictEqual([foo, entry])
32+
expect(Object.keys(chunk.modules).length).toStrictEqual(2)
3033
},
3134
},
3235
],

packages/rolldown/tests/fixtures/plugin/write-bundle/foo.js

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
import './foo'
12
console.log()

0 commit comments

Comments
 (0)