@@ -208,7 +208,7 @@ impl Fold for ResolveFold {
208
208
let mut resolver = self . resolver . borrow_mut ( ) ;
209
209
let ( resolved_path, fixed_url) = resolver. resolve ( src. value . as_ref ( ) , false ) ;
210
210
if resolver. bundle_mode && resolver. bundle_external . contains ( fixed_url. as_str ( ) ) {
211
- resolver. star_exports . push ( fixed_url. clone ( ) ) ;
211
+ resolver. bundle_star_exports . push ( fixed_url. clone ( ) ) ;
212
212
ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( ExportDecl {
213
213
span : DUMMY_SP ,
214
214
decl : Decl :: Var ( VarDecl {
@@ -217,7 +217,7 @@ impl Fold for ResolveFold {
217
217
declare : false ,
218
218
decls : vec ! [ create_aleph_pack_var_decl(
219
219
fixed_url. as_ref( ) ,
220
- quote_ident!( format!( "$$star_{}" , resolver. star_exports . len( ) - 1 ) ) ,
220
+ quote_ident!( format!( "$$star_{}" , resolver. bundle_star_exports . len( ) - 1 ) ) ,
221
221
) ] ,
222
222
} ) ,
223
223
} ) )
@@ -304,15 +304,17 @@ impl Fold for ResolveFold {
304
304
prop : quote_ident ! ( "import" ) ,
305
305
} ) ) )
306
306
}
307
- call. args = vec ! [ ExprOrSpread {
308
- spread: None ,
309
- expr: Box :: new( Expr :: Lit ( Lit :: Str ( new_str( resolver. resolve( url, true ) . 0 ) ) ) ) ,
310
- } ] ;
307
+ let ( resolved_path, fixed_url) = resolver. resolve ( url, true ) ;
311
308
if resolver. bundle_mode {
312
- call. args . push ( ExprOrSpread {
309
+ call. args = vec ! [ ExprOrSpread {
313
310
spread: None ,
314
- expr : Box :: new ( Expr :: Lit ( Lit :: Str ( new_str ( resolver. specifier . clone ( ) ) ) ) ) ,
315
- } )
311
+ expr: Box :: new( Expr :: Lit ( Lit :: Str ( new_str( fixed_url) ) ) ) ,
312
+ } ] ;
313
+ } else {
314
+ call. args = vec ! [ ExprOrSpread {
315
+ spread: None ,
316
+ expr: Box :: new( Expr :: Lit ( Lit :: Str ( new_str( resolved_path) ) ) ) ,
317
+ } ] ;
316
318
}
317
319
} else if is_call_expr_by_name ( & call, "useDeno" ) {
318
320
let callback_span = match call. args . first ( ) {
@@ -330,6 +332,7 @@ impl Fold for ResolveFold {
330
332
let bundle_mode = self . resolver . borrow ( ) . bundle_mode ;
331
333
let id = self . new_use_deno_hook_ident ( span) ;
332
334
if bundle_mode {
335
+ // tree-shake useDeno callback in bundle mode
333
336
call. args [ 0 ] = ExprOrSpread {
334
337
spread : None ,
335
338
expr : Box :: new ( Expr :: Lit ( Lit :: Null ( Null { span : DUMMY_SP } ) ) ) ,
@@ -552,23 +555,70 @@ mod tests {
552
555
use crate :: resolve:: Resolver ;
553
556
use crate :: swc:: { st, EmitOptions , SWC } ;
554
557
use sha1:: { Digest , Sha1 } ;
558
+ use std:: collections:: HashMap ;
559
+
560
+ #[ test]
561
+ fn resolve_import_export ( ) {
562
+ let source = r#"
563
+ import React from 'react'
564
+ import { redirect } from 'aleph'
565
+ import { useDeno } from 'aleph/hooks.ts'
566
+ import { render } from 'react-dom/server'
567
+ import { render as _render } from 'https://cdn.esm.sh/v1/[email protected] /es2020/react-dom.js'
568
+ import Logo from '../component/logo.tsx'
569
+ import Logo2 from '~/component/logo.tsx'
570
+ import Logo3 from '@/component/logo.tsx'
571
+ const AsyncLogo = React.lazy(() => import('../components/async-logo.tsx'))
572
+ export { useState } from 'https://esm.sh/react'
573
+ export * from 'https://esm.sh/swr'
574
+ export { React, redirect, useDeno, render, _render, Logo, Logo2, Logo3, AsyncLogo }
575
+ "# ;
576
+ let module = SWC :: parse ( "/pages/index.tsx" , source, None ) . expect ( "could not parse module" ) ;
577
+ let mut imports: HashMap < String , String > = HashMap :: new ( ) ;
578
+ imports. insert ( "@/" . into ( ) , "./" . into ( ) ) ;
579
+ imports. insert ( "~/" . into ( ) , "./" . into ( ) ) ;
580
+ imports. insert ( "aleph" . into ( ) , "https://deno.land/x/aleph/mod.ts" . into ( ) ) ;
581
+ imports. insert ( "aleph/" . into ( ) , "https://deno.land/x/aleph/" . into ( ) ) ;
582
+ imports. insert ( "react" . into ( ) , "https://esm.sh/react" . into ( ) ) ;
583
+ imports. insert ( "react-dom/" . into ( ) , "https://esm.sh/react-dom/" . into ( ) ) ;
584
+ let resolver = Rc :: new ( RefCell :: new ( Resolver :: new (
585
+ "/pages/index.tsx" ,
586
+ ImportHashMap {
587
+ imports,
588
+ scopes : HashMap :: new ( ) ,
589
+ } ,
590
+ Some ( "https://deno.land/x/[email protected] " . into ( ) ) ,
591
+ Some ( "17.0.1" . into ( ) ) ,
592
+ false ,
593
+ vec ! [ ] ,
594
+ ) ) ) ;
595
+ let ( code, _) = module
596
+ . transform ( resolver. clone ( ) , & EmitOptions :: default ( ) )
597
+ . expect ( "could not transform module" ) ;
598
+ println ! ( "{}" , code) ;
599
+ assert ! ( code
. contains
( "import React from \" ../-/esm.sh/[email protected] \" " ) ) ;
600
+ assert ! ( code
. contains
( "import { redirect } from \" ../-/deno.land/x/[email protected] /mod.js\" " ) ) ;
601
+ assert ! ( code
. contains
( "import { useDeno } from \" ../-/deno.land/x/[email protected] /hooks.js\" " ) ) ;
602
+ assert ! ( code
. contains
( "import { render } from \" ../-/esm.sh/[email protected] /server.js\" " ) ) ;
603
+ assert ! ( code
. contains
( "import { render as _render } from \" ../-/cdn.esm.sh/v1/[email protected] /es2020/react-dom.js\" " ) ) ;
604
+ assert ! ( code. contains( "import Logo from \" ../component/logo.js#/component/logo.tsx@000000\" " ) ) ;
605
+ assert ! ( code. contains( "import Logo2 from \" ../component/logo.js#/component/logo.tsx@000000\" " ) ) ;
606
+ assert ! ( code. contains( "import Logo3 from \" ../component/logo.js#/component/logo.tsx@000000\" " ) ) ;
607
+ assert ! ( code. contains( "const AsyncLogo = React.lazy(()=>import(\" ../components/async-logo.js#/components/async-logo.tsx@000000\" )" ) ) ;
608
+ assert ! ( code
. contains
( "export { useState } from \" ../-/esm.sh/[email protected] \" " ) ) ;
609
+ assert ! ( code. contains( "export * from \" ../-/esm.sh/swr.js\" " ) ) ;
610
+ }
555
611
556
612
#[ test]
557
613
fn sign_use_deno_hook ( ) {
558
614
let specifer = "/pages/index.tsx" ;
559
615
let source = r#"
560
616
export default function Index() {
561
617
const verison = useDeno(() => Deno.version)
562
- const verison = useDeno(async function() {
618
+ const data = useDeno(async function() {
563
619
return await readJson("./data.json")
564
620
}, 1000)
565
- return (
566
- <>
567
- <p>Deno v{version.deno}</p>
568
- <V8 />
569
- <TS />
570
- </>
571
- )
621
+ return null
572
622
}
573
623
"# ;
574
624
@@ -623,8 +673,8 @@ mod tests {
623
673
import '../shared/iife.ts'
624
674
import '../shared/iife2.ts'
625
675
export * from "https://esm.sh/react"
626
- export { render } from "https://esm.sh/react-dom"
627
676
export * as ReactDom from "https://esm.sh/react-dom"
677
+ export { render } from "https://esm.sh/react-dom"
628
678
629
679
const AsyncLogo = React.lazy(() => import('../components/async-logo.tsx'))
630
680
@@ -667,8 +717,9 @@ mod tests {
667
717
) ;
668
718
assert ! ( !code. contains( "__ALEPH.pack[\" /shared/iife.ts\" ]" ) ) ;
669
719
assert ! ( code. contains( "import \" ../shared/iife2.bundling.js#/shared/iife2.ts@000000\" " ) ) ;
670
- assert ! ( code
671
- . contains( "AsyncLogo = React.lazy(()=>__ALEPH.import(\" ../components/async-logo.bundle.js#/components/async-logo.tsx@000000\" , \" /pages/index.tsx\" " ) ) ;
720
+ assert ! (
721
+ code. contains( "AsyncLogo = React.lazy(()=>__ALEPH.import(\" /components/async-logo.tsx\" " )
722
+ ) ;
672
723
assert ! ( code. contains(
673
724
"const { default: __ALEPH_Head } = __ALEPH.pack[\" https://deno.land/x/aleph/framework/react/head.ts\" ]"
674
725
) ) ;
0 commit comments