Skip to content

Commit 0bc7709

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 <xusd320@gmail.com>
1 parent 4e12c75 commit 0bc7709

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

crates/next-core/src/next_client/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{iter::once, str::FromStr};
1+
use std::iter::once;
22

33
use anyhow::Result;
44
use serde::{Deserialize, Serialize};

crates/next-core/src/next_server/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{iter::once, str::FromStr};
1+
use std::iter::once;
22

33
use anyhow::{Result, bail};
44
use serde::{Deserialize, Serialize};

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
@@ -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
}
@@ -2778,7 +2780,10 @@ async fn resolve_import_map_result(
27782780
ExternalType::EcmaScriptModule => {
27792781
node_esm_resolve_options(alias_lookup_path.root().await?.clone_value())
27802782
}
2781-
ExternalType::Script | ExternalType::Url | ExternalType::Global => options,
2783+
ExternalType::Script
2784+
| ExternalType::Url
2785+
| ExternalType::Global
2786+
| ExternalType::Umd => options,
27822787
},
27832788
)
27842789
.await?

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub enum CachedExternalType {
4747
EcmaScriptViaImport,
4848
Global,
4949
Script,
50+
Umd,
5051
}
5152

5253
impl Display for CachedExternalType {
@@ -57,6 +58,7 @@ impl Display for CachedExternalType {
5758
CachedExternalType::EcmaScriptViaImport => write!(f, "esm_import"),
5859
CachedExternalType::Global => write!(f, "global"),
5960
CachedExternalType::Script => write!(f, "script"),
61+
CachedExternalType::Umd => write!(f, "umd"),
6062
}
6163
}
6264
}
@@ -98,11 +100,11 @@ impl CachedExternalModule {
98100
CachedExternalType::Global => {
99101
if self.request.is_empty() {
100102
writeln!(code, "const mod = {{}};")?;
101-
} else if self.request.contains('/') {
103+
} else if self.request.contains(' ') {
102104
// Handle requests with '/' by splitting into nested global access
103105
let global_access = self
104106
.request
105-
.split('/')
107+
.split(' ')
106108
.fold("globalThis".to_string(), |acc, part| {
107109
format!("{}[{}]", acc, StringifyJs(part))
108110
});
@@ -116,6 +118,22 @@ impl CachedExternalModule {
116118
)?;
117119
}
118120
}
121+
CachedExternalType::Umd => {
122+
// request format is: "root React commonjs react"
123+
let parts = self.request.split(' ').collect::<Vec<_>>();
124+
let global_name = parts[1];
125+
let module_name = parts[3];
126+
127+
writeln!(
128+
code,
129+
"let mod; if (typeof exports === 'object' && typeof module === 'object') {{ \
130+
mod = {TURBOPACK_EXTERNAL_REQUIRE}({}, () => require({})); }} else {{ mod = \
131+
globalThis[{}] }}",
132+
StringifyJs(module_name),
133+
StringifyJs(module_name),
134+
StringifyJs(global_name),
135+
)?;
136+
}
119137
CachedExternalType::Script => {
120138
// Parse the request format: "variableName@url"
121139
// 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
@@ -660,7 +660,9 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
660660
custom_conditions: match ty {
661661
ExternalType::CommonJs => vec!["require".into()],
662662
ExternalType::EcmaScriptModule => vec!["import".into()],
663-
ExternalType::Url | ExternalType::Global | ExternalType::Script => vec![],
663+
ExternalType::Url | ExternalType::Global | ExternalType::Script | ExternalType::Umd => {
664+
vec![]
665+
}
664666
},
665667
..Default::default()
666668
};
@@ -1014,6 +1016,7 @@ pub async fn replace_external(
10141016
}
10151017
ExternalType::Global => CachedExternalType::Global,
10161018
ExternalType::Script => CachedExternalType::Script,
1019+
ExternalType::Umd => CachedExternalType::Umd,
10171020
ExternalType::Url => {
10181021
// we don't want to wrap url externals.
10191022
return Ok(None);

0 commit comments

Comments
 (0)