Skip to content

Commit 39fe3b1

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 bb74999 commit 39fe3b1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ pub enum ExternalType {
486486
EcmaScriptModule,
487487
Global,
488488
Script,
489+
Umd,
489490
}
490491

491492
impl Display for ExternalType {
@@ -496,6 +497,7 @@ impl Display for ExternalType {
496497
ExternalType::Url => write!(f, "url"),
497498
ExternalType::Global => write!(f, "global"),
498499
ExternalType::Script => write!(f, "script"),
500+
ExternalType::Umd => write!(f, "umd"),
499501
}
500502
}
501503
}
@@ -2815,7 +2817,10 @@ async fn resolve_import_map_result(
28152817
ExternalType::EcmaScriptModule => {
28162818
node_esm_resolve_options(alias_lookup_path.root().owned().await?)
28172819
}
2818-
ExternalType::Script | ExternalType::Url | ExternalType::Global => options,
2820+
ExternalType::Script
2821+
| ExternalType::Url
2822+
| ExternalType::Global
2823+
| ExternalType::Umd => options,
28192824
},
28202825
)
28212826
.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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ pub async fn externals_tracing_module_context(
683683
custom_conditions: match ty {
684684
ExternalType::CommonJs => vec![rcstr!("require"), rcstr!("node")],
685685
ExternalType::EcmaScriptModule => vec![rcstr!("import"), rcstr!("node")],
686-
ExternalType::Url | ExternalType::Global | ExternalType::Script => vec![rcstr!("node")],
686+
ExternalType::Url | ExternalType::Global | ExternalType::Script | ExternalType::Umd => vec![rcstr!("node")],
687687
},
688688
..Default::default()
689689
};
@@ -1006,6 +1006,7 @@ pub async fn replace_external(
10061006
}
10071007
ExternalType::Global => CachedExternalType::Global,
10081008
ExternalType::Script => CachedExternalType::Script,
1009+
ExternalType::Umd => CachedExternalType::Umd,
10091010
ExternalType::Url => {
10101011
// we don't want to wrap url externals.
10111012
return Ok(None);

0 commit comments

Comments
 (0)