forked from webpack/tapable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTapable.js
73 lines (68 loc) · 1.91 KB
/
Tapable.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const util = require("util");
const SyncBailHook = require("./SyncBailHook");
function Tapable() {
this._pluginCompat = new SyncBailHook(["options"]);
this._pluginCompat.tap({
name: "Tapable camelCase",
stage: 100
}, options => {
options.names.add(options.name.replace(/[- ]([a-z])/g, str => str.substr(1).toUpperCase()));
});
this._pluginCompat.tap({
name: "Tapable this.hooks",
stage: 200
}, options => {
let hook;
for(const name of options.names) {
hook = this.hooks[name];
if(hook !== undefined) {
break;
}
}
if(hook !== undefined) {
const tapOpt = {
name: options.fn.name || "unnamed compat plugin",
stage: options.stage || 0
};
if(options.async)
hook.tapAsync(tapOpt, options.fn);
else
hook.tap(tapOpt, options.fn);
return true;
}
});
}
module.exports = Tapable;
Tapable.addCompatLayer = function addCompatLayer(instance) {
Tapable.call(instance);
instance.plugin = Tapable.prototype.plugin;
instance.apply = Tapable.prototype.apply;
};
Tapable.prototype.plugin = util.deprecate(function plugin(name, fn) {
if(Array.isArray(name)) {
name.forEach(function(name) {
this.plugin(name, fn);
}, this);
return;
}
const result = this._pluginCompat.call({
name: name,
fn: fn,
names: new Set([name])
});
if(!result) {
throw new Error(`Plugin could not be registered at '${name}'. Hook was not found.\n` +
"BREAKING CHANGE: There need to exist a hook at 'this.hooks'. " +
"To create a compatiblity layer for this hook, hook into 'this._pluginCompat'.");
}
}, "Tapable.plugin is deprecated. Use new API on `.hooks` instead");
Tapable.prototype.apply = util.deprecate(function apply() {
for(var i = 0; i < arguments.length; i++) {
arguments[i].apply(this);
}
}, "Tapable.apply is deprecated. Call apply on the plugin directly instead");