1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const util = require("util");
|
8
|
const SyncBailHook = require("./SyncBailHook");
|
9
|
|
10
|
function Tapable() {
|
11
|
this._pluginCompat = new SyncBailHook(["options"]);
|
12
|
this._pluginCompat.tap(
|
13
|
{
|
14
|
name: "Tapable camelCase",
|
15
|
stage: 100
|
16
|
},
|
17
|
options => {
|
18
|
options.names.add(
|
19
|
options.name.replace(/[- ]([a-z])/g, (str, ch) => ch.toUpperCase())
|
20
|
);
|
21
|
}
|
22
|
);
|
23
|
this._pluginCompat.tap(
|
24
|
{
|
25
|
name: "Tapable this.hooks",
|
26
|
stage: 200
|
27
|
},
|
28
|
options => {
|
29
|
let hook;
|
30
|
for (const name of options.names) {
|
31
|
hook = this.hooks[name];
|
32
|
if (hook !== undefined) {
|
33
|
break;
|
34
|
}
|
35
|
}
|
36
|
if (hook !== undefined) {
|
37
|
const tapOpt = {
|
38
|
name: options.fn.name || "unnamed compat plugin",
|
39
|
stage: options.stage || 0
|
40
|
};
|
41
|
if (options.async) hook.tapAsync(tapOpt, options.fn);
|
42
|
else hook.tap(tapOpt, options.fn);
|
43
|
return true;
|
44
|
}
|
45
|
}
|
46
|
);
|
47
|
}
|
48
|
module.exports = Tapable;
|
49
|
|
50
|
Tapable.addCompatLayer = function addCompatLayer(instance) {
|
51
|
Tapable.call(instance);
|
52
|
instance.plugin = Tapable.prototype.plugin;
|
53
|
instance.apply = Tapable.prototype.apply;
|
54
|
};
|
55
|
|
56
|
Tapable.prototype.plugin = util.deprecate(function plugin(name, fn) {
|
57
|
if (Array.isArray(name)) {
|
58
|
name.forEach(function(name) {
|
59
|
this.plugin(name, fn);
|
60
|
}, this);
|
61
|
return;
|
62
|
}
|
63
|
const result = this._pluginCompat.call({
|
64
|
name: name,
|
65
|
fn: fn,
|
66
|
names: new Set([name])
|
67
|
});
|
68
|
if (!result) {
|
69
|
throw new Error(
|
70
|
`Plugin could not be registered at '${name}'. Hook was not found.\n` +
|
71
|
"BREAKING CHANGE: There need to exist a hook at 'this.hooks'. " +
|
72
|
"To create a compatibility layer for this hook, hook into 'this._pluginCompat'."
|
73
|
);
|
74
|
}
|
75
|
}, "Tapable.plugin is deprecated. Use new API on `.hooks` instead");
|
76
|
|
77
|
Tapable.prototype.apply = util.deprecate(function apply() {
|
78
|
for (var i = 0; i < arguments.length; i++) {
|
79
|
arguments[i].apply(this);
|
80
|
}
|
81
|
}, "Tapable.apply is deprecated. Call apply on the plugin directly instead");
|