1 Star 0 Fork 0

animalcoder/Vue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Plugin.js 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
animalcoder 提交于 2020-03-23 09:59 +08:00 . Vue6
const ChainedMap = require('./ChainedMap');
const Orderable = require('./Orderable');
module.exports = Orderable(
class extends ChainedMap {
constructor(parent, name, type = 'plugin') {
super(parent);
this.name = name;
this.type = type;
this.extend(['init']);
this.init((Plugin, args = []) => {
if (typeof Plugin === 'function') {
return new Plugin(...args);
}
return Plugin;
});
}
use(plugin, args = []) {
return this.set('plugin', plugin).set('args', args);
}
tap(f) {
this.set('args', f(this.get('args') || []));
return this;
}
set(key, value) {
if (key === 'args' && !Array.isArray(value)) {
throw new Error('args must be an array of arguments');
}
return super.set(key, value);
}
merge(obj, omit = []) {
if ('plugin' in obj) {
this.set('plugin', obj.plugin);
}
if ('args' in obj) {
this.set('args', obj.args);
}
return super.merge(obj, [...omit, 'args', 'plugin']);
}
toConfig() {
const init = this.get('init');
let plugin = this.get('plugin');
const args = this.get('args');
let pluginPath = null;
// Support using the path to a plugin rather than the plugin itself,
// allowing expensive require()s to be skipped in cases where the plugin
// or webpack configuration won't end up being used.
if (typeof plugin === 'string') {
pluginPath = plugin;
// eslint-disable-next-line global-require, import/no-dynamic-require
plugin = require(pluginPath);
}
const constructorName = plugin.__expression
? `(${plugin.__expression})`
: plugin.name;
const config = init(plugin, args);
Object.defineProperties(config, {
__pluginName: { value: this.name },
__pluginType: { value: this.type },
__pluginArgs: { value: args },
__pluginConstructorName: { value: constructorName },
__pluginPath: { value: pluginPath },
});
return config;
}
},
);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/animalcoder/Vue.git
git@gitee.com:animalcoder/Vue.git
animalcoder
Vue
Vue
master

搜索帮助