extract webpack config validation to keep validate API

This commit is contained in:
Tobias Koppers 2019-08-08 12:44:45 +02:00
parent 1952d5c882
commit eb3ea4d39e
3 changed files with 68 additions and 54 deletions

View File

@ -5,7 +5,7 @@
"use strict";
const validateSchema = require("schema-utils");
const validate = require("schema-utils");
const util = require("util");
const { version } = require("../package.json");
const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
@ -13,12 +13,13 @@ const Compiler = require("./Compiler");
const MultiCompiler = require("./MultiCompiler");
const WebpackOptionsApply = require("./WebpackOptionsApply");
const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
const validateSchema = require("./validateSchema");
const webpack = require("./webpack");
module.exports = webpack;
module.exports.WebpackOptionsApply = WebpackOptionsApply;
module.exports.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
module.exports.WebpackOptionsValidationError = validateSchema.ValidationError;
module.exports.WebpackOptionsValidationError = validate.ValidationError;
module.exports.Compiler = Compiler;
module.exports.MultiCompiler = MultiCompiler;
module.exports.validate = validateSchema.bind(null, webpackOptionsSchema);

63
lib/validateSchema.js Normal file
View File

@ -0,0 +1,63 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const validate = require("schema-utils");
const validateSchema = (schema, options) => {
validate(schema, options, {
name: "Webpack",
postFormatter: (formattedError, error) => {
if (
error.children &&
error.children.some(
child =>
child.keyword === "absolutePath" &&
child.dataPath === ".output.filename"
)
) {
return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`;
}
if (error.keyword === "additionalProperties" && !error.dataPath) {
if (error.params.additionalProperty === "debug") {
return (
`${formattedError}\n` +
"The 'debug' property was removed in webpack 2.0.0.\n" +
"Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
"Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
"plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" debug: true\n" +
" })\n" +
"]"
);
}
if (error.params.additionalProperty) {
return (
`${formattedError}\n` +
"For typos: please correct them.\n" +
"For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" +
" Loaders should be updated to allow passing options via loader options in module.rules.\n" +
" Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
" plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" // test: /\\.xxx$/, // may apply this only for some modules\n" +
" options: {\n" +
` ${error.params.additionalProperty}: …\n` +
" }\n" +
" })\n" +
" ]"
);
}
}
return formattedError;
}
});
};
module.exports = validateSchema;

View File

@ -5,13 +5,13 @@
"use strict";
const validateSchema = require("schema-utils");
const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
const Compiler = require("./Compiler");
const MultiCompiler = require("./MultiCompiler");
const WebpackOptionsApply = require("./WebpackOptionsApply");
const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
const validateSchema = require("./validateSchema");
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/** @typedef {import("./MultiStats")} MultiStats */
@ -75,57 +75,7 @@ const createCompiler = options => {
* @returns {Compiler | MultiCompiler} the compiler object
*/
const webpack = (options, callback) => {
validateSchema(webpackOptionsSchema, options, {
name: "Webpack",
postFormatter: (formattedError, error) => {
if (
error.children &&
error.children.some(
child =>
child.keyword === "absolutePath" &&
child.dataPath === ".output.filename"
)
) {
return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`;
}
if (error.keyword === "additionalProperties" && !error.dataPath) {
if (error.params.additionalProperty === "debug") {
return (
`${formattedError}\n` +
"The 'debug' property was removed in webpack 2.0.0.\n" +
"Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
"Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
"plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" debug: true\n" +
" })\n" +
"]"
);
}
if (error.params.additionalProperty) {
return (
`${formattedError}\n` +
"For typos: please correct them.\n" +
"For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" +
" Loaders should be updated to allow passing options via loader options in module.rules.\n" +
" Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
" plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" // test: /\\.xxx$/, // may apply this only for some modules\n" +
" options: {\n" +
` ${error.params.additionalProperty}: …\n` +
" }\n" +
" })\n" +
" ]"
);
}
}
return formattedError;
}
});
validateSchema(webpackOptionsSchema, options);
/** @type {TODO} */
let compiler;
let watch = false;