Skip to content

feat(turbopack): externalType support script #80768

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

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ pub enum ExternalType {
CommonJs,
EcmaScriptModule,
Global,
Script,
}

impl Display for ExternalType {
Expand All @@ -487,6 +488,7 @@ impl Display for ExternalType {
ExternalType::EcmaScriptModule => write!(f, "esm"),
ExternalType::Url => write!(f, "url"),
ExternalType::Global => write!(f, "global"),
ExternalType::Script => write!(f, "script"),
}
}
}
Expand Down Expand Up @@ -2785,15 +2787,14 @@ async fn resolve_import_map_result(
**alias_lookup_path,
request,
match ty {
ExternalType::Url => options,
// TODO is that root correct?
ExternalType::CommonJs => {
node_cjs_resolve_options(alias_lookup_path.root())
}
ExternalType::EcmaScriptModule => {
node_esm_resolve_options(alias_lookup_path.root())
}
ExternalType::Global => options,
ExternalType::Script | ExternalType::Url | ExternalType::Global => options,
},
)
.await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
references::async_module::{AsyncModule, OptionAsyncModule},
runtime_functions::{
TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXTERNAL_IMPORT, TURBOPACK_EXTERNAL_REQUIRE,
TURBOPACK_LOAD_BY_URL,
},
utils::StringifyJs,
};
Expand All @@ -45,6 +46,7 @@ pub enum CachedExternalType {
EcmaScriptViaRequire,
EcmaScriptViaImport,
Global,
Script,
}

impl Display for CachedExternalType {
Expand All @@ -54,6 +56,7 @@ impl Display for CachedExternalType {
CachedExternalType::EcmaScriptViaRequire => write!(f, "esm_require"),
CachedExternalType::EcmaScriptViaImport => write!(f, "esm_import"),
CachedExternalType::Global => write!(f, "global"),
CachedExternalType::Script => write!(f, "script"),
}
}
}
Expand Down Expand Up @@ -103,6 +106,60 @@ impl CachedExternalModule {
)?;
}
}
CachedExternalType::Script => {
// Parse the request format: "variableName@url"
// e.g., "foo@https://test.test.com"
if let Some(at_index) = self.request.find('@') {
let variable_name = &self.request[..at_index];
let url = &self.request[at_index + 1..];

// Wrap the loading and variable access in a try-catch block
writeln!(code, "let mod;")?;
writeln!(code, "try {{")?;

// First load the URL
writeln!(
code,
" await {TURBOPACK_LOAD_BY_URL}({});",
StringifyJs(url)
)?;

// Then get the variable from global with existence check
writeln!(
code,
" if (typeof global[{}] === 'undefined') {{",
StringifyJs(variable_name)
)?;
writeln!(
code,
" throw new Error('Variable {} is not available on global object after \
loading {}');",
StringifyJs(variable_name),
StringifyJs(url)
)?;
writeln!(code, " }}")?;
writeln!(code, " mod = global[{}];", StringifyJs(variable_name))?;

// Catch and re-throw errors with more context
writeln!(code, "}} catch (error) {{")?;
writeln!(
code,
" throw new Error('Failed to load external URL module {}: ' + \
(error.message || error));",
StringifyJs(&self.request)
)?;
writeln!(code, "}}")?;
} else {
// Invalid format - throw error
writeln!(
code,
"throw new Error('Invalid URL external format. Expected \"variable@url\", \
got: {}');",
StringifyJs(&self.request)
)?;
writeln!(code, "const mod = undefined;")?;
}
}
CachedExternalType::EcmaScriptViaRequire | CachedExternalType::CommonJs => {
writeln!(
code,
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,7 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
custom_conditions: match ty {
ExternalType::CommonJs => vec!["require".into()],
ExternalType::EcmaScriptModule => vec!["import".into()],
ExternalType::Url => vec![],
ExternalType::Global => vec![],
ExternalType::Url | ExternalType::Global | ExternalType::Script => vec![],
},
..Default::default()
};
Expand Down Expand Up @@ -1014,6 +1013,7 @@ pub async fn replace_external(
}
}
ExternalType::Global => CachedExternalType::Global,
ExternalType::Script => CachedExternalType::Script,
ExternalType::Url => {
// we don't want to wrap url externals.
return Ok(None);
Expand Down
Loading