提交 3f65817c 编写于 作者: U ULIVZ

feat: new theme api

上级 9693ae10
...@@ -26,7 +26,7 @@ module.exports = async function prepare (sourceDir, isProd) { ...@@ -26,7 +26,7 @@ module.exports = async function prepare (sourceDir, isProd) {
await plugin.hooks.ready.run() await plugin.hooks.ready.run()
// 4. Apply plugin options to extend markdown. // 4. apply plugin options to extend markdown.
plugin.options.extendMarkdown.run(markdown) plugin.options.extendMarkdown.run(markdown)
plugin.options.clientDynamicModules.run() plugin.options.clientDynamicModules.run()
......
...@@ -6,6 +6,15 @@ const loadConfig = require('./loadConfig') ...@@ -6,6 +6,15 @@ const loadConfig = require('./loadConfig')
const { sort } = require('./util') const { sort } = require('./util')
module.exports = async function resolveOptions (sourceDir) { module.exports = async function resolveOptions (sourceDir) {
function requireResolve (target) {
return require.resolve(target, {
paths: [
path.resolve(__dirname, '../../node_modules'),
path.resolve(sourceDir)
]
})
}
const vuepressDir = path.resolve(sourceDir, '.vuepress') const vuepressDir = path.resolve(sourceDir, '.vuepress')
const siteConfig = loadConfig(vuepressDir) const siteConfig = loadConfig(vuepressDir)
...@@ -33,54 +42,48 @@ module.exports = async function resolveOptions (sourceDir) { ...@@ -33,54 +42,48 @@ module.exports = async function resolveOptions (sourceDir) {
: path.resolve(sourceDir, '.vuepress/dist') : path.resolve(sourceDir, '.vuepress/dist')
// resolve theme // resolve theme
const useDefaultTheme = ( const localThemePath = path.resolve(vuepressDir, 'theme')
!siteConfig.theme && const useLocalTheme = fs.existsSync(localThemePath)
!fs.existsSync(path.resolve(vuepressDir, 'theme'))
)
const defaultThemePath = path.resolve(__dirname, '../default-theme')
let themePath = null let themePath = null
let themeLayoutPath = null let themeLayoutPath = null
let themeNotFoundPath = null let themeNotFoundPath = null
let themeIndexFile = null
let themePlugins = []
console.log(useDefaultTheme) if (useLocalTheme) {
console.log(siteConfig) // use local custom theme
if (useDefaultTheme) { themePath = localThemePath
// use default theme themeLayoutPath = path.resolve(localThemePath, 'Layout.vue')
themePath = defaultThemePath themeNotFoundPath = path.resolve(localThemePath, 'NotFound.vue')
themeLayoutPath = path.resolve(defaultThemePath, 'Layout.vue') if (!fs.existsSync(themeLayoutPath)) {
themeNotFoundPath = path.resolve(defaultThemePath, 'NotFound.vue') throw new Error(`[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.`)
} else { }
// resolve theme Layout if (!fs.existsSync(themeNotFoundPath)) {
if (siteConfig.theme) { throw new Error(`[vuepress] Cannot resolve NotFound.vue file in .vuepress/theme.`)
// use external theme }
// backward-compatible } else if (siteConfig.theme) {
// use external theme
try {
// backward-compatible 0.x.x.
themeLayoutPath = requireResolve(`vuepress-theme-${siteConfig.theme}/Layout.vue`)
themePath = path.dirname(themeLayoutPath)
themeNotFoundPath = path.resolve(themeLayoutPath, 'NotFound.vue')
} catch (e) {
try { try {
themeLayoutPath = require.resolve(`vuepress-theme-${siteConfig.theme}/Layout.vue`, { themeIndexFile = requireResolve(`vuepress-theme-${siteConfig.theme}/index.js`)
paths: [
path.resolve(__dirname, '../../node_modules'),
path.resolve(sourceDir)
]
})
themePath = path.dirname(themeLayoutPath)
} catch (e) { } catch (e) {
throw new Error(`[vuepress] Failed to load custom theme "${ try {
siteConfig.theme themeIndexFile = requireResolve(`@vuepress/theme-${siteConfig.theme}`)
}". File vuepress-theme-${siteConfig.theme}/Layout.vue does not exist.`) themePath = path.dirname(themeIndexFile)
} themeIndexFile = require(themeIndexFile)
} else { themeLayoutPath = themeIndexFile.layout
// use custom theme themeNotFoundPath = themeIndexFile.notFound
themePath = path.resolve(vuepressDir, 'theme') themePlugins = themeIndexFile.plugins
themeLayoutPath = path.resolve(themePath, 'Layout.vue') } catch (e) {
if (!fs.existsSync(themeLayoutPath)) { throw new Error(`[vuepress] Failed to load custom theme "${siteConfig.theme}". File vuepress-theme-${siteConfig.theme}/Layout.vue does not exist.`)
throw new Error(`[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.`) }
} }
} }
// resolve theme NotFound
themeNotFoundPath = path.resolve(themePath, 'NotFound.vue')
if (!fs.existsSync(themeNotFoundPath)) {
themeNotFoundPath = path.resolve(defaultThemePath, 'NotFound.vue')
}
} }
// resolve theme config // resolve theme config
...@@ -118,7 +121,7 @@ module.exports = async function resolveOptions (sourceDir) { ...@@ -118,7 +121,7 @@ module.exports = async function resolveOptions (sourceDir) {
themePath, themePath,
themeLayoutPath, themeLayoutPath,
themeNotFoundPath, themeNotFoundPath,
useDefaultTheme, themePlugins,
isAlgoliaSearch, isAlgoliaSearch,
markdown markdown
} }
......
...@@ -58,6 +58,9 @@ module.exports = async function ({ ...@@ -58,6 +58,9 @@ module.exports = async function ({
// resolve additional pagesData // resolve additional pagesData
const additionalPagesData = await Promise.all( const additionalPagesData = await Promise.all(
plugin.options.additionalPages.values.map(async ({ route: routePath, path: filepath }) => { plugin.options.additionalPages.values.map(async ({ route: routePath, path: filepath }) => {
if (!fs.existsSync(filepath)) {
throw new Error(`[vuepress] Cannot resolve additional page: ${filepath}`)
}
return getPageData({ filepath, routePath }) return getPageData({ filepath, routePath })
}) })
) )
......
...@@ -69,7 +69,7 @@ module.exports = (options, context) => ({ ...@@ -69,7 +69,7 @@ module.exports = (options, context) => ({
return [ return [
{ {
route: '/readme/', route: '/readme/',
path: path.resolve(__dirname, '../../README.md') path: path.resolve(__dirname, '../../../README.md')
} }
] ]
}, },
......
...@@ -21,7 +21,7 @@ export default { ...@@ -21,7 +21,7 @@ export default {
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>
@import '../../default-theme/styles/config.styl' @import '../src/styles/config.styl'
.badge .badge
display inline-block display inline-block
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册