Skip to content

Commit

Permalink
feat: generate default export for cjs entry (#219)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

Requirement for pre-bundling react and react-dom.
<!-- 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 11, 2023
1 parent 763dedb commit 699fc5d
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/bundle/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<'a> Bundle<'a> {
.iter()
.enumerate()
.map(|(_chunk_id, c)| {
let content = c.render(self.graph, &chunk_graph).unwrap();
let content = c.render(self.graph, &chunk_graph, self.output_options).unwrap();

Asset { file_name: c.file_name.clone().unwrap(), content }
})
Expand Down
9 changes: 7 additions & 2 deletions crates/rolldown/src/bundler/chunk/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ impl Chunk {
}

#[allow(clippy::unnecessary_wraps)]
pub fn render(&self, graph: &Graph, chunk_graph: &ChunkGraph) -> BatchedResult<String> {
pub fn render(
&self,
graph: &Graph,
chunk_graph: &ChunkGraph,
output_options: &NormalizedOutputOptions,
) -> BatchedResult<String> {
use rayon::prelude::*;
let mut joiner = Joiner::with_options(JoinerOptions { separator: Some("\n".to_string()) });
joiner.append(self.render_imports_for_esm(graph, chunk_graph));
Expand All @@ -81,7 +86,7 @@ impl Chunk {
joiner.append(item);
});

if let Some(exports) = self.render_exports_for_esm(graph) {
if let Some(exports) = self.render_exports(graph, output_options) {
joiner.append(exports);
}

Expand Down
24 changes: 22 additions & 2 deletions crates/rolldown/src/bundler/chunk/render_chunk_exports.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
use rolldown_common::WrapKind;
use string_wizard::MagicString;

use crate::bundler::graph::graph::Graph;
use crate::bundler::{
graph::graph::Graph,
options::{normalized_output_options::NormalizedOutputOptions, output_options},
};

use super::chunk::Chunk;

impl Chunk {
pub fn render_exports_for_esm(&self, graph: &Graph) -> Option<MagicString<'static>> {
pub fn render_exports(
&self,
graph: &Graph,
output_options: &NormalizedOutputOptions,
) -> Option<MagicString<'static>> {
if let Some(entry) = self.entry_module {
let linking_info = &graph.linking_infos[entry];
if matches!(linking_info.wrap_kind, WrapKind::Cjs) {
match output_options.format {
output_options::OutputFormat::Esm => {
let wrap_ref_name = &self.canonical_names[&linking_info.wrap_ref.unwrap()];
return Some(MagicString::new(format!("export default {wrap_ref_name}();\n")));
}
}
}
}

let export_items = self.entry_module.map_or_else(
|| {
self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use derivative::Derivative;

use super::file_name_template::FileNameTemplate;
use super::{file_name_template::FileNameTemplate, output_options::OutputFormat};
use crate::OutputOptions;

#[derive(Derivative)]
#[derivative(Debug)]
pub struct NormalizedOutputOptions {
pub entry_file_names: FileNameTemplate,
pub chunk_file_names: FileNameTemplate,
pub format: OutputFormat,
}

impl NormalizedOutputOptions {
Expand All @@ -19,6 +20,7 @@ impl NormalizedOutputOptions {
chunk_file_names: FileNameTemplate::from(
opts.chunk_file_names.unwrap_or_else(|| "[name]-[hash].js".to_string()),
),
format: opts.format.unwrap_or(OutputFormat::Esm),
}
}
}
6 changes: 6 additions & 0 deletions crates/rolldown/src/bundler/options/output_options.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use derivative::Derivative;

#[derive(Debug)]
pub enum OutputFormat {
Esm,
}

#[derive(Derivative, Default)]
#[derivative(Debug)]
pub struct OutputOptions {
pub entry_file_names: Option<String>,
pub chunk_file_names: Option<String>,
pub dir: Option<String>,
pub format: Option<OutputFormat>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ var require_entry = __commonJS({
console.log('cache:', require.cache);
}
});
export default require_entry();
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"import": "entry.js"
}
]
}
},
"_comment": "Build output are executed in esm environment, `require` is not available",
"expectExecuted": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ var require_entry = __commonJS({
}
}
});
export default require_entry();
```
4 changes: 4 additions & 0 deletions crates/rolldown/tests/fixtures/compat/cjs_entry/_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import assert from 'assert'
import main from './dist/main.mjs'

assert.equal(main, 'main')
20 changes: 20 additions & 0 deletions crates/rolldown/tests/fixtures/compat/cjs_entry/artifacts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/compat/cjs_entry
---
# Assets

## main.mjs

```js
import { __commonJS } from "./_rolldown_runtime.mjs";
// main.js
var require_main = __commonJS({
'main.js'(exports, module) {
module.exports = 'main'
}
});
export default require_main();
```
1 change: 1 addition & 0 deletions crates/rolldown/tests/fixtures/compat/cjs_entry/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'main'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions crates/rolldown_binding/src/options/output_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ pub fn resolve_output_options(opts: OutputOptions) -> napi::Result<rolldown::Out
entry_file_names: opts.entry_file_names,
chunk_file_names: opts.chunk_file_names,
dir: opts.dir,
format: None,
})
}

0 comments on commit 699fc5d

Please sign in to comment.