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

feat: new theme api

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