Skip to content

Commit 4bed719

Browse files
fireairforcexusd320
andcommitted
feat(turbopack): externalType support umd (#30)
* feat(turbopack): externalType support umd * fix: clippy * fix: use CompileTimeDefineValue::Evaluate --------- Co-authored-by: xusd320 <[email protected]>
1 parent 9aec3f7 commit 4bed719

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

crates/next-core/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn defines(define_env: &FxIndexMap<RcStr, Option<RcStr>>) -> CompileTimeDefi
6666
Ok(serde_json::Value::String(v)) => {
6767
CompileTimeDefineValue::String(v.into())
6868
}
69-
_ => CompileTimeDefineValue::JSON(v.clone()),
69+
_ => CompileTimeDefineValue::Evaluate(v.clone()),
7070
}
7171
} else {
7272
CompileTimeDefineValue::Undefined

turbopack/crates/turbopack-core/src/resolve/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ pub enum ExternalType {
485485
EcmaScriptModule,
486486
Global,
487487
Script,
488+
Umd,
488489
}
489490

490491
impl Display for ExternalType {
@@ -495,6 +496,7 @@ impl Display for ExternalType {
495496
ExternalType::Url => write!(f, "url"),
496497
ExternalType::Global => write!(f, "global"),
497498
ExternalType::Script => write!(f, "script"),
499+
ExternalType::Umd => write!(f, "umd"),
498500
}
499501
}
500502
}
@@ -2773,7 +2775,10 @@ async fn resolve_import_map_result(
27732775
ExternalType::EcmaScriptModule => {
27742776
node_esm_resolve_options(alias_lookup_path.root().owned().await?)
27752777
}
2776-
ExternalType::Script | ExternalType::Url | ExternalType::Global => options,
2778+
ExternalType::Script
2779+
| ExternalType::Url
2780+
| ExternalType::Global
2781+
| ExternalType::Umd => options,
27772782
},
27782783
)
27792784
.await?

turbopack/crates/turbopack-ecmascript/src/references/external_module.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub enum CachedExternalType {
5353
EcmaScriptViaImport,
5454
Global,
5555
Script,
56+
Umd,
5657
}
5758

5859
#[derive(
@@ -76,6 +77,7 @@ impl Display for CachedExternalType {
7677
CachedExternalType::EcmaScriptViaImport => write!(f, "esm_import"),
7778
CachedExternalType::Global => write!(f, "global"),
7879
CachedExternalType::Script => write!(f, "script"),
80+
CachedExternalType::Umd => write!(f, "umd"),
7981
}
8082
}
8183
}
@@ -117,11 +119,11 @@ impl CachedExternalModule {
117119
CachedExternalType::Global => {
118120
if self.request.is_empty() {
119121
writeln!(code, "const mod = {{}};")?;
120-
} else if self.request.contains('/') {
122+
} else if self.request.contains(' ') {
121123
// Handle requests with '/' by splitting into nested global access
122124
let global_access = self
123125
.request
124-
.split('/')
126+
.split(' ')
125127
.fold("globalThis".to_string(), |acc, part| {
126128
format!("{}[{}]", acc, StringifyJs(part))
127129
});
@@ -135,6 +137,22 @@ impl CachedExternalModule {
135137
)?;
136138
}
137139
}
140+
CachedExternalType::Umd => {
141+
// request format is: "root React commonjs react"
142+
let parts = self.request.split(' ').collect::<Vec<_>>();
143+
let global_name = parts[1];
144+
let module_name = parts[3];
145+
146+
writeln!(
147+
code,
148+
"let mod; if (typeof exports === 'object' && typeof module === 'object') {{ \
149+
mod = {TURBOPACK_EXTERNAL_REQUIRE}({}, () => require({})); }} else {{ mod = \
150+
globalThis[{}] }}",
151+
StringifyJs(module_name),
152+
StringifyJs(module_name),
153+
StringifyJs(global_name),
154+
)?;
155+
}
138156
CachedExternalType::Script => {
139157
// Parse the request format: "variableName@url"
140158
// e.g., "foo@https://test.test.com"

turbopack/crates/turbopack/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,9 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
656656
custom_conditions: match ty {
657657
ExternalType::CommonJs => vec!["require".into()],
658658
ExternalType::EcmaScriptModule => vec!["import".into()],
659-
ExternalType::Url | ExternalType::Global | ExternalType::Script => vec![],
659+
ExternalType::Url | ExternalType::Global | ExternalType::Script | ExternalType::Umd => {
660+
vec![]
661+
}
660662
},
661663
..Default::default()
662664
};
@@ -972,6 +974,7 @@ pub async fn replace_external(
972974
}
973975
ExternalType::Global => CachedExternalType::Global,
974976
ExternalType::Script => CachedExternalType::Script,
977+
ExternalType::Umd => CachedExternalType::Umd,
975978
ExternalType::Url => {
976979
// we don't want to wrap url externals.
977980
return Ok(None);

0 commit comments

Comments
 (0)