Skip to content

Commit

Permalink
fix: allow cjs to be both entry and dependency (#336)
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 -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Nov 20, 2023
1 parent 3166816 commit d53155b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
17 changes: 13 additions & 4 deletions crates/rolldown/src/bundler/stages/bundle_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::{
stages::link_stage::LinkStageOutput,
utils::bitset::BitSet,
},
InputOptions, Output,
InputOptions, Output, OutputFormat,
};
use index_vec::{index_vec, IndexVec};
use rolldown_common::{ImportKind, ModuleId, SymbolRef};
use rolldown_common::{ExportsKind, ImportKind, ModuleId, SymbolRef};
use rustc_hash::{FxHashMap, FxHashSet};

pub struct BundleStage<'a> {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a> BundleStage<'a> {
}

// TODO(hyf0): refactor this function
#[allow(clippy::too_many_lines)]
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
fn compute_cross_chunk_links(&mut self, chunk_graph: &mut ChunkGraph) {
// Determine which symbols belong to which chunk
let mut chunk_meta_imports_vec =
Expand Down Expand Up @@ -144,7 +144,16 @@ impl<'a> BundleStage<'a> {

if let Some(entry_module) = chunk.entry_module {
let entry_module = &self.link_output.modules[entry_module];
let entry_linking_info = &self.link_output.linking_infos[entry_module.id()];
let Module::Normal(entry_module) = entry_module else {
return;
};
let entry_linking_info = &self.link_output.linking_infos[entry_module.id];
if matches!(entry_module.exports_kind, ExportsKind::CommonJs)
&& matches!(self.output_options.format, OutputFormat::Esm)
{
chunk_meta_imports
.insert(entry_linking_info.wrapper_ref.expect("cjs should be wrapped in esm output"));
}
for export_ref in entry_linking_info.resolved_exports.values() {
let mut canonical_ref = self.link_output.symbols.canonical_ref_for(export_ref.symbol_ref);
let symbol = self.link_output.symbols.get(canonical_ref);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import assert from 'assert'
import main from './dist/main.mjs'
import main2 from './dist/main2.mjs'
assert(main === main2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/misc/cjs_entry_as_dependency
---
# Assets

## 2.mjs

```js
import { __commonJS } from "./_rolldown_runtime.mjs";
// main2.js
var require_main2 = __commonJS({
'main2.js'(exports, module) {
module.exports = {}
}
});
export { require_main2 };
```
## main.mjs

```js
import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
import { require_main2 } from "./2.mjs";
// main.js
var require_main = __commonJS({
'main.js'(exports, module) {
var import_main2 = __toESM(require_main2());
module.exports = import_main2.default
}
});
export default require_main();
```
## main2.mjs

```js
import { require_main2 } from "./2.mjs";
export default require_main2();
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import main2 from './main2.js'
module.exports = main2

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"input": {
"input": [
{
"name": "main",
"import": "./main.js"
},
{
"name": "main2",
"import": "./main2.js"
}
]
}
}

0 comments on commit d53155b

Please sign in to comment.