@@ -4,6 +4,44 @@ import { fileURLToPath, URL } from 'url'
4
4
import { relative , join } from 'path'
5
5
6
6
const normalizePath = ( path : string ) => path . replace ( / \\ { 1 , 2 } / g, '/' )
7
+
8
+ const quote = ( value : string ) => `"${ value } "`
9
+
10
+ function formatJson ( str : string ) : string
11
+ function formatJson ( obj : object ) : string
12
+ function formatJson ( value : string | object ) {
13
+ if ( typeof value === 'string' ) value = JSON . parse ( value )
14
+ value = value as object
15
+ const indent = 2
16
+ const cache = new WeakSet ( )
17
+ const wrapper = ( val : string , level : number ) => {
18
+ return `${ ' ' . repeat ( level * indent ) } ${ val } `
19
+ }
20
+ const stringify = ( val : unknown , level : number ) => {
21
+ // json 数值
22
+ if ( typeof val === 'string' ) return wrapper ( quote ( val ) , level )
23
+ if ( typeof val === null || typeof val === 'boolean' || typeof val === 'number' ) return wrapper ( String ( val ) , level )
24
+ if ( typeof val === 'object' ) {
25
+ if ( cache . has ( val as object ) ) throw new Error ( 'Converting circular structure to JSON' )
26
+ cache . add ( val as object )
27
+ if ( Array . isArray ( val ) ) {
28
+ const newline = val . length > 2 && val . some ( v => v === null && typeof v !== 'object' )
29
+ const v : string = val . map < string > ( ( child ) => stringify ( child , newline ? level + 1 : 0 ) ) . join ( newline ? ',\n' : ', ' )
30
+ return `[${ newline ? '\n' : ' ' } ${ v } ${ newline ? wrapper ( ']' , level ) : ' ]' } `
31
+ } else {
32
+ const entries = Object . entries ( val as object )
33
+ const v : string = entries . map < string > ( ( [ key , child ] ) => {
34
+ return `${ wrapper ( quote ( key ) , level + 1 ) } : ${ stringify ( child , level + 1 ) } `
35
+ } ) . join ( ',\n' )
36
+ return `{\n${ v } \n${ wrapper ( '}' , level ) } `
37
+ }
38
+ }
39
+ throw new TypeError ( `Unsupported value: ${ val } ` )
40
+ }
41
+
42
+ return stringify ( value , 0 )
43
+ }
44
+
7
45
const createTsConfigPaths = ( alias : Record < string , string > , basePath ?: string ) => {
8
46
const tsconfigPaths : Record < string , string [ ] > = Object . create ( null )
9
47
for ( const [ name , path ] of Object . entries ( alias ) ) {
@@ -12,7 +50,7 @@ const createTsConfigPaths = (alias: Record<string, string>, basePath?: string) =
12
50
if ( ! name . endsWith ( '*' ) ) tsconfigPaths [ normalizePath ( join ( name , '*' ) ) ] = [ normalizePath ( join ( resolvePath , '*' ) ) ]
13
51
if ( name !== '@' ) tsconfigPaths [ name ] = [ resolvePath ]
14
52
}
15
- return JSON . stringify ( tsconfigPaths , null , 2 )
53
+ return formatJson ( tsconfigPaths )
16
54
}
17
55
const createPath = ( ...base : string [ ] ) => {
18
56
return ( ...paths : string [ ] ) => fileURLToPath (
@@ -23,10 +61,10 @@ const src = createPath('src')
23
61
const typings = createPath ( 'typings' )
24
62
25
63
const alias = {
26
- '@' : src ( ) ,
27
64
'@components' : src ( 'components' ) ,
28
65
'@views' : src ( 'views' ) ,
29
- '@utils' : src ( 'utils' )
66
+ '@utils' : src ( 'utils' ) ,
67
+ '@' : src ( )
30
68
}
31
69
console . log ( createTsConfigPaths ( alias , src ( ) ) )
32
70
// https://vitejs.dev/config/
0 commit comments