UNPKG

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