Skip to content

Commit f14c301

Browse files
committed
Improvement: Ensure util.depracation use for deprecation warnings
1 parent 61d91f5 commit f14c301

File tree

4 files changed

+56
-53
lines changed

4 files changed

+56
-53
lines changed

lib/Compiler.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const path = require("path");
88
const Tapable = require("tapable");
9+
const util = require("util");
910

1011
const Compilation = require("./Compilation");
1112
const Stats = require("./Stats");
@@ -186,35 +187,30 @@ class Compiler extends Tapable {
186187
loader: null,
187188
context: null
188189
};
189-
let deprecationReported = false;
190190
this.parser = {
191-
plugin: (hook, fn) => {
192-
if(!deprecationReported && process.noDeprecation !== true) {
193-
console.warn("webpack: Using compiler.parser is deprecated.\n" +
194-
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. " +
195-
"It was called " + new Error().stack.split("\n")[2].trim() + ".");
196-
deprecationReported = true;
197-
}
198-
this.plugin("compilation", (compilation, data) => {
199-
data.normalModuleFactory.plugin("parser", parser => {
200-
parser.plugin(hook, fn);
191+
plugin: util.deprecate(
192+
(hook, fn) => {
193+
this.plugin("compilation", (compilation, data) => {
194+
data.normalModuleFactory.plugin("parser", parser => {
195+
parser.plugin(hook, fn);
196+
});
201197
});
202-
});
203-
},
204-
apply: () => {
205-
const args = arguments;
206-
if(!deprecationReported && process.noDeprecation !== true) {
207-
console.warn("webpack: Using compiler.parser is deprecated.\n" +
208-
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. " +
209-
"It was called " + new Error().stack.split("\n")[2].trim() + ".");
210-
deprecationReported = true;
211-
}
212-
this.plugin("compilation", (compilation, data) => {
213-
data.normalModuleFactory.plugin("parser", parser => {
214-
parser.apply.apply(parser, args);
198+
},
199+
"webpack: Using compiler.parser is deprecated.\n" +
200+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. "
201+
),
202+
apply: util.deprecate(
203+
() => {
204+
const args = arguments;
205+
this.plugin("compilation", (compilation, data) => {
206+
data.normalModuleFactory.plugin("parser", parser => {
207+
parser.apply(parser, args);
208+
});
215209
});
216-
});
217-
}
210+
},
211+
"webpack: Using compiler.parser is deprecated.\n" +
212+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. "
213+
)
218214
};
219215

220216
this.options = {};

lib/NoErrorsPlugin.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
*/
55
"use strict";
66

7-
let deprecationReported = false;
7+
const util = require("util");
88

99
class NoErrorsPlugin {
1010
apply(compiler) {
11-
compiler.plugin("should-emit", (compilation) => {
12-
if(!deprecationReported && process.noDeprecation !== true) {
13-
compilation.warnings.push("webpack: Using NoErrorsPlugin is deprecated.\n" +
14-
"Use NoEmitOnErrorsPlugin instead.\n");
15-
deprecationReported = true;
16-
}
17-
if(compilation.errors.length > 0)
18-
return false;
19-
});
11+
compiler.plugin("should-emit", util.deprecate(
12+
(compilation) => {
13+
if(compilation.errors.length > 0)
14+
return false;
15+
},
16+
"webpack: Using NoErrorsPlugin is deprecated.\n" +
17+
"Use NoEmitOnErrorsPlugin instead.\n"
18+
));
2019
compiler.plugin("compilation", (compilation) => {
2120
compilation.plugin("should-record", () => {
2221
if(compilation.errors.length > 0)

test/Compiler.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ describe("Compiler", () => {
178178
});
179179
});
180180
describe("parser", () => {
181+
describe("plugin", () => {
182+
it("invokes sets a 'compilation' plugin", (done) => {
183+
compiler.plugin = sinon.spy();
184+
compiler.parser.plugin();
185+
compiler.plugin.callCount.should.be.exactly(1);
186+
done();
187+
});
188+
});
181189
describe("apply", () => {
182190
it("invokes sets a 'compilation' plugin", (done) => {
183191
compiler.plugin = sinon.spy();

test/Errors.test.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,23 @@ describe("Errors", () => {
9494
done();
9595
});
9696
});
97-
it("should warn about NoErrorsPlugin being deprecated in favor of NoEmitOnErrorsPlugin", (done) => {
98-
getErrors({
99-
entry: "./no-errors-deprecate",
100-
plugins: [
101-
new webpack.NoErrorsPlugin()
102-
]
103-
}, (errors, warnings) => {
104-
warnings.length.should.be.eql(1);
105-
const lines = warnings[0].split("\n");
106-
lines[0].should.match(/webpack/);
107-
lines[0].should.match(/NoErrorsPlugin/);
108-
lines[0].should.match(/deprecated/);
109-
lines[1].should.match(/NoEmitOnErrorsPlugin/);
110-
lines[1].should.match(/instead/);
111-
done();
112-
});
113-
});
97+
// it("should warn about NoErrorsPlugin being deprecated in favor of NoEmitOnErrorsPlugin", (done) => {
98+
// getErrors({
99+
// entry: "./no-errors-deprecate",
100+
// plugins: [
101+
// new webpack.NoErrorsPlugin()
102+
// ]
103+
// }, (errors, warnings) => {
104+
// warnings.length.should.be.eql(1);
105+
// const lines = warnings[0].split("\n");
106+
// lines[0].should.match(/webpack/);
107+
// lines[0].should.match(/NoErrorsPlugin/);
108+
// lines[0].should.match(/deprecated/);
109+
// lines[1].should.match(/NoEmitOnErrorsPlugin/);
110+
// lines[1].should.match(/instead/);
111+
// done();
112+
// });
113+
// });
114114
it("should not warn if the NoEmitOnErrorsPlugin is used over the NoErrorsPlugin", (done) => {
115115
getErrors({
116116
entry: "./no-errors-deprecate",

0 commit comments

Comments
 (0)