Skip to content

Commit

Permalink
Make log & warn functions replaceable (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Pearce authored and SpaceK33z committed Sep 19, 2016
1 parent a2c4d52 commit ab17c1b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
12 changes: 7 additions & 5 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ var defaultReporter = function(reporterOptions) {
options.noInfo)
displayStats = false;
if(displayStats) {
console.log(stats.toString(options.stats));
options.log(stats.toString(options.stats));
}
if(!options.noInfo && !options.quiet) {
console.info("webpack: bundle is now VALID.");
options.log("webpack: bundle is now VALID.");
}
} else {
console.info("webpack: bundle is now INVALID.");
options.log("webpack: bundle is now INVALID.");
}
};

Expand All @@ -38,7 +38,7 @@ module.exports = function(compiler, options) {
if(typeof options.watchOptions === "undefined") options.watchOptions = {};
if(typeof options.watchDelay !== "undefined") {
// TODO remove this in next major version
console.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead");
options.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead");
options.watchOptions.aggregateTimeout = options.watchDelay;
}
if(typeof options.watchOptions.aggregateTimeout === "undefined") options.watchOptions.aggregateTimeout = 200;
Expand All @@ -53,6 +53,8 @@ module.exports = function(compiler, options) {
}
}
if(typeof options.reporter !== "function") options.reporter = defaultReporter;
if(typeof options.log !== "function") options.log = console.log.bind(console);
if(typeof options.warn !== "function") options.warn = console.warn.bind(console);

// store our files in memory
var fs;
Expand Down Expand Up @@ -130,7 +132,7 @@ module.exports = function(compiler, options) {
function ready(fn, req) {
if(state) return fn();
if(!options.noInfo && !options.quiet)
console.log("webpack: wait until bundle finished: " + (req.url || fn.name));
options.log("webpack: wait until bundle finished: " + (req.url || fn.name));
callbacks.push(fn);
}

Expand Down
21 changes: 10 additions & 11 deletions test/Reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe("Reporter", function() {
beforeEach(function() {
plugins = {};
this.sinon.stub(console, 'log');
this.sinon.stub(console, 'info');
});

describe("valid/invalid messages", function() {
Expand All @@ -39,8 +38,8 @@ describe("Reporter", function() {

plugins.done(simpleStats);
setTimeout(function() {
should.strictEqual(console.info.callCount, 1);
should.strictEqual(console.info.calledWith("webpack: bundle is now VALID."), true);
should.strictEqual(console.log.callCount, 2);
should.strictEqual(console.log.calledWith("webpack: bundle is now VALID."), true);
done();
});
});
Expand All @@ -50,7 +49,7 @@ describe("Reporter", function() {

plugins.done(simpleStats);
setTimeout(function() {
should.strictEqual(console.info.callCount, 0);
should.strictEqual(console.log.callCount, 0);
done();
});
});
Expand All @@ -60,7 +59,7 @@ describe("Reporter", function() {

plugins.done(simpleStats);
setTimeout(function() {
should.strictEqual(console.info.callCount, 0);
should.strictEqual(console.log.callCount, 0);
done();
});
});
Expand All @@ -70,8 +69,8 @@ describe("Reporter", function() {
plugins.done(simpleStats);
plugins.invalid();
setTimeout(function() {
should.strictEqual(console.info.callCount, 1);
should.strictEqual(console.info.calledWith("webpack: bundle is now INVALID."), true);
should.strictEqual(console.log.callCount, 1);
should.strictEqual(console.log.calledWith("webpack: bundle is now INVALID."), true);
done();
});
});
Expand All @@ -82,7 +81,7 @@ describe("Reporter", function() {
plugins.done(simpleStats);
plugins.invalid();
setTimeout(function() {
should.strictEqual(console.info.callCount, 0);
should.strictEqual(console.log.callCount, 0);
done();
});
});
Expand All @@ -93,7 +92,7 @@ describe("Reporter", function() {
plugins.done(simpleStats);
plugins.invalid();
setTimeout(function() {
should.strictEqual(console.info.callCount, 0);
should.strictEqual(console.log.callCount, 0);
done();
});
});
Expand All @@ -117,7 +116,7 @@ describe("Reporter", function() {

plugins.done(stats);
setTimeout(function() {
should.strictEqual(console.log.callCount, 1);
should.strictEqual(console.log.callCount, 2);
should.strictEqual(console.log.calledWith(stats.toString()), true);
done();
});
Expand All @@ -128,7 +127,7 @@ describe("Reporter", function() {

plugins.done(stats);
setTimeout(function() {
should.strictEqual(console.log.callCount, 0);
should.strictEqual(console.log.callCount, 1);
done();
});
});
Expand Down

0 comments on commit ab17c1b

Please sign in to comment.