Skip to content

Commit

Permalink
feat(core): ensure input item always have a name (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 authored Jun 18, 2024
1 parent c42f7b3 commit 0e6afa2
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 12 deletions.
4 changes: 2 additions & 2 deletions crates/rolldown/src/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl ModuleLoader {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn fetch_all_modules(
mut self,
user_defined_entries: Vec<(Option<String>, ResolvedRequestInfo)>,
user_defined_entries: Vec<(String, ResolvedRequestInfo)>,
) -> anyhow::Result<ModuleLoaderOutput> {
if self.input_options.input.is_empty() {
return Err(anyhow::format_err!("You must supply options.input to rolldown"));
Expand All @@ -191,7 +191,7 @@ impl ModuleLoader {
let mut entry_points = user_defined_entries
.into_iter()
.map(|(name, info)| EntryPoint {
name,
name: Some(name),
id: self.try_spawn_new_task(info, true).expect_normal(),
kind: EntryPointKind::UserDefined,
})
Expand Down
4 changes: 1 addition & 3 deletions crates/rolldown/src/stages/scan_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ impl ScanStage {
/// Resolve `InputOptions.input`
#[tracing::instrument(level = "debug", skip_all)]
async fn resolve_user_defined_entries(
&mut self,
) -> Result<Vec<(Option<String>, ResolvedRequestInfo)>> {
async fn resolve_user_defined_entries(&mut self) -> Result<Vec<(String, ResolvedRequestInfo)>> {
let resolver = &self.resolver;
let plugin_driver = &self.plugin_driver;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::path::Path;

pub fn try_extract_meaningful_input_name_from_path(path: impl AsRef<Path>) -> Option<String> {
let path = path.as_ref();
let file_name = path.file_stem().and_then(|f| f.to_str()).map(ToString::to_string)?;

Some(file_name)
}

#[test]
fn test_try_extract_meaningful_input_name_from_path() {
assert_eq!(
try_extract_meaningful_input_name_from_path("foo/bar/baz.js"),
Some("baz".to_string())
);
assert_eq!(
try_extract_meaningful_input_name_from_path("react-dom"),
Some("react-dom".to_string())
);
}
1 change: 1 addition & 0 deletions crates/rolldown/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod augment_chunk_hash;
pub mod call_expression_ext;
pub mod chunk;
pub mod extract_hash_pattern;
pub mod extract_meaningful_input_name_from_path;
pub mod hash_placeholder;
pub mod load_source;
pub mod make_ast_symbol_and_scope;
Expand Down
32 changes: 30 additions & 2 deletions crates/rolldown/src/utils/normalize_options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use rolldown_common::{ModuleType, NormalizedBundlerOptions, Platform, SourceMapType};
use rolldown_common::{
ModuleType, NormalizedBundlerOptions, NormalizedInputItem, Platform, SourceMapType,
};
use rustc_hash::FxHashMap;

use super::extract_meaningful_input_name_from_path::try_extract_meaningful_input_name_from_path;

pub struct NormalizeOptionsReturn {
pub options: NormalizedBundlerOptions,
pub resolve_options: rolldown_resolver::ResolveOptions,
Expand Down Expand Up @@ -44,8 +48,32 @@ pub fn normalize_options(mut raw_options: crate::BundlerOptions) -> NormalizeOpt

loaders.extend(user_defined_loaders);

let has_only_one_input_item = matches!(&raw_options.input, Some(items) if items.len() == 1);
let input = raw_options
.input
.unwrap_or_default()
.into_iter()
.enumerate()
.map(|(idx, raw)| {
let name = raw.name.unwrap_or_else(|| {
// We try to give a meaningful name for unnamed input item.
let fallback_name =
|| if has_only_one_input_item { "input".to_string() } else { format!("input~{idx}") };

// If the input is a data URL, no way we can get a meaningful name. Just fallback to the default.
if raw.import.starts_with("data:") {
return fallback_name();
}

// If it's a file path, use the file name of it.
try_extract_meaningful_input_name_from_path(&raw.import).unwrap_or_else(fallback_name)
});
NormalizedInputItem { name, import: raw.import }
})
.collect::<Vec<_>>();

let normalized = NormalizedBundlerOptions {
input: raw_options.input.unwrap_or_default(),
input,
cwd: raw_options
.cwd
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current dir")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod input_item;
pub mod is_external;
pub mod module_type;
pub mod normalized_bundler_options;
pub mod normalized_input_item;
pub mod output_format;
pub mod output_option;
pub mod platform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use rustc_hash::FxHashMap;
use crate::ModuleType;

use super::{
filename_template::FilenameTemplate, input_item::InputItem, is_external::IsExternal,
output_format::OutputFormat, output_option::AddonOutputOption, platform::Platform,
source_map_type::SourceMapType, sourcemap_ignore_list::SourceMapIgnoreList,
sourcemap_path_transform::SourceMapPathTransform,
filename_template::FilenameTemplate, is_external::IsExternal,
normalized_input_item::NormalizedInputItem, output_format::OutputFormat,
output_option::AddonOutputOption, platform::Platform, source_map_type::SourceMapType,
sourcemap_ignore_list::SourceMapIgnoreList, sourcemap_path_transform::SourceMapPathTransform,
};

#[derive(Debug)]
pub struct NormalizedBundlerOptions {
// --- Input
pub input: Vec<InputItem>,
pub input: Vec<NormalizedInputItem>,
pub cwd: PathBuf,
pub external: Option<IsExternal>,
pub treeshake: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug)]
pub struct NormalizedInputItem {
pub name: String,
pub import: String,
}
1 change: 1 addition & 0 deletions crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod bundler_options {
is_external::IsExternal,
module_type::ModuleType,
normalized_bundler_options::NormalizedBundlerOptions,
normalized_input_item::NormalizedInputItem,
output_format::OutputFormat,
output_option::{AddonFunction, AddonOutputOption},
platform::Platform,
Expand Down

0 comments on commit 0e6afa2

Please sign in to comment.