Skip to content

Commit 7345515

Browse files
committed
feat($core): 'plugins' in plugin.
1 parent 3fbef0d commit 7345515

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

packages/@vuepress/core/lib/plugin-api/index.js

+35-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { flattenPlugin, normalizePluginsConfig } = require('./util')
99
const { PLUGIN_OPTION_MAP } = require('./constants')
1010
const {
1111
shortcutPackageResolver: { resolvePlugin },
12-
datatypes: { assertTypes },
12+
datatypes: { assertTypes, isPlainObject },
1313
env: { isDebug },
1414
logger, chalk
1515
} = require('@vuepress/shared-utils')
@@ -79,22 +79,50 @@ module.exports = class PluginAPI {
7979
if (this._initialized) {
8080
throw new Error(`Cannot add new plugins after initialization.`)
8181
}
82-
let plugin = resolvePlugin(pluginRaw)
83-
if (!plugin.module) {
84-
console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`)
85-
return this
82+
83+
let plugin
84+
if (isPlainObject(pluginRaw) && pluginRaw.$$normalized) {
85+
plugin = pluginRaw
86+
} else {
87+
plugin = this.normalizePlugin(pluginRaw, pluginOptions)
8688
}
87-
plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this)
89+
8890
if (plugin.multiple !== true) {
8991
const duplicateIndex = this._pluginQueue.findIndex(({ name }) => name === plugin.name)
9092
if (duplicateIndex !== -1) {
9193
this._pluginQueue.splice(duplicateIndex, 1)
9294
}
9395
}
96+
9497
this._pluginQueue.push(plugin)
98+
99+
if (plugin.plugins) {
100+
logger.debug(`\nStart to use plugins defined at ${chalk.gray(plugin.name)}`)
101+
logger.debug(JSON.stringify(plugin.plugins, null, 2))
102+
this.useByPluginsConfig(plugin.plugins)
103+
}
104+
95105
return this
96106
}
97107

108+
/**
109+
* normalize plugin
110+
* @param pluginRaw
111+
* @param pluginOptions
112+
* @api public
113+
*/
114+
115+
normalizePlugin (pluginRaw, pluginOptions = {}) {
116+
let plugin = resolvePlugin(pluginRaw)
117+
if (!plugin.module) {
118+
console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`)
119+
return this
120+
}
121+
plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this)
122+
plugin.$$normalized = true
123+
return plugin
124+
}
125+
98126
/**
99127
* Use plugin by config.
100128
*
@@ -183,7 +211,7 @@ module.exports = class PluginAPI {
183211
}) {
184212
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
185213
if (shortcut) {
186-
logger.tip(`\nApply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`, !isInternalPlugin)
214+
logger.tip(`\nApply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`)
187215
} else if (!isInternalPlugin || isDebug) {
188216
logger.tip(`\nApply plugin ${chalk.magenta(pluginName)} ...`)
189217
}

packages/@vuepress/core/lib/prepare/AppContext.js

+24-16
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ module.exports = class AppContext {
7171
this.normalizeHeadTagUrls()
7272
await this.resolveTheme()
7373
this.resolveTemplates()
74-
this.resolvePlugins()
74+
75+
this.applyInternalPlugins()
76+
this.applyUserPlugins()
77+
this.pluginAPI.initialize()
78+
7579
this.markdown = createMarkdown(this)
7680

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

9094
/**
91-
* Apply internal and user plugins
95+
* Apply internal plugins
9296
*
9397
* @api private
9498
*/
9599

96-
resolvePlugins () {
100+
applyInternalPlugins () {
97101
const themeConfig = this.themeConfig
98102
const siteConfig = this.siteConfig
99103

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

106110
this.pluginAPI
107111
// internl core plugins
108-
.use(Object.assign({}, siteConfig, { name: '@vuepress/internal-site-config' }))
109-
.use(Object.assign({}, this.themeEntryFile, { name: '@vuepress/internal-theme-entry-file' }))
110112
.use(require('../internal-plugins/siteData'))
111113
.use(require('../internal-plugins/routes'))
112114
.use(require('../internal-plugins/rootMixins'))
@@ -117,18 +119,25 @@ module.exports = class AppContext {
117119
.use(require('../internal-plugins/pageComponents'))
118120
.use(require('../internal-plugins/transformModule'))
119121
.use(require('../internal-plugins/dataBlock'))
120-
// user plugin
121-
.useByPluginsConfig(this.cliOptions.plugins)
122-
.useByPluginsConfig(this.siteConfig.plugins)
123-
.useByPluginsConfig(this.themePlugins)
124-
// built-in plugins
125122
.use('@vuepress/last-updated', shouldUseLastUpdated)
126123
.use('@vuepress/register-components', {
127124
componentsDir: [
128125
path.resolve(this.sourceDir, '.vuepress/components')
129126
]
130127
})
131-
.initialize()
128+
}
129+
130+
/**
131+
* Apply user plugins
132+
*
133+
* @api private
134+
*/
135+
136+
applyUserPlugins () {
137+
this.pluginAPI
138+
.useByPluginsConfig(this.cliOptions.plugins)
139+
.use(this.themeEntryFile)
140+
.use(Object.assign({}, this.siteConfig, { name: '@vuepress/internal-site-config' }))
132141
}
133142

134143
/**
@@ -194,8 +203,8 @@ module.exports = class AppContext {
194203
defaultDevTemplate
195204
])
196205

197-
logger.debug('SSR Template File: ' + chalk.gray(ssrTemplate))
198-
logger.debug('DEV Template File: ' + chalk.gray(devTemplate))
206+
logger.debug('\nSSR Template File: ' + chalk.gray(ssrTemplate))
207+
logger.debug('\nDEV Template File: ' + chalk.gray(devTemplate))
199208
this.devTemplate = devTemplate
200209
this.ssrTemplate = ssrTemplate
201210
}
@@ -252,8 +261,7 @@ module.exports = class AppContext {
252261
*/
253262

254263
async resolveTheme () {
255-
const theme = this.siteConfig.theme || this.cliOptions.theme
256-
Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir)))
264+
Object.assign(this, (await loadTheme(this)))
257265
}
258266

259267
/**
@@ -312,7 +320,7 @@ function createTemp (tempPath) {
312320
fs.emptyDirSync(tempPath)
313321
}
314322

315-
logger.tip(`Temp directory: ${chalk.gray(tempPath)}`)
323+
logger.tip(`\nTemp directory: ${chalk.gray(tempPath)}`)
316324
const tempCache = new Map()
317325

318326
async function writeTemp (file, content) {

packages/@vuepress/core/lib/prepare/loadTheme.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const {
2828
* @returns {Promise}
2929
*/
3030

31-
module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
31+
module.exports = async function loadTheme (ctx) {
32+
const { siteConfig, cliOptions, sourceDir, vuepressDir, pluginAPI } = ctx
33+
const theme = siteConfig.theme || cliOptions.theme
34+
3235
const localThemePath = path.resolve(vuepressDir, 'theme')
3336
const useLocalTheme =
3437
!fs.existsSync(theme) &&
@@ -58,16 +61,15 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
5861
}
5962

6063
try {
61-
themeEntryFile = require(themePath)
64+
themeEntryFile = pluginAPI.normalizePlugin(themePath, ctx.themeConfig)
65+
themeEntryFile.name = '@vuepress/internal-theme-entry-file'
66+
themeEntryFile.shortcut = null
6267
} catch (error) {
6368
themeEntryFile = {}
6469
}
6570

6671
// handle theme api
67-
const {
68-
plugins: themePlugins,
69-
palette: themePalette
70-
} = themeEntryFile
72+
const { palette: themePalette } = themeEntryFile
7173

7274
const layoutDirs = [
7375
path.resolve(themePath, 'layouts'),
@@ -133,7 +135,6 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
133135
themePath,
134136
layoutComponentMap,
135137
themeEntryFile,
136-
themePlugins,
137138
themePalette,
138139
themeName,
139140
themeShortcut

packages/@vuepress/core/lib/webpack/createBaseConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = function createBaseConfig ({
8282
} else {
8383
cacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
8484
}
85-
logger.debug('Cache directory: ' + chalk.gray(cacheDirectory))
85+
logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
8686
if (!cache) {
8787
logger.tip('\nClean cache...\n')
8888
fs.emptyDirSync(cacheDirectory)

0 commit comments

Comments
 (0)