Skip to content

Commit 6714bb7

Browse files
committed
phase 1 for RFC webpack#6534. WebpackError / WebpackErrorArray intro
1 parent d668a23 commit 6714bb7

File tree

6 files changed

+121
-11
lines changed

6 files changed

+121
-11
lines changed

lib/Compilation.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const createHash = require("./util/createHash");
3333
const Queue = require("./util/Queue");
3434
const SortableSet = require("./util/SortableSet");
3535
const GraphHelpers = require("./GraphHelpers");
36+
const WebpackErrorArray = require("./WebpackErrorArray");
3637

3738
const byId = (a, b) => {
3839
if (a.id < b.id) return -1;
@@ -241,8 +242,8 @@ class Compilation extends Tapable {
241242
this.nextFreeModuleIndex2 = undefined;
242243
this.additionalChunkAssets = [];
243244
this.assets = {};
244-
this.errors = [];
245-
this.warnings = [];
245+
this.errors = new WebpackErrorArray();
246+
this.warnings = new WebpackErrorArray();
246247
this.children = [];
247248
this.dependencyFactories = new Map();
248249
this.dependencyTemplates = new Map();

lib/Module.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const DependenciesBlock = require("./DependenciesBlock");
1010
const ModuleReason = require("./ModuleReason");
1111
const SortableSet = require("./util/SortableSet");
1212
const Template = require("./Template");
13+
const WebpackErrorArray = require("./WebpackErrorArray");
1314

1415
const EMPTY_RESOLVE_OPTIONS = {};
1516

@@ -41,8 +42,8 @@ class Module extends DependenciesBlock {
4142
this.factoryMeta = {};
4243

4344
// Info from Build
44-
this.warnings = [];
45-
this.errors = [];
45+
this.errors = new WebpackErrorArray();
46+
this.warnings = new WebpackErrorArray();
4647
this.buildMeta = undefined;
4748
this.buildInfo = undefined;
4849

lib/WebpackError.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,81 @@
44
*/
55
"use strict";
66

7+
const createHash = require("./util/createHash");
8+
9+
const hash = Symbol();
10+
const meta = Symbol();
11+
712
module.exports = class WebpackError extends Error {
13+
constructor(
14+
message,
15+
line = 0,
16+
column = 0,
17+
columnEnd = 0,
18+
category = "error"
19+
) {
20+
// TODO: enable message assertion during construction in a commit to follow
21+
// if (!message) {
22+
// WebpackError.deprecate("message must be a non-null, non-empty value.");
23+
// }
24+
// TODO: enable message length limit at a commit to follow
25+
// else if (message.length > 160) {
26+
// WebpackError.deprecate("message cannot be longer than 80 characters.");
27+
// }
28+
29+
super(message);
30+
31+
this[hash] = createHash("md4");
32+
this[meta] = {};
33+
this.category = category;
34+
this.column = column;
35+
this.columnEnd = columnEnd;
36+
this.line = line;
37+
38+
// TODO: enable abstract protection at a commit to follow
39+
// if (this.constructor === WebpackError) {
40+
// const message = `A WebpackError cannot be directly constructed.`;
41+
// WebpackError.deprecate(message);
42+
// }
43+
}
44+
45+
static deprecate(message) {
46+
const err = new Error();
47+
const stack = err.stack
48+
.split("\n")
49+
.slice(3, 4)
50+
.join("\n");
51+
// use process.stdout to assert the message will be displayed in
52+
// environments where console has been proxied. this mimics node's
53+
// util.deprecate method.
54+
process.stdout.write(`DeprecationWarning: ${message}\n${stack}\n`);
55+
}
56+
57+
get id() {
58+
return this[hash];
59+
}
60+
861
inspect() {
962
return this.stack + (this.details ? `\n${this.details}` : "");
1063
}
64+
65+
get meta() {
66+
return this[meta];
67+
}
68+
69+
// TODO: enable this standard output in a later PR. at present, webpack tests
70+
// rely heavily on arbitrary toString() output of different errors.
71+
// toString(formatFn) {
72+
// if (formatFn) {
73+
// return formatFn(this);
74+
// }
75+
//
76+
// let preface = `${this.line}:${this.column} `;
77+
//
78+
// if (this.category) {
79+
// preface += `${this.category}: `;
80+
// }
81+
//
82+
// return `${preface}${this.message}`;
83+
// }
1184
};

lib/WebpackErrorArray.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Andrew Powell <andrew@shellscape.org>
4+
*/
5+
"use strict";
6+
7+
const WebpackError = require("./WebpackError");
8+
9+
module.exports = class WebpackErrorArray extends Array {
10+
constructor(...args) {
11+
// TODO: validation will be enabled in a later PR
12+
// if (args.length > 0 && typeof args[0] !== "number") {
13+
// WebpackErrorArray.validate(...args);
14+
// }
15+
super(...args);
16+
}
17+
18+
push(...args) {
19+
// TODO: validation will be enabled in a later PR
20+
// WebpackErrorArray.validate(...args);
21+
super.push(...args);
22+
}
23+
24+
static validate(...args) {
25+
for (const arg of args) {
26+
if (!(arg instanceof WebpackError)) {
27+
const message = `A WebpackErrorArray can only contain WebpackError objects.
28+
Offending Argument: ${arg}`;
29+
WebpackError.deprecate(message);
30+
}
31+
}
32+
}
33+
};

lib/optimize/ConcatenatedModule.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const HarmonyExportExpressionDependency = require("../dependencies/HarmonyExport
1818
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
1919
const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
2020
const createHash = require("../util/createHash");
21+
const WebpackErrorArray = require("../WebpackErrorArray");
2122

2223
const ensureNsObjSource = (
2324
info,
@@ -288,8 +289,8 @@ class ConcatenatedModule extends Module {
288289

289290
this.dependencies = [];
290291

291-
this.warnings = [];
292-
this.errors = [];
292+
this.errors = new WebpackErrorArray();
293+
this.warnings = new WebpackErrorArray();
293294
this._orderedConcatenationList = this._createOrderedConcatenationList(
294295
rootModule,
295296
modulesSet

test/RemovedPlugins.unittest.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ require("should");
44

55
describe("removed plugin errors", () => {
66
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-
);
7+
(() => webpack.optimize.UglifyJsPlugin).should.throw(RemovedPluginError, {
8+
message: /webpack\.optimize\.UglifyJsPlugin has been removed, please use config\.optimization\.minimize instead\./
9+
});
1110

1211
(() => webpack.optimize.CommonsChunkPlugin).should.throw(
1312
RemovedPluginError,
14-
/webpack\.optimize\.CommonsChunkPlugin has been removed, please use config\.optimization\.splitChunks instead\./
13+
{
14+
message: /webpack\.optimize\.CommonsChunkPlugin has been removed, please use config\.optimization\.splitChunks instead\./
15+
}
1516
);
1617
});
1718
});

0 commit comments

Comments
 (0)