Skip to content

Commit ce354bd

Browse files
Fix up SMP to support webpack 1 and 2
1 parent 5fba3cc commit ce354bd

5 files changed

Lines changed: 50 additions & 18 deletions

File tree

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ Options are (optionally) passed in to the constructor
6363
const smp = new SpeedMeasurePlugin(options);
6464
```
6565

66+
### `options.disable`
67+
68+
Type: `Boolean`<br>
69+
Default: `false`
70+
71+
If truthy, this plugin does nothing at all. It is recommended to set this with something similar to `{ disable: !process.env.MEASURE }` to allow opt-in measurements with a `MEASURE=true npm run build`
72+
6673
### `options.outputFormat`
6774

6875
Type: `String|Function`<br>
@@ -83,12 +90,29 @@ Default: `console.log`
8390
* If a string, it specifies the path to a file to output to.
8491
* If a function, it will call the function with the output as the first parameter
8592

86-
### `options.disable`
93+
### `options.pluginNames`
8794

88-
Type: `Boolean`<br>
89-
Default: `false`
95+
Type: `Object`<br>
96+
Default: `{}`
9097

91-
If truthy, this plugin does nothing at all. It is recommended to set this with something similar to `{ disable: !process.env.MEASURE }` to allow opt-in measurements with a `MEASURE=true npm run build`
98+
By default, SMP derives plugin names through `plugin.constructor.name`. For some
99+
plugins this doesn't work (or you may want to override this default). This option
100+
takes an object of `pluginName: PluginConstructor`, e.g.
101+
102+
```javascript
103+
const uglify = new UglifyJSPlugin();
104+
const smp = new SpeedMeasurePlugin({
105+
pluginNames: {
106+
customUglifyName: uglify
107+
}
108+
});
109+
110+
const webpackConfig = smp.wrap({
111+
plugins: [
112+
uglify
113+
]
114+
});
115+
```
92116

93117
### `options.granularLoaderData` _(experimental)_
94118

WrappedPlugin/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
77
const id = idInc++;
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-
let endCallCount = 0;
11-
const addEndEvent = () => {
12-
endCallCount++;
13-
smp.addTimeEvent("plugins", timeEventName, "end", { id });
14-
};
10+
const addEndEvent = () =>
11+
smp.addTimeEvent("plugins", timeEventName, "end", {
12+
id,
13+
// we need to allow failure, since webpack can finish compilation and
14+
// cause our callbacks to fall on deaf ears
15+
allowFailure: true,
16+
});
1517

1618
smp.addTimeEvent("plugins", timeEventName, "start", {
1719
id,
1820
name: pluginName,
1921
});
22+
// invoke an end event immediately in case the callback here causes webpack
23+
// to complete compilation. If this gets invoked and not the subsequent
24+
// call, then our data will be inaccurate, sadly
25+
addEndEvent();
2026
const ret = func.apply(
2127
this,
2228
args.map(a => wrap(a, pluginName, smp, addEndEvent))
2329
);
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();
30+
addEndEvent();
2931

3032
return ret;
3133
};

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module.exports = class SpeedMeasurePlugin {
3131

3232
config.plugins = (config.plugins || []).map(plugin => {
3333
const pluginName =
34+
Object.keys(this.options.pluginNames || {}).find(
35+
pluginName => plugin === this.options.pluginNames[pluginName]
36+
) ||
3437
(plugin.constructor && plugin.constructor.name) ||
3538
"(unable to deduce plugin name)";
3639
return new WrappedPlugin(plugin, pluginName, this);
@@ -67,6 +70,9 @@ module.exports = class SpeedMeasurePlugin {
6770
}
6871

6972
addTimeEvent(category, event, eventType, data = {}) {
73+
const allowFailure = data.allowFailure;
74+
delete data.allowFailure;
75+
7076
const tED = this.timeEventData;
7177
if (!tED[category]) tED[category] = {};
7278
if (!tED[category][event]) tED[category][event] = [];
@@ -93,6 +99,7 @@ module.exports = class SpeedMeasurePlugin {
9399
event,
94100
data
95101
);
102+
if (allowFailure) return;
96103
throw new Error("No matching event!");
97104
}
98105

@@ -157,7 +164,7 @@ module.exports = class SpeedMeasurePlugin {
157164

158165
provideLoaderTiming(info) {
159166
const infoData = { id: info.id };
160-
if(info.type !== "end") {
167+
if (info.type !== "end") {
161168
infoData.loader = info.loaderName;
162169
infoData.name = info.module;
163170
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"node": ">=6.0.0"
2424
},
2525
"peerDependencies": {
26-
"loader-runner": "^2",
27-
"webpack": "^3"
26+
"webpack": "^1"
2827
},
2928
"devDependencies": {
3029
"jest": "^22.3.0",

utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports.getModuleName = module => module.userRequest;
4949
module.exports.getLoaderNames = loaders =>
5050
loaders && loaders.length
5151
? loaders
52-
.map(l => l.loader)
52+
.map(l => l.loader || l)
5353
.map(l => l.replace(/^.*\/node_modules\/([^\/]+).*$/, (_, m) => m))
5454
.filter(l => !l.includes("speed-measure-webpack-plugin"))
5555
: ["modules with no loaders"];

0 commit comments

Comments
 (0)