@@ -2,44 +2,49 @@ import fs from 'node:fs'
2
2
import path from 'node:path'
3
3
import chokidar from 'chokidar'
4
4
import type { Alias , AliasOptions } from 'vite'
5
- import type { Options } from '../types'
5
+ import type { IOptions , TFileType } from '../types'
6
6
import { replacePath , throwError , toHump } from './utils'
7
7
8
- export function unpluginAutoExport ( config : Options , alias : AliasOptions & Alias [ ] ) {
8
+ export function unpluginAutoExport ( config : IOptions , alias : AliasOptions & Alias [ ] ) : chokidar . FSWatcher {
9
9
const reg = / \/ \* ( \. [ \w \d ] + ) ? $ /
10
+ const splitReg = / \. (? = [ ^ \. ] + $ ) / g
11
+ const defaultOptions : IOptions = {
12
+ path : [ ] ,
13
+ ignore : [ ] ,
14
+ extname : 'ts' ,
15
+ }
16
+ config = Object . assign ( defaultOptions , config )
10
17
11
18
const handleDir = ( _path : string ) => {
12
- const { extname = 'ts' , componentDirs = [ ] } = config
13
- const indexFileName = `index.${ extname } `
19
+ const { extname } = config
14
20
const dirPath = path . dirname ( _path )
21
+ const indexFileName = `index.${ extname } `
15
22
const indexFilePath = `${ dirPath } /${ indexFileName } `
16
- const dir = dirPath . split ( '/' ) . at ( - 1 ) !
17
- const isVue = componentDirs . includes ( dir )
18
23
const files = fs . readdirSync ( dirPath ) . filter ( e => e !== indexFileName && fs . statSync ( `${ dirPath } /${ e } ` ) . isFile ( ) )
19
24
if ( path . basename ( _path ) === indexFileName )
20
25
return
21
- const indexFileContent
22
- = `${ files
23
- . map ( ( file ) => {
24
- const basename = path . basename ( file )
25
- const [ filename ] = basename . split ( '.' )
26
- const s = isVue ? `{ default as ${ toHump ( filename ) } }` : '*'
27
- const name = isVue ? basename : filename
28
- return `export ${ s } from './${ name } '`
29
- } )
30
- . join ( '\n' ) } \n`
26
+ const exportList = files
27
+ . map ( ( basename ) => {
28
+ const [ filename , extname ] = basename . split ( splitReg ) as [ string , TFileType ]
29
+ if ( config . formatter )
30
+ return config . formatter ( filename , extname )
31
+
32
+ switch ( extname ) {
33
+ case 'vue' :
34
+ case 'json' :
35
+ return `export { default as ${ toHump ( filename ) } } from './${ filename } .${ extname } '`
36
+ case 'js' :
37
+ case 'ts' :
38
+ default :
39
+ return `export * from './${ filename } '`
40
+ }
41
+ } ) . join ( '\n' )
42
+ const indexFileContent = `${ exportList || 'export {}' } \n`
31
43
fs . writeFileSync ( indexFilePath , indexFileContent )
32
44
}
33
- const setupWatcher = ( watcher : fs . FSWatcher ) => {
34
- watcher . on ( 'unlink' , handleDir )
35
- watcher . on ( 'add' , handleDir )
36
- }
37
- const init = ( path : string | string [ ] , ignore : string [ ] ) => {
38
- setupWatcher ( chokidar . watch ( path , { ignored : ignore } ) )
39
- }
40
- const handleAlias = ( ) => {
45
+ const init = ( ) => {
41
46
let path = config . path
42
- let ignore = config . ignore ?? [ ]
47
+ let ignore = config . ignore
43
48
if ( Array . isArray ( path ) ) {
44
49
! path . every ( p => reg . test ( p ) ) && throwError ( )
45
50
path = path . map ( e => replacePath ( e , alias ) )
@@ -48,11 +53,14 @@ export function unpluginAutoExport(config: Options, alias: AliasOptions & Alias[
48
53
! reg . test ( path ) && throwError ( )
49
54
path = replacePath ( path , alias )
50
55
}
51
- ! ignore . every ( p => reg . test ( p ) ) && throwError ( )
56
+ ! ignore ? .every ( p => reg . test ( p ) ) && throwError ( )
52
57
ignore = ignore ?. map ( e => replacePath ( e , alias ) )
53
58
54
- init ( path , ignore )
59
+ const watcher = chokidar . watch ( path , { ignored : ignore } )
60
+ watcher . on ( 'unlink' , handleDir )
61
+ watcher . on ( 'add' , handleDir )
62
+ return watcher
55
63
}
56
64
57
- handleAlias ( )
65
+ return init ( )
58
66
}
0 commit comments