Skip to content

Commit

Permalink
feat($core): 'plugins' in plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Oct 5, 2018
1 parent 3fbef0d commit 7345515
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
42 changes: 35 additions & 7 deletions packages/@vuepress/core/lib/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { flattenPlugin, normalizePluginsConfig } = require('./util')
const { PLUGIN_OPTION_MAP } = require('./constants')
const {
shortcutPackageResolver: { resolvePlugin },
datatypes: { assertTypes },
datatypes: { assertTypes, isPlainObject },
env: { isDebug },
logger, chalk
} = require('@vuepress/shared-utils')
Expand Down Expand Up @@ -79,22 +79,50 @@ module.exports = class PluginAPI {
if (this._initialized) {
throw new Error(`Cannot add new plugins after initialization.`)
}
let plugin = resolvePlugin(pluginRaw)
if (!plugin.module) {
console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`)
return this

let plugin
if (isPlainObject(pluginRaw) && pluginRaw.$$normalized) {
plugin = pluginRaw
} else {
plugin = this.normalizePlugin(pluginRaw, pluginOptions)
}
plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this)

if (plugin.multiple !== true) {
const duplicateIndex = this._pluginQueue.findIndex(({ name }) => name === plugin.name)
if (duplicateIndex !== -1) {
this._pluginQueue.splice(duplicateIndex, 1)
}
}

this._pluginQueue.push(plugin)

if (plugin.plugins) {
logger.debug(`\nStart to use plugins defined at ${chalk.gray(plugin.name)}`)
logger.debug(JSON.stringify(plugin.plugins, null, 2))
this.useByPluginsConfig(plugin.plugins)
}

return this
}

/**
* normalize plugin
* @param pluginRaw
* @param pluginOptions
* @api public
*/

normalizePlugin (pluginRaw, pluginOptions = {}) {
let plugin = resolvePlugin(pluginRaw)
if (!plugin.module) {
console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`)
return this
}
plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this)
plugin.$$normalized = true
return plugin
}

/**
* Use plugin by config.
*
Expand Down Expand Up @@ -183,7 +211,7 @@ module.exports = class PluginAPI {
}) {
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
if (shortcut) {
logger.tip(`\nApply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`, !isInternalPlugin)
logger.tip(`\nApply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`)
} else if (!isInternalPlugin || isDebug) {
logger.tip(`\nApply plugin ${chalk.magenta(pluginName)} ...`)
}
Expand Down
40 changes: 24 additions & 16 deletions packages/@vuepress/core/lib/prepare/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ module.exports = class AppContext {
this.normalizeHeadTagUrls()
await this.resolveTheme()
this.resolveTemplates()
this.resolvePlugins()

this.applyInternalPlugins()
this.applyUserPlugins()
this.pluginAPI.initialize()

this.markdown = createMarkdown(this)

await this.resolvePages()
Expand All @@ -88,12 +92,12 @@ module.exports = class AppContext {
}

/**
* Apply internal and user plugins
* Apply internal plugins
*
* @api private
*/

resolvePlugins () {
applyInternalPlugins () {
const themeConfig = this.themeConfig
const siteConfig = this.siteConfig

Expand All @@ -105,8 +109,6 @@ module.exports = class AppContext {

this.pluginAPI
// internl core plugins
.use(Object.assign({}, siteConfig, { name: '@vuepress/internal-site-config' }))
.use(Object.assign({}, this.themeEntryFile, { name: '@vuepress/internal-theme-entry-file' }))
.use(require('../internal-plugins/siteData'))
.use(require('../internal-plugins/routes'))
.use(require('../internal-plugins/rootMixins'))
Expand All @@ -117,18 +119,25 @@ module.exports = class AppContext {
.use(require('../internal-plugins/pageComponents'))
.use(require('../internal-plugins/transformModule'))
.use(require('../internal-plugins/dataBlock'))
// user plugin
.useByPluginsConfig(this.cliOptions.plugins)
.useByPluginsConfig(this.siteConfig.plugins)
.useByPluginsConfig(this.themePlugins)
// built-in plugins
.use('@vuepress/last-updated', shouldUseLastUpdated)
.use('@vuepress/register-components', {
componentsDir: [
path.resolve(this.sourceDir, '.vuepress/components')
]
})
.initialize()
}

/**
* Apply user plugins
*
* @api private
*/

applyUserPlugins () {
this.pluginAPI
.useByPluginsConfig(this.cliOptions.plugins)
.use(this.themeEntryFile)
.use(Object.assign({}, this.siteConfig, { name: '@vuepress/internal-site-config' }))
}

/**
Expand Down Expand Up @@ -194,8 +203,8 @@ module.exports = class AppContext {
defaultDevTemplate
])

logger.debug('SSR Template File: ' + chalk.gray(ssrTemplate))
logger.debug('DEV Template File: ' + chalk.gray(devTemplate))
logger.debug('\nSSR Template File: ' + chalk.gray(ssrTemplate))
logger.debug('\nDEV Template File: ' + chalk.gray(devTemplate))
this.devTemplate = devTemplate
this.ssrTemplate = ssrTemplate
}
Expand Down Expand Up @@ -252,8 +261,7 @@ module.exports = class AppContext {
*/

async resolveTheme () {
const theme = this.siteConfig.theme || this.cliOptions.theme
Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir)))
Object.assign(this, (await loadTheme(this)))
}

/**
Expand Down Expand Up @@ -312,7 +320,7 @@ function createTemp (tempPath) {
fs.emptyDirSync(tempPath)
}

logger.tip(`Temp directory: ${chalk.gray(tempPath)}`)
logger.tip(`\nTemp directory: ${chalk.gray(tempPath)}`)
const tempCache = new Map()

async function writeTemp (file, content) {
Expand Down
15 changes: 8 additions & 7 deletions packages/@vuepress/core/lib/prepare/loadTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const {
* @returns {Promise}
*/

module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
module.exports = async function loadTheme (ctx) {
const { siteConfig, cliOptions, sourceDir, vuepressDir, pluginAPI } = ctx
const theme = siteConfig.theme || cliOptions.theme

const localThemePath = path.resolve(vuepressDir, 'theme')
const useLocalTheme =
!fs.existsSync(theme) &&
Expand Down Expand Up @@ -58,16 +61,15 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
}

try {
themeEntryFile = require(themePath)
themeEntryFile = pluginAPI.normalizePlugin(themePath, ctx.themeConfig)
themeEntryFile.name = '@vuepress/internal-theme-entry-file'
themeEntryFile.shortcut = null
} catch (error) {
themeEntryFile = {}
}

// handle theme api
const {
plugins: themePlugins,
palette: themePalette
} = themeEntryFile
const { palette: themePalette } = themeEntryFile

const layoutDirs = [
path.resolve(themePath, 'layouts'),
Expand Down Expand Up @@ -133,7 +135,6 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
themePath,
layoutComponentMap,
themeEntryFile,
themePlugins,
themePalette,
themeName,
themeShortcut
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/core/lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = function createBaseConfig ({
} else {
cacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
}
logger.debug('Cache directory: ' + chalk.gray(cacheDirectory))
logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
if (!cache) {
logger.tip('\nClean cache...\n')
fs.emptyDirSync(cacheDirectory)
Expand Down

0 comments on commit 7345515

Please sign in to comment.