Skip to content

Commit 7f80942

Browse files
Fix wrapping around plugin callbacks
1 parent b87c24e commit 7f80942

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

WrappedPlugin/index.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
77
const timeEventName = pluginName + "/" + type + "/" + method;
88
// we don't know if there's going to be a callback applied to a particular
99
// call, so we just set it multiple times, letting each one override the last
10-
const addEndEvent = () =>
10+
let endCallCount = 0;
11+
const addEndEvent = () => {
12+
endCallCount++;
1113
smp.addTimeEvent("plugins", timeEventName, "end", { id });
14+
};
1215

1316
smp.addTimeEvent("plugins", timeEventName, "start", {
1417
id,
@@ -18,7 +21,11 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
1821
this,
1922
args.map(a => wrap(a, pluginName, smp, addEndEvent))
2023
);
21-
addEndEvent();
24+
25+
// If the end event was invoked as a callback immediately, we can
26+
// don't want to add another end event here (and it can actually cause
27+
// errors, if webpack has finished compilation entirely)
28+
if (!endCallCount) addEndEvent();
2229

2330
return ret;
2431
};
@@ -45,19 +52,19 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
4552
return construcNamesToWrap.includes(origConstrucName);
4653
};
4754
const shouldWrap = getShouldWrap(orig);
48-
const shouldSoftWrap = Object.values(orig).some(getShouldWrap);
55+
const shouldSoftWrap = Object.keys(orig)
56+
.map(k => orig[k])
57+
.some(getShouldWrap);
4958

5059
if (!shouldWrap && !shouldSoftWrap) {
51-
const vanillaFunc =
52-
typeof orig === "function" &&
53-
orig &&
54-
orig.prototype &&
55-
orig.prototype.constructor !== orig;
60+
const vanillaFunc = orig.name === "next";
5661
return vanillaFunc
5762
? function() {
58-
const ret = orig();
63+
// do this before calling the callback, since the callback can start
64+
// the next plugin step
5965
addEndEvent();
60-
return ret;
66+
67+
return orig.apply(this, arguments);
6168
}
6269
: orig;
6370
}

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.2",
3+
"version": "0.3.3",
44
"description": "Measure + analyse the speed of your webpack loaders and plugins",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)