Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add constructor to prototype #15

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*.js]
charset = utf-8
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
History
=======

## 0.1.4
* Add constructor definition. new plugin().constructor.name === 'StatsWriterPlugin';

## 0.1.3

* Add `opts.compiler` to transform function.
Expand Down
65 changes: 35 additions & 30 deletions lib/stats-writer-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,50 @@
*
* @api public
*/
var StatsWriterPlugin = function (opts) {
function StatsWriterPlugin(opts) {
opts = opts || {};
this.opts = {};
this.opts.filename = opts.filename || "stats.json";
this.opts.fields = typeof opts.fields !== "undefined" ? opts.fields : ["assetsByChunkName"];
this.opts.transform = opts.transform || function (data) { return JSON.stringify(data, null, 2); };
};

StatsWriterPlugin.prototype.apply = function (compiler) {
var self = this;
compiler.plugin("emit", function (curCompiler, callback) {
// Get stats.
// **Note**: In future, could pass something like `{ showAssets: true }`
// to the `getStats()` function for more limited object returned.
var stats = curCompiler.getStats().toJson();
}

// Filter to fields.
if (self.opts.fields) {
stats = self.opts.fields.reduce(function (memo, key) {
memo[key] = stats[key];
return memo;
}, {});
}
StatsWriterPlugin.prototype = {
constructor: StatsWriterPlugin,

// Transform to string.
var statsStr = self.opts.transform(stats, {
compiler: curCompiler
});
apply: function (compiler) {
var self = this;
compiler.plugin("emit", function (curCompiler, callback) {
// Get stats.
// **Note**: In future, could pass something like `{ showAssets: true }`
// to the `getStats()` function for more limited object returned.
var stats = curCompiler.getStats().toJson();

curCompiler.assets[self.opts.filename] = {
source: function () {
return statsStr;
},
size: function () {
return statsStr.length;
// Filter to fields.
if (self.opts.fields) {
stats = self.opts.fields.reduce(function (memo, key) {
memo[key] = stats[key];
return memo;
}, {});
}
};
callback();
});

// Transform to string.
var statsStr = self.opts.transform(stats, {
compiler: curCompiler
});

curCompiler.assets[self.opts.filename] = {
source: function () {
return statsStr;
},
size: function () {
return statsStr.length;
}
};

callback();
});
}
};

module.exports = StatsWriterPlugin;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-stats-plugin",
"version": "0.1.3",
"version": "0.1.4",
"description": "Webpack stats plugin",
"main": "webpack-stats-plugin.js",
"dependencies": {},
Expand Down