Skip to content

Commit

Permalink
feat: only split runtime module into runtime chunk in rust test (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Oct 29, 2023
1 parent 7fd3765 commit 7d56461
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
10 changes: 7 additions & 3 deletions crates/rolldown/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::path::PathBuf;

use rolldown::{Bundler, InputOptions};
use rolldown::{Bundler, InputItem, InputOptions};
use sugar_path::SugarPathBuf;

#[tokio::main]
async fn main() {
let root = PathBuf::from(&std::env::var("CARGO_MANIFEST_DIR").unwrap());
let cwd = root.join("./examples").into_normalize();
let mut bundler = Bundler::new(InputOptions {
input: Some(vec!["./index.js".to_string().into()]),
input: Some(vec![InputItem {
name: Some("basic".to_string()),
import: "./index.js".to_string(),
}]),
cwd: Some(cwd),
});

bundler.generate(Default::default()).await.unwrap();
let outputs = bundler.write(Default::default()).await.unwrap();
println!("{outputs:#?}");
}
34 changes: 24 additions & 10 deletions crates/rolldown/src/bundler/bundle/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ impl<'a> Bundle<'a> {
FxHashMap::with_capacity_and_hasher(self.graph.entries.len(), BuildHasherDefault::default());
let mut chunks = ChunksVec::with_capacity(self.graph.entries.len());

// FIXME: should only do this while `ROLLDOWN_TEST=1`
let _runtime_chunk_id = chunks.push(Chunk::new(
Some("_rolldown_runtime".to_string()),
None,
BitSet::new(0),
vec![self.graph.runtime.id],
));

// Create chunk for each static and dynamic entry
for (entry_index, (name, module_id)) in self.graph.entries.iter().enumerate() {
let count: u32 = u32::try_from(entry_index).unwrap();
Expand All @@ -196,6 +188,16 @@ impl<'a> Bundle<'a> {

// Determine which modules belong to which chunk. A module could belong to multiple chunks.
self.graph.entries.iter().enumerate().for_each(|(i, (_, entry))| {
// runtime module are shared by all chunks, so we mark it as reachable for all entries.
// FIXME: But this solution is not perfect. If we have two entries, one of them relies on runtime module, the other one doesn't.
// In this case, we only need to generate two chunks, but currently we will generate three chunks. We need to analyze the usage of runtime module
// to make sure only necessary chunks mark runtime module as reachable.
self.determine_reachable_modules_for_entry(
self.graph.runtime.id,
i.try_into().unwrap(),
&mut module_to_bits,
);

self.determine_reachable_modules_for_entry(
*entry,
i.try_into().unwrap(),
Expand All @@ -208,11 +210,23 @@ impl<'a> Bundle<'a> {
self.graph.modules.len()
];

// FIXME: should remove this when tree shaking is supported
let is_rolldown_test = std::env::var("ROLLDOWN_TEST").is_ok();
if is_rolldown_test {
let runtime_chunk_id = chunks.push(Chunk::new(
Some("_rolldown_runtime".to_string()),
None,
BitSet::new(0),
vec![self.graph.runtime.id],
));
module_to_chunk[self.graph.runtime.id] = Some(runtime_chunk_id);
}

// 1. Assign modules to corresponding chunks
// 2. Create shared chunks to store modules that belong to multiple chunks.
for module in &self.graph.modules {
if module.id() == self.graph.runtime.id {
// TODO: render runtime module
// FIXME: should remove this when tree shaking is supported
if is_rolldown_test && module.id() == self.graph.runtime.id {
continue;
}
let bits = &module_to_bits[module.id()];
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/tests/common/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl Case {
}

pub fn exec(self) {
std::env::set_var("ROLLDOWN_TEST", "1");
tokio::runtime::Runtime::new().unwrap().block_on(self.exec_inner())
}

Expand Down
2 changes: 2 additions & 0 deletions crates/rolldown_common/src/module_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl ResourceId {
} else {
path.to_string()
};
// remove \0
let pretty = pretty.replace('\0', "");

Self(Inner { path, pretty }.into())
}
Expand Down

0 comments on commit 7d56461

Please sign in to comment.