forked from vuejs/vuepress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadTheme.js
186 lines (158 loc) Β· 5 KB
/
loadTheme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict'
/**
* Module dependencies.
*/
const {
fs, path,
moduleResolver: { getThemeResolver },
datatypes: { isString },
logger, chalk
} = require('@vuepress/shared-utils')
/**
* Resolve theme.
*
* Resolving Priority:
*
* 1. If the theme was a absolute path and that path exists, respect it
* as the theme directory.
* 2. If 'theme' directory located at vuepressDir exists, respect it as
* the theme directory.
* 3. If 'theme' was a shortcut string, resolve it from deps.
*
* @param {string} theme
* @param {string} sourceDir
* @param {string} vuepressDir
* @returns {Promise}
*/
module.exports = async function loadTheme (ctx) {
const { siteConfig, cliOptions, sourceDir, vuepressDir, pluginAPI } = ctx
const theme = siteConfig.theme || cliOptions.theme
const themeResolver = getThemeResolver()
const localThemePath = path.resolve(vuepressDir, 'theme')
const useLocalTheme =
!fs.existsSync(theme) &&
fs.existsSync(localThemePath) &&
(fs.readdirSync(localThemePath)).length > 0
let themePath = null // Mandatory
let themeEntryFile = null // Optional
let themeName
let themeShortcut
let parentThemePath = null // Optional
let parentThemeEntryFile = null // Optional
if (useLocalTheme) {
themePath = localThemePath
logger.tip(`Apply theme located at ${chalk.gray(themePath)}...`)
} else if (isString(theme)) {
const resolved = themeResolver.resolve(theme, sourceDir)
const { entry, name, shortcut } = resolved
if (entry === null) {
throw new Error(`Cannot resolve theme ${theme}.`)
}
themePath = normalizeThemePath(resolved)
themeName = name
themeShortcut = shortcut
logger.tip(`Apply theme ${chalk.gray(themeName)}`)
} else {
throw new Error(`[vuepress] You must specify a theme, or create a local custom theme. \n For more details, refer to https://vuepress.vuejs.org/guide/custom-themes.html#custom-themes. \n`)
}
try {
themeEntryFile = pluginAPI.normalizePlugin(themePath, ctx.themeConfig)
} catch (error) {
themeEntryFile = {}
}
themeEntryFile.name = '@vuepress/internal-theme-entry-file'
themeEntryFile.shortcut = null
// handle theme api
const layoutDirs = [
path.resolve(themePath, '.'),
path.resolve(themePath, 'layouts')
]
if (themeEntryFile.extend) {
const resolved = themeResolver.resolve(themeEntryFile.extend, sourceDir)
if (resolved.entry === null) {
throw new Error(`Cannot resolve parent theme ${themeEntryFile.extend}.`)
}
parentThemePath = normalizeThemePath(resolved)
try {
parentThemeEntryFile = pluginAPI.normalizePlugin(parentThemePath, ctx.themeConfig)
} catch (error) {
parentThemeEntryFile = {}
}
parentThemeEntryFile.name = '@vuepress/internal-parent-theme-entry-file'
parentThemeEntryFile.shortcut = null
layoutDirs.unshift(
path.resolve(parentThemePath, '.'),
path.resolve(parentThemePath, 'layouts'),
)
themeEntryFile.alias = Object.assign(
themeEntryFile.alias || {},
{ '@parent-theme': parentThemePath }
)
}
// normalize component name
const getComponentName = filename => {
filename = filename.slice(0, -4)
if (filename === '404') {
filename = 'NotFound'
}
return filename
}
const readdirSync = dir => fs.existsSync(dir) && fs.readdirSync(dir) || []
// built-in named layout or not.
const isInternal = componentName => componentName === 'Layout' ||
componentName === 'NotFound'
const layoutComponentMap = layoutDirs
.map(
layourDir => readdirSync(layourDir)
.filter(filename => filename.endsWith('.vue'))
.map(filename => {
const componentName = getComponentName(filename)
return {
filename, componentName,
isInternal: isInternal(componentName),
path: path.resolve(layourDir, filename)
}
})
)
.reduce((arr, next) => {
arr.push(...next)
return arr
}, [])
.reduce((map, component) => {
map[component.componentName] = component
return map
}, {})
const { Layout = {}, NotFound = {}} = layoutComponentMap
if (!Layout && !fs.existsSync(Layout.path)) {
throw new Error(`[vuepress] Cannot resolve Layout.vue file in \n ${Layout.path}`)
}
// use default 404 component.
if (!NotFound || !fs.existsSync(NotFound.path)) {
layoutComponentMap.NotFound = {
filename: 'Layout.vue',
componentName: 'NotFound',
path: path.resolve(__dirname, '../app/components/NotFound.vue'),
isInternal: true
}
}
return {
themePath,
layoutComponentMap,
themeEntryFile,
themeName,
themeShortcut,
parentThemePath,
parentThemeEntryFile
}
}
function normalizeThemePath (resolved) {
const { entry, name, fromDep } = resolved
if (fromDep) {
const pkgPath = require.resolve(name)
return path.parse(pkgPath).dir
} else if (entry.endsWith('.js') || entry.endsWith('.vue')) {
return path.parse(entry).dir
} else {
return entry
}
}