Skip to content

Commit

Permalink
feat: exclude modules in code splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Nov 23, 2023
1 parent 937684f commit bce19e8
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 99 deletions.
18 changes: 17 additions & 1 deletion crates/rolldown/src/bundler/stages/bundle_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ impl<'a> BundleStage<'a> {
entry_index: u32,
module_to_bits: &mut IndexVec<ModuleId, BitSet>,
) {
let Module::Normal(module) = &self.link_output.modules[module_id] else { return };
if !module.is_included {
return;
}
if module_to_bits[module_id].has_bit(entry_index) {
return;
}
module_to_bits[module_id].set_bit(entry_index);
let Module::Normal(module) = &self.link_output.modules[module_id] else { return };
module.import_records.iter().for_each(|rec| {
// Module imported dynamically will be considered as an entry,
// so we don't need to include it in this chunk
Expand Down Expand Up @@ -303,6 +306,12 @@ impl<'a> BundleStage<'a> {
// 1. Assign modules to corresponding chunks
// 2. Create shared chunks to store modules that belong to multiple chunks.
for module in &self.link_output.modules {
let Module::Normal(normal_module) = module else {
continue;
};
if !normal_module.is_included {
continue;
}
let bits = &module_to_bits[module.id()];
if let Some(chunk_id) = bits_to_chunk.get(bits).copied() {
chunks[chunk_id].modules.push(module.id());
Expand All @@ -324,6 +333,7 @@ impl<'a> BundleStage<'a> {
}

fn generate_chunk_filenames(&self, chunk_graph: &mut ChunkGraph) {
let mut used_chunk_names = FxHashSet::default();
chunk_graph.chunks.iter_mut().for_each(|chunk| {
let file_name_tmp = chunk.file_name_template(self.output_options);
let chunk_name = chunk.name.clone().unwrap_or_else(|| {
Expand All @@ -342,6 +352,12 @@ impl<'a> BundleStage<'a> {
module.resource_id().expect_file().unique(&self.input_options.cwd)
});

let mut chunk_name = chunk_name;
while used_chunk_names.contains(&chunk_name) {
chunk_name = format!("{}-{}", chunk_name, used_chunk_names.len());
}
used_chunk_names.insert(chunk_name.clone());

chunk.file_name =
Some(file_name_tmp.render(&FileNameRenderOptions { name: Some(&chunk_name) }));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ input_file: crates/rolldown/tests/esbuild/default/duplicate_entry_point
---
# Assets

## entry_js.mjs
## entry2_js.mjs

```js
```
## entry_js.mjs
## entry_js-2.mjs

```js
// entry.js
console.log(123)
```
## runtime.mjs
## entry_js.mjs

```js
// entry.js
console.log(123)
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"import": "entry.js"
},
{
"name": "entry_js",
"name": "entry2_js",
"import": "entry.js"
}
]
Expand Down
5 changes: 0 additions & 5 deletions crates/rolldown/tests/esbuild/default/outbase/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,3 @@ console.log('c')
// d.js
console.log('d')
```
## runtime.mjs

```js
```
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ if (true) {
for (var { j, x: [k] } in {});
function l() {}
}
```
## runtime.mjs
```js

```
## top-level_js.mjs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input_file: crates/rolldown/tests/esbuild/splitting/assign-to-local
## a.mjs

```js
import { foo, setFoo } from "./runtime.mjs";
import { foo, setFoo } from "./shared_js.mjs";
// a.js
Expand All @@ -18,13 +18,13 @@ console.log(foo)
## b.mjs

```js
import { foo } from "./runtime.mjs";
import { foo } from "./shared_js.mjs";
// b.js
console.log(foo)
```
## runtime.mjs
## shared_js.mjs

```js
// shared.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input_file: crates/rolldown/tests/esbuild/splitting/cross_chunk_assignment_depen
## a.mjs

```js
import { setValue } from "./runtime.mjs";
import { setValue } from "./shared_js.mjs";
// a.js
Expand All @@ -19,7 +19,7 @@ setValue(123)
```js
// b.js
```
## runtime.mjs
## shared_js.mjs

```js
// shared.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ console.log(123)

```js
// b.js
```
## c.mjs

```js
```
## cd_js.mjs

Expand All @@ -33,13 +38,3 @@ console.log(123)
```js
```
## d.mjs

```js
```
## runtime.mjs

```js
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"import": "./b.js"
},
{
"name": "d",
"name": "c",
"import": "./d.js"
},
{
"name": "d",
"import": "./d.js"
}
]
],
"treeshake": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ export { bar };
// main.js
import('./foo_js.mjs').then(({bar}) => console.log(bar))
```
## runtime.mjs

```js
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ input_file: crates/rolldown/tests/esbuild/splitting/dynamic_and_not_dynamic_es6_
---
# Assets

## foo_js-2.mjs

```js
// foo.js
let bar = 123
export { bar };
```
## foo_js.mjs

```js
import { bar } from "./runtime.mjs";
import { bar } from "./foo_js-2.mjs";
export { bar };
```
## main.mjs

```js
import { bar } from "./runtime.mjs";
import { bar } from "./foo_js-2.mjs";
// main.js
import('./runtime.mjs').then(({bar: b}) => console.log(bar, b))
```
## runtime.mjs

```js
// foo.js
let bar = 123
export { bar };
import('./foo_js-2.mjs').then(({bar: b}) => console.log(bar, b))
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ import('./b.mjs')
var b_default = 1
export { b_default as default };
```
## runtime.mjs

```js
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input_file: crates/rolldown/tests/esbuild/splitting/nested_directories
## a.mjs

```js
import { shared_default } from "./runtime.mjs";
import { shared_default } from "./shared_js.mjs";
// src/a.js
Expand All @@ -17,13 +17,13 @@ console.log(shared_default)
## b.mjs

```js
import { shared_default } from "./runtime.mjs";
import { shared_default } from "./shared_js.mjs";
// src/b.js
console.log(-shared_default)
```
## runtime.mjs
## shared_js.mjs

```js
// shared.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ input_file: crates/rolldown/tests/esbuild/splitting/re_export_issue273
## a.mjs

```js
import { a } from "./runtime.mjs";
import { a } from "./a_js.mjs";
export { a };
```
## b.mjs
## a_js.mjs

```js
import { a } from "./runtime.mjs";
// b.js
// a.js
const a = 1
export { a };
```
## runtime.mjs
## b.mjs

```js
// a.js
const a = 1
import { a } from "./a_js.mjs";
// b.js
export { a };
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input_file: crates/rolldown/tests/esbuild/splitting/shared-es6-into-es6
## a.mjs

```js
import { foo } from "./runtime.mjs";
import { foo } from "./shared_js.mjs";
// a.js
Expand All @@ -17,13 +17,13 @@ console.log(foo)
## b.mjs

```js
import { foo } from "./runtime.mjs";
import { foo } from "./shared_js.mjs";
// b.js
console.log(foo)
```
## runtime.mjs
## shared_js.mjs

```js
// shared.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input_file: crates/rolldown/tests/esbuild/splitting/side_effects_without_depende
## a.mjs

```js
import { a } from "./runtime.mjs";
import { a } from "./shared_js.mjs";
// a.js
Expand All @@ -17,13 +17,13 @@ console.log(a)
## b.mjs

```js
import { b } from "./runtime.mjs";
import { b } from "./shared_js.mjs";
// b.js
console.log(b)
```
## runtime.mjs
## shared_js.mjs

```js
// shared.js
Expand Down
6 changes: 0 additions & 6 deletions crates/rolldown/tests/fixtures/code_splitting/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import('./dynamic_js.mjs')

```js
// main2.js
```
## runtime.mjs

```js
```
## share_js.mjs

Expand All @@ -40,5 +35,4 @@ console.log('shared');
- dynamic_js.mjs, is_entry false, facade_module_id Some("dynamic.js"), exports []
- main1.mjs, is_entry true, facade_module_id Some("main1.js"), exports []
- main2.mjs, is_entry true, facade_module_id Some("main2.js"), exports []
- runtime.mjs, is_entry false, facade_module_id None, exports []
- share_js.mjs, is_entry false, facade_module_id None, exports []
Loading

0 comments on commit bce19e8

Please sign in to comment.