1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { validate } = require("schema-utils");
|
---|
9 |
|
---|
10 | /* cSpell:disable */
|
---|
11 | const DID_YOU_MEAN = {
|
---|
12 | rules: "module.rules",
|
---|
13 | loaders: "module.rules or module.rules.*.use",
|
---|
14 | query: "module.rules.*.options (BREAKING CHANGE since webpack 5)",
|
---|
15 | noParse: "module.noParse",
|
---|
16 | filename: "output.filename or module.rules.*.generator.filename",
|
---|
17 | file: "output.filename",
|
---|
18 | chunkFilename: "output.chunkFilename",
|
---|
19 | chunkfilename: "output.chunkFilename",
|
---|
20 | ecmaVersion:
|
---|
21 | "output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)",
|
---|
22 | ecmaversion:
|
---|
23 | "output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)",
|
---|
24 | ecma: "output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)",
|
---|
25 | path: "output.path",
|
---|
26 | pathinfo: "output.pathinfo",
|
---|
27 | pathInfo: "output.pathinfo",
|
---|
28 | jsonpFunction: "output.chunkLoadingGlobal (BREAKING CHANGE since webpack 5)",
|
---|
29 | chunkCallbackName:
|
---|
30 | "output.chunkLoadingGlobal (BREAKING CHANGE since webpack 5)",
|
---|
31 | jsonpScriptType: "output.scriptType (BREAKING CHANGE since webpack 5)",
|
---|
32 | hotUpdateFunction: "output.hotUpdateGlobal (BREAKING CHANGE since webpack 5)",
|
---|
33 | splitChunks: "optimization.splitChunks",
|
---|
34 | immutablePaths: "snapshot.immutablePaths",
|
---|
35 | managedPaths: "snapshot.managedPaths",
|
---|
36 | maxModules: "stats.modulesSpace (BREAKING CHANGE since webpack 5)",
|
---|
37 | hashedModuleIds:
|
---|
38 | 'optimization.moduleIds: "hashed" (BREAKING CHANGE since webpack 5)',
|
---|
39 | namedChunks:
|
---|
40 | 'optimization.chunkIds: "named" (BREAKING CHANGE since webpack 5)',
|
---|
41 | namedModules:
|
---|
42 | 'optimization.moduleIds: "named" (BREAKING CHANGE since webpack 5)',
|
---|
43 | occurrenceOrder:
|
---|
44 | 'optimization.chunkIds: "size" and optimization.moduleIds: "size" (BREAKING CHANGE since webpack 5)',
|
---|
45 | automaticNamePrefix:
|
---|
46 | "optimization.splitChunks.[cacheGroups.*].idHint (BREAKING CHANGE since webpack 5)",
|
---|
47 | noEmitOnErrors:
|
---|
48 | "optimization.emitOnErrors (BREAKING CHANGE since webpack 5: logic is inverted to avoid negative flags)",
|
---|
49 | Buffer:
|
---|
50 | "to use the ProvidePlugin to process the Buffer variable to modules as polyfill\n" +
|
---|
51 | "BREAKING CHANGE: webpack 5 no longer provided Node.js polyfills by default.\n" +
|
---|
52 | "Note: if you are using 'node.Buffer: false', you can just remove that as this is the default behavior now.\n" +
|
---|
53 | "To provide a polyfill to modules use:\n" +
|
---|
54 | 'new ProvidePlugin({ Buffer: ["buffer", "Buffer"] }) and npm install buffer.',
|
---|
55 | process:
|
---|
56 | "to use the ProvidePlugin to process the process variable to modules as polyfill\n" +
|
---|
57 | "BREAKING CHANGE: webpack 5 no longer provided Node.js polyfills by default.\n" +
|
---|
58 | "Note: if you are using 'node.process: false', you can just remove that as this is the default behavior now.\n" +
|
---|
59 | "To provide a polyfill to modules use:\n" +
|
---|
60 | 'new ProvidePlugin({ process: "process" }) and npm install buffer.'
|
---|
61 | };
|
---|
62 |
|
---|
63 | const REMOVED = {
|
---|
64 | concord:
|
---|
65 | "BREAKING CHANGE: resolve.concord has been removed and is no longer available.",
|
---|
66 | devtoolLineToLine:
|
---|
67 | "BREAKING CHANGE: output.devtoolLineToLine has been removed and is no longer available."
|
---|
68 | };
|
---|
69 | /* cSpell:enable */
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * @param {Parameters<typeof validate>[0]} schema a json schema
|
---|
73 | * @param {Parameters<typeof validate>[1]} options the options that should be validated
|
---|
74 | * @param {Parameters<typeof validate>[2]=} validationConfiguration configuration for generating errors
|
---|
75 | * @returns {void}
|
---|
76 | */
|
---|
77 | const validateSchema = (schema, options, validationConfiguration) => {
|
---|
78 | validate(
|
---|
79 | schema,
|
---|
80 | options,
|
---|
81 | validationConfiguration || {
|
---|
82 | name: "Webpack",
|
---|
83 | postFormatter: (formattedError, error) => {
|
---|
84 | const children = error.children;
|
---|
85 | if (
|
---|
86 | children &&
|
---|
87 | children.some(
|
---|
88 | child =>
|
---|
89 | child.keyword === "absolutePath" &&
|
---|
90 | child.dataPath === ".output.filename"
|
---|
91 | )
|
---|
92 | ) {
|
---|
93 | return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (
|
---|
97 | children &&
|
---|
98 | children.some(
|
---|
99 | child =>
|
---|
100 | child.keyword === "pattern" && child.dataPath === ".devtool"
|
---|
101 | )
|
---|
102 | ) {
|
---|
103 | return (
|
---|
104 | `${formattedError}\n` +
|
---|
105 | "BREAKING CHANGE since webpack 5: The devtool option is more strict.\n" +
|
---|
106 | "Please strictly follow the order of the keywords in the pattern."
|
---|
107 | );
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (error.keyword === "additionalProperties") {
|
---|
111 | const params =
|
---|
112 | /** @type {import("ajv").AdditionalPropertiesParams} */ (
|
---|
113 | error.params
|
---|
114 | );
|
---|
115 | if (
|
---|
116 | Object.prototype.hasOwnProperty.call(
|
---|
117 | DID_YOU_MEAN,
|
---|
118 | params.additionalProperty
|
---|
119 | )
|
---|
120 | ) {
|
---|
121 | return `${formattedError}\nDid you mean ${
|
---|
122 | DID_YOU_MEAN[params.additionalProperty]
|
---|
123 | }?`;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (
|
---|
127 | Object.prototype.hasOwnProperty.call(
|
---|
128 | REMOVED,
|
---|
129 | params.additionalProperty
|
---|
130 | )
|
---|
131 | ) {
|
---|
132 | return `${formattedError}\n${REMOVED[params.additionalProperty]}?`;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!error.dataPath) {
|
---|
136 | if (params.additionalProperty === "debug") {
|
---|
137 | return (
|
---|
138 | `${formattedError}\n` +
|
---|
139 | "The 'debug' property was removed in webpack 2.0.0.\n" +
|
---|
140 | "Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
|
---|
141 | "Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
|
---|
142 | "plugins: [\n" +
|
---|
143 | " new webpack.LoaderOptionsPlugin({\n" +
|
---|
144 | " debug: true\n" +
|
---|
145 | " })\n" +
|
---|
146 | "]"
|
---|
147 | );
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (params.additionalProperty) {
|
---|
151 | return (
|
---|
152 | `${formattedError}\n` +
|
---|
153 | "For typos: please correct them.\n" +
|
---|
154 | "For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" +
|
---|
155 | " Loaders should be updated to allow passing options via loader options in module.rules.\n" +
|
---|
156 | " Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
|
---|
157 | " plugins: [\n" +
|
---|
158 | " new webpack.LoaderOptionsPlugin({\n" +
|
---|
159 | " // test: /\\.xxx$/, // may apply this only for some modules\n" +
|
---|
160 | " options: {\n" +
|
---|
161 | ` ${params.additionalProperty}: …\n` +
|
---|
162 | " }\n" +
|
---|
163 | " })\n" +
|
---|
164 | " ]"
|
---|
165 | );
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return formattedError;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | );
|
---|
174 | };
|
---|
175 | module.exports = validateSchema;
|
---|