-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
155 lines (141 loc) Β· 3.74 KB
/
index.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
const {
logger,
fs,
path: { resolve }
} = require('@vuepress/shared-utils')
const readdirSync = dir => (fs.existsSync(dir) && fs.readdirSync(dir)) || []
module.exports = class ThemeAPI {
constructor (theme, parentTheme) {
this.theme = theme
this.parentTheme = parentTheme || {}
this.existsParentTheme = !!this.parentTheme.path
this.vuepressPlugin = {
name: '@vuepress/internal-theme-api',
alias: {}
}
this.init()
}
setAlias (alias) {
this.vuepressPlugin.alias = {
...this.vuepressPlugin.alias,
...alias
}
}
init () {
const alias = {
'@current-theme': this.theme.path
}
if (this.existsParentTheme) {
alias['@parent-theme'] = this.parentTheme.path
}
this.componentMap = this.getComponents()
this.layoutComponentMap = this.getLayoutComponentMap()
Object.keys(this.componentMap).forEach(name => {
const { filename, path } = this.componentMap[name]
alias[`@theme/components/${filename}`] = path
})
Object.keys(this.layoutComponentMap).forEach(name => {
const { filename, path } = this.layoutComponentMap[name]
alias[`@theme/layouts/${filename}`] = path
})
alias['@theme'] = this.theme.path
this.setAlias(alias)
}
getComponents () {
const componentDirs = [resolve(this.theme.path, 'components')]
if (this.existsParentTheme) {
componentDirs.unshift(resolve(this.parentTheme.path, 'components'))
}
return resolveSFCs(componentDirs)
}
getLayoutComponentMap () {
const layoutDirs = [
resolve(this.theme.path, '.'),
resolve(this.theme.path, 'layouts')
]
if (this.existsParentTheme) {
layoutDirs.unshift(
resolve(this.parentTheme.path, '.'),
resolve(this.parentTheme.path, 'layouts')
)
}
// built-in named layout or not.
const layoutComponentMap = resolveSFCs(layoutDirs)
const { Layout, NotFound } = layoutComponentMap
// layout component does not exist.
if (!Layout) {
const fallbackLayoutPath = resolve(__dirname, 'Layout.fallback.vue')
layoutComponentMap.Layout = {
filename: 'Layout.vue',
componentName: 'Layout',
path: fallbackLayoutPath,
isInternal: true
}
logger.warn(
`[vuepress] Cannot resolve Layout.vue file in \n ${Layout.path}, `
+ `fallback to default layout: ${fallbackLayoutPath}`
)
}
if (!NotFound) {
layoutComponentMap.NotFound = {
filename: 'NotFound.vue',
componentName: 'NotFound',
path: resolve(__dirname, '../../client/components/NotFound.vue'),
isInternal: true
}
}
return layoutComponentMap
}
}
/**
* Resolve Vue SFCs, return a Map
*
* @param dirs
* @returns {*}
*/
function resolveSFCs (dirs) {
return dirs
.map(layoutDir =>
readdirSync(layoutDir)
.filter(filename => filename.endsWith('.vue'))
.map(filename => {
const componentName = getComponentName(filename)
return {
filename,
componentName,
isInternal: isInternal(componentName),
path: resolve(layoutDir, filename)
}
})
)
.reduce((arr, next) => {
arr.push(...next)
return arr
}, [])
.reduce((map, component) => {
map[component.componentName] = component
return map
}, {})
}
/**
* normalize component name
*
* @param {strin} filename
* @returns {string}
*/
function getComponentName (filename) {
filename = filename.slice(0, -4)
if (filename === '404') {
filename = 'NotFound'
}
return filename
}
/**
* Whether it's agreed layout component
*
* @param name
* @returns {boolean}
*/
function isInternal (name) {
return name === 'Layout' || name === 'NotFound'
}