UNPKG

1.2 kBJavaScriptView Raw
1'use strict'
2
3const logger = require('./logger')
4const isObject = require('./is').Object
5const isArray = require('./is').Array
6const exec = require('./exec')
7const pluginExists = require('./check').pluginExists
8
9/* istanbul ignore next */
10const importExtend = (extend, xdc, options) => {
11 require(`xdc-${extend}`)(xdc, options)
12 logger.success(`插件加载成功: ${extend}`)
13}
14
15/* istanbul ignore next */
16const installExtend = name => {
17 logger.warn(`插件不存在,自动下载插件: ${name}`)
18 exec('xdc', ['import', name], {
19 stdio: 'inherit'
20 })
21}
22
23/* istanbul ignore next */
24/**
25 * 加载并装配插件
26 * @param {array} extends
27 * @param {object} config - webpack config
28 */
29module.exports = (_extends, xdc) => {
30 const isObj = isObject(_extends)
31
32 Object.keys(_extends || {}).reverse().forEach(key => {
33 let extend = isObj ? key : _extends[key]
34 let options = isObj ? _extends[key] : {}
35
36 if (isArray(extend)) {
37 options = extend[1]
38 extend = extend[0]
39 }
40
41 const extendName = extend.split('@')[0]
42
43 if (!pluginExists(`xdc-${extendName}`)) {
44 installExtend(extend)
45 }
46
47 importExtend(extendName, xdc, options)
48 })
49 console.log()
50}