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 ) ) || [ ]
37
48module . exports = class ThemeAPI {
59 constructor ( theme , parentTheme ) {
@@ -30,12 +34,12 @@ module.exports = class ThemeAPI {
3034 this . componentMap = this . getComponents ( )
3135 this . layoutComponentMap = this . getLayoutComponentMap ( )
3236
33- Object . keys ( this . componentMap ) . forEach ( ( name ) => {
37+ Object . keys ( this . componentMap ) . forEach ( name => {
3438 const { filename, path } = this . componentMap [ name ]
3539 alias [ `@theme/components/${ filename } ` ] = path
3640 } )
3741
38- Object . keys ( this . layoutComponentMap ) . forEach ( ( name ) => {
42+ Object . keys ( this . layoutComponentMap ) . forEach ( name => {
3943 const { filename, path } = this . layoutComponentMap [ name ]
4044 alias [ `@theme/layouts/${ filename } ` ] = path
4145 } )
@@ -44,13 +48,9 @@ module.exports = class ThemeAPI {
4448 }
4549
4650 getComponents ( ) {
47- const componentDirs = [
48- resolve ( this . theme . path , 'components' )
49- ]
51+ const componentDirs = [ resolve ( this . theme . path , 'components' ) ]
5052 if ( this . existsParentTheme ) {
51- componentDirs . unshift (
52- resolve ( this . parentTheme . path , 'components' ) ,
53- )
53+ componentDirs . unshift ( resolve ( this . parentTheme . path , 'components' ) )
5454 }
5555 return resolveSFCs ( componentDirs )
5656 }
@@ -63,15 +63,15 @@ module.exports = class ThemeAPI {
6363 if ( this . existsParentTheme ) {
6464 layoutDirs . unshift (
6565 resolve ( this . parentTheme . path , '.' ) ,
66- resolve ( this . parentTheme . path , 'layouts' ) ,
66+ resolve ( this . parentTheme . path , 'layouts' )
6767 )
6868 }
6969 // built-in named layout or not.
7070 const layoutComponentMap = resolveSFCs ( layoutDirs )
7171
72- const { Layout = { } , NotFound = { } } = layoutComponentMap
72+ const { Layout, NotFound } = layoutComponentMap
7373 // layout component does not exist.
74- if ( ! Layout || ! fs . existsSync ( Layout . path ) ) {
74+ if ( ! Layout ) {
7575 const fallbackLayoutPath = resolve ( __dirname , 'Layout.fallback.vue' )
7676 layoutComponentMap . Layout = {
7777 filename : 'Layout.vue' ,
@@ -81,10 +81,10 @@ module.exports = class ThemeAPI {
8181 }
8282 logger . warn (
8383 `[vuepress] Cannot resolve Layout.vue file in \n ${ Layout . path } , `
84- + `fallback to default layout: ${ fallbackLayoutPath } `
84+ + `fallback to default layout: ${ fallbackLayoutPath } `
8585 )
8686 }
87- if ( ! NotFound || ! fs . existsSync ( NotFound . path ) ) {
87+ if ( ! NotFound ) {
8888 layoutComponentMap . NotFound = {
8989 filename : 'NotFound.vue' ,
9090 componentName : 'NotFound' ,
@@ -104,25 +104,28 @@ module.exports = class ThemeAPI {
104104 */
105105
106106function 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+ } , { } )
126129}
127130
128131/**
0 commit comments