1
- const { logger, fs, path : { resolve } } = require ( '@vuepress/shared-utils' )
2
- const readdirSync = dir => fs . existsSync ( dir ) && fs . readdirSync ( dir ) || [ ]
1
+ const {
2
+ logger,
3
+ fs,
4
+ path : { resolve }
5
+ } = require ( '@vuepress/shared-utils' )
6
+ const readdirSync = dir => ( fs . existsSync ( dir ) && fs . readdirSync ( dir ) ) || [ ]
3
7
4
8
module . exports = class ThemeAPI {
5
9
constructor ( theme , parentTheme ) {
@@ -30,12 +34,12 @@ module.exports = class ThemeAPI {
30
34
this . componentMap = this . getComponents ( )
31
35
this . layoutComponentMap = this . getLayoutComponentMap ( )
32
36
33
- Object . keys ( this . componentMap ) . forEach ( ( name ) => {
37
+ Object . keys ( this . componentMap ) . forEach ( name => {
34
38
const { filename, path } = this . componentMap [ name ]
35
39
alias [ `@theme/components/${ filename } ` ] = path
36
40
} )
37
41
38
- Object . keys ( this . layoutComponentMap ) . forEach ( ( name ) => {
42
+ Object . keys ( this . layoutComponentMap ) . forEach ( name => {
39
43
const { filename, path } = this . layoutComponentMap [ name ]
40
44
alias [ `@theme/layouts/${ filename } ` ] = path
41
45
} )
@@ -44,13 +48,9 @@ module.exports = class ThemeAPI {
44
48
}
45
49
46
50
getComponents ( ) {
47
- const componentDirs = [
48
- resolve ( this . theme . path , 'components' )
49
- ]
51
+ const componentDirs = [ resolve ( this . theme . path , 'components' ) ]
50
52
if ( this . existsParentTheme ) {
51
- componentDirs . unshift (
52
- resolve ( this . parentTheme . path , 'components' ) ,
53
- )
53
+ componentDirs . unshift ( resolve ( this . parentTheme . path , 'components' ) )
54
54
}
55
55
return resolveSFCs ( componentDirs )
56
56
}
@@ -63,15 +63,15 @@ module.exports = class ThemeAPI {
63
63
if ( this . existsParentTheme ) {
64
64
layoutDirs . unshift (
65
65
resolve ( this . parentTheme . path , '.' ) ,
66
- resolve ( this . parentTheme . path , 'layouts' ) ,
66
+ resolve ( this . parentTheme . path , 'layouts' )
67
67
)
68
68
}
69
69
// built-in named layout or not.
70
70
const layoutComponentMap = resolveSFCs ( layoutDirs )
71
71
72
- const { Layout = { } , NotFound = { } } = layoutComponentMap
72
+ const { Layout, NotFound } = layoutComponentMap
73
73
// layout component does not exist.
74
- if ( ! Layout || ! fs . existsSync ( Layout . path ) ) {
74
+ if ( ! Layout ) {
75
75
const fallbackLayoutPath = resolve ( __dirname , 'Layout.fallback.vue' )
76
76
layoutComponentMap . Layout = {
77
77
filename : 'Layout.vue' ,
@@ -81,10 +81,10 @@ module.exports = class ThemeAPI {
81
81
}
82
82
logger . warn (
83
83
`[vuepress] Cannot resolve Layout.vue file in \n ${ Layout . path } , `
84
- + `fallback to default layout: ${ fallbackLayoutPath } `
84
+ + `fallback to default layout: ${ fallbackLayoutPath } `
85
85
)
86
86
}
87
- if ( ! NotFound || ! fs . existsSync ( NotFound . path ) ) {
87
+ if ( ! NotFound ) {
88
88
layoutComponentMap . NotFound = {
89
89
filename : 'NotFound.vue' ,
90
90
componentName : 'NotFound' ,
@@ -104,25 +104,28 @@ module.exports = class ThemeAPI {
104
104
*/
105
105
106
106
function resolveSFCs ( dirs ) {
107
- return dirs . map (
108
- layoutDir => readdirSync ( layoutDir )
109
- . filter ( filename => filename . endsWith ( '.vue' ) )
110
- . map ( filename => {
111
- const componentName = getComponentName ( filename )
112
- return {
113
- filename,
114
- componentName,
115
- isInternal : isInternal ( componentName ) ,
116
- path : resolve ( layoutDir , filename )
117
- }
118
- } )
119
- ) . reduce ( ( arr , next ) => {
120
- arr . push ( ...next )
121
- return arr
122
- } , [ ] ) . reduce ( ( map , component ) => {
123
- map [ component . componentName ] = component
124
- return map
125
- } , { } )
107
+ return dirs
108
+ . map ( layoutDir =>
109
+ readdirSync ( layoutDir )
110
+ . filter ( filename => filename . endsWith ( '.vue' ) )
111
+ . map ( filename => {
112
+ const componentName = getComponentName ( filename )
113
+ return {
114
+ filename,
115
+ componentName,
116
+ isInternal : isInternal ( componentName ) ,
117
+ path : resolve ( layoutDir , filename )
118
+ }
119
+ } )
120
+ )
121
+ . reduce ( ( arr , next ) => {
122
+ arr . push ( ...next )
123
+ return arr
124
+ } , [ ] )
125
+ . reduce ( ( map , component ) => {
126
+ map [ component . componentName ] = component
127
+ return map
128
+ } , { } )
126
129
}
127
130
128
131
/**
0 commit comments