Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{iter::once, str::FromStr};
use std::iter::once;

use anyhow::Result;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{iter::once, str::FromStr};
use std::iter::once;

use anyhow::{Result, bail};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn defines(define_env: &FxIndexMap<RcStr, Option<RcStr>>) -> CompileTimeDefi
Ok(serde_json::Value::String(v)) => {
CompileTimeDefineValue::String(v.into())
}
_ => CompileTimeDefineValue::JSON(v.clone()),
_ => CompileTimeDefineValue::Evaluate(v.clone()),
}
} else {
CompileTimeDefineValue::Undefined
Expand Down
7 changes: 6 additions & 1 deletion turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ pub enum ExternalType {
EcmaScriptModule,
Global,
Script,
Umd,
}

impl Display for ExternalType {
Expand All @@ -486,6 +487,7 @@ impl Display for ExternalType {
ExternalType::Url => write!(f, "url"),
ExternalType::Global => write!(f, "global"),
ExternalType::Script => write!(f, "script"),
ExternalType::Umd => write!(f, "umd"),
}
}
}
Expand Down Expand Up @@ -2768,7 +2770,10 @@ async fn resolve_import_map_result(
ExternalType::EcmaScriptModule => {
node_esm_resolve_options(alias_lookup_path.root().await?.clone_value())
}
ExternalType::Script | ExternalType::Url | ExternalType::Global => options,
ExternalType::Script
| ExternalType::Url
| ExternalType::Global
| ExternalType::Umd => options,
},
)
.await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum CachedExternalType {
EcmaScriptViaImport,
Global,
Script,
Umd,
}

impl Display for CachedExternalType {
Expand All @@ -57,6 +58,7 @@ impl Display for CachedExternalType {
CachedExternalType::EcmaScriptViaImport => write!(f, "esm_import"),
CachedExternalType::Global => write!(f, "global"),
CachedExternalType::Script => write!(f, "script"),
CachedExternalType::Umd => write!(f, "umd"),
}
}
}
Expand Down Expand Up @@ -98,11 +100,11 @@ impl CachedExternalModule {
CachedExternalType::Global => {
if self.request.is_empty() {
writeln!(code, "const mod = {{}};")?;
} else if self.request.contains('/') {
} else if self.request.contains(' ') {
// Handle requests with '/' by splitting into nested global access
let global_access = self
.request
.split('/')
.split(' ')
.fold("globalThis".to_string(), |acc, part| {
format!("{}[{}]", acc, StringifyJs(part))
});
Expand All @@ -116,6 +118,22 @@ impl CachedExternalModule {
)?;
}
}
CachedExternalType::Umd => {
// request format is: "root React commonjs react"
let parts = self.request.split(' ').collect::<Vec<_>>();
let global_name = parts[1];
let module_name = parts[3];

writeln!(
code,
"let mod; if (typeof exports === 'object' && typeof module === 'object') {{ \
mod = {TURBOPACK_EXTERNAL_REQUIRE}({}, () => require({})); }} else {{ mod = \
globalThis[{}] }}",
StringifyJs(module_name),
StringifyJs(module_name),
StringifyJs(global_name),
)?;
}
CachedExternalType::Script => {
// Parse the request format: "variableName@url"
// e.g., "foo@https://test.test.com"
Expand Down
5 changes: 4 additions & 1 deletion turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,9 @@ 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 | ExternalType::Global | ExternalType::Script => vec![],
ExternalType::Url | ExternalType::Global | ExternalType::Script | ExternalType::Umd => {
vec![]
}
},
..Default::default()
};
Expand Down Expand Up @@ -1014,6 +1016,7 @@ pub async fn replace_external(
}
ExternalType::Global => CachedExternalType::Global,
ExternalType::Script => CachedExternalType::Script,
ExternalType::Umd => CachedExternalType::Umd,
ExternalType::Url => {
// we don't want to wrap url externals.
return Ok(None);
Expand Down
Loading