-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathutil.js
59 lines (51 loc) · 1.39 KB
/
util.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
'use strict'
/**
* Module dependencies.
*/
const { logger, chalk, datatypes: { assertTypes }} = require('@vuepress/shared-utils')
/**
* flatten your plugin config by passing in name, options and context.
*
* @param {function|object} module
* @param {string} name
* @param {string} hortcut
* @param {object} pluginOptions
* @param {object} pluginContext
*/
exports.flattenPlugin = function (
{ entry: config, name, shortcut, fromDep },
pluginOptions,
pluginContext,
self
) {
const { valid, warnMsg } = assertTypes(pluginOptions, [Object, Array, Boolean])
if (!valid) {
if (pluginOptions !== undefined) {
logger.warn(
`[${chalk.gray(shortcut)}] `
+ `Invalid value for "pluginOptions" ${chalk.cyan(name)}: ${warnMsg}`
)
}
pluginOptions = {}
}
let enabled = true
if (typeof pluginOptions === 'boolean') {
enabled = pluginOptions
pluginOptions = {}
}
if (typeof config === 'function') {
// 'Object.create' here is to give each plugin a separate context,
// but also own the inheritance context.
config = config(pluginOptions, Object.create(pluginContext), self)
}
// respect name in local plugin config
if (!fromDep && config.name) {
name = config.name
}
return Object.assign({}, config, {
name,
shortcut: fromDep ? shortcut : null,
enabled,
$$options: pluginOptions /* used for test */
})
}