Skip to content

Commit 885e9ee

Browse files
authored
Merge pull request webpack#5985 from EugeneHlushko/respect-no-deprecation-flag
Improvement: Ensure respect for --no-deprecation flag when adding non…
2 parents 20759bb + 6a1f424 commit 885e9ee

2 files changed

Lines changed: 30 additions & 26 deletions

File tree

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) {
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) {
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 = {};

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();

0 commit comments

Comments
 (0)