index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
  4. class ConfigTestCommand {
  5. async apply(cli) {
  6. await cli.makeCommand({
  7. name: "configtest [config-path]",
  8. alias: "t",
  9. description: "Validate a webpack configuration.",
  10. pkg: "@webpack-cli/configtest",
  11. dependencies: [WEBPACK_PACKAGE],
  12. }, [], async (configPath) => {
  13. cli.webpack = await cli.loadWebpack();
  14. const config = await cli.loadConfig(configPath ? { config: [configPath] } : {});
  15. const configPaths = new Set();
  16. if (Array.isArray(config.options)) {
  17. config.options.forEach((options) => {
  18. if (config.path.get(options)) {
  19. configPaths.add(config.path.get(options));
  20. }
  21. });
  22. }
  23. else {
  24. if (config.path.get(config.options)) {
  25. configPaths.add(config.path.get(config.options));
  26. }
  27. }
  28. if (configPaths.size === 0) {
  29. cli.logger.error("No configuration found.");
  30. process.exit(2);
  31. }
  32. cli.logger.info(`Validate '${Array.from(configPaths).join(" ,")}'.`);
  33. try {
  34. // @ts-expect-error cli.webpack.validate returns void
  35. const error = cli.webpack.validate(config.options);
  36. // TODO remove this after drop webpack@4
  37. if (error && error.length > 0) {
  38. // @ts-expect-error schema argument is missing
  39. throw new cli.webpack.WebpackOptionsValidationError(error);
  40. }
  41. }
  42. catch (error) {
  43. if (cli.isValidationError(error)) {
  44. cli.logger.error(error.message);
  45. }
  46. else {
  47. cli.logger.error(error);
  48. }
  49. process.exit(2);
  50. }
  51. cli.logger.success("There are no validation errors in the given webpack configuration.");
  52. });
  53. }
  54. }
  55. exports.default = ConfigTestCommand;