Skip to content

Commit 3b68913

Browse files
shigmafgiraud
authored and
fgiraud
committedSep 7, 2019
feat($core): better layout check (#1455)
* better layout check * enhance resolve SFCs * fix(layout): fix broken class content * fix(delete): delete useless use of context
1 parent 6b5a002 commit 3b68913

File tree

3 files changed

+47
-40
lines changed

3 files changed

+47
-40
lines changed
 

‎packages/@vuepress/core/lib/node/loadTheme.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const {
99
path: { resolve, parse },
1010
moduleResolver: { getThemeResolver },
1111
datatypes: { isString },
12-
logger, chalk
12+
logger,
13+
chalk
1314
} = require('@vuepress/shared-utils')
1415
const ThemeAPI = require('./theme-api')
1516

@@ -37,7 +38,7 @@ module.exports = function loadTheme (ctx) {
3738
if (!theme.path) {
3839
throw new Error(
3940
'[vuepress] You must specify a theme, or create a local custom theme. \n'
40-
+ 'For more details, refer to https://vuepress.vuejs.org/guide/custom-themes.html#custom-themes. \n'
41+
+ 'For more details, refer to https://vuepress.vuejs.org/guide/custom-themes.html#custom-themes. \n'
4142
)
4243
}
4344

@@ -55,7 +56,7 @@ module.exports = function loadTheme (ctx) {
5556

5657
logger.debug('theme', theme.name, theme.path)
5758
logger.debug('parentTheme', parentTheme.name, parentTheme.path)
58-
return new ThemeAPI(theme, parentTheme, ctx)
59+
return new ThemeAPI(theme, parentTheme)
5960
}
6061

6162
function normalizeThemePath (resolved) {
@@ -82,10 +83,11 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) {
8283
/**
8384
* 1. From `.vuepress/theme` directory.
8485
*/
85-
if (!ignoreLocal
86+
if (
87+
!ignoreLocal
8688
&& !fs.existsSync(theme)
8789
&& fs.existsSync(localThemePath)
88-
&& (fs.readdirSync(localThemePath)).length > 0
90+
&& fs.readdirSync(localThemePath).length > 0
8991
) {
9092
path = localThemePath
9193
name = shortcut = 'local'

‎packages/@vuepress/core/lib/node/theme-api/index.js

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
const { logger, fs, path: { resolve }} = require('@vuepress/shared-utils')
2-
const readdirSync = dir => fs.existsSync(dir) && fs.readdirSync(dir) || []
1+
const {
2+
logger,
3+
fs,
4+
path: { resolve }
5+
} = require('@vuepress/shared-utils')
6+
const readdirSync = dir => (fs.existsSync(dir) && fs.readdirSync(dir)) || []
37

48
module.exports = class ThemeAPI {
59
constructor (theme, parentTheme) {
@@ -30,12 +34,12 @@ module.exports = class ThemeAPI {
3034
this.componentMap = this.getComponents()
3135
this.layoutComponentMap = this.getLayoutComponentMap()
3236

33-
Object.keys(this.componentMap).forEach((name) => {
37+
Object.keys(this.componentMap).forEach(name => {
3438
const { filename, path } = this.componentMap[name]
3539
alias[`@theme/components/${filename}`] = path
3640
})
3741

38-
Object.keys(this.layoutComponentMap).forEach((name) => {
42+
Object.keys(this.layoutComponentMap).forEach(name => {
3943
const { filename, path } = this.layoutComponentMap[name]
4044
alias[`@theme/layouts/${filename}`] = path
4145
})
@@ -44,13 +48,9 @@ module.exports = class ThemeAPI {
4448
}
4549

4650
getComponents () {
47-
const componentDirs = [
48-
resolve(this.theme.path, 'components')
49-
]
51+
const componentDirs = [resolve(this.theme.path, 'components')]
5052
if (this.existsParentTheme) {
51-
componentDirs.unshift(
52-
resolve(this.parentTheme.path, 'components'),
53-
)
53+
componentDirs.unshift(resolve(this.parentTheme.path, 'components'))
5454
}
5555
return resolveSFCs(componentDirs)
5656
}
@@ -63,15 +63,15 @@ module.exports = class ThemeAPI {
6363
if (this.existsParentTheme) {
6464
layoutDirs.unshift(
6565
resolve(this.parentTheme.path, '.'),
66-
resolve(this.parentTheme.path, 'layouts'),
66+
resolve(this.parentTheme.path, 'layouts')
6767
)
6868
}
6969
// built-in named layout or not.
7070
const layoutComponentMap = resolveSFCs(layoutDirs)
7171

72-
const { Layout = {}, NotFound = {}} = layoutComponentMap
72+
const { Layout, NotFound } = layoutComponentMap
7373
// layout component does not exist.
74-
if (!Layout || !fs.existsSync(Layout.path)) {
74+
if (!Layout) {
7575
const fallbackLayoutPath = resolve(__dirname, 'Layout.fallback.vue')
7676
layoutComponentMap.Layout = {
7777
filename: 'Layout.vue',
@@ -81,10 +81,10 @@ module.exports = class ThemeAPI {
8181
}
8282
logger.warn(
8383
`[vuepress] Cannot resolve Layout.vue file in \n ${Layout.path}, `
84-
+ `fallback to default layout: ${fallbackLayoutPath}`
84+
+ `fallback to default layout: ${fallbackLayoutPath}`
8585
)
8686
}
87-
if (!NotFound || !fs.existsSync(NotFound.path)) {
87+
if (!NotFound) {
8888
layoutComponentMap.NotFound = {
8989
filename: 'NotFound.vue',
9090
componentName: 'NotFound',
@@ -104,25 +104,28 @@ module.exports = class ThemeAPI {
104104
*/
105105

106106
function resolveSFCs (dirs) {
107-
return dirs.map(
108-
layoutDir => readdirSync(layoutDir)
109-
.filter(filename => filename.endsWith('.vue'))
110-
.map(filename => {
111-
const componentName = getComponentName(filename)
112-
return {
113-
filename,
114-
componentName,
115-
isInternal: isInternal(componentName),
116-
path: resolve(layoutDir, filename)
117-
}
118-
})
119-
).reduce((arr, next) => {
120-
arr.push(...next)
121-
return arr
122-
}, []).reduce((map, component) => {
123-
map[component.componentName] = component
124-
return map
125-
}, {})
107+
return dirs
108+
.map(layoutDir =>
109+
readdirSync(layoutDir)
110+
.filter(filename => filename.endsWith('.vue'))
111+
.map(filename => {
112+
const componentName = getComponentName(filename)
113+
return {
114+
filename,
115+
componentName,
116+
isInternal: isInternal(componentName),
117+
path: resolve(layoutDir, filename)
118+
}
119+
})
120+
)
121+
.reduce((arr, next) => {
122+
arr.push(...next)
123+
return arr
124+
}, [])
125+
.reduce((map, component) => {
126+
map[component.componentName] = component
127+
return map
128+
}, {})
126129
}
127130

128131
/**

‎packages/@vuepress/theme-default/components/Page.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<main class="page">
33
<slot name="top" />
44

5-
<Content />
5+
<Content class="theme-default-content" />
66
<PageEdit />
77

88
<PageNav v-bind="{ sidebarItems }" />
@@ -22,6 +22,8 @@ export default {
2222
</script>
2323

2424
<style lang="stylus">
25+
@require '../styles/wrapper.styl';
26+
2527
.page {
2628
padding-bottom: 2rem;
2729
display: block;

0 commit comments

Comments
 (0)
Please sign in to comment.