Skip to content

Add external plugin config #216

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 6 commits into from
Closed
Show file tree
Hide file tree
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
26 changes: 25 additions & 1 deletion lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const yamlParser = require('js-yaml')
const tomlParser = require('toml')
const createMarkdown = require('./markdown')
const tempPath = path.resolve(__dirname, 'app/.temp')
const { inferTitle, extractHeaders, parseFrontmatter } = require('./util')
const { inferTitle, extractHeaders, parseFrontmatter, loadPlugin, cloneOptions } = require('./util')

fs.ensureDirSync(tempPath)

Expand Down Expand Up @@ -74,6 +74,30 @@ if (!Object.assign) Object.assign = require('object-assign')`
fs.existsSync(options.themeEnhanceAppPath)
)

// 7. handle user plugins
if (options.siteConfig.plugins && Array.isArray(options.siteConfig.plugins)) {
const { plugins } = options.siteConfig

for (const plugin of plugins) {
let pluginRequired
let pluginOptions
const exposedOptions = cloneOptions(options)
if (typeof plugin === 'string') {
pluginRequired = loadPlugin(plugin, sourceDir)
} else if (Array.isArray(plugin)) {
pluginRequired = loadPlugin(plugin[0], sourceDir)
pluginOptions = plugin[1]
}
if (pluginRequired) {
try {
await pluginRequired({ exposedOptions, pluginOptions })
} catch (e) {
throw e
}
}
}
}

return options
}

Expand Down
40 changes: 40 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,43 @@ exports.extractHeaders = (content, include = [], md) => {
cache.set(key, res)
return res
}

exports.loadPlugin = (pluginPath, sourceDir) => {
const fs = require('fs-extra')
const path = require('path')
const vuepressDir = path.resolve(sourceDir, '.vuepress')
let plugin
try {
if (fs.existsSync(path.resolve(vuepressDir, pluginPath))) {
plugin = require(path.resolve(vuepressDir, pluginPath))
} else {
plugin = require(pluginPath)
}
} catch (e) {
throw e
}
return plugin
}

exports.cloneOptions = (options) => {
const optionClone = {}
const readonlyProperties = ['sourceDir', 'outDir', 'publicPath', 'markdown', 'themePath', 'useDefaultTheme', 'notFoundPath']
const writableProperties = ['siteConfig', 'pageFiles', 'siteData']

readonlyProperties.forEach(prop => {
Object.defineProperty(optionClone, prop, {
value: options[prop],
enumerable: true
})
})

writableProperties.forEach(prop => {
Object.defineProperty(optionClone, prop, {
value: options[prop],
writable: true,
enumerable: true
})
})

return optionClone
}