@@ -5,7 +5,7 @@ use path_slash::PathBufExt;
5
5
use pathdiff:: diff_paths;
6
6
use regex:: Regex ;
7
7
use relative_path:: RelativePath ;
8
- use serde:: Serialize ;
8
+ use serde:: { Deserialize , Serialize } ;
9
9
use std:: {
10
10
collections:: HashMap ,
11
11
path:: { Path , PathBuf } ,
@@ -39,6 +39,15 @@ pub struct InlineStyle {
39
39
pub exprs : Vec < String > ,
40
40
}
41
41
42
+ #[ derive( Clone , Debug , Deserialize ) ]
43
+ #[ serde( deny_unknown_fields, rename_all = "camelCase" ) ]
44
+ pub struct ReactResolve {
45
+ #[ serde( default ) ]
46
+ pub version : String ,
47
+ #[ serde( default ) ]
48
+ pub esm_sh_build_version : usize ,
49
+ }
50
+
42
51
/// A Resolver to resolve aleph.js import/export URL.
43
52
pub struct Resolver {
44
53
/// the text specifier associated with the import/export statement.
@@ -63,8 +72,7 @@ pub struct Resolver {
63
72
// private
64
73
import_map : ImportMap ,
65
74
aleph_pkg_uri : Option < String > ,
66
- react_version : Option < String > ,
67
- fixed_react_esm_sh_build_version : Option < usize > ,
75
+ react : Option < ReactResolve > ,
68
76
}
69
77
70
78
impl Resolver {
@@ -74,8 +82,7 @@ impl Resolver {
74
82
bundle_mode : bool ,
75
83
bundle_external : Vec < String > ,
76
84
aleph_pkg_uri : Option < String > ,
77
- react_version : Option < String > ,
78
- fixed_react_esm_sh_build_version : Option < usize > ,
85
+ react : Option < ReactResolve > ,
79
86
) -> Self {
80
87
let mut set = IndexSet :: < String > :: new ( ) ;
81
88
for url in bundle_external {
@@ -93,8 +100,7 @@ impl Resolver {
93
100
extra_imports : IndexSet :: new ( ) ,
94
101
import_map : ImportMap :: from_hashmap ( import_map) ,
95
102
aleph_pkg_uri,
96
- react_version,
97
- fixed_react_esm_sh_build_version,
103
+ react,
98
104
}
99
105
}
100
106
@@ -243,7 +249,7 @@ impl Resolver {
243
249
}
244
250
}
245
251
// fix react/react-dom url
246
- if let Some ( version ) = & self . react_version {
252
+ if let Some ( react ) = & self . react {
247
253
if RE_REACT_URL . is_match ( fixed_url. as_str ( ) ) {
248
254
let caps = RE_REACT_URL . captures ( fixed_url. as_str ( ) ) . unwrap ( ) ;
249
255
let mut host = caps. get ( 1 ) . map_or ( "" , |m| m. as_str ( ) ) ;
@@ -253,41 +259,34 @@ impl Resolver {
253
259
let dom = caps. get ( 3 ) . map_or ( "" , |m| m. as_str ( ) ) ;
254
260
let ver = caps. get ( 4 ) . map_or ( "" , |m| m. as_str ( ) ) ;
255
261
let path = caps. get ( 5 ) . map_or ( "" , |m| m. as_str ( ) ) ;
256
- let ( target_build_version, should_replace_build_version) =
257
- match self . fixed_react_esm_sh_build_version {
258
- Some ( v) => {
259
- if build_version != "" && v > 0 && !build_version. eq ( v. to_string ( ) . as_str ( ) ) {
260
- ( v. to_string ( ) , true )
261
- } else {
262
- ( "" . to_owned ( ) , false )
263
- }
264
- }
265
- _ => ( "" . to_owned ( ) , false ) ,
266
- } ;
267
- println ! (
268
- "---{}-{}---" ,
269
- target_build_version, should_replace_build_version
270
- ) ;
262
+ let ( target_build_version, should_replace_build_version) = if build_version != ""
263
+ && react. esm_sh_build_version > 0
264
+ && !build_version. eq ( react. esm_sh_build_version . to_string ( ) . as_str ( ) )
265
+ {
266
+ ( react. esm_sh_build_version . to_string ( ) , true )
267
+ } else {
268
+ ( "" . to_owned ( ) , false )
269
+ } ;
271
270
let non_esm_sh_cdn = match host {
272
271
"esm.sh" | "cdn.esm.sh" | "cdn.esm.sh.cn" | "esm.x-static.io" => false ,
273
272
_ => true ,
274
273
} ;
275
274
if non_esm_sh_cdn {
276
275
host = "esm.sh"
277
276
}
278
- if non_esm_sh_cdn || ver != version || should_replace_build_version {
277
+ if non_esm_sh_cdn || ver != react . version || should_replace_build_version {
279
278
if should_replace_build_version {
280
279
fixed_url = format ! (
281
280
"https://{}/v{}/react{}@{}{}" ,
282
- host, target_build_version, dom, version, path
281
+ host, target_build_version, dom, react . version, path
283
282
) ;
284
283
} else if build_version != "" {
285
284
fixed_url = format ! (
286
285
"https://{}/v{}/react{}@{}{}" ,
287
- host, build_version, dom, version, path
286
+ host, build_version, dom, react . version, path
288
287
) ;
289
288
} else {
290
- fixed_url = format ! ( "https://{}/react{}@{}{}" , host, dom, version, path) ;
289
+ fixed_url = format ! ( "https://{}/react{}@{}{}" , host, dom, react . version, path) ;
291
290
}
292
291
}
293
292
}
@@ -420,7 +419,6 @@ mod tests {
420
419
vec ! [ ] ,
421
420
None ,
422
421
None ,
423
- None ,
424
422
) ;
425
423
assert_eq ! (
426
424
resolver. fix_import_url( "https://esm.sh/react" ) ,
@@ -477,14 +475,16 @@ mod tests {
477
475
false ,
478
476
vec ! [ ] ,
479
477
None ,
480
- Some ( "17.0.1" . into ( ) ) ,
481
- None ,
478
+ Some ( ReactResolve {
479
+ version : "17.0.2" . into ( ) ,
480
+ esm_sh_build_version : 2 ,
481
+ } ) ,
482
482
) ;
483
483
assert_eq ! (
484
484
resolver. resolve( "https://esm.sh/react" , false ) ,
485
485
(
486
- "../-/esm.sh/[email protected] .1 .js" . into
( ) ,
487
- "https://esm.sh/[email protected] .1 " . into
( )
486
+ "../-/esm.sh/[email protected] .2 .js" . into
( ) ,
487
+ "https://esm.sh/[email protected] .2 " . into
( )
488
488
)
489
489
) ;
490
490
assert_eq ! (
@@ -504,50 +504,50 @@ mod tests {
504
504
assert_eq ! (
505
505
resolver. resolve( "https://esm.sh/react@16" , false ) ,
506
506
(
507
- "../-/esm.sh/[email protected] .1 .js" . into
( ) ,
508
- "https://esm.sh/[email protected] .1 " . into
( )
507
+ "../-/esm.sh/[email protected] .2 .js" . into
( ) ,
508
+ "https://esm.sh/[email protected] .2 " . into
( )
509
509
)
510
510
) ;
511
511
assert_eq ! (
512
512
resolver. resolve( "https://esm.sh/react-dom" , false ) ,
513
513
(
514
- "../-/esm.sh/[email protected] .1 .js" . into
( ) ,
515
- "https://esm.sh/[email protected] .1 " . into
( )
514
+ "../-/esm.sh/[email protected] .2 .js" . into
( ) ,
515
+ "https://esm.sh/[email protected] .2 " . into
( )
516
516
)
517
517
) ;
518
518
assert_eq ! (
519
519
resolver
. resolve
( "https://esm.sh/[email protected] " , false ) ,
520
520
(
521
- "../-/esm.sh/[email protected] .1 .js" . into
( ) ,
522
- "https://esm.sh/[email protected] .1 " . into
( )
521
+ "../-/esm.sh/[email protected] .2 .js" . into
( ) ,
522
+ "https://esm.sh/[email protected] .2 " . into
( )
523
523
)
524
524
) ;
525
525
assert_eq ! (
526
526
resolver. resolve( "https://esm.sh/react-dom/server" , false ) ,
527
527
(
528
- "../-/esm.sh/[email protected] .1 /server.js" . into
( ) ,
529
- "https://esm.sh/[email protected] .1 /server" . into
( )
528
+ "../-/esm.sh/[email protected] .2 /server.js" . into
( ) ,
529
+ "https://esm.sh/[email protected] .2 /server" . into
( )
530
530
)
531
531
) ;
532
532
assert_eq ! (
533
533
resolver
. resolve
( "https://esm.sh/[email protected] /server" , false ) ,
534
534
(
535
- "../-/esm.sh/[email protected] .1 /server.js" . into
( ) ,
536
- "https://esm.sh/[email protected] .1 /server" . into
( )
535
+ "../-/esm.sh/[email protected] .2 /server.js" . into
( ) ,
536
+ "https://esm.sh/[email protected] .2 /server" . into
( )
537
537
)
538
538
) ;
539
539
assert_eq ! (
540
540
resolver. resolve( "react-dom/server" , false ) ,
541
541
(
542
- "../-/esm.sh/[email protected] .1 /server.js" . into
( ) ,
543
- "https://esm.sh/[email protected] .1 /server" . into
( )
542
+ "../-/esm.sh/[email protected] .2 /server.js" . into
( ) ,
543
+ "https://esm.sh/[email protected] .2 /server" . into
( )
544
544
)
545
545
) ;
546
546
assert_eq ! (
547
547
resolver. resolve( "react" , false ) ,
548
548
(
549
- "../-/esm.sh/[email protected] .1 .js" . into
( ) ,
550
- "https://esm.sh/[email protected] .1 " . into
( )
549
+ "../-/esm.sh/[email protected] .2 .js" . into
( ) ,
550
+ "https://esm.sh/[email protected] .2 " . into
( )
551
551
)
552
552
) ;
553
553
assert_eq ! (
@@ -602,14 +602,16 @@ mod tests {
602
602
false ,
603
603
vec ! [ ] ,
604
604
None ,
605
- Some ( "17.0.1" . into ( ) ) ,
606
- Some ( 2 ) ,
605
+ Some ( ReactResolve {
606
+ version : "17.0.2" . into ( ) ,
607
+ esm_sh_build_version : 2 ,
608
+ } ) ,
607
609
) ;
608
610
assert_eq ! (
609
611
resolver
. resolve
( "https://cdn.esm.sh/v1/[email protected] /es2020/react.js" , false ) ,
610
612
(
611
- "../cdn.esm.sh/v2/[email protected] .1 /es2020/react.js" . into
( ) ,
612
- "https://cdn.esm.sh/v2/[email protected] .1 /es2020/react.js" . into
( )
613
+ "../cdn.esm.sh/v2/[email protected] .2 /es2020/react.js" . into
( ) ,
614
+ "https://cdn.esm.sh/v2/[email protected] .2 /es2020/react.js" . into
( )
613
615
)
614
616
) ;
615
617
assert_eq ! (
@@ -618,22 +620,22 @@ mod tests {
618
620
false
619
621
) ,
620
622
(
621
- "../cdn.esm.sh/v2/[email protected] .1 /es2020/react.js" . into
( ) ,
622
- "https://cdn.esm.sh/v2/[email protected] .1 /es2020/react.js" . into
( )
623
+ "../cdn.esm.sh/v2/[email protected] .2 /es2020/react.js" . into
( ) ,
624
+ "https://cdn.esm.sh/v2/[email protected] .2 /es2020/react.js" . into
( )
623
625
)
624
626
) ;
625
627
assert_eq ! (
626
628
resolver. resolve( "./react" , false ) ,
627
629
(
628
- "./[email protected] .1 .js" . into
( ) ,
629
- "https://esm.sh/[email protected] .1 " . into
( )
630
+ "./[email protected] .2 .js" . into
( ) ,
631
+ "https://esm.sh/[email protected] .2 " . into
( )
630
632
)
631
633
) ;
632
634
assert_eq ! (
633
635
resolver. resolve( "/react" , false ) ,
634
636
(
635
- "./[email protected] .1 .js" . into
( ) ,
636
- "https://esm.sh/[email protected] .1 " . into
( )
637
+ "./[email protected] .2 .js" . into
( ) ,
638
+ "https://esm.sh/[email protected] .2 " . into
( )
637
639
)
638
640
) ;
639
641
}
@@ -646,8 +648,10 @@ mod tests {
646
648
false ,
647
649
vec ! [ ] ,
648
650
None ,
649
- None ,
650
- Some ( 2 ) ,
651
+ Some ( ReactResolve {
652
+ version : "17.0.2" . into ( ) ,
653
+ esm_sh_build_version : 2 ,
654
+ } ) ,
651
655
) ;
652
656
assert_eq ! (
653
657
resolver. resolve(
0 commit comments