Skip to content

Commit 1de3d48

Browse files
authored
Merge pull request webpack#6416 from kingdaro/removed-plugin-errors
feat: add helpful error messages for removed plugins
2 parents b3f3100 + 0b3eabd commit 1de3d48

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎lib/RemovedPluginError.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
const WebpackError = require("./WebpackError");
4+
5+
module.exports = class RemovedPluginError extends WebpackError {
6+
constructor(message) {
7+
super();
8+
9+
this.message = message;
10+
11+
Error.captureStackTrace(this, this.constructor);
12+
}
13+
};

‎lib/webpack.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
1212
const validateSchema = require("./validateSchema");
1313
const WebpackOptionsValidationError = require("./WebpackOptionsValidationError");
1414
const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
15+
const RemovedPluginError = require("./RemovedPluginError");
1516

1617
const webpack = (options, callback) => {
1718
const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, options);
@@ -132,3 +133,27 @@ exportPlugins(exports.node = {}, {
132133
exportPlugins(exports.debug = {}, {
133134
"ProfilingPlugin": () => require("./debug/ProfilingPlugin"),
134135
});
136+
137+
const defineMissingPluginError = (namespace, pluginName, errorMessage) => {
138+
Reflect.defineProperty(namespace, pluginName, {
139+
configurable: false,
140+
enumerable: true,
141+
get() {
142+
throw new RemovedPluginError(errorMessage);
143+
}
144+
});
145+
};
146+
147+
// TODO remove in webpack 5
148+
defineMissingPluginError(
149+
exports.optimize,
150+
"UglifyJsPlugin",
151+
"webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead."
152+
);
153+
154+
// TODO remove in webpack 5
155+
defineMissingPluginError(
156+
exports.optimize,
157+
"CommonsChunkPlugin",
158+
"webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead."
159+
);

‎test/RemovedPlugins.unittest.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const webpack = require("../lib/webpack");
2+
const RemovedPluginError = require("../lib/RemovedPluginError");
3+
require("should");
4+
5+
describe("removed plugin errors", () => {
6+
it("should error when accessing removed plugins", () => {
7+
(() => webpack.optimize.UglifyJsPlugin).should.throw(
8+
RemovedPluginError,
9+
/webpack\.optimize\.UglifyJsPlugin has been removed, please use config\.optimization\.minimize instead\./
10+
);
11+
12+
(() => webpack.optimize.CommonsChunkPlugin).should.throw(
13+
RemovedPluginError,
14+
/webpack\.optimize\.CommonsChunkPlugin has been removed, please use config\.optimization\.splitChunks instead\./
15+
);
16+
});
17+
});

0 commit comments

Comments
 (0)