Skip to content

Commit ba03cf6

Browse files
committed
fewer conversions
1 parent 6ccdf47 commit ba03cf6

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

crates/next-custom-transforms/src/transforms/warn_for_edge_runtime.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::sync::Arc;
22

33
use swc_core::{
44
atoms::{Atom, Wtf8Atom},
5-
common::{errors::HANDLER, SourceMap, Span},
5+
common::{SourceMap, Span, errors::HANDLER},
66
ecma::{
77
ast::{
8-
op, BinExpr, CallExpr, Callee, CondExpr, Expr, IdentName, IfStmt, ImportDecl, Lit,
9-
MemberExpr, MemberProp, NamedExport, UnaryExpr,
8+
BinExpr, CallExpr, Callee, CondExpr, Expr, IdentName, IfStmt, ImportDecl, Lit,
9+
MemberExpr, MemberProp, NamedExport, UnaryExpr, op,
1010
},
1111
utils::{ExprCtx, ExprExt},
1212
visit::{Visit, VisitWith},
@@ -188,18 +188,19 @@ where
188188
EmitError: Fn(Span, String),
189189
{
190190
fn warn_if_nodejs_module(&self, span: Span, module_specifier: &Wtf8Atom) -> Option<()> {
191-
let module_specifier = module_specifier.as_str()?;
191+
let module_specifier_str = module_specifier.as_str()?;
192192
if self.guarded_runtime {
193193
return None;
194194
}
195195

196196
// Node.js modules can be loaded with `node:` prefix or directly
197-
if module_specifier.starts_with("node:") || NODEJS_MODULE_NAMES.contains(&module_specifier)
197+
if module_specifier.starts_with("node:")
198+
|| NODEJS_MODULE_NAMES.contains(&module_specifier_str)
198199
{
199200
let loc = self.cm.lookup_line(span.lo).ok()?;
200201

201202
let msg = format!(
202-
"A Node.js module is loaded ('{module_specifier}' at line {}) which is not \
203+
"A Node.js module is loaded ('{module_specifier_str}' at line {}) which is not \
203204
supported in the Edge Runtime.
204205
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime",
205206
loc.line + 1

turbopack/crates/turbopack-ecmascript/src/analyzer/imports.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ use crate::{
2929
pub struct ImportAnnotations {
3030
// TODO store this in more structured way
3131
#[turbo_tasks(trace_ignore)]
32-
map: BTreeMap<Atom, Atom>,
32+
map: BTreeMap<Wtf8Atom, Wtf8Atom>,
3333
}
3434

3535
/// Enables a specified transition for the annotated import
36-
static ANNOTATION_TRANSITION: Lazy<Atom> =
36+
static ANNOTATION_TRANSITION: Lazy<Wtf8Atom> =
3737
Lazy::new(|| crate::annotations::ANNOTATION_TRANSITION.into());
3838

3939
/// Changes the chunking type for the annotated import
40-
static ANNOTATION_CHUNKING_TYPE: Lazy<Atom> =
40+
static ANNOTATION_CHUNKING_TYPE: Lazy<Wtf8Atom> =
4141
Lazy::new(|| crate::annotations::ANNOTATION_CHUNKING_TYPE.into());
4242

4343
/// Changes the type of the resolved module (only "json" is supported currently)
44-
static ATTRIBUTE_MODULE_TYPE: Lazy<Atom> = Lazy::new(|| atom!("type"));
44+
static ATTRIBUTE_MODULE_TYPE: Lazy<Wtf8Atom> = Lazy::new(|| atom!("type").into());
4545

4646
impl ImportAnnotations {
4747
pub fn parse(with: Option<&ObjectLit>) -> ImportAnnotations {
@@ -64,16 +64,13 @@ impl ImportAnnotations {
6464
Some((&kv.key, str))
6565
}) {
6666
let key = match key {
67-
PropName::Ident(ident) => Cow::Borrowed(ident.sym.as_str()),
68-
PropName::Str(str) => str.value.to_string_lossy(),
67+
PropName::Ident(ident) => ident.sym.clone().into(),
68+
PropName::Str(str) => str.value.clone(),
6969
// the rest are invalid, ignore for now till SWC ast is correct
7070
_ => continue,
7171
};
7272

73-
map.insert(
74-
key.into(),
75-
value.value.to_string_lossy().into_owned().into(),
76-
);
73+
map.insert(key, value.value.clone());
7774
}
7875

7976
ImportAnnotations { map }
@@ -98,42 +95,46 @@ impl ImportAnnotations {
9895
continue;
9996
};
10097

101-
map.insert(key.as_str().into(), value.as_str().into());
98+
map.insert(
99+
key.as_atom().into_owned().into(),
100+
value.as_atom().into_owned().into(),
101+
);
102102
}
103103

104104
Some(ImportAnnotations { map })
105105
}
106106

107107
/// Returns the content on the transition annotation
108-
pub fn transition(&self) -> Option<&str> {
108+
pub fn transition(&self) -> Option<Cow<'_, str>> {
109109
self.get(&ANNOTATION_TRANSITION)
110+
.map(|v| v.to_string_lossy())
110111
}
111112

112113
/// Returns the content on the chunking-type annotation
113-
pub fn chunking_type(&self) -> Option<&str> {
114+
pub fn chunking_type(&self) -> Option<&Wtf8Atom> {
114115
self.get(&ANNOTATION_CHUNKING_TYPE)
115116
}
116117

117118
/// Returns the content on the type attribute
118-
pub fn module_type(&self) -> Option<&str> {
119+
pub fn module_type(&self) -> Option<&Wtf8Atom> {
119120
self.get(&ATTRIBUTE_MODULE_TYPE)
120121
}
121122

122-
pub fn get(&self, key: &Atom) -> Option<&str> {
123-
self.map.get(key).map(|w| w.as_str())
123+
pub fn get(&self, key: &Wtf8Atom) -> Option<&Wtf8Atom> {
124+
self.map.get(key)
124125
}
125126
}
126127

127128
impl Display for ImportAnnotations {
128129
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129130
let mut it = self.map.iter();
130131
if let Some((k, v)) = it.next() {
131-
write!(f, "{{ {k}: {v}")?
132+
write!(f, "{{ {}: {}", k.to_string_lossy(), v.to_string_lossy())?
132133
} else {
133134
return f.write_str("{}");
134135
};
135136
for (k, v) in it {
136-
write!(f, ", {k}: {v}")?
137+
write!(f, ", {}: {}", k.to_string_lossy(), v.to_string_lossy())?
137138
}
138139
f.write_str(" }")
139140
}

turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ impl EsmAssetReference {
391391
impl ModuleReference for EsmAssetReference {
392392
#[turbo_tasks::function]
393393
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
394-
let ty = if matches!(self.annotations.module_type(), Some("json")) {
394+
let ty = if self.annotations.module_type().is_some_and(|v| v == "json") {
395395
EcmaScriptModulesReferenceSubType::ImportWithType(ImportWithType::Json)
396-
} else if matches!(self.annotations.module_type(), Some("bytes")) {
396+
} else if self.annotations.module_type().is_some_and(|v| v == "bytes") {
397397
EcmaScriptModulesReferenceSubType::ImportWithType(ImportWithType::Bytes)
398398
} else if let Some(part) = &self.export_name {
399399
EcmaScriptModulesReferenceSubType::ImportPart(part.clone())
@@ -489,13 +489,18 @@ impl ChunkableModuleReference for EsmAssetReference {
489489
fn chunking_type(&self) -> Result<Vc<ChunkingTypeOption>> {
490490
Ok(Vc::cell(
491491
if let Some(chunking_type) = self.annotations.chunking_type() {
492-
match chunking_type {
493-
"parallel" => Some(ChunkingType::Parallel {
492+
if chunking_type == "parallel" {
493+
Some(ChunkingType::Parallel {
494494
inherit_async: true,
495495
hoisted: true,
496-
}),
497-
"none" => None,
498-
_ => return Err(anyhow!("unknown chunking_type: {}", chunking_type)),
496+
})
497+
} else if chunking_type == "none" {
498+
None
499+
} else {
500+
return Err(anyhow!(
501+
"unknown chunking_type: {}",
502+
chunking_type.to_string_lossy()
503+
));
499504
}
500505
} else {
501506
Some(ChunkingType::Parallel {
@@ -525,7 +530,7 @@ impl EsmAssetReference {
525530
let this = &*self.await?;
526531

527532
// only chunked references can be imported
528-
if this.annotations.chunking_type() != Some("none") {
533+
if this.annotations.chunking_type().is_none_or(|v| v != "none") {
529534
let import_externals = this.import_externals;
530535
let referenced_asset = self.get_referenced_asset().await?;
531536

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use regex::Regex;
4141
use rustc_hash::{FxHashMap, FxHashSet};
4242
use serde::{Deserialize, Serialize};
4343
use swc_core::{
44-
atoms::{Atom, atom},
44+
atoms::{Atom, Wtf8Atom, atom},
4545
common::{
4646
GLOBALS, Globals, Span, Spanned,
4747
comments::{CommentKind, Comments},
@@ -3920,11 +3920,13 @@ impl From<Vec<AstParentKind>> for AstPath {
39203920
}
39213921

39223922
pub static TURBOPACK_HELPER: Lazy<Atom> = Lazy::new(|| atom!("__turbopack-helper__"));
3923+
pub static TURBOPACK_HELPER_WTF8: Lazy<Wtf8Atom> =
3924+
Lazy::new(|| atom!("__turbopack-helper__").into());
39233925

39243926
pub fn is_turbopack_helper_import(import: &ImportDecl) -> bool {
39253927
let annotations = ImportAnnotations::parse(import.with.as_deref());
39263928

3927-
annotations.get(&TURBOPACK_HELPER).is_some()
3929+
annotations.get(&TURBOPACK_HELPER_WTF8).is_some()
39283930
}
39293931

39303932
pub fn is_swc_helper_import(import: &ImportDecl) -> bool {

0 commit comments

Comments
 (0)