@@ -19,6 +19,15 @@ async function writeTemp (file, content) {
19
19
}
20
20
}
21
21
22
+ async function writeEnhanceTemp ( destName , srcPath ) {
23
+ await writeTemp (
24
+ destName ,
25
+ fs . existsSync ( srcPath )
26
+ ? `export { default } from ${ JSON . stringify ( srcPath ) } `
27
+ : `export default function () {}`
28
+ )
29
+ }
30
+
22
31
module . exports = async function prepare ( sourceDir ) {
23
32
// 1. load options
24
33
const options = await resolveOptions ( sourceDir )
@@ -50,29 +59,12 @@ if (!Object.assign) Object.assign = require('object-assign')`
50
59
const hasUserOverride = options . useDefaultTheme && fs . existsSync ( overridePath )
51
60
await writeTemp ( `override.styl` , hasUserOverride ? `@import(${ JSON . stringify ( overridePath ) } )` : `` )
52
61
53
- async function writeEnhanceTemp ( destName , srcPath , isEnhanceExist ) {
54
- await writeTemp (
55
- destName ,
56
- isEnhanceExist
57
- ? `export { default } from ${ JSON . stringify ( srcPath ) } `
58
- : `export default function () {}`
59
- )
60
- }
61
-
62
62
// 6. handle enhanceApp.js
63
63
const enhanceAppPath = path . resolve ( sourceDir , '.vuepress/enhanceApp.js' )
64
- writeEnhanceTemp (
65
- 'enhanceApp.js' ,
66
- enhanceAppPath ,
67
- fs . existsSync ( enhanceAppPath )
68
- )
64
+ await writeEnhanceTemp ( 'enhanceApp.js' , enhanceAppPath )
69
65
70
66
// 7. handle the theme enhanceApp.js
71
- writeEnhanceTemp (
72
- 'themeEnhanceApp.js' ,
73
- options . themeEnhanceAppPath ,
74
- fs . existsSync ( options . themeEnhanceAppPath )
75
- )
67
+ await writeEnhanceTemp ( 'themeEnhanceApp.js' , options . themeEnhanceAppPath )
76
68
77
69
return options
78
70
}
@@ -84,6 +76,8 @@ async function resolveOptions (sourceDir) {
84
76
const configTomlPath = path . resolve ( vuepressDir , 'config.toml' )
85
77
86
78
delete require . cache [ configPath ]
79
+
80
+ // resolve siteConfig
87
81
let siteConfig = { }
88
82
if ( fs . existsSync ( configYmlPath ) ) {
89
83
siteConfig = await parseConfig ( configYmlPath )
@@ -111,77 +105,79 @@ async function resolveOptions (sourceDir) {
111
105
} )
112
106
}
113
107
108
+ // resolve outDir
109
+ const outDir = siteConfig . dest
110
+ ? path . resolve ( siteConfig . dest )
111
+ : path . resolve ( sourceDir , '.vuepress/dist' )
112
+
114
113
// resolve theme
115
114
const useDefaultTheme = (
116
115
! siteConfig . theme &&
117
116
! fs . existsSync ( path . resolve ( vuepressDir , 'theme' ) )
118
117
)
119
-
120
- // resolve algolia
121
- const themeConfig = siteConfig . themeConfig || { }
122
- const isAlgoliaSearch = (
123
- themeConfig . algolia ||
124
- Object . keys ( siteConfig . locales && themeConfig . locales || { } )
125
- . some ( base => themeConfig . locales [ base ] . algolia )
126
- )
127
-
128
- const options = {
129
- siteConfig,
130
- sourceDir,
131
- outDir : siteConfig . dest
132
- ? path . resolve ( siteConfig . dest )
133
- : path . resolve ( sourceDir , '.vuepress/dist' ) ,
134
- publicPath : base ,
135
- pageFiles : sort ( await globby ( [ '**/*.md' , '!.vuepress' , '!node_modules' ] , { cwd : sourceDir } ) ) ,
136
- pagesData : null ,
137
- themePath : null ,
138
- notFoundPath : null ,
139
- useDefaultTheme,
140
- isAlgoliaSearch,
141
- markdown : createMarkdown ( siteConfig )
142
- }
118
+ const defaultThemePath = path . resolve ( __dirname , 'default-theme' )
119
+ let themePath = null
120
+ let themeLayoutPath = null
121
+ let themeNotFoundPath = null
122
+ let themeEnhanceAppPath = null
143
123
144
124
if ( useDefaultTheme ) {
145
125
// use default theme
146
- options . themePath = path . resolve ( __dirname , 'default-theme/Layout.vue' )
147
- options . notFoundPath = path . resolve ( __dirname , 'default-theme/NotFound.vue' )
126
+ themePath = defaultThemePath
127
+ themeLayoutPath = path . resolve ( defaultThemePath , 'Layout.vue' )
128
+ themeNotFoundPath = path . resolve ( defaultThemePath , 'NotFound.vue' )
148
129
} else {
149
- let themeDir
150
- let themePath
151
- // resolve custom theme
130
+ // resolve theme Layout
152
131
if ( siteConfig . theme ) {
132
+ // use external theme
153
133
try {
154
- themePath = require . resolve ( `vuepress-theme-${ siteConfig . theme } /Layout.vue` )
155
- themeDir = path . dirname ( themePath )
134
+ themeLayoutPath = require . resolve ( `vuepress-theme-${ siteConfig . theme } /Layout.vue` )
135
+ themePath = path . dirname ( themeLayoutPath )
156
136
} catch ( e ) {
157
137
throw new Error ( `[vuepress] Failed to load custom theme "${
158
138
siteConfig . theme
159
139
} ". File vuepress-theme-${ siteConfig . theme } /Layout.vue does not exist.`)
160
140
}
161
141
} else {
162
- themeDir = path . resolve ( vuepressDir , 'theme' )
163
- themePath = path . resolve ( themeDir , 'Layout.vue' )
164
- if ( ! fs . existsSync ( themePath ) ) {
142
+ // use custom theme
143
+ themePath = path . resolve ( vuepressDir , 'theme' )
144
+ themeLayoutPath = path . resolve ( themePath , 'Layout.vue' )
145
+ if ( ! fs . existsSync ( themeLayoutPath ) ) {
165
146
throw new Error ( `[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.` )
166
147
}
167
148
}
168
- options . themePath = themePath
169
149
170
- const notFoundPath = path . resolve ( themeDir , 'NotFound.vue' )
171
- if ( fs . existsSync ( notFoundPath ) ) {
172
- options . notFoundPath = notFoundPath
173
- } else {
174
- options . notFoundPath = path . resolve ( __dirname , 'default-theme/NotFound.vue' )
150
+ // resolve theme NotFound
151
+ themeNotFoundPath = path . resolve ( themePath , 'NotFound.vue' )
152
+ if ( ! fs . existsSync ( themeNotFoundPath ) ) {
153
+ themeNotFoundPath = path . resolve ( defaultThemePath , 'NotFound.vue' )
175
154
}
176
155
177
- const themeEnhanceAppPath = path . resolve ( themeDir , 'enhanceApp.js' )
178
- if ( fs . existsSync ( themeEnhanceAppPath ) ) {
179
- options . themeEnhanceAppPath = themeEnhanceAppPath
156
+ // resolve theme enhanceApp
157
+ themeEnhanceAppPath = path . resolve ( themePath , 'enhanceApp.js' )
158
+ if ( ! fs . existsSync ( themeEnhanceAppPath ) ) {
159
+ themeEnhanceAppPath = null
180
160
}
181
161
}
182
162
183
- // resolve pages
184
- const pagesData = await Promise . all ( options . pageFiles . map ( async ( file ) => {
163
+ // resolve theme config
164
+ const themeConfig = siteConfig . themeConfig || { }
165
+
166
+ // resolve algolia
167
+ const isAlgoliaSearch = (
168
+ themeConfig . algolia ||
169
+ Object . keys ( siteConfig . locales && themeConfig . locales || { } )
170
+ . some ( base => themeConfig . locales [ base ] . algolia )
171
+ )
172
+
173
+ // resolve markdown
174
+ const markdown = createMarkdown ( siteConfig )
175
+
176
+ // resolve pageFiles
177
+ const pageFiles = sort ( await globby ( [ '**/*.md' , '!.vuepress' , '!node_modules' ] , { cwd : sourceDir } ) )
178
+
179
+ // resolve pagesData
180
+ const pagesData = await Promise . all ( pageFiles . map ( async ( file ) => {
185
181
const data = {
186
182
path : fileToPath ( file )
187
183
}
@@ -197,7 +193,7 @@ async function resolveOptions (sourceDir) {
197
193
const headers = extractHeaders (
198
194
frontmatter . content ,
199
195
[ 'h2' , 'h3' ] ,
200
- options . markdown
196
+ markdown
201
197
)
202
198
if ( headers . length ) {
203
199
data . headers = headers
@@ -212,15 +208,32 @@ async function resolveOptions (sourceDir) {
212
208
} ) )
213
209
214
210
// resolve site data
215
- options . siteData = {
211
+ const siteData = {
216
212
title : siteConfig . title || '' ,
217
213
description : siteConfig . description || '' ,
218
- base : siteConfig . base || '/' ,
214
+ base,
219
215
pages : pagesData ,
220
216
themeConfig,
221
217
locales : siteConfig . locales
222
218
}
223
219
220
+ const options = {
221
+ siteConfig,
222
+ siteData,
223
+ sourceDir,
224
+ outDir,
225
+ publicPath : base ,
226
+ pageFiles,
227
+ pagesData,
228
+ themePath,
229
+ themeLayoutPath,
230
+ themeNotFoundPath,
231
+ themeEnhanceAppPath,
232
+ useDefaultTheme,
233
+ isAlgoliaSearch,
234
+ markdown
235
+ }
236
+
224
237
return options
225
238
}
226
239
@@ -279,30 +292,38 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
279
292
const file = pageFiles [ index ]
280
293
const filePath = path . resolve ( sourceDir , file )
281
294
let code = `
282
- {
283
- path: ${ JSON . stringify ( pagePath ) } ,
284
- component: Theme ,
285
- beforeEnter: (to, from, next) => {
286
- import(${ JSON . stringify ( filePath ) } ).then(comp => {
287
- Vue.component(${ JSON . stringify ( fileToComponentName ( file ) ) } , comp.default)
288
- next()
289
- })
290
- }
291
- }`
295
+ {
296
+ path: ${ JSON . stringify ( pagePath ) } ,
297
+ component: ThemeLayout ,
298
+ beforeEnter: (to, from, next) => {
299
+ import(${ JSON . stringify ( filePath ) } ).then(comp => {
300
+ Vue.component(${ JSON . stringify ( fileToComponentName ( file ) ) } , comp.default)
301
+ next()
302
+ })
303
+ }
304
+ }`
292
305
293
306
if ( / \/ $ / . test ( pagePath ) ) {
294
- code += `,{
295
- path: ${ JSON . stringify ( pagePath + 'index.html' ) } ,
296
- redirect: ${ JSON . stringify ( pagePath ) }
297
- }`
307
+ code += `,
308
+ {
309
+ path: ${ JSON . stringify ( pagePath + 'index.html' ) } ,
310
+ redirect: ${ JSON . stringify ( pagePath ) }
311
+ }`
298
312
}
299
313
300
314
return code
301
315
}
302
316
317
+ const notFoundRoute = `,
318
+ {
319
+ path: '*',
320
+ component: ThemeNotFound
321
+ }`
322
+
303
323
return (
304
- `import Theme from '@theme'\n` +
305
- `export const routes = [${ pages . map ( genRoute ) . join ( ',' ) } \n]`
324
+ `import ThemeLayout from '@themeLayout'\n` +
325
+ `import ThemeNotFound from '@themeNotFound'\n` +
326
+ `export const routes = [${ pages . map ( genRoute ) . join ( ',' ) } ${ notFoundRoute } \n]`
306
327
)
307
328
}
308
329
0 commit comments