Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit f6acfc8

Browse files
committed
refactor(compiler): rewrite useDeno/inline-style signature
1 parent c4180d7 commit f6acfc8

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

compiler/src/jsx.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::resolve::{
66
};
77

88
use path_slash::PathBufExt;
9-
use rand::{distributions::Alphanumeric, Rng};
9+
use sha1::{Digest, Sha1};
1010
use std::{cell::RefCell, path::PathBuf, rc::Rc};
1111
use swc_common::{SourceMap, Spanned, DUMMY_SP};
1212
use swc_ecma_ast::*;
@@ -22,6 +22,7 @@ pub fn aleph_jsx_fold(
2222
AlephJsxFold {
2323
resolver: resolver.clone(),
2424
source,
25+
inline_style_idx: 0,
2526
is_dev,
2627
},
2728
AlephJsxBuiltinModuleResolveFold {
@@ -42,27 +43,41 @@ pub fn aleph_jsx_fold(
4243
struct AlephJsxFold {
4344
resolver: Rc<RefCell<Resolver>>,
4445
source: Rc<SourceMap>,
46+
inline_style_idx: i32,
4547
is_dev: bool,
4648
}
4749

4850
impl AlephJsxFold {
51+
fn new_inline_style_ident(&mut self) -> String {
52+
let resolver = self.resolver.borrow_mut();
53+
self.inline_style_idx = self.inline_style_idx + 1;
54+
let path = format!("{}-{}", resolver.specifier, self.inline_style_idx);
55+
let mut ident: String = "inline-style-".to_owned();
56+
let mut hasher = Sha1::new();
57+
hasher.update(path.as_bytes());
58+
let hash = hasher.finalize();
59+
ident.push_str(format!("{:x}", hash).as_str());
60+
ident
61+
}
62+
4963
fn fold_jsx_opening_element(
5064
&mut self,
5165
mut el: JSXOpeningElement,
5266
) -> (JSXOpeningElement, Option<(String, String)>) {
53-
let mut resolver = self.resolver.borrow_mut();
5467
let mut inline_style: Option<(String, String)> = None;
5568

5669
match &el.name {
5770
JSXElementName::Ident(id) => {
5871
let name = id.sym.as_ref();
5972
match name {
6073
"head" | "script" => {
74+
let mut resolver = self.resolver.borrow_mut();
6175
resolver.builtin_jsx_tags.insert(name.into());
6276
el.name = JSXElementName::Ident(quote_ident!(rename_builtin_tag(name)));
6377
}
6478

6579
"a" => {
80+
let mut resolver = self.resolver.borrow_mut();
6681
let mut should_replace = true;
6782

6883
for attr in &el.attrs {
@@ -143,6 +158,7 @@ impl AlephJsxFold {
143158
};
144159
}
145160

161+
let mut resolver = self.resolver.borrow_mut();
146162
let (resolved_path, fixed_url) =
147163
resolver.resolve(href_prop_value, true);
148164

@@ -229,7 +245,7 @@ impl AlephJsxFold {
229245
};
230246
}
231247

232-
let id = new_inline_style_ident();
248+
let id = self.new_inline_style_ident();
233249
let id_attr = JSXAttrOrSpread::JSXAttr(JSXAttr {
234250
span: DUMMY_SP,
235251
name: JSXAttrName::Ident(quote_ident!("__styleId")),
@@ -246,11 +262,11 @@ impl AlephJsxFold {
246262
el.attrs.push(id_attr);
247263
}
248264

265+
let mut resolver = self.resolver.borrow_mut();
249266
resolver.dep_graph.push(DependencyDescriptor {
250267
specifier: "#".to_owned() + id.as_str(),
251268
is_dynamic: false,
252269
});
253-
254270
resolver.builtin_jsx_tags.insert(name.into());
255271
el.name = JSXElementName::Ident(quote_ident!(rename_builtin_tag(name)));
256272
inline_style = Some((type_prop_value, id.into()));
@@ -268,6 +284,7 @@ impl AlephJsxFold {
268284

269285
// copy from https://github.com/swc-project/swc/blob/master/ecmascript/transforms/src/react/jsx_src.rs
270286
if self.is_dev {
287+
let resolver = self.resolver.borrow_mut();
271288
match self.source.span_to_lines(el.span) {
272289
Ok(file_lines) => {
273290
el.attrs.push(JSXAttrOrSpread::JSXAttr(JSXAttr {
@@ -463,14 +480,3 @@ fn rename_builtin_tag(name: &str) -> String {
463480
}
464481
"__ALEPH_".to_owned() + name.as_str()
465482
}
466-
467-
fn new_inline_style_ident() -> String {
468-
let mut ident: String = "inline-style-".to_owned();
469-
let rand_id = rand::thread_rng()
470-
.sample_iter(&Alphanumeric)
471-
.take(9)
472-
.map(char::from)
473-
.collect::<String>();
474-
ident.push_str(rand_id.as_str());
475-
return ident;
476-
}

compiler/src/resolve.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::import_map::{ImportHashMap, ImportMap};
66
use indexmap::IndexSet;
77
use path_slash::PathBufExt;
88
use pathdiff::diff_paths;
9-
use rand::{distributions::Alphanumeric, Rng};
109
use regex::Regex;
1110
use relative_path::RelativePath;
1211
use serde::Serialize;
12+
use sha1::{Digest, Sha1};
1313
use std::{
1414
cell::RefCell,
1515
collections::HashMap,
@@ -359,13 +359,31 @@ impl Resolver {
359359
}
360360

361361
pub fn aleph_resolve_fold(resolver: Rc<RefCell<Resolver>>) -> impl Fold {
362-
AlephResolveFold { resolver }
362+
AlephResolveFold {
363+
deno_hooks_idx: 0,
364+
resolver,
365+
}
363366
}
364367

365368
pub struct AlephResolveFold {
369+
deno_hooks_idx: i32,
366370
resolver: Rc<RefCell<Resolver>>,
367371
}
368372

373+
impl AlephResolveFold {
374+
fn new_use_deno_hook_ident(&mut self) -> String {
375+
let resolver = self.resolver.borrow_mut();
376+
self.deno_hooks_idx = self.deno_hooks_idx + 1;
377+
let path = format!("{}-{}", resolver.specifier, self.deno_hooks_idx);
378+
let mut ident: String = "useDeno-".to_owned();
379+
let mut hasher = Sha1::new();
380+
hasher.update(path.as_bytes());
381+
let hash = hasher.finalize();
382+
ident.push_str(format!("{:x}", hash).as_str());
383+
ident
384+
}
385+
}
386+
369387
impl Fold for AlephResolveFold {
370388
noop_fold_type!();
371389

@@ -623,7 +641,6 @@ impl Fold for AlephResolveFold {
623641
// - `import("https://esm.sh/rect")` -> `import("/-/esm.sh/react.js")`
624642
// - `useDeno(() => {})` -> `useDeno(() => {}, false, "useDeno.RANDOM_KEY")`
625643
fn fold_call_expr(&mut self, mut call: CallExpr) -> CallExpr {
626-
let mut resolver = self.resolver.borrow_mut();
627644
if is_call_expr_by_name(&call, "import") {
628645
let url = match call.args.first() {
629646
Some(ExprOrSpread { expr, .. }) => match expr.as_ref() {
@@ -635,6 +652,7 @@ impl Fold for AlephResolveFold {
635652
},
636653
_ => return call,
637654
};
655+
let mut resolver = self.resolver.borrow_mut();
638656
call.args = vec![ExprOrSpread {
639657
spread: None,
640658
expr: Box::new(Expr::Lit(Lit::Str(new_js_str(
@@ -651,7 +669,7 @@ impl Fold for AlephResolveFold {
651669
_ => false,
652670
};
653671
if has_callback {
654-
let id = new_use_deno_hook_ident();
672+
let id = self.new_use_deno_hook_ident();
655673
if call.args.len() == 1 {
656674
call.args.push(ExprOrSpread {
657675
spread: None,
@@ -672,6 +690,7 @@ impl Fold for AlephResolveFold {
672690
expr: Box::new(Expr::Lit(Lit::Str(new_js_str(id.clone())))),
673691
});
674692
}
693+
let mut resolver = self.resolver.borrow_mut();
675694
resolver.dep_graph.push(DependencyDescriptor {
676695
specifier: "#".to_owned() + id.clone().as_str(),
677696
is_dynamic: false,
@@ -698,17 +717,6 @@ pub fn is_call_expr_by_name(call: &CallExpr, name: &str) -> bool {
698717
}
699718
}
700719

701-
fn new_use_deno_hook_ident() -> String {
702-
let mut ident: String = "useDeno-".to_owned();
703-
let rand_id = rand::thread_rng()
704-
.sample_iter(&Alphanumeric)
705-
.take(9)
706-
.map(char::from)
707-
.collect::<String>();
708-
ident.push_str(rand_id.as_str());
709-
ident
710-
}
711-
712720
pub fn create_aleph_pack_var_decl(ident: Ident, url: &str, prop: Option<&str>) -> VarDeclarator {
713721
let m = Expr::Member(MemberExpr {
714722
span: DUMMY_SP,

0 commit comments

Comments
 (0)