Skip to content
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

feat: support theme default config and extra pages rendering #315

Closed
Closed
Changes from 2 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
41 changes: 25 additions & 16 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,7 @@ 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 @@ -159,9 +145,12 @@ async function resolveOptions (sourceDir) {
themeEnhanceAppPath = null
}
}
// 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 @@ -335,6 +324,26 @@ 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')

delete require.cache[configPath]

// resolve siteConfig
let config = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my honest opinion 😊❤️🙈, I think 335-344 would be cleaner with the following:

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

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

if (fs.existsSync(configPath)) {
  return require(configPath)
}

return {}

Copy link
Member Author

@meteorlxy meteorlxy May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh looks better. I just moved things down into a function without much refactor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea yea! Just spot that when I see the changes, might be worth it to make the change.

if (fs.existsSync(configYmlPath)) {
config = await parseConfig(configYmlPath)
} else if (fs.existsSync(configTomlPath)) {
config = await parseConfig(configTomlPath)
} else if (fs.existsSync(configPath)) {
config = require(configPath)
}

return config
}

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