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

Commit fcc9893

Browse files
committed
refactor(compiler): improve resolve fold
1 parent 3dd2cd8 commit fcc9893

File tree

5 files changed

+452
-343
lines changed

5 files changed

+452
-343
lines changed

compiler/src/jsx.rs

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::resolve::{is_remote_url, DependencyDescriptor, InlineStyle, Resolver};
2-
use crate::resolve_fold::create_aleph_pack_var_decl;
2+
use crate::resolve_fold::create_aleph_pack_var_decl_member;
33

44
use sha1::{Digest, Sha1};
55
use std::{cell::RefCell, rc::Rc};
@@ -371,12 +371,11 @@ impl Fold for AlephJsxBuiltinModuleResolveFold {
371371
if resolver.bundle_mode {
372372
items.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
373373
span: DUMMY_SP,
374-
kind: VarDeclKind::Var,
374+
kind: VarDeclKind::Const,
375375
declare: false,
376-
decls: vec![create_aleph_pack_var_decl(
377-
id,
376+
decls: vec![create_aleph_pack_var_decl_member(
378377
fixed_url.as_str(),
379-
Some("default"),
378+
vec![(id, Some("default".into()))],
380379
)],
381380
}))));
382381
} else {
@@ -431,3 +430,126 @@ fn rename_builtin_tag(name: &str) -> String {
431430
}
432431
"__ALEPH_".to_owned() + name.as_str()
433432
}
433+
434+
#[cfg(test)]
435+
mod tests {
436+
use crate::resolve::DependencyDescriptor;
437+
use crate::swc::st;
438+
439+
#[test]
440+
fn resolve_jsx_builtin_tags() {
441+
let source = r#"
442+
import React from "https://esm.sh/react"
443+
export default function Index() {
444+
return (
445+
<>
446+
<head>
447+
<title>Hello World!</title>
448+
<link rel="stylesheet" href="../style/index.css" />
449+
</head>
450+
<a href="/about">About</a>
451+
<a href="https://github.com">About</a>
452+
<a href="/about" target="_blank">About</a>
453+
<script src="ga.js"></script>
454+
<script>{`
455+
function gtag() {
456+
dataLayer.push(arguments)
457+
}
458+
window.dataLayer = window.dataLayer || [];
459+
gtag("js", new Date());
460+
gtag("config", "G-1234567890");
461+
`}</script>
462+
</>
463+
)
464+
}
465+
"#;
466+
let (code, resolver) = st("/pages/index.tsx", source, false);
467+
assert!(code.contains(
468+
"import __ALEPH_Anchor from \"../-/deno.land/x/[email protected]/framework/react/anchor.js\""
469+
));
470+
assert!(code.contains(
471+
"import __ALEPH_Head from \"../-/deno.land/x/[email protected]/framework/react/head.js\""
472+
));
473+
assert!(code.contains(
474+
"import __ALEPH_Stylelink from \"../-/deno.land/x/[email protected]/framework/react/stylelink.js\""
475+
));
476+
assert!(code.contains(
477+
"import __ALEPH_Script from \"../-/deno.land/x/[email protected]/framework/react/script.js\""
478+
));
479+
assert!(code.contains("React.createElement(\"a\","));
480+
assert!(code.contains("React.createElement(__ALEPH_Anchor,"));
481+
assert!(code.contains("React.createElement(__ALEPH_Head,"));
482+
assert!(code.contains("React.createElement(__ALEPH_Stylelink,"));
483+
assert!(code.contains("href: \"/style/index.css\""));
484+
assert!(code.contains(
485+
format!(
486+
"import \"../style/index.css.js#{}@000000\"",
487+
"/style/index.css"
488+
)
489+
.as_str()
490+
));
491+
assert!(code.contains("React.createElement(__ALEPH_Script,"));
492+
let r = resolver.borrow_mut();
493+
assert_eq!(
494+
r.dep_graph,
495+
vec![
496+
DependencyDescriptor {
497+
specifier: "https://esm.sh/react".into(),
498+
is_dynamic: false,
499+
},
500+
DependencyDescriptor {
501+
specifier: "/style/index.css".into(),
502+
is_dynamic: false,
503+
},
504+
DependencyDescriptor {
505+
specifier: "https://deno.land/x/[email protected]/framework/react/head.ts".into(),
506+
is_dynamic: false,
507+
},
508+
DependencyDescriptor {
509+
specifier: "https://deno.land/x/[email protected]/framework/react/stylelink.ts".into(),
510+
is_dynamic: false,
511+
},
512+
DependencyDescriptor {
513+
specifier: "https://deno.land/x/[email protected]/framework/react/anchor.ts".into(),
514+
is_dynamic: false,
515+
},
516+
DependencyDescriptor {
517+
specifier: "https://deno.land/x/[email protected]/framework/react/script.ts".into(),
518+
is_dynamic: false,
519+
}
520+
]
521+
);
522+
}
523+
524+
#[test]
525+
fn resolve_inlie_style() {
526+
let source = r#"
527+
export default function Index() {
528+
const [color, setColor] = useState('white');
529+
530+
return (
531+
<>
532+
<style>{`
533+
:root {
534+
--color: ${color};
535+
}
536+
`}</style>
537+
<style>{`
538+
h1 {
539+
font-size: 12px;
540+
}
541+
`}</style>
542+
</>
543+
)
544+
}
545+
"#;
546+
let (code, resolver) = st("/pages/index.tsx", source, false);
547+
assert!(code.contains(
548+
"import __ALEPH_Style from \"../-/deno.land/x/[email protected]/framework/react/style.js\""
549+
));
550+
assert!(code.contains("React.createElement(__ALEPH_Style,"));
551+
assert!(code.contains("__styleId: \"inline-style-"));
552+
let r = resolver.borrow_mut();
553+
assert!(r.inline_styles.len() == 2);
554+
}
555+
}

compiler/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub struct TransformOutput {
9999
pub map: Option<String>,
100100
pub deps: Vec<DependencyDescriptor>,
101101
pub inline_styles: HashMap<String, InlineStyle>,
102+
pub star_exports: Vec<String>,
102103
}
103104

104105
#[wasm_bindgen(js_name = "transformSync")]
@@ -151,6 +152,7 @@ pub fn transform_sync(url: &str, code: &str, options: JsValue) -> Result<JsValue
151152
map,
152153
deps: r.dep_graph.clone(),
153154
inline_styles: r.inline_styles.clone(),
155+
star_exports: r.star_exports.clone(),
154156
})
155157
.unwrap(),
156158
)

compiler/src/resolve.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub struct Resolver {
4949
pub used_builtin_jsx_tags: IndexSet<String>,
5050
/// dependency graph
5151
pub dep_graph: Vec<DependencyDescriptor>,
52+
/// star exports
53+
pub star_exports: Vec<String>,
5254
/// inline styles
5355
pub inline_styles: HashMap<String, InlineStyle>,
5456
/// bundle mode
@@ -82,6 +84,7 @@ impl Resolver {
8284
specifier_is_remote: is_remote_url(specifier),
8385
used_builtin_jsx_tags: IndexSet::new(),
8486
dep_graph: Vec::new(),
87+
star_exports: Vec::new(),
8588
inline_styles: HashMap::new(),
8689
import_map: ImportMap::from_hashmap(import_map),
8790
aleph_pkg_uri,
@@ -189,13 +192,11 @@ impl Resolver {
189192

190193
/// resolve import/export url.
191194
// [/pages/index.tsx]
192-
// - `https://esm.sh/swr` -> `/-/esm.sh/swr.js`
193-
// - `https://esm.sh/react` -> `/-/esm.sh/react@${REACT_VERSION}.js`
194-
// - `https://deno.land/x/aleph/mod.ts` -> `/-/deno.land/x/aleph@v${CURRENT_ALEPH_VERSION}/mod.ts`
195-
// - `../components/logo.tsx` -> `/components/logo.js#{HASH}`
196-
// - `../styles/app.css` -> `/styles/app.css.js#{HASH}`
197-
// - `@/components/logo.tsx` -> `/components/logo.js#{HASH}`
198-
// - `~/components/logo.tsx` -> `/components/logo.js#{HASH}`
195+
// - `https://esm.sh/swr` -> `../-/esm.sh/swr.js`
196+
// - `https://esm.sh/react` -> `../-/esm.sh/react@${REACT_VERSION}.js`
197+
// - `https://deno.land/x/aleph/mod.ts` -> `../-/deno.land/x/aleph@v${CURRENT_ALEPH_VERSION}/mod.ts`
198+
// - `../components/logo.tsx` -> `../components/logo.js#/styles/app.css@000000`
199+
// - `../styles/app.css` -> `../styles/app.css.js#/styles/app.css@000000`
199200
pub fn resolve(&mut self, url: &str, is_dynamic: bool) -> (String, String) {
200201
// apply import map
201202
let url = self.import_map.resolve(self.specifier.as_str(), url);

0 commit comments

Comments
 (0)