@@ -31,26 +31,45 @@ module.exports = async function prepare (sourceDir) {
31
31
32
32
async function resolveOptions ( sourceDir ) {
33
33
const configPath = path . resolve ( sourceDir , 'vuepress.config.js' )
34
- const siteConfig = fs . existsSync ( configPath ) ? require ( configPath ) : { }
34
+ const userConfig = fs . existsSync ( configPath ) ? require ( configPath ) : { }
35
+
36
+ const hasTheme = userConfig . theme || fs . existsSync ( path . resolve ( sourceDir , '_theme' ) )
35
37
36
38
const options = {
37
- siteConfig,
38
39
sourceDir,
39
- publicPath : siteConfig . baseUrl || '/' ,
40
- themePath : path . resolve ( __dirname , 'default-theme/Layout.vue' ) ,
41
- notFoundPath : path . resolve ( __dirname , 'default-theme/NotFound.vue' ) ,
40
+ publicPath : userConfig . baseUrl || '/' ,
42
41
pages : await globby ( [ '**/*.md' ] , { cwd : sourceDir } )
43
42
}
44
43
45
- // resolve theme
46
- const themePath = path . resolve ( sourceDir , '_theme/Layout.vue' )
47
- if ( fs . existsSync ( themePath ) ) {
48
- options . themePath = themePath
49
- }
44
+ if ( ! hasTheme ) {
45
+ // use default theme
46
+ options . siteConfig = userConfig
47
+ options . themePath = path . resolve ( __dirname , 'default-theme/Layout.vue' )
48
+ options . notFoundPath = path . resolve ( __dirname , 'default-theme/NotFound.vue' )
49
+ } else {
50
+ // resolve theme
51
+ const themeDir = userConfig . theme
52
+ ? path . resolve ( __dirname , `../../${ userConfig . theme } ` )
53
+ : path . resolve ( sourceDir , '_theme' )
54
+ const themeConfigPath = path . resolve ( themeDir , 'config.js' )
55
+ const themeConfig = fs . existsSync ( themeConfigPath )
56
+ ? require ( themeConfigPath )
57
+ : { }
58
+ options . siteConfig = Object . assign ( themeConfig , userConfig )
59
+
60
+ const themePath = path . resolve ( themeDir , 'Layout.vue' )
61
+ if ( fs . existsSync ( themePath ) ) {
62
+ options . themePath = themePath
63
+ } else {
64
+ throw new Error ( '[vuepress] Custom theme must have a Layout.vue file.' )
65
+ }
50
66
51
- const notFoundPath = path . resolve ( sourceDir , '_theme/NotFound.vue' )
52
- if ( fs . existsSync ( notFoundPath ) ) {
53
- options . notFoundPath = notFoundPath
67
+ const notFoundPath = path . resolve ( themeDir , '/NotFound.vue' )
68
+ if ( fs . existsSync ( notFoundPath ) ) {
69
+ options . notFoundPath = notFoundPath
70
+ } else {
71
+ throw new Error ( '[vuepress] Custom theme must have a NotFound.vue file.' )
72
+ }
54
73
}
55
74
56
75
const pagesData = options . pages . map ( file => {
@@ -75,7 +94,7 @@ async function resolveOptions (sourceDir) {
75
94
return data
76
95
} )
77
96
78
- options . siteData = Object . assign ( { } , siteConfig . data , {
97
+ options . siteData = Object . assign ( { } , userConfig . data , {
79
98
pages : pagesData
80
99
} )
81
100
0 commit comments