Skip to content

Commit 1615741

Browse files
Merge branch 'master' into 1.0.0-beta
2 parents 69dd0fb + 7f80942 commit 1615741

3 files changed

Lines changed: 71 additions & 22 deletions

File tree

WrappedPlugin/index.js

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@ 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+
let endCallCount = 0;
11+
const addEndEvent = () => {
12+
endCallCount++;
13+
smp.addTimeEvent("plugins", timeEventName, "end", { id });
14+
};
15+
916
smp.addTimeEvent("plugins", timeEventName, "start", {
1017
id,
1118
name: pluginName,
1219
});
13-
const ret = func.apply(this, args.map(a => wrap(a, pluginName, smp)));
14-
smp.addTimeEvent("plugins", timeEventName, "end", { id });
20+
const ret = func.apply(
21+
this,
22+
args.map(a => wrap(a, pluginName, smp, addEndEvent))
23+
);
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();
29+
1530
return ret;
1631
};
1732

@@ -27,27 +42,52 @@ const construcNamesToWrap = [
2742
"ContextModuleFactory",
2843
];
2944

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;
45+
const wrap = (orig, pluginName, smp, addEndEvent) => {
46+
if (!orig) return orig;
47+
48+
const getOrigConstrucName = target =>
49+
target && target.constructor && target.constructor.name;
50+
const getShouldWrap = target => {
51+
const origConstrucName = getOrigConstrucName(target);
52+
return construcNamesToWrap.includes(origConstrucName);
53+
};
54+
const shouldWrap = getShouldWrap(orig);
55+
const shouldSoftWrap = Object.keys(orig)
56+
.map(k => orig[k])
57+
.some(getShouldWrap);
58+
59+
if (!shouldWrap && !shouldSoftWrap) {
60+
const vanillaFunc = orig.name === "next";
61+
return vanillaFunc
62+
? function() {
63+
// do this before calling the callback, since the callback can start
64+
// the next plugin step
65+
addEndEvent();
66+
67+
return orig.apply(this, arguments);
68+
}
69+
: orig;
70+
}
3471

3572
const proxy = new Proxy(orig, {
3673
get: (target, property) => {
37-
if (property === "plugin")
38-
return genPluginMethod(orig, pluginName, smp, origConstrucName).bind(
39-
proxy
40-
);
74+
if (shouldWrap && property === "plugin")
75+
return genPluginMethod(
76+
orig,
77+
pluginName,
78+
smp,
79+
getOrigConstrucName(target)
80+
).bind(proxy);
4181

4282
if (typeof orig[property] === "function")
4383
return orig[property].bind(proxy);
4484
return wrap(orig[property], pluginName, smp);
4585
},
4686
set: (target, property, value) => {
47-
target[property] = value;
87+
return Reflect.set(target, property, value);
4888
},
4989
deleteProperty: (target, property) => {
50-
delete target[property];
90+
return Reflect.deleteProperty(target, property);
5191
},
5292
});
5393

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ module.exports = class SpeedMeasurePlugin {
6868
data.start = curTime;
6969
eventList.push(data);
7070
} else if (eventType === "end") {
71-
const matchingEvent = eventList.find(
72-
e => !e.end && (e.id === data.id || e.name === data.name)
73-
);
74-
const eventToModify = matchingEvent || eventList.find(e => !e.end);
71+
const matchingEvent = eventList.find(e => {
72+
const allowOverwrite = !e.end || !data.fillLast;
73+
const idMatch = e.id !== undefined && e.id === data.id;
74+
const nameMatch =
75+
!data.id && e.name !== undefined && e.name === data.name;
76+
return allowOverwrite && (idMatch || nameMatch);
77+
});
78+
const eventToModify =
79+
matchingEvent || (data.fillLast && eventList.find(e => !e.end));
7580
if (!eventToModify) {
7681
console.log(
7782
"Could not find a matching event to end",
@@ -93,7 +98,7 @@ module.exports = class SpeedMeasurePlugin {
9398
this.addTimeEvent("misc", "compile", "start", { watch: false });
9499
});
95100
compiler.plugin("done", () => {
96-
this.addTimeEvent("misc", "compile", "end");
101+
this.addTimeEvent("misc", "compile", "end", { fillLast: true });
97102

98103
const output = this.getOutput();
99104
if (typeof this.options.outputTarget === "string") {
@@ -119,6 +124,7 @@ module.exports = class SpeedMeasurePlugin {
119124
if (name) {
120125
this.addTimeEvent("loaders", "build", "start", {
121126
name,
127+
fillLast: true,
122128
loaders: getLoaderNames(module.loaders),
123129
});
124130
}
@@ -127,7 +133,10 @@ module.exports = class SpeedMeasurePlugin {
127133
compilation.plugin("succeed-module", module => {
128134
const name = getModuleName(module);
129135
if (name) {
130-
this.addTimeEvent("loaders", "build", "end", { name });
136+
this.addTimeEvent("loaders", "build", "end", {
137+
name,
138+
fillLast: true,
139+
});
131140
}
132141
});
133142
});

utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const range = xs =>
4747
module.exports.getModuleName = module => module.userRequest;
4848

4949
module.exports.getLoaderNames = loaders =>
50-
loaders.length
50+
loaders && loaders.length
5151
? loaders
5252
.map(l => l.loader)
5353
.map(l => l.replace(/^.*\/node_modules\/([^\/]+).*$/, (_, m) => m))

0 commit comments

Comments
 (0)