1
1
const { fs, path, globby, datatypes : { isString } } = require ( '@vuepress/shared-utils' )
2
2
3
3
function fileToComponentName ( file ) {
4
- return file
5
- . replace ( / \/ | \\ / g, '-' )
6
- . replace ( / \. v u e $ / , '' )
4
+ return file . replace ( / \/ | \\ / g, '-' )
7
5
}
8
6
9
7
async function resolveComponents ( componentDir ) {
10
8
if ( ! fs . existsSync ( componentDir ) ) {
11
9
return
12
10
}
13
- return ( await globby ( [ '**/*.vue' ] , { cwd : componentDir } ) )
11
+ return ( await globby ( [ '**/*.vue' ] , { cwd : componentDir } ) ) . map ( file => file . slice ( 0 , - 4 ) )
14
12
}
15
13
16
14
// Since this plugin can ben used by multiple times, we need to
@@ -22,15 +20,15 @@ module.exports = (options, context) => ({
22
20
multiple : true ,
23
21
24
22
async enhanceAppFiles ( ) {
25
- const { componentsDir = [ ] , components = [ ] } = options
23
+ const { componentsDir = [ ] , components = [ ] , getComponentName = fileToComponentName } = options
26
24
const baseDirs = Array . isArray ( componentsDir ) ? componentsDir : [ componentsDir ]
27
25
28
26
function importCode ( name , absolutePath ) {
29
27
return `Vue.component(${ JSON . stringify ( name ) } , () => import(${ JSON . stringify ( absolutePath ) } ))`
30
28
}
31
29
32
30
function genImport ( baseDir , file ) {
33
- const name = fileToComponentName ( file )
31
+ const name = getComponentName ( file )
34
32
const absolutePath = path . resolve ( baseDir , file )
35
33
const code = importCode ( name , absolutePath )
36
34
return code
0 commit comments