Skip to content

feat: allow themes to set default themeConfig #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,9 @@ if (!Object.assign) Object.assign = require('object-assign')`

async function resolveOptions (sourceDir) {
const vuepressDir = path.resolve(sourceDir, '.vuepress')
const configPath = path.resolve(vuepressDir, 'config.js')
const configYmlPath = path.resolve(vuepressDir, 'config.yml')
const configTomlPath = path.resolve(vuepressDir, 'config.toml')

delete require.cache[configPath]

// resolve siteConfig
let siteConfig = {}
if (fs.existsSync(configYmlPath)) {
siteConfig = await parseConfig(configYmlPath)
} else if (fs.existsSync(configTomlPath)) {
siteConfig = await parseConfig(configTomlPath)
} else if (fs.existsSync(configPath)) {
siteConfig = require(configPath)
}
const siteConfig = await resolveConfig(vuepressDir)

// normalize head tag urls for base
const base = siteConfig.base || '/'
Expand Down Expand Up @@ -170,8 +158,12 @@ async function resolveOptions (sourceDir) {
}
}

// resolve theme default config
const themeDefaultConfig = await resolveConfig(themePath)

// resolve theme config
const themeConfig = siteConfig.themeConfig || {}
const themeConfig = Object.assign({}, themeDefaultConfig, siteConfig.themeConfig || {})
siteConfig.themeConfig = themeConfig

// resolve algolia
const isAlgoliaSearch = (
Expand Down Expand Up @@ -357,6 +349,28 @@ function sort (arr) {
})
}

async function resolveConfig (dir) {
const configPath = path.resolve(dir, 'config.js')
const configYmlPath = path.resolve(dir, 'config.yml')
const configTomlPath = path.resolve(dir, 'config.toml')

// resolve siteConfig
if (fs.existsSync(configPath)) {
delete require.cache[configPath]
return require(configPath)
}

if (fs.existsSync(configYmlPath)) {
return await parseConfig(configYmlPath)
}

if (fs.existsSync(configTomlPath)) {
return await parseConfig(configTomlPath)
}

return {}
}

async function parseConfig (file) {
const content = await fs.readFile(file, 'utf-8')
const [extension] = /.\w+$/.exec(file)
Expand Down