Skip to content

Commit

Permalink
fix: dynamic import chunk is_entry should be false (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Nov 22, 2023
1 parent 25e25f9 commit 6f2b75a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
10 changes: 5 additions & 5 deletions crates/rolldown/src/bundler/chunk/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc::span::Atom;
use rolldown_common::{ModuleId, NamedImport, Specifier, SymbolRef};
use rolldown_common::{EntryPoint, ModuleId, NamedImport, Specifier, SymbolRef};
use rustc_hash::FxHashMap;
use string_wizard::{Joiner, JoinerOptions};

Expand All @@ -26,7 +26,7 @@ pub struct CrossChunkImportItem {

#[derive(Debug, Default)]
pub struct Chunk {
pub entry_module: Option<ModuleId>,
pub entry_point: Option<EntryPoint>,
pub modules: Vec<ModuleId>,
pub name: Option<String>,
pub file_name: Option<String>,
Expand All @@ -41,15 +41,15 @@ pub struct Chunk {
impl Chunk {
pub fn new(
name: Option<String>,
entry_module: Option<ModuleId>,
entry_point: Option<EntryPoint>,
bits: BitSet,
modules: Vec<ModuleId>,
) -> Self {
Self { entry_module, modules, name, bits, ..Self::default() }
Self { entry_point, modules, name, bits, ..Self::default() }
}

pub fn render_file_name(&mut self, output_options: &OutputOptions) {
let pat = if self.entry_module.is_some() {
let pat = if self.entry_point.is_some() {
&output_options.entry_file_names
} else {
&output_options.chunk_file_names
Expand Down
16 changes: 8 additions & 8 deletions crates/rolldown/src/bundler/chunk/render_chunk_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ impl Chunk {
graph: &LinkStageOutput,
output_options: &OutputOptions,
) -> Option<MagicString<'static>> {
if let Some(entry) = self.entry_module {
let linking_info = &graph.linking_infos[entry];
if let Some(entry) = &self.entry_point {
let linking_info = &graph.linking_infos[entry.module_id];
if matches!(linking_info.wrap_kind, WrapKind::Cjs) {
match output_options.format {
OutputFormat::Esm => {
Expand All @@ -22,7 +22,7 @@ impl Chunk {
panic!(
"Cannot find canonical name for wrap ref {:?} of {:?}",
linking_info.wrapper_ref.unwrap(),
graph.modules[entry].resource_id()
graph.modules[entry.module_id].resource_id()
)
});
return Some(MagicString::new(format!("export default {wrap_ref_name}();\n")));
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Chunk {
}

fn get_export_items(&self, graph: &LinkStageOutput) -> Vec<(Atom, SymbolRef)> {
self.entry_module.map_or_else(
self.entry_point.as_ref().map_or_else(
|| {
let mut tmp = self
.exports_to_other_chunks
Expand All @@ -75,8 +75,8 @@ impl Chunk {

tmp
},
|entry_module_id| {
let linking_info = &graph.linking_infos[entry_module_id];
|entry_point| {
let linking_info = &graph.linking_infos[entry_point.module_id];
linking_info
.sorted_exports()
.map(|(name, export)| (name.clone(), export.symbol_ref))
Expand All @@ -90,8 +90,8 @@ impl Chunk {
graph: &LinkStageOutput,
output_options: &OutputOptions,
) -> Vec<String> {
if let Some(entry) = self.entry_module {
let linking_info = &graph.linking_infos[entry];
if let Some(entry_point) = &self.entry_point {
let linking_info = &graph.linking_infos[entry_point.module_id];
if matches!(linking_info.wrap_kind, WrapKind::Cjs) {
match output_options.format {
OutputFormat::Esm => {
Expand Down
6 changes: 5 additions & 1 deletion crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::sync::Arc;

use index_vec::IndexVec;
use rolldown_common::{EntryPoint, FilePath, ImportKind, ImportRecordId, ModuleId, ResourceId};
use rolldown_common::{
EntryPoint, EntryPointKind, FilePath, ImportKind, ImportRecordId, ModuleId, ResourceId,
};
use rolldown_error::BuildError;
use rolldown_fs::FileSystem;
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -136,6 +138,7 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
.map(|(name, info)| EntryPoint {
name: name.clone(),
module_id: self.try_spawn_new_task(info, true),
kind: EntryPointKind::UserSpecified,
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -168,6 +171,7 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
dynamic_entries.insert(EntryPoint {
name: Some(info.path.unique(&self.input_options.cwd)),
module_id: id,
kind: EntryPointKind::DynamicImport,
});
}
raw_rec.into_import_record(id)
Expand Down
18 changes: 9 additions & 9 deletions crates/rolldown/src/bundler/stages/bundle_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
InputOptions, Output, OutputFormat,
};
use index_vec::{index_vec, IndexVec};
use rolldown_common::{ExportsKind, ImportKind, ModuleId, NamedImport, SymbolRef};
use rolldown_common::{EntryPointKind, ExportsKind, ImportKind, ModuleId, NamedImport, SymbolRef};
use rustc_hash::{FxHashMap, FxHashSet};

pub struct BundleStage<'a> {
Expand Down Expand Up @@ -63,10 +63,10 @@ impl<'a> BundleStage<'a> {
Output::Chunk(Box::new(OutputChunk {
file_name: c.file_name.clone().unwrap(),
code: content,
is_entry: c.entry_module.is_some(),
facade_module_id: c
.entry_module
.map(|id| self.link_output.modules[id].expect_normal().pretty_path.to_string()),
is_entry: matches!(&c.entry_point, Some(e) if e.kind == EntryPointKind::UserSpecified),
facade_module_id: c.entry_point.as_ref().map(|entry_point| {
self.link_output.modules[entry_point.module_id].expect_normal().pretty_path.to_string()
}),
modules: rendered_modules,
exports: c.get_export_names(self.link_output, self.output_options),
}))
Expand Down Expand Up @@ -163,8 +163,8 @@ impl<'a> BundleStage<'a> {
}
}

if let Some(entry_module) = chunk.entry_module {
let entry_module = &self.link_output.modules[entry_module];
if let Some(entry_point) = &chunk.entry_point {
let entry_module = &self.link_output.modules[entry_point.module_id];
let Module::Normal(entry_module) = entry_module else {
return;
};
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<'a> BundleStage<'a> {
}
}

if chunk.entry_module.is_none() {
if chunk.entry_point.is_none() {
continue;
}
// If this is an entry point, make sure we import all chunks belonging to
Expand Down Expand Up @@ -273,7 +273,7 @@ impl<'a> BundleStage<'a> {
bits.set_bit(count);
let chunk = chunks.push(Chunk::new(
entry_point.name.clone(),
Some(entry_point.module_id),
Some(entry_point.clone()),
bits.clone(),
vec![],
));
Expand Down
9 changes: 8 additions & 1 deletion crates/rolldown_common/src/entry_point.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use crate::ModuleId;

#[derive(Debug, Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct EntryPoint {
pub name: Option<String>,
pub module_id: ModuleId,
pub kind: EntryPointKind,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub enum EntryPointKind {
UserSpecified,
DynamicImport,
}
2 changes: 1 addition & 1 deletion crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod stmt_info;
mod symbol_ref;
mod wrap_kind;
pub use crate::{
entry_point::EntryPoint,
entry_point::{EntryPoint, EntryPointKind},
exports_kind::ExportsKind,
file_path::{representative_name, FilePath},
import_record::{ImportKind, ImportRecord, ImportRecordId, RawImportRecord},
Expand Down

0 comments on commit 6f2b75a

Please sign in to comment.