-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
243 lines (212 loc) Β· 6.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict'
/**
* Module dependencies.
*/
const instantiateOption = require('./override/instantiateOption')
const { flattenPlugin, normalizePluginsConfig } = require('./util')
const { PLUGIN_OPTION_MAP } = require('./constants')
const {
moduleResolver: { getPluginResolver },
datatypes: { assertTypes, isPlainObject },
logger, chalk
} = require('@vuepress/shared-utils')
/**
* Expose PluginAPI class.
*/
module.exports = class PluginAPI {
constructor (context) {
this.options = {}
this._pluginContext = context
this._pluginQueue = []
this._initialized = false
this._pluginResolver = getPluginResolver()
this.initializeOptions(PLUGIN_OPTION_MAP)
}
/**
* Get enabled plugins
*
* @returns {array}
* @api public
*/
get enabledPlugins () {
return this._pluginQueue.filter(({ enabled }) => enabled)
}
/**
* Get disabled plugins
*
* @returns {array}
* @api public
*/
get disabledPlugins () {
return this._pluginQueue.filter(({ enabled }) => !enabled)
}
/**
* initialize plugin.
*
* @api public
*/
initialize () {
this._initialized = true
this._pluginQueue.forEach(plugin => {
if (plugin.enabled) {
this.applyPlugin(plugin)
} else {
logger.debug(`${chalk.gray(`[${plugin.name}]`)} disabled.`)
}
})
}
/**
* Normalize plugin and push it to the plugin queue.
*
* @param {object} pluginRaw
* @param {object} pluginOptions
* @returns {module.PluginAPI}
* @api public
*/
use (pluginRaw, pluginOptions = {}) {
if (this._initialized) {
throw new Error(`Cannot add new plugins after initialization.`)
}
let plugin
if (isPlainObject(pluginRaw) && pluginRaw.$$normalized) {
plugin = pluginRaw
} else {
plugin = this.normalizePlugin(pluginRaw, pluginOptions)
}
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(`Plugins defined at ${chalk.gray(plugin.name)}`, plugin.plugins)
this.useByPluginsConfig(plugin.plugins)
}
return this
}
/**
* normalize plugin
* @param pluginRaw
* @param pluginOptions
* @api public
*/
normalizePlugin (pluginRaw, pluginOptions = {}) {
let plugin = this._pluginResolver.resolve(pluginRaw)
if (!plugin.entry) {
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.
*
* @param pluginsConfig
* @returns {module.PluginAPI}
* @api public
*/
useByPluginsConfig (pluginsConfig) {
pluginsConfig = normalizePluginsConfig(pluginsConfig)
pluginsConfig.forEach(([pluginRaw, pluginOptions]) => {
this.use(pluginRaw, pluginOptions)
})
return this
}
/**
* initialize plugin options.
*
* @api private
*/
initializeOptions () {
Object.keys(PLUGIN_OPTION_MAP).forEach(key => {
const option = PLUGIN_OPTION_MAP[key]
this.options[option.name] = instantiateOption(option)
})
}
/**
* Register plugin option.
*
* @param {string} key
* @param {any} value
* @param {string} pluginName
* @returns {module.PluginAPI}
* @api private
*/
registerOption (key, value, pluginName) {
const option = PLUGIN_OPTION_MAP[key]
const types = option.types
const { valid, warnMsg } = assertTypes(value, types)
if (valid) {
this.options[option.name].add(pluginName, value)
} else if (value !== undefined) {
logger.warn(
`${chalk.gray(pluginName)} ` +
`Invalid value for "option" ${chalk.cyan(option.name)}: ${warnMsg}`
)
}
return this
}
/**
* apply plugin.
*
* @api private
*/
applyPlugin ({
// info
name: pluginName,
shortcut,
// hooks
ready,
compiled,
updated,
generated,
// options
chainWebpack,
extendMarkdown,
chainMarkdown,
enhanceAppFiles,
outFiles,
extendPageData,
clientDynamicModules,
clientRootMixin,
additionalPages,
globalUIComponents,
define,
alias,
extendCli,
beforeDevServer,
afterDevServer
}) {
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
logger[isInternalPlugin ? 'debug' : 'tip'](pluginLog(pluginName, shortcut))
this
.registerOption(PLUGIN_OPTION_MAP.READY.key, ready, pluginName)
.registerOption(PLUGIN_OPTION_MAP.COMPILED.key, compiled, pluginName)
.registerOption(PLUGIN_OPTION_MAP.UPDATED.key, updated, pluginName)
.registerOption(PLUGIN_OPTION_MAP.GENERATED.key, generated, pluginName)
.registerOption(PLUGIN_OPTION_MAP.CHAIN_WEBPACK.key, chainWebpack, pluginName)
.registerOption(PLUGIN_OPTION_MAP.EXTEND_MARKDOWN.key, extendMarkdown, pluginName)
.registerOption(PLUGIN_OPTION_MAP.CHAIN_MARKDOWN.key, chainMarkdown, pluginName)
.registerOption(PLUGIN_OPTION_MAP.EXTEND_PAGE_DATA.key, extendPageData, pluginName)
.registerOption(PLUGIN_OPTION_MAP.ENHANCE_APP_FILES.key, enhanceAppFiles, pluginName)
.registerOption(PLUGIN_OPTION_MAP.OUT_FILES.key, outFiles, pluginName)
.registerOption(PLUGIN_OPTION_MAP.CLIENT_DYNAMIC_MODULES.key, clientDynamicModules, pluginName)
.registerOption(PLUGIN_OPTION_MAP.CLIENT_ROOT_MIXIN.key, clientRootMixin, pluginName)
.registerOption(PLUGIN_OPTION_MAP.ADDITIONAL_PAGES.key, additionalPages, pluginName)
.registerOption(PLUGIN_OPTION_MAP.GLOBAL_UI_COMPONENTS.key, globalUIComponents, pluginName)
.registerOption(PLUGIN_OPTION_MAP.DEFINE.key, define, pluginName)
.registerOption(PLUGIN_OPTION_MAP.ALIAS.key, alias, pluginName)
.registerOption(PLUGIN_OPTION_MAP.EXTEND_CLI.key, extendCli, pluginName)
.registerOption(PLUGIN_OPTION_MAP.BEFORE_DEV_SERVER.key, beforeDevServer, pluginName)
.registerOption(PLUGIN_OPTION_MAP.AFTER_DEV_SERVER.key, afterDevServer, pluginName)
}
}
function pluginLog (name, shortcut) {
return shortcut
? `Apply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${name}")`)} ...`
: `Apply plugin ${chalk.magenta(name)} ...`
}