forked from smooth80/webpack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPluginEnvironment.js
55 lines (51 loc) · 1.25 KB
/
PluginEnvironment.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
module.exports = function PluginEnvironment() {
const events = [];
function addEvent(name, handler) {
events.push({
name,
handler
});
}
function getEventName(hookName) {
// Convert a hook name to an event name.
// e.g. `buildModule` -> `build-module`
return hookName.replace(/[A-Z]/g, c => "-" + c.toLowerCase());
}
this.getEnvironmentStub = function () {
const hooks = new Map();
return {
plugin: addEvent,
// TODO: Figure out a better way of doing this
// In the meanwhile, `hooks` is a `Proxy` which creates fake hooks
// on demand. Instead of creating a dummy object with a few `Hook`
// method, a custom `Hook` class could be used.
hooks: new Proxy(
{},
{
get(target, hookName) {
let hook = hooks.get(hookName);
if (hook === undefined) {
const eventName = getEventName(hookName);
hook = {
tap(_, handler) {
addEvent(eventName, handler);
},
tapAsync(_, handler) {
addEvent(eventName, handler);
},
tapPromise(_, handler) {
addEvent(eventName, handler);
}
};
hooks.set(hookName, hook);
}
return hook;
}
}
)
};
};
this.getEventBindings = function () {
return events;
};
};