@@ -67,13 +67,13 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
67
67
// handle theme api
68
68
const {
69
69
plugins : themePlugins ,
70
- palette : themePalette ,
71
- layoutDir = useLocalTheme
72
- ? '.'
73
- : 'layouts'
70
+ palette : themePalette
74
71
} = themeIndexFile
75
72
76
- const layoutDirPath = path . resolve ( themePath , layoutDir )
73
+ const layoutDirs = [
74
+ path . resolve ( themePath , 'layouts' ) ,
75
+ path . resolve ( themePath , '.' )
76
+ ]
77
77
78
78
// normalize component name
79
79
const getComponentName = filename => {
@@ -84,29 +84,45 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
84
84
return filename
85
85
}
86
86
87
+ const readdirSync = dir => fs . existsSync ( dir ) && fs . readdirSync ( dir ) || [ ]
88
+
87
89
// built-in named layout or not.
88
90
const isInternal = componentName => componentName === 'Layout' ||
89
91
componentName === 'NotFound'
90
92
91
- const layoutComponentMap = fs . readdirSync ( layoutDirPath )
92
- . filter ( filename => filename . endsWith ( '.vue' ) )
93
- . reduce ( ( map , filename ) => {
94
- const componentName = getComponentName ( filename )
95
- const componentPath = path . resolve ( layoutDirPath , filename )
96
- map [ componentName ] = { filename, componentName, path : componentPath }
97
- if ( isInternal ( componentName ) ) {
98
- map [ componentName ] . isInternal = true
99
- }
93
+ const layoutComponentMap = layoutDirs
94
+ . map (
95
+ layourDir => readdirSync ( layourDir )
96
+ . filter ( filename => filename . endsWith ( '.vue' ) )
97
+ . map ( filename => {
98
+ const componentName = getComponentName ( filename )
99
+ return {
100
+ filename, componentName,
101
+ isInternal : isInternal ( componentName ) ,
102
+ path : path . resolve ( layourDir , filename )
103
+ }
104
+ } )
105
+ )
106
+
107
+ . reduce ( ( arr , next ) => {
108
+ arr . push ( ...next )
109
+ return arr
110
+ } , [ ] )
111
+
112
+ . reduce ( ( map , component ) => {
113
+ map [ component . componentName ] = component
100
114
return map
101
115
} , { } )
102
116
103
- if ( ! layoutComponentMap . Layout && ! fs . existsSync ( layoutComponentMap . Layout . path ) ) {
104
- throw new Error ( `[vuepress] Cannot resolve Layout.vue file in \n ${ layoutComponentMap . Layout . path } ` )
117
+ const { Layout = { } , NotFound = { } } = layoutComponentMap
118
+
119
+ if ( ! Layout && ! fs . existsSync ( Layout . path ) ) {
120
+ throw new Error ( `[vuepress] Cannot resolve Layout.vue file in \n ${ Layout . path } ` )
105
121
}
106
122
107
123
// use default 404 component.
108
- if ( ! layoutComponentMap . NotFound || ! fs . existsSync ( layoutComponentMap . NotFound . path ) ) {
109
- layoutComponentMap [ ' NotFound' ] = {
124
+ if ( ! NotFound || ! fs . existsSync ( NotFound . path ) ) {
125
+ layoutComponentMap . NotFound = {
110
126
filename : 'Layout.vue' ,
111
127
componentName : 'NotFound' ,
112
128
path : path . resolve ( __dirname , '../app/components/NotFound.vue' ) ,
0 commit comments