@@ -5,7 +5,7 @@ import path from 'upath'
5
5
6
6
const files = fg . sync ( 'lib/**/*.d.ts' )
7
7
8
- const importRegexp = / (?< a1 > (?: i m p o r t | e x p o r t ) (?: [ ^ ; ] ) * ?f r o m [ ' " ] ) (?< a2 > \. \. ? (?: \/ [ ^ ; ' " ] * ) ? ) (?< a3 > [ ' " ] ; ) | (?< b1 > i m p o r t \( [ ' " ] ) (?< b2 > \. \. ? (?: \/ [ ^ ; ' " ] * ) ? ) (?< b3 > [ ' " ] \) ) / gm
8
+ const importRegexp = / (?< a1 > (?: i m p o r t | e x p o r t ) (?: [ ^ ; ] ) * ?f r o m [ ' " ] ) (?< a2 > (?: \. \. ? | @ ) (?: \/ [ ^ ; ' " ] * ) ? ) (?< a3 > [ ' " ] ; ) | (?< b1 > i m p o r t \( [ ' " ] ) (?< b2 > (?: \. \. ? | @ ) (?: \/ [ ^ ; ' " ] * ) ? ) (?< b3 > [ ' " ] \) ) / gm
9
9
10
10
for ( const name of files ) {
11
11
const code = await fs . readFile ( name , 'utf-8' )
@@ -14,19 +14,29 @@ for (const name of files) {
14
14
15
15
for ( const match of code . matchAll ( importRegexp ) ) {
16
16
const pre = match . groups . a1 ?? match . groups . b1
17
- const matchPath = match . groups . a2 ?? match . groups . b2
17
+ let matchPath = match . groups . a2 ?? match . groups . b2
18
18
const post = match . groups . a3 ?? match . groups . b3
19
- if ( / \. [ j t ] s $ / . test ( matchPath ) ) continue
20
-
21
- let target
22
- try {
23
- const dir = await fs . stat ( path . join ( path . dirname ( name ) , matchPath ) )
24
- target = matchPath + '/index.js'
25
- } catch ( err ) {
26
- if ( err . code !== 'ENOENT' ) throw err
27
- target = matchPath + '.js'
19
+
20
+ let target = matchPath
21
+ if ( target . startsWith ( '@/' ) ) {
22
+ target = path . relative ( path . dirname ( name ) , target . replace ( / ^ @ \/ / , 'lib/' ) )
23
+ if ( ! target . startsWith ( '.' ) ) {
24
+ target = './' + target
25
+ }
26
+ }
27
+
28
+ if ( ! / \. [ j t ] s $ / . test ( target ) ) {
29
+ try {
30
+ const dir = await fs . stat ( path . join ( path . dirname ( name ) , target ) )
31
+ target = target + '/index.js'
32
+ } catch ( err ) {
33
+ if ( err . code !== 'ENOENT' ) throw err
34
+ target = target + '.js'
35
+ }
36
+ }
37
+ if ( target !== matchPath ) {
38
+ newCode = newCode . replace ( match [ 0 ] , pre + target + post )
28
39
}
29
- newCode = newCode . replace ( match [ 0 ] , pre + target + post )
30
40
}
31
41
32
42
if ( newCode === code ) continue
0 commit comments