1
+ 'use strict'
2
+
3
+ /**
4
+ * Module dependencies.
5
+ */
6
+
1
7
const path = require ( 'path' )
2
8
const fs = require ( 'fs' )
3
9
const {
4
- logger , chalk ,
10
+ shortcutPackageResolver : { resolveTheme } ,
5
11
datatypes : { isString } ,
6
- shortcutPackageResolver : { resolveTheme }
12
+ logger , chalk
7
13
} = require ( '@vuepress/shared-utils' )
8
14
15
+ /**
16
+ * Resolve theme.
17
+ *
18
+ * Resolving Priority:
19
+ *
20
+ * 1. If the theme was a absolute path and that path exists, respect it
21
+ * as the theme directory.
22
+ * 2. If 'theme' directory located at vuepressDir exists, respect it as
23
+ * the theme directory.
24
+ * 3. If 'theme' was a shortcut string, resolve it from deps.
25
+ *
26
+ * @param {string } theme
27
+ * @param {string } sourceDir
28
+ * @param {string } vuepressDir
29
+ * @returns {Promise }
30
+ */
31
+
9
32
module . exports = async function loadTheme ( theme , sourceDir , vuepressDir ) {
10
- // resolve theme
11
33
const localThemePath = path . resolve ( vuepressDir , 'theme' )
12
34
const useLocalTheme =
13
- fs . existsSync ( localThemePath ) && ( fs . readdirSync ( localThemePath ) ) . length > 0
35
+ ! fs . existsSync ( theme ) &&
36
+ fs . existsSync ( localThemePath ) &&
37
+ ( fs . readdirSync ( localThemePath ) ) . length > 0
14
38
15
39
let themePath = null // Mandatory
16
40
let themeIndexFile = null // Optional
17
- let themePlugins = [ ] // Optional
18
41
let themeName
19
42
let themeShortcut
20
43
21
44
if ( useLocalTheme ) {
22
- // use local custom theme
23
45
themePath = localThemePath
24
- logger . tip ( `\nApply theme located at ${ themePath } ...` )
46
+ logger . tip ( `\nApply theme located at ${ chalk . gray ( themePath ) } ...` )
25
47
} else if ( isString ( theme ) ) {
26
- // use external theme
27
48
const { module : modulePath , name, shortcut } = resolveTheme ( theme , sourceDir )
28
49
if ( modulePath . endsWith ( '.js' ) || modulePath . endsWith ( '.vue' ) ) {
29
50
themePath = path . parse ( modulePath ) . dir
@@ -44,8 +65,13 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
44
65
}
45
66
46
67
// handle theme api
47
- const { plugins, layoutDir = useLocalTheme ? '.' : 'layouts' } = themeIndexFile
48
- themePlugins = plugins
68
+ const {
69
+ plugins : themePlugins ,
70
+ palette : themePalette ,
71
+ layoutDir = useLocalTheme
72
+ ? '.'
73
+ : 'layouts'
74
+ } = themeIndexFile
49
75
50
76
const layoutDirPath = path . resolve ( themePath , layoutDir )
51
77
@@ -93,6 +119,7 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
93
119
layoutComponentMap,
94
120
themeIndexFile,
95
121
themePlugins,
122
+ themePalette,
96
123
themeName,
97
124
themeShortcut
98
125
}
0 commit comments