Repository navigation
bundler: give each entry point its own output and module order without --splitting #41263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ce2d1cd
afd9c2a
b6e0e76
53c2620
4701edf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -687,39 +687,26 @@ impl<'a> LinkerGraph<'a> { | |
| + server_component_boundaries.list.len() | ||
| + dynamic_import_entry_points.len(), | ||
| )?; | ||
| // SAFETY: capacity reserved; columns initialized below. | ||
| unsafe { self.entry_points.set_len(entry_points.len()) }; | ||
|
|
||
| // Note: `source_indices` / `path_strings` are disjoint columns of | ||
| // the same `MultiArrayList`. `split_mut()` hands out both at once; | ||
| // `self.entry_points` is not | ||
| // reallocated until after `path_strings`/`source_indices` are done | ||
| // with (the next `append_assume_capacity` is within the | ||
| // pre-reserved capacity, so no realloc). | ||
| let mut ep_slice = self.entry_points.slice(); | ||
| let ep_cols = ep_slice.split_mut(); | ||
| let source_indices: &mut [index::Int] = ep_cols.source_index; | ||
| let path_strings: &mut [RawSlice<u8>] = ep_cols.output_path; | ||
|
|
||
| debug_assert_eq!(entry_points.len(), path_strings.len()); | ||
| debug_assert_eq!(entry_points.len(), source_indices.len()); | ||
| for ((i, path_string), source_index) in entry_points | ||
| .iter() | ||
| .zip(path_strings.iter_mut()) | ||
| .zip(source_indices.iter_mut()) | ||
| { | ||
| for i in entry_points { | ||
| let source = &sources[i.get() as usize]; | ||
| debug_assert!(source.index.0 == i.get()); | ||
| entry_point_kinds[source.index.0 as usize] = entry_point::Kind::UserSpecified; | ||
|
|
||
| // Check if this entry point has an original name (from virtual entry resolution) | ||
| if let Some(original_name) = entry_point_original_names.get(i.get()) { | ||
| *path_string = RawSlice::new(original_name); | ||
| } else { | ||
| *path_string = RawSlice::new(source.path.text); | ||
| // Two entry points can name one file, e.g. when an onResolve plugin | ||
| // maps two names to it. The file gets one entry point and one output. | ||
|
Comment on lines
+694
to
+695
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| if entry_point_kinds[source.index.0 as usize] != entry_point::Kind::None { | ||
| continue; | ||
| } | ||
| entry_point_kinds[source.index.0 as usize] = entry_point::Kind::UserSpecified; | ||
|
|
||
| *source_index = source.index.0; | ||
| // Check if this entry point has an original name (from virtual entry resolution) | ||
| let output_path = match entry_point_original_names.get(i.get()) { | ||
| Some(original_name) => RawSlice::new(original_name), | ||
| None => RawSlice::new(source.path.text), | ||
| }; | ||
| self.entry_points.append_assume_capacity(EntryPoint { | ||
| source_index: source.index.0, | ||
| output_path, | ||
| }); | ||
| } | ||
|
|
||
| for &id in dynamic_import_entry_points { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| use crate::mal_prelude::*; | ||
| use bun_alloc::ArenaVecExt as _; | ||
| use core::sync::atomic::AtomicUsize; | ||
|
|
||
| use bun_alloc::Arena; // bumpalo::Bump re-export | ||
|
|
@@ -109,28 +108,16 @@ pub(crate) fn compute_chunks( | |
|
|
||
| let has_html_chunk = loaders[source_index as usize] == Loader::Html; | ||
|
|
||
| // For code splitting, entry point chunks should be keyed by ONLY the entry point's | ||
| // own bit, not the full entry_bits. This ensures that if an entry point file is | ||
| // reachable from other entry points (e.g., via re-exports), its content goes into | ||
| // a shared chunk rather than staying in the entry point's chunk. | ||
| // Entry point chunks are keyed by ONLY the entry point's own bit, not the full | ||
| // entry_bits. Entry points that reach each other have the same entry_bits, and | ||
| // each one still needs its own chunk. With code splitting, an entry point file | ||
| // that other entry points reach (e.g., via re-exports) goes into a shared chunk. | ||
|
Comment on lines
+111
to
+114
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| // https://github.com/evanw/esbuild/blob/cd832972927f1f67b6d2cc895c06a8759c1cf309/internal/linker/linker.go#L3882 | ||
| let mut entry_point_chunk_bits = AutoBitSet::init_empty(this.graph.entry_points.len())?; | ||
| entry_point_chunk_bits.set(entry_bit as usize); | ||
|
|
||
| let js_chunk_key: &[u8] = 'brk: { | ||
| if code_splitting { | ||
| break 'brk temp | ||
| .alloc_slice_copy(entry_point_chunk_bits.bytes(this.graph.entry_points.len())); | ||
| } else { | ||
| // Force HTML chunks to always be generated, even if there's an identical JS file. | ||
| // Build the byte key directly since | ||
| // entry_bits is arbitrary bytes (not UTF-8) and cannot go through fmt::Display. | ||
| let mut v = bun_alloc::ArenaVec::new_in(temp); | ||
| v.push((!has_html_chunk) as u8); | ||
| v.extend_from_slice(entry_bits.bytes(this.graph.entry_points.len())); | ||
| break 'brk v.into_bump_slice(); | ||
| } | ||
| }; | ||
| let js_chunk_key: &[u8] = | ||
| temp.alloc_slice_copy(entry_point_chunk_bits.bytes(this.graph.entry_points.len())); | ||
|
|
||
| // Put this early on in this loop so that CSS-only entry points work. | ||
| if has_html_chunk { | ||
|
|
@@ -162,9 +149,7 @@ pub(crate) fn compute_chunks( | |
| // Create a chunk for the entry point here to ensure that the chunk is | ||
| // always generated even if the resulting file is empty | ||
| let hash_to_use = if !this.options.css_chunking { | ||
| bun_wyhash::hash( | ||
| temp.alloc_slice_copy(entry_bits.bytes(this.graph.entry_points.len())), | ||
| ) | ||
| bun_wyhash::hash(entry_point_chunk_bits.bytes(this.graph.entry_points.len())) | ||
| } else { | ||
| let mut hasher = Wyhash::init(5); | ||
| bun_core::write_any_to_hasher(&mut hasher, order.len()); | ||
|
|
@@ -203,6 +188,7 @@ pub(crate) fn compute_chunks( | |
| // Create a chunk for the entry point here to ensure that the chunk is | ||
| // always generated even if the resulting file is empty | ||
| let js_chunk_entry = js_chunks.get_or_put(js_chunk_key)?; | ||
| debug_assert!(!js_chunk_entry.found_existing); | ||
| entry_point_to_js_chunk_idx[entry_id_] = | ||
| u32::try_from(js_chunk_entry.index).expect("int cast"); | ||
| *js_chunk_entry.value_ptr = Chunk { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,15 +62,15 @@ pub(crate) fn convert_stmts_for_chunk( | |
|
|
||
| let output_format = c.options.output_format; | ||
|
|
||
| // If this file is a CommonJS entry point, double-write re-exports to the | ||
| // external CommonJS "module.exports" object in addition to our internal ESM | ||
| // If this file is the CommonJS entry point of this chunk, double-write re-exports | ||
| // to the external CommonJS "module.exports" object in addition to our internal ESM | ||
|
Comment on lines
+65
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| // export namespace object. The difference between these two objects is that | ||
| // our internal one must not have the "__esModule" marker while the external | ||
| // one must have the "__esModule" marker. This is done because an ES module | ||
| // importing itself should not see the "__esModule" marker but a CommonJS module | ||
| // importing us should see the "__esModule" marker. | ||
| let mut module_exports_for_export: Option<Expr> = None; | ||
| if output_format == Format::Cjs && chunk.is_entry_point() { | ||
| if output_format == Format::Cjs && chunk.is_entry_point_file(source_index) { | ||
| module_exports_for_export = Some(Expr::allocate( | ||
| bump, | ||
| E::Dot { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -758,6 +758,8 @@ impl LinkerContext<'_> { | |
| // instead of by mutating the exports object because other modules in the | ||
| // bundle (including the entry point module) may do "import * as" to get | ||
| // access to the exports object and should NOT see the "__esModule" flag. | ||
| // This stays the last statement of the part: `generate_code_for_file_in_chunk_js` | ||
| // drops it when another entry point's chunk prints this file. | ||
|
Comment on lines
+761
to
+762
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| if force_include_exports_for_entry_point { | ||
| let to_common_js_ref = self.runtime_function(b"__toCommonJS"); | ||
| emit_export_stmt!(Stmt::assign( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,20 @@ pub(crate) fn find_imported_parts_in_js_order( | |
|
|
||
| Order::sort(&mut chunk_order_array); | ||
|
|
||
| // Without code splitting, a chunk holds every file that its entry point reaches. | ||
| // Distances are the minimum over all entry points, so other entry points in this | ||
| // chunk also sort at distance 0. Visit this chunk's own entry point first, so the | ||
| // files print in the order that this entry point imports them. | ||
|
Comment on lines
+109
to
+112
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| if !this.graph.code_splitting && chunk.entry_point.is_entry_point() { | ||
| let entry_point = chunk.entry_point.source_index(); | ||
| if let Some(i) = chunk_order_array | ||
| .iter() | ||
| .position(|order| order.source_index == entry_point) | ||
| { | ||
| chunk_order_array[..=i].rotate_right(1); | ||
| } | ||
| } | ||
|
|
||
| part_ranges_shared.clear(); | ||
| parts_prefix_shared.clear(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,8 +286,17 @@ pub fn generate_code_for_file_in_chunk_js<'r, 'src>( | |
| && parts_live.is_set(namespace_export_part_index as usize) | ||
| { | ||
| // SAFETY: see `parts` raw-pointer note above; index bounded by the range check just above. | ||
| let ns_part_stmts: &[Stmt] = | ||
| let mut ns_part_stmts: &[Stmt] = | ||
| unsafe { (*parts)[namespace_export_part_index as usize].stmts }.slice(); | ||
| // Step 5 ends this part of a CommonJS entry point with | ||
| // `module.exports = __toCommonJS(exports)`. Only the chunk of that entry point runs it. | ||
|
Comment on lines
+291
to
+292
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| if output_format == OutputFormat::Cjs | ||
| && flags.force_include_exports_for_entry_point | ||
| && !chunk.is_entry_point_file(source_index as u32) | ||
| && let Some((_module_exports, rest)) = ns_part_stmts.split_last() | ||
| { | ||
| ns_part_stmts = rest; | ||
| } | ||
| if let Err(err) = convert_stmts_for_chunk( | ||
| c, | ||
| source_index as u32, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code