Skip to content

Commit

Permalink
Fix up SMP to support webpack 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencookdev committed Feb 19, 2018
1 parent 5fba3cc commit ce354bd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Options are (optionally) passed in to the constructor
const smp = new SpeedMeasurePlugin(options);
```

### `options.disable`

Type: `Boolean`<br>
Default: `false`

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`

### `options.outputFormat`

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

### `options.disable`
### `options.pluginNames`

Type: `Boolean`<br>
Default: `false`
Type: `Object`<br>
Default: `{}`

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`
By default, SMP derives plugin names through `plugin.constructor.name`. For some
plugins this doesn't work (or you may want to override this default). This option
takes an object of `pluginName: PluginConstructor`, e.g.

```javascript
const uglify = new UglifyJSPlugin();
const smp = new SpeedMeasurePlugin({
pluginNames: {
customUglifyName: uglify
}
});

const webpackConfig = smp.wrap({
plugins: [
uglify
]
});
```

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

Expand Down
22 changes: 12 additions & 10 deletions WrappedPlugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
const id = idInc++;
// we don't know if there's going to be a callback applied to a particular
// call, so we just set it multiple times, letting each one override the last
let endCallCount = 0;
const addEndEvent = () => {
endCallCount++;
smp.addTimeEvent("plugins", timeEventName, "end", { id });
};
const addEndEvent = () =>
smp.addTimeEvent("plugins", timeEventName, "end", {
id,
// we need to allow failure, since webpack can finish compilation and
// cause our callbacks to fall on deaf ears
allowFailure: true,
});

smp.addTimeEvent("plugins", timeEventName, "start", {
id,
name: pluginName,
});
// invoke an end event immediately in case the callback here causes webpack
// to complete compilation. If this gets invoked and not the subsequent
// call, then our data will be inaccurate, sadly
addEndEvent();
const ret = func.apply(
this,
args.map(a => wrap(a, pluginName, smp, addEndEvent))
);

// If the end event was invoked as a callback immediately, we can
// don't want to add another end event here (and it can actually cause
// errors, if webpack has finished compilation entirely)
if (!endCallCount) addEndEvent();
addEndEvent();

return ret;
};
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = class SpeedMeasurePlugin {

config.plugins = (config.plugins || []).map(plugin => {
const pluginName =
Object.keys(this.options.pluginNames || {}).find(
pluginName => plugin === this.options.pluginNames[pluginName]
) ||
(plugin.constructor && plugin.constructor.name) ||
"(unable to deduce plugin name)";
return new WrappedPlugin(plugin, pluginName, this);
Expand Down Expand Up @@ -67,6 +70,9 @@ module.exports = class SpeedMeasurePlugin {
}

addTimeEvent(category, event, eventType, data = {}) {
const allowFailure = data.allowFailure;
delete data.allowFailure;

const tED = this.timeEventData;
if (!tED[category]) tED[category] = {};
if (!tED[category][event]) tED[category][event] = [];
Expand All @@ -93,6 +99,7 @@ module.exports = class SpeedMeasurePlugin {
event,
data
);
if (allowFailure) return;
throw new Error("No matching event!");
}

Expand Down Expand Up @@ -157,7 +164,7 @@ module.exports = class SpeedMeasurePlugin {

provideLoaderTiming(info) {
const infoData = { id: info.id };
if(info.type !== "end") {
if (info.type !== "end") {
infoData.loader = info.loaderName;
infoData.name = info.module;
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"node": ">=6.0.0"
},
"peerDependencies": {
"loader-runner": "^2",
"webpack": "^3"
"webpack": "^1"
},
"devDependencies": {
"jest": "^22.3.0",
Expand Down
2 changes: 1 addition & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports.getModuleName = module => module.userRequest;
module.exports.getLoaderNames = loaders =>
loaders && loaders.length
? loaders
.map(l => l.loader)
.map(l => l.loader || l)
.map(l => l.replace(/^.*\/node_modules\/([^\/]+).*$/, (_, m) => m))
.filter(l => !l.includes("speed-measure-webpack-plugin"))
: ["modules with no loaders"];
Expand Down

0 comments on commit ce354bd

Please sign in to comment.