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

Commit a5d0664

Browse files
author
Wenjie Xia
committed
refactor(compiler): improve id generating
1 parent 28d8a6b commit a5d0664

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

compiler/src/jsx.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ impl AlephJsxFold {
5353
let mut hasher = Sha1::new();
5454
hasher.update(resolver.specifier.clone());
5555
hasher.update(self.inline_style_idx.to_string());
56-
ident.push_str(base64::encode(hasher.finalize()).as_str());
56+
ident.push_str(
57+
base64::encode(hasher.finalize())
58+
.replace("/", "")
59+
.replace("+", "")
60+
.as_str()
61+
.trim_end_matches("="),
62+
);
5763
ident
5864
}
5965

@@ -69,7 +75,7 @@ impl AlephJsxFold {
6975
match name {
7076
"head" | "script" => {
7177
let mut resolver = self.resolver.borrow_mut();
72-
resolver.builtin_jsx_tags.insert(name.into());
78+
resolver.used_builtin_jsx_tags.insert(name.into());
7379
el.name = JSXElementName::Ident(quote_ident!(rename_builtin_tag(name)));
7480
}
7581

@@ -97,7 +103,7 @@ impl AlephJsxFold {
97103
}
98104

99105
if should_replace {
100-
resolver.builtin_jsx_tags.insert(name.into());
106+
resolver.used_builtin_jsx_tags.insert(name.into());
101107
el.name = JSXElementName::Ident(quote_ident!(rename_builtin_tag(name)));
102108
}
103109
}
@@ -214,7 +220,7 @@ impl AlephJsxFold {
214220
}
215221

216222
if name.eq("link") {
217-
resolver.builtin_jsx_tags.insert(name.into());
223+
resolver.used_builtin_jsx_tags.insert(name.into());
218224
el.name =
219225
JSXElementName::Ident(quote_ident!(rename_builtin_tag(name)));
220226
}
@@ -268,7 +274,7 @@ impl AlephJsxFold {
268274
is_dynamic: false,
269275
rel: None,
270276
});
271-
resolver.builtin_jsx_tags.insert(name.into());
277+
resolver.used_builtin_jsx_tags.insert(name.into());
272278
el.name = JSXElementName::Ident(quote_ident!(rename_builtin_tag(name)));
273279
inline_style = Some((type_prop_value, id.into()));
274280
}
@@ -419,7 +425,7 @@ impl Fold for AlephJsxBuiltinModuleResolveFold {
419425
let mut items = Vec::<ModuleItem>::new();
420426
let mut resolver = self.resolver.borrow_mut();
421427

422-
for mut name in resolver.builtin_jsx_tags.clone() {
428+
for mut name in resolver.used_builtin_jsx_tags.clone() {
423429
if name.eq("a") {
424430
name = "anchor".to_owned()
425431
}

compiler/src/resolve.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{
1717
rc::Rc,
1818
str::FromStr,
1919
};
20-
use swc_common::DUMMY_SP;
20+
use swc_common::{SourceMap, Span, DUMMY_SP};
2121
use swc_ecma_ast::*;
2222
use swc_ecma_utils::quote_ident;
2323
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
@@ -53,14 +53,13 @@ pub struct InlineStyle {
5353
}
5454

5555
/// A Resolver to resolve aleph.js import/export URL.
56-
#[derive(Clone, Debug, Eq, PartialEq)]
5756
pub struct Resolver {
5857
/// The text specifier associated with the import/export statement.
5958
pub specifier: String,
6059
/// A flag indicating if the specifier is remote url or not.
6160
pub specifier_is_remote: bool,
6261
/// builtin jsx tags like `a`, `link`, `head`, etc
63-
pub builtin_jsx_tags: IndexSet<String>,
62+
pub used_builtin_jsx_tags: IndexSet<String>,
6463
/// dependency graph
6564
pub dep_graph: Vec<DependencyDescriptor>,
6665
/// inline styles
@@ -88,7 +87,7 @@ impl Resolver {
8887
Resolver {
8988
specifier: specifier.into(),
9089
specifier_is_remote: is_remote_url(specifier),
91-
builtin_jsx_tags: IndexSet::new(),
90+
used_builtin_jsx_tags: IndexSet::new(),
9291
dep_graph: Vec::new(),
9392
inline_styles: HashMap::new(),
9493
import_map: ImportMap::from_hashmap(import_map),
@@ -360,27 +359,38 @@ impl Resolver {
360359
}
361360
}
362361

363-
pub fn aleph_resolve_fold(resolver: Rc<RefCell<Resolver>>) -> impl Fold {
362+
pub fn aleph_resolve_fold(resolver: Rc<RefCell<Resolver>>, source: Rc<SourceMap>) -> impl Fold {
364363
AlephResolveFold {
365364
deno_hooks_idx: 0,
366365
resolver,
366+
source,
367367
}
368368
}
369369

370370
pub struct AlephResolveFold {
371371
deno_hooks_idx: i32,
372372
resolver: Rc<RefCell<Resolver>>,
373+
source: Rc<SourceMap>,
373374
}
374375

375376
impl AlephResolveFold {
376-
fn new_use_deno_hook_ident(&mut self) -> String {
377+
fn new_use_deno_hook_ident(&mut self, callback_span: &Span) -> String {
377378
let resolver = self.resolver.borrow_mut();
378379
self.deno_hooks_idx = self.deno_hooks_idx + 1;
379380
let mut ident: String = "useDeno-".to_owned();
380381
let mut hasher = Sha1::new();
382+
let callback_code = self.source.span_to_snippet(callback_span.clone()).unwrap();
381383
hasher.update(resolver.specifier.clone());
382384
hasher.update(self.deno_hooks_idx.to_string());
383-
ident.push_str(base64::encode(hasher.finalize()).as_str());
385+
hasher.update(callback_code.clone());
386+
println!("---{}---", callback_code);
387+
ident.push_str(
388+
base64::encode(hasher.finalize())
389+
.replace("/", "")
390+
.replace("+", "")
391+
.as_str()
392+
.trim_end_matches("="),
393+
);
384394
ident
385395
}
386396
}
@@ -661,17 +671,20 @@ impl Fold for AlephResolveFold {
661671
)))),
662672
}];
663673
} else if is_call_expr_by_name(&call, "useDeno") {
664-
let has_callback = match call.args.first() {
674+
let callback_span = match call.args.first() {
665675
Some(ExprOrSpread { expr, .. }) => match expr.as_ref() {
666-
Expr::Fn(_) => true,
667-
Expr::Arrow(_) => true,
668-
_ => false,
676+
Expr::Fn(FnExpr {
677+
function: Function { span, .. },
678+
..
679+
}) => Some(span),
680+
Expr::Arrow(ArrowExpr { span, .. }) => Some(span),
681+
_ => None,
669682
},
670-
_ => false,
683+
_ => None,
671684
};
672-
if has_callback {
685+
if let Some(span) = callback_span {
673686
let bundle_mode = self.resolver.borrow().bundle_mode;
674-
let id = self.new_use_deno_hook_ident();
687+
let id = self.new_use_deno_hook_ident(span);
675688
if bundle_mode {
676689
call.args[0] = ExprOrSpread {
677690
spread: None,

compiler/src/swc.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl ParsedModule {
145145
);
146146
let root_mark = Mark::fresh(Mark::root());
147147
let mut passes = chain!(
148-
aleph_resolve_fold(resolver.clone()),
148+
aleph_resolve_fold(resolver.clone(), self.source_map.clone()),
149149
Optional::new(aleph_jsx_fold, is_jsx),
150150
Optional::new(aleph_jsx_builtin_resolve_fold, is_jsx),
151151
Optional::new(jsx_link_fixer_fold(resolver.clone()), is_jsx && bundle_mode),
@@ -368,7 +368,9 @@ mod tests {
368368
let source = r#"
369369
export default function Index() {
370370
const verison = useDeno(() => Deno.version)
371-
const verison = useDeno(async () => await readJson("data.json"), 1000)
371+
const verison = useDeno(async function() {
372+
return await readJson("./data.json")
373+
}, 1000)
372374
return (
373375
<>
374376
<p>Deno v{version.deno}</p>
@@ -382,12 +384,24 @@ mod tests {
382384
let mut hasher = Sha1::new();
383385
hasher.update(specifer.clone());
384386
hasher.update("1");
385-
let id_1 = base64::encode(hasher.finalize());
387+
hasher.update("() => Deno.version");
388+
let id_1 = base64::encode(hasher.finalize())
389+
.replace("/", "")
390+
.replace("+", "");
391+
let id_1 = id_1.trim_end_matches("=");
386392

387393
let mut hasher = Sha1::new();
388394
hasher.update(specifer.clone());
389395
hasher.update("2");
390-
let id_2 = base64::encode(hasher.finalize());
396+
hasher.update(
397+
r#"async function() {
398+
return await readJson("./data.json")
399+
}"#,
400+
);
401+
let id_2 = base64::encode(hasher.finalize())
402+
.replace("/", "")
403+
.replace("+", "");
404+
let id_2 = id_2.trim_end_matches("=");
391405

392406
for _ in 0..3 {
393407
let (code, _) = t(specifer, source, false);

0 commit comments

Comments
 (0)