Skip to content

Commit

Permalink
refactor(rust): make NormalizedBundlerOptions#external optional (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Apr 11, 2024
1 parent c092e0b commit 7c1bcda
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
23 changes: 12 additions & 11 deletions crates/rolldown/src/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,25 @@ impl NormalModuleTask {
options: HookResolveIdExtraOptions,
) -> BatchedResult<ResolvedRequestInfo> {
// Check external with unresolved path
if input_options.external.call(specifier.to_string(), Some(importer.to_string()), false).await?
{
return Ok(ResolvedRequestInfo {
path: specifier.to_string().into(),
module_type: ModuleType::Unknown,
is_external: true,
});
if let Some(external) = input_options.external.as_ref() {
if external.call(specifier.to_string(), Some(importer.to_string()), false).await? {
return Ok(ResolvedRequestInfo {
path: specifier.to_string().into(),
module_type: ModuleType::Unknown,
is_external: true,
});
}
}

let mut info =
resolve_id(resolver, plugin_driver, specifier, Some(importer), options, false).await?;

if !info.is_external {
// Check external with resolved path
info.is_external = input_options
.external
.call(specifier.to_string(), Some(importer.to_string()), true)
.await?;
if let Some(external) = input_options.external.as_ref() {
info.is_external =
external.call(specifier.to_string(), Some(importer.to_string()), true).await?;
}
}
Ok(info)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/utils/normalize_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn normalize_options(mut raw_options: crate::BundlerOptions) -> NormalizeOpt
cwd: raw_options
.cwd
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current dir")),
external: raw_options.external.unwrap_or_default(),
external: raw_options.external,
treeshake: raw_options.treeshake.unwrap_or(true),
platform: raw_options.platform.unwrap_or(Platform::Browser),
entry_file_names: raw_options
Expand Down
21 changes: 9 additions & 12 deletions crates/rolldown_binding/src/utils/normalize_binding_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ pub fn normalize_binding_options(
debug_assert!(PathBuf::from(&input_options.cwd) != PathBuf::from("/"), "{input_options:#?}");
let cwd = PathBuf::from(input_options.cwd);

let external = input_options
.external
.map(|ts_fn| {
rolldown::External::Fn(Box::new(move |source, importer, is_resolved| {
let ts_fn = ts_fn.clone();
Box::pin(async move {
ts_fn.call_async((source, importer, is_resolved)).await.map_err(BuildError::from)
})
}))
})
.unwrap_or_default();
let external = input_options.external.map(|ts_fn| {
rolldown::External::Fn(Box::new(move |source, importer, is_resolved| {
let ts_fn = ts_fn.clone();
Box::pin(async move {
ts_fn.call_async((source, importer, is_resolved)).await.map_err(BuildError::from)
})
}))
});

let bundler_options = BundlerOptions {
input: Some(input_options.input.into_iter().map(Into::into).collect()),
cwd: cwd.into(),
external: external.into(),
external,
treeshake: true.into(),
resolve: input_options.resolve.map(Into::into),
platform: input_options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ impl Debug for External {
}
}

impl Default for External {
fn default() -> Self {
Self::ArrayString(vec![])
}
}

impl External {
pub async fn call(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct NormalizedBundlerOptions {
// --- Input
pub input: Vec<InputItem>,
pub cwd: PathBuf,
pub external: External,
pub external: Option<External>,
pub treeshake: bool,
pub platform: Platform,
pub shim_missing_exports: bool,
Expand Down

0 comments on commit 7c1bcda

Please sign in to comment.