@@ -32,7 +32,7 @@ export function findImports(body: Node, root: string, sourcePath: string) {
32
32
function findImport ( node ) {
33
33
if ( isStringLiteral ( node . source ) ) {
34
34
const value = getStringLiteralValue ( node . source ) ;
35
- if ( isLocalImport ( value , root , sourcePath ) ) {
35
+ if ( isLocalImport ( value , sourcePath ) ) {
36
36
findLocalImports ( normalize ( value ) ) ;
37
37
} else {
38
38
imports . push ( { name : value , type : "global" } ) ;
@@ -62,7 +62,7 @@ export function findImports(body: Node, root: string, sourcePath: string) {
62
62
function findLocalImport ( node ) {
63
63
if ( isStringLiteral ( node . source ) ) {
64
64
const value = getStringLiteralValue ( node . source ) ;
65
- if ( isLocalImport ( value , root , sourcePath ) ) {
65
+ if ( isLocalImport ( value , sourcePath ) ) {
66
66
findLocalImports ( join ( dirname ( path ) , value ) ) ;
67
67
} else {
68
68
imports . push ( { name : value , type : "global" } ) ;
@@ -78,21 +78,21 @@ export function findImports(body: Node, root: string, sourcePath: string) {
78
78
// TODO parallelize multiple static imports
79
79
export function rewriteImports ( output : any , rootNode : JavaScriptNode , root : string , sourcePath : string ) {
80
80
simple ( rootNode . body , {
81
- ImportExpression ( node : any ) {
81
+ ImportExpression ( node ) {
82
82
if ( isStringLiteral ( node . source ) ) {
83
83
const value = getStringLiteralValue ( node . source ) ;
84
84
output . replaceLeft (
85
85
node . source . start ,
86
86
node . source . end ,
87
87
JSON . stringify (
88
- isLocalImport ( value , root , sourcePath )
88
+ isLocalImport ( value , sourcePath )
89
89
? join ( "/_import/" , join ( dirname ( sourcePath ) , value ) )
90
90
: resolveImport ( value )
91
91
)
92
92
) ;
93
93
}
94
94
} ,
95
- ImportDeclaration ( node : any ) {
95
+ ImportDeclaration ( node ) {
96
96
if ( isStringLiteral ( node . source ) ) {
97
97
const value = getStringLiteralValue ( node . source ) ;
98
98
rootNode . async = true ;
@@ -102,11 +102,9 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, root: stri
102
102
`const ${
103
103
node . specifiers . some ( isNotNamespaceSpecifier )
104
104
? `{${ node . specifiers . filter ( isNotNamespaceSpecifier ) . map ( rewriteImportSpecifier ) . join ( ", " ) } }`
105
- : node . specifiers . some ( isNamespaceSpecifier )
106
- ? node . specifiers . find ( isNamespaceSpecifier ) . local . name
107
- : "{}"
105
+ : node . specifiers . find ( isNamespaceSpecifier ) ?. local . name ?? "{}"
108
106
} = await import(${ JSON . stringify (
109
- isLocalImport ( value , root , sourcePath )
107
+ isLocalImport ( value , sourcePath )
110
108
? join ( "/_import/" , join ( dirname ( sourcePath ) , value ) )
111
109
: resolveImport ( value )
112
110
) } );`
@@ -124,10 +122,10 @@ function rewriteImportSpecifier(node) {
124
122
: `${ node . imported . name } : ${ node . local . name } ` ;
125
123
}
126
124
127
- export function isLocalImport ( value : string , root : string , sourcePath : string ) : boolean {
125
+ export function isLocalImport ( value : string , sourcePath : string ) : boolean {
128
126
return (
129
127
[ "./" , "../" , "/" ] . some ( ( prefix ) => value . startsWith ( prefix ) ) &&
130
- join ( root + "/ ", dirname ( sourcePath ) , value ) . startsWith ( root )
128
+ ! join ( ". ", dirname ( sourcePath ) , value ) . startsWith ( "../" )
131
129
) ;
132
130
}
133
131
0 commit comments