| 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 | * Processes the provided schema.
|
|---|
| 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 | */
|
|---|
| 78 | const 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.instancePath === "/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.instancePath === "/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 = 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[
|
|---|
| 121 | /** @type {keyof DID_YOU_MEAN} */ (params.additionalProperty)
|
|---|
| 122 | ]
|
|---|
| 123 | }?`;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (
|
|---|
| 127 | Object.prototype.hasOwnProperty.call(
|
|---|
| 128 | REMOVED,
|
|---|
| 129 | params.additionalProperty
|
|---|
| 130 | )
|
|---|
| 131 | ) {
|
|---|
| 132 | return `${formattedError}\n${
|
|---|
| 133 | REMOVED[/** @type {keyof REMOVED} */ (params.additionalProperty)]
|
|---|
| 134 | }?`;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | if (!error.instancePath) {
|
|---|
| 138 | if (params.additionalProperty === "debug") {
|
|---|
| 139 | return (
|
|---|
| 140 | `${formattedError}\n` +
|
|---|
| 141 | "The 'debug' property was removed in webpack 2.0.0.\n" +
|
|---|
| 142 | "Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
|
|---|
| 143 | "Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
|
|---|
| 144 | "plugins: [\n" +
|
|---|
| 145 | " new webpack.LoaderOptionsPlugin({\n" +
|
|---|
| 146 | " debug: true\n" +
|
|---|
| 147 | " })\n" +
|
|---|
| 148 | "]"
|
|---|
| 149 | );
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (params.additionalProperty) {
|
|---|
| 153 | return (
|
|---|
| 154 | `${formattedError}\n` +
|
|---|
| 155 | "For typos: please correct them.\n" +
|
|---|
| 156 | "For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" +
|
|---|
| 157 | " Loaders should be updated to allow passing options via loader options in module.rules.\n" +
|
|---|
| 158 | " Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
|
|---|
| 159 | " plugins: [\n" +
|
|---|
| 160 | " new webpack.LoaderOptionsPlugin({\n" +
|
|---|
| 161 | " // test: /\\.xxx$/, // may apply this only for some modules\n" +
|
|---|
| 162 | " options: {\n" +
|
|---|
| 163 | ` ${params.additionalProperty}: …\n` +
|
|---|
| 164 | " }\n" +
|
|---|
| 165 | " })\n" +
|
|---|
| 166 | " ]"
|
|---|
| 167 | );
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | return formattedError;
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | );
|
|---|
| 176 | };
|
|---|
| 177 |
|
|---|
| 178 | module.exports = validateSchema;
|
|---|