-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from dlhandsome/feat-plugin
feat(gulp-tool): support plugins
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.PLUGIN_NAME = '@we-debug/gulp-tool'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const PluginError = require('plugin-error'); | ||
|
||
const PLUGIN_NAME = '@we-debug/gulp-tool'; | ||
|
||
class Plugin { | ||
/** 子插件生命周期 */ | ||
lifecycles = []; | ||
/** 插件元信息 */ | ||
meta = {}; | ||
|
||
getLifecycles () { | ||
return this.lifecycles; | ||
} | ||
|
||
register (options) { | ||
this.lifecycles.push(options); | ||
} | ||
|
||
execLifecycle (lifetime, file) { | ||
this.lifecycles.forEach(i => { | ||
if (typeof i[lifetime] === 'function') { | ||
i[lifetime].call(this, file); | ||
} | ||
}) | ||
} | ||
|
||
loadPlugin(pkg) { | ||
const pkgType = typeof pkg; | ||
|
||
switch(pkgType) { | ||
case 'function': | ||
// 函数直接返回 | ||
return pkg; | ||
case 'string': | ||
// 字符串识别为 npm 包 | ||
return require(pkg); | ||
} | ||
} | ||
|
||
initPlugin (packges, meta) { | ||
this.meta = meta; | ||
|
||
packges.map(p => { | ||
try { | ||
// 加载插件 | ||
let fn = this.loadPlugin(p.package); | ||
fn.call(this, this, p.options); | ||
} catch (e) { | ||
throw new PluginError(PLUGIN_NAME, `load package ${p.package} error,please run 'npm install ${p.package}', ${e}`); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = new Plugin(); |