Skip to content

Commit 710219d

Browse files
author
Stephen Cook
committed
Simplify wrapping api
1 parent ad3232a commit 710219d

3 files changed

Lines changed: 22 additions & 58 deletions

File tree

README.md

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,54 +43,24 @@ to
4343
```javascript
4444
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
4545

46-
const webpackConfig = {
47-
plugins: SpeedMeasurePlugin.wrapPlugins({
48-
MyPlugin: new MyPlugin(),
49-
MyOtherPlugin: new MyOtherPlugin()
50-
})
51-
}
52-
```
53-
54-
If you're using `webpack-merge`, then you can do:
55-
56-
```javascript
5746
const smp = new SpeedMeasurePlugin();
5847

59-
const baseConfig = {
60-
plugins: smp.wrapPlugins({
61-
MyPlugin: new MyPlugin()
62-
}).concat(smp)
63-
// ^ note the `.concat(smp)`
64-
};
65-
66-
const envSpecificConfig = {
67-
plugins: smp.wrapPlugins({
68-
MyOtherPlugin: new MyOtherPlugin()
69-
})
70-
// ^ note no `.concat(smp)`
71-
}
72-
73-
const finalWebpackConfig = webpackMerge([
74-
baseConfig,
75-
envSpecificConfig
76-
]);
77-
48+
const webpackConfig = smp.wrap({
49+
plugins: [
50+
new MyPlugin(),
51+
new MyOtherPlugin()
52+
]
53+
});
7854
```
7955

8056
## Options
8157

82-
Options are passed in to the constructor
58+
Options are (optionally) passed in to the constructor
8359

8460
```javascript
8561
const smp = new SpeedMeasurePlugin(options);
8662
```
8763

88-
or as the second argument to the static `wrapPlugins`
89-
90-
```javascript
91-
SpeedMeasurePlugin.wrapPlugins(pluginMap, options);
92-
```
93-
9464
### `options.outputFormat`
9565

9666
Type: `String|Function`<br>

index.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,29 @@ module.exports = class SpeedMeasurePlugin {
1414
this.options = options || {};
1515

1616
this.timeEventData = {};
17+
this.smpPluginAdded = false;
1718

18-
this.wrapPlugins = this.wrapPlugins.bind(this);
19+
this.wrap = this.wrap.bind(this);
1920
this.getOutput = this.getOutput.bind(this);
2021
this.addTimeEvent = this.addTimeEvent.bind(this);
2122
this.apply = this.apply.bind(this);
2223
}
2324

24-
static wrapPlugins(plugins, options) {
25-
if (options.disable) return Object.keys(plugins).map(k => plugins[k]);
25+
wrap(config) {
26+
if (this.options.disable) return config;
2627

27-
const smp = new SpeedMeasurePlugin(options);
28-
29-
const wrappedPlugins = smp.wrapPlugins(plugins);
30-
31-
return wrappedPlugins.concat(smp);
32-
}
28+
config.plugins = (config.plugins || []).map(plugin => {
29+
const pluginName = (plugin.constructor && plugin.constructor.name) ||
30+
"(unable to deduce plugin name)";
31+
return new WrappedPlugin(plugin, pluginName, this);
32+
});
3333

34-
wrapPlugins(plugins) {
35-
if (Array.isArray(plugins)) {
36-
let i = 1;
37-
plugins = plugins.reduce((acc, p) => {
38-
acc["plugin " + i++] = p;
39-
return acc;
40-
});
34+
if(!this.smpPluginAdded) {
35+
config.plugins = config.plugins.concat(this);
36+
this.smpPluginAdded = true;
4137
}
42-
plugins = plugins || {};
4338

44-
return Object.keys(plugins).map(
45-
pluginName => new WrappedPlugin(plugins[pluginName], pluginName, this)
46-
);
39+
return config;
4740
}
4841

4942
getOutput() {

package.json

Lines changed: 2 additions & 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.0",
3+
"version": "1.0.0",
44
"description": "Measure + analyse the speed of your webpack loaders and plugins",
55
"main": "index.js",
66
"repository": {
@@ -20,6 +20,7 @@
2020
"node": ">=6.0.0"
2121
},
2222
"peerDependencies": {
23+
"loader-runner": "^2",
2324
"webpack": "^3"
2425
},
2526
"devDependencies": {

0 commit comments

Comments
 (0)