Skip to content

Commit f7988d0

Browse files
Fix up a few minor plugin inaccuracies
1 parent f4e866a commit f7988d0

3 files changed

Lines changed: 63 additions & 21 deletions

File tree

WrappedPlugin/index.js

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@ let idInc = 0;
22

33
const genPluginMethod = (orig, pluginName, smp, type) =>
44
function(method, func) {
5-
const id = idInc++;
6-
const timeEventName = type + "/" + method;
7-
85
const wrappedFunc = (...args) => {
6+
const id = idInc++;
7+
const timeEventName = pluginName + "/" + type + "/" + method;
8+
// we don't know if there's going to be a callback applied to a particular
9+
// call, so we just set it multiple times, letting each one override the last
10+
const addEndEvent = () =>
11+
smp.addTimeEvent("plugins", timeEventName, "end", { id });
12+
913
smp.addTimeEvent("plugins", timeEventName, "start", {
1014
id,
1115
name: pluginName,
1216
});
13-
const ret = func.apply(this, args.map(a => wrap(a, pluginName, smp)));
14-
smp.addTimeEvent("plugins", timeEventName, "end", { id });
17+
const ret = func.apply(
18+
this,
19+
args.map(a => wrap(a, pluginName, smp, addEndEvent))
20+
);
21+
addEndEvent();
22+
1523
return ret;
1624
};
1725

@@ -27,17 +35,42 @@ const construcNamesToWrap = [
2735
"ContextModuleFactory",
2836
];
2937

30-
const wrap = (orig, pluginName, smp) => {
31-
const origConstrucName = orig && orig.constructor && orig.constructor.name;
32-
const shouldWrap = construcNamesToWrap.includes(origConstrucName);
33-
if (!shouldWrap) return orig;
38+
const wrap = (orig, pluginName, smp, addEndEvent) => {
39+
if (!orig) return orig;
40+
41+
const getOrigConstrucName = target =>
42+
target && target.constructor && target.constructor.name;
43+
const getShouldWrap = target => {
44+
const origConstrucName = getOrigConstrucName(target);
45+
return construcNamesToWrap.includes(origConstrucName);
46+
};
47+
const shouldWrap = getShouldWrap(orig);
48+
const shouldSoftWrap = Object.values(orig).some(getShouldWrap);
49+
50+
if (!shouldWrap && !shouldSoftWrap) {
51+
const vanillaFunc =
52+
typeof orig === "function" &&
53+
orig &&
54+
orig.prototype &&
55+
orig.prototype.constructor !== orig;
56+
return vanillaFunc
57+
? function() {
58+
const ret = orig();
59+
addEndEvent();
60+
return ret;
61+
}
62+
: orig;
63+
}
3464

3565
const proxy = new Proxy(orig, {
3666
get: (target, property) => {
37-
if (property === "plugin")
38-
return genPluginMethod(orig, pluginName, smp, origConstrucName).bind(
39-
proxy
40-
);
67+
if (shouldWrap && property === "plugin")
68+
return genPluginMethod(
69+
orig,
70+
pluginName,
71+
smp,
72+
getOrigConstrucName(target)
73+
).bind(proxy);
4174

4275
if (typeof orig[property] === "function")
4376
return orig[property].bind(proxy);
@@ -47,7 +80,7 @@ const wrap = (orig, pluginName, smp) => {
4780
return Reflect.set(target, property, value);
4881
},
4982
deleteProperty: (target, property) => {
50-
delete target[property];
83+
return Reflect.deleteProperty(target, property);
5184
},
5285
});
5386

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ module.exports = class SpeedMeasurePlugin {
7575
data.start = curTime;
7676
eventList.push(data);
7777
} else if (eventType === "end") {
78-
const matchingEvent = eventList.find(
79-
e => !e.end && (e.id === data.id || e.name === data.name)
80-
);
81-
const eventToModify = matchingEvent || eventList.find(e => !e.end);
78+
const matchingEvent = eventList.find(e => {
79+
const allowOverwrite = !e.end || !data.fillLast;
80+
const idMatch = e.id !== undefined && e.id === data.id;
81+
const nameMatch =
82+
!data.id && e.name !== undefined && e.name === data.name;
83+
return allowOverwrite && (idMatch || nameMatch);
84+
});
85+
const eventToModify =
86+
matchingEvent || (data.fillLast && eventList.find(e => !e.end));
8287
if (!eventToModify) {
8388
console.log(
8489
"Could not find a matching event to end",
@@ -100,7 +105,7 @@ module.exports = class SpeedMeasurePlugin {
100105
this.addTimeEvent("misc", "compile", "start", { watch: false });
101106
});
102107
compiler.plugin("done", () => {
103-
this.addTimeEvent("misc", "compile", "end");
108+
this.addTimeEvent("misc", "compile", "end", { fillLast: true });
104109

105110
const output = this.getOutput();
106111
if (typeof this.options.outputTarget === "string") {
@@ -126,6 +131,7 @@ module.exports = class SpeedMeasurePlugin {
126131
if (name) {
127132
this.addTimeEvent("loaders", "build", "start", {
128133
name,
134+
fillLast: true,
129135
loaders: getLoaderNames(module.loaders),
130136
});
131137
}
@@ -134,7 +140,10 @@ module.exports = class SpeedMeasurePlugin {
134140
compilation.plugin("succeed-module", module => {
135141
const name = getModuleName(module);
136142
if (name) {
137-
this.addTimeEvent("loaders", "build", "end", { name });
143+
this.addTimeEvent("loaders", "build", "end", {
144+
name,
145+
fillLast: true,
146+
});
138147
}
139148
});
140149
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "speed-measure-webpack-plugin",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "Measure + analyse the speed of your webpack loaders and plugins",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)