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

Commit 9e9323e

Browse files
committed
refactor(compiler): use aleph_module_url instead
of the static build version
1 parent db5b845 commit 9e9323e

File tree

5 files changed

+61
-72
lines changed

5 files changed

+61
-72
lines changed

compiler/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface TransformOptions {
1717
url: string
1818
importMap?: ImportMap
1919
reactVersion?: string,
20+
alephModuleUrl?: string,
2021
swcOptions?: SWCOptions
2122
isDev?: boolean,
2223
bundleMode?: boolean,

compiler/src/jsx.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2020-2021 postUI Lab. All rights reserved. MIT license.
22

3-
use crate::aleph::VERSION;
43
use crate::resolve::{
54
create_aleph_pack_var_decl, is_remote_url, DependencyDescriptor, InlineStyle, Resolver,
65
};
@@ -409,6 +408,7 @@ impl Fold for AlephJsxBuiltinModuleResolveFold {
409408

410409
fn fold_module_items(&mut self, module_items: Vec<ModuleItem>) -> Vec<ModuleItem> {
411410
let mut items = Vec::<ModuleItem>::new();
411+
let aleph_module_url = self.resolver.borrow().get_aleph_module_url();
412412
let mut resolver = self.resolver.borrow_mut();
413413

414414
for mut name in resolver.used_builtin_jsx_tags.clone() {
@@ -417,12 +417,7 @@ impl Fold for AlephJsxBuiltinModuleResolveFold {
417417
}
418418
let id = quote_ident!(rename_builtin_tag(name.as_str()));
419419
let (resolved_path, fixed_url) = resolver.resolve(
420-
format!(
421-
"https://deno.land/x/aleph@v{}/framework/react/{}.ts",
422-
VERSION.as_str(),
423-
name
424-
)
425-
.as_str(),
420+
format!("{}/framework/react/{}.ts", aleph_module_url, name).as_str(),
426421
false,
427422
None,
428423
);

compiler/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#[macro_use]
44
extern crate lazy_static;
55

6-
mod aleph;
76
mod error;
87
mod fast_refresh;
98
mod fixer;
@@ -31,7 +30,10 @@ pub struct Options {
3130
#[serde(default)]
3231
pub import_map: ImportHashMap,
3332

34-
#[serde(default = "default_react_version")]
33+
#[serde(default)]
34+
pub aleph_module_url: String,
35+
36+
#[serde(default)]
3537
pub react_version: String,
3638

3739
#[serde(default)]
@@ -90,10 +92,6 @@ fn default_pragma_frag() -> String {
9092
"React.Fragment".into()
9193
}
9294

93-
fn default_react_version() -> String {
94-
"17.0.1".into()
95-
}
96-
9795
#[derive(Debug, Serialize)]
9896
#[serde(rename_all = "camelCase")]
9997
pub struct TransformOutput {
@@ -115,7 +113,14 @@ pub fn transform_sync(s: &str, opts: JsValue) -> Result<JsValue, JsValue> {
115113
let resolver = Rc::new(RefCell::new(Resolver::new(
116114
opts.url.as_str(),
117115
opts.import_map,
118-
Some(opts.react_version),
116+
match opts.aleph_module_url.as_str() {
117+
"" => None,
118+
_ => Some(opts.aleph_module_url),
119+
},
120+
match opts.react_version.as_str() {
121+
"" => None,
122+
_ => Some(opts.react_version),
123+
},
119124
opts.bundle_mode,
120125
opts.bundled_modules,
121126
)));

compiler/src/resolve.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2020-2021 postUI Lab. All rights reserved. MIT license.
22

3-
use crate::aleph::VERSION;
43
use crate::import_map::{ImportHashMap, ImportMap};
54

65
use indexmap::IndexSet;
@@ -54,11 +53,11 @@ pub struct InlineStyle {
5453

5554
/// A Resolver to resolve aleph.js import/export URL.
5655
pub struct Resolver {
57-
/// The text specifier associated with the import/export statement.
56+
/// the text specifier associated with the import/export statement.
5857
pub specifier: String,
59-
/// A flag indicating if the specifier is remote url or not.
58+
/// a flag indicating if the specifier is remote url or not.
6059
pub specifier_is_remote: bool,
61-
/// builtin jsx tags like `a`, `link`, `head`, etc
60+
/// builtin jsx tags like `a`, `link`, `head`, etc
6261
pub used_builtin_jsx_tags: IndexSet<String>,
6362
/// dependency graph
6463
pub dep_graph: Vec<DependencyDescriptor>,
@@ -68,14 +67,18 @@ pub struct Resolver {
6867
pub bundle_mode: bool,
6968
/// bundled modules
7069
pub bundled_modules: IndexSet<String>,
70+
71+
// private
7172
import_map: ImportMap,
73+
aleph_module_url: Option<String>,
7274
react_version: Option<String>,
7375
}
7476

7577
impl Resolver {
7678
pub fn new(
7779
specifier: &str,
7880
import_map: ImportHashMap,
81+
aleph_module_url: Option<String>,
7982
react_version: Option<String>,
8083
bundle_mode: bool,
8184
bundled_modules: Vec<String>,
@@ -91,12 +94,20 @@ impl Resolver {
9194
dep_graph: Vec::new(),
9295
inline_styles: HashMap::new(),
9396
import_map: ImportMap::from_hashmap(import_map),
97+
aleph_module_url,
9498
react_version,
9599
bundle_mode,
96100
bundled_modules: set,
97101
}
98102
}
99103

104+
pub fn get_aleph_module_url(&self) -> String {
105+
if let Some(aleph_module_url) = &self.aleph_module_url {
106+
return aleph_module_url.into();
107+
}
108+
"https://deno.land/x/aleph".into()
109+
}
110+
100111
/// fix import/export url.
101112
// - `https://esm.sh/react` -> `/-/esm.sh/react.js`
102113
// - `https://esm.sh/[email protected]?target=es2015&dev` -> `/-/esm.sh/[email protected]_target=es2015&dev.js`
@@ -231,12 +242,14 @@ impl Resolver {
231242
}
232243
};
233244
// fix deno.land/x/aleph url
234-
if fixed_url.starts_with("https://deno.land/x/aleph/") {
235-
fixed_url = format!(
236-
"https://deno.land/x/aleph@v{}/{}",
237-
VERSION.as_str(),
238-
fixed_url.trim_start_matches("https://deno.land/x/aleph/")
239-
);
245+
if let Some(aleph_module_url) = &self.aleph_module_url {
246+
if fixed_url.starts_with("https://deno.land/x/aleph/") {
247+
fixed_url = format!(
248+
"{}/{}",
249+
aleph_module_url.as_str(),
250+
fixed_url.trim_start_matches("https://deno.land/x/aleph/")
251+
);
252+
}
240253
}
241254
// fix react/react-dom url
242255
if let Some(version) = &self.react_version {
@@ -800,7 +813,14 @@ mod tests {
800813

801814
#[test]
802815
fn test_resolver_fix_import_url() {
803-
let resolver = Resolver::new("/app.tsx", ImportHashMap::default(), None, false, vec![]);
816+
let resolver = Resolver::new(
817+
"/app.tsx",
818+
ImportHashMap::default(),
819+
None,
820+
None,
821+
false,
822+
vec![],
823+
);
804824
assert_eq!(
805825
resolver.fix_import_url("https://esm.sh/react"),
806826
"/-/esm.sh/react.js"
@@ -855,6 +875,7 @@ mod tests {
855875
imports,
856876
scopes: HashMap::new(),
857877
},
878+
None,
858879
Some("17.0.1".into()),
859880
false,
860881
vec![],
@@ -975,6 +996,7 @@ mod tests {
975996
let mut resolver = Resolver::new(
976997
"https://esm.sh/react-dom",
977998
ImportHashMap::default(),
999+
None,
9781000
Some("17.0.1".into()),
9791001
false,
9801002
vec![],
@@ -1012,6 +1034,7 @@ mod tests {
10121034
"https://esm.sh/preact/hooks",
10131035
ImportHashMap::default(),
10141036
None,
1037+
None,
10151038
false,
10161039
vec![],
10171040
);

compiler/src/swc.rs

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ fn get_syntax(source_type: &SourceType) -> Syntax {
286286
#[cfg(test)]
287287
mod tests {
288288
use super::*;
289-
use crate::aleph::VERSION;
290289
use crate::import_map::ImportHashMap;
291290
use crate::resolve::{DependencyDescriptor, Resolver, HASH_PLACEHOLDER};
292291
use sha1::{Digest, Sha1};
@@ -296,6 +295,7 @@ mod tests {
296295
let resolver = Rc::new(RefCell::new(Resolver::new(
297296
specifer,
298297
ImportHashMap::default(),
298+
Some("https://deno.land/x/[email protected]".into()),
299299
None,
300300
bundling,
301301
vec![],
@@ -442,32 +442,16 @@ mod tests {
442442
"#;
443443
let (code, resolver) = t("/pages/index.tsx", source, false);
444444
assert!(code.contains(
445-
format!(
446-
"import __ALEPH_Anchor from \"../-/deno.land/x/aleph@v{}/framework/react/anchor.js\"",
447-
VERSION.as_str()
448-
)
449-
.as_str()
445+
"import __ALEPH_Anchor from \"../-/deno.land/x/[email protected]/framework/react/anchor.js\""
450446
));
451447
assert!(code.contains(
452-
format!(
453-
"import __ALEPH_Head from \"../-/deno.land/x/aleph@v{}/framework/react/head.js\"",
454-
VERSION.as_str()
455-
)
456-
.as_str()
448+
"import __ALEPH_Head from \"../-/deno.land/x/[email protected]/framework/react/head.js\""
457449
));
458450
assert!(code.contains(
459-
format!(
460-
"import __ALEPH_Link from \"../-/deno.land/x/aleph@v{}/framework/react/link.js\"",
461-
VERSION.as_str()
462-
)
463-
.as_str()
451+
"import __ALEPH_Link from \"../-/deno.land/x/[email protected]/framework/react/link.js\""
464452
));
465453
assert!(code.contains(
466-
format!(
467-
"import __ALEPH_Script from \"../-/deno.land/x/aleph@v{}/framework/react/script.js\"",
468-
VERSION.as_str()
469-
)
470-
.as_str()
454+
"import __ALEPH_Script from \"../-/deno.land/x/[email protected]/framework/react/script.js\""
471455
));
472456
assert!(code.contains("React.createElement(\"a\","));
473457
assert!(code.contains("React.createElement(__ALEPH_Anchor,"));
@@ -498,34 +482,22 @@ mod tests {
498482
rel: Some("stylesheet".into()),
499483
},
500484
DependencyDescriptor {
501-
specifier: format!(
502-
"https://deno.land/x/aleph@v{}/framework/react/head.ts",
503-
VERSION.as_str()
504-
),
485+
specifier: "https://deno.land/x/[email protected]/framework/react/head.ts".into(),
505486
is_dynamic: false,
506487
rel: None,
507488
},
508489
DependencyDescriptor {
509-
specifier: format!(
510-
"https://deno.land/x/aleph@v{}/framework/react/link.ts",
511-
VERSION.as_str()
512-
),
490+
specifier: "https://deno.land/x/[email protected]/framework/react/link.ts".into(),
513491
is_dynamic: false,
514492
rel: None,
515493
},
516494
DependencyDescriptor {
517-
specifier: format!(
518-
"https://deno.land/x/aleph@v{}/framework/react/anchor.ts",
519-
VERSION.as_str()
520-
),
495+
specifier: "https://deno.land/x/[email protected]/framework/react/anchor.ts".into(),
521496
is_dynamic: false,
522497
rel: None,
523498
},
524499
DependencyDescriptor {
525-
specifier: format!(
526-
"https://deno.land/x/aleph@v{}/framework/react/script.ts",
527-
VERSION.as_str()
528-
),
500+
specifier: "https://deno.land/x/[email protected]/framework/react/script.ts".into(),
529501
is_dynamic: false,
530502
rel: None,
531503
}
@@ -557,11 +529,7 @@ mod tests {
557529
"#;
558530
let (code, resolver) = t("/pages/index.tsx", source, false);
559531
assert!(code.contains(
560-
format!(
561-
"import __ALEPH_Style from \"../-/deno.land/x/aleph@v{}/framework/react/style.js\"",
562-
VERSION.as_str()
563-
)
564-
.as_str()
532+
"import __ALEPH_Style from \"../-/deno.land/x/[email protected]/framework/react/style.js\""
565533
));
566534
assert!(code.contains("React.createElement(__ALEPH_Style,"));
567535
assert!(code.contains("__styleId: \"inline-style-"));
@@ -595,6 +563,7 @@ mod tests {
595563
"/pages/index.tsx",
596564
ImportHashMap::default(),
597565
None,
566+
None,
598567
true,
599568
vec!["/components/logo.ts".into(), "/shared/iife.ts".into()],
600569
)));
@@ -612,11 +581,7 @@ mod tests {
612581
assert!(code.contains("import \"../style/index.css.xxxxxxxxx.js\""));
613582
assert!(!code.contains("__ALEPH.pack[\"/shared/iife.ts\"]"));
614583
assert!(code.contains(
615-
format!(
616-
"__ALEPH_Head = __ALEPH.pack[\"https://deno.land/x/aleph@v{}/framework/react/head.ts\"].default",
617-
VERSION.as_str()
618-
)
619-
.as_str()
584+
"__ALEPH_Head = __ALEPH.pack[\"https://deno.land/x/aleph/framework/react/head.ts\"].default"
620585
));
621586
}
622587

0 commit comments

Comments
 (0)