Skip to content

Commit

Permalink
fix(cjs): aovid generate assgin pattern if chunk imports nothing (#1959)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
underfin authored Aug 12, 2024
1 parent e0dd96c commit 32c3363
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
16 changes: 10 additions & 6 deletions crates/rolldown/src/ecmascript/format/cjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,17 @@ fn render_cjs_chunk_imports(ctx: &GenerateContext<'_>) -> String {
let mut s = String::new();

// render imports from other chunks
ctx.chunk.imports_from_other_chunks.iter().for_each(|(exporter_id, _items)| {
ctx.chunk.imports_from_other_chunks.iter().for_each(|(exporter_id, items)| {
let importee_chunk = &ctx.chunk_graph.chunks[*exporter_id];
s.push_str(&format!(
"const {} = require('{}');\n",
ctx.chunk.require_binding_names_for_other_chunks[exporter_id],
ctx.chunk.import_path_for(importee_chunk)
));
let require_path_str = format!("require('{}');\n", ctx.chunk.import_path_for(importee_chunk));
if items.is_empty() {
s.push_str(&require_path_str);
} else {
s.push_str(&format!(
"const {} = {require_path_str}",
ctx.chunk.require_binding_names_for_other_chunks[exporter_id],
));
}
});

render_import_stmts.iter().for_each(|stmt| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"config": {
"input": [
{
"name": "entry1",
"import": "./main.js"
},
{
"name": "entry2",
"import": "./main.js"
}
],
"format": "cjs"
},
"expectExecuted": false,
"hiddenRuntimeModule": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/rolldown_testing/src/integration_test.rs
---
# Assets

## entry1.cjs

```js
"use strict";
require('./main.cjs');
```
## entry2.cjs

```js
"use strict";
require('./main.cjs');
```
## main.cjs

```js
//#region main.js
console.log("share");
//#endregion
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('share')
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,12 @@ expression: "snapshot_outputs.join(\"\\n\")"

- main-!~{000}~.cjs => main-P0_yQQlj.cjs

# tests/rolldown/function/format/cjs/share_chunk_without_symbol

- entry1-!~{000}~.cjs => entry1-oDOCJOca.cjs
- entry2-!~{001}~.cjs => entry2-_HPn1u-R.cjs
- main-!~{002}~.cjs => main-Bdwd-Fr6.cjs

# tests/rolldown/function/format/cjs/shared_entry_modules

- entry1-!~{000}~.cjs => entry1-i7oOHHhW.cjs
Expand Down

0 comments on commit 32c3363

Please sign in to comment.