options.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getOptions = getOptions;
  6. exports.getStylelintOptions = getStylelintOptions;
  7. var _schemaUtils = require("schema-utils");
  8. var _options = _interopRequireDefault(require("./options.json"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. // @ts-ignore
  11. /** @typedef {import("stylelint")} stylelint */
  12. /** @typedef {import("stylelint").LinterOptions} StylelintOptions */
  13. /** @typedef {import("stylelint").FormatterType} FormatterType */
  14. /**
  15. * @typedef {Object} OutputReport
  16. * @property {string=} filePath
  17. * @property {FormatterType=} formatter
  18. */
  19. /**
  20. * @typedef {Object} PluginOptions
  21. * @property {string} context
  22. * @property {boolean} emitError
  23. * @property {boolean} emitWarning
  24. * @property {string|string[]=} exclude
  25. * @property {string|string[]} extensions
  26. * @property {boolean} failOnError
  27. * @property {boolean} failOnWarning
  28. * @property {string|string[]} files
  29. * @property {FormatterType} formatter
  30. * @property {boolean} lintDirtyModulesOnly
  31. * @property {boolean} quiet
  32. * @property {string} stylelintPath
  33. * @property {OutputReport} outputReport
  34. * @property {number|boolean=} threads
  35. */
  36. /** @typedef {Partial<PluginOptions & StylelintOptions>} Options */
  37. /**
  38. * @param {Options} pluginOptions
  39. * @returns {Partial<PluginOptions>}
  40. */
  41. function getOptions(pluginOptions) {
  42. const options = {
  43. extensions: ['css', 'scss', 'sass'],
  44. emitError: true,
  45. emitWarning: true,
  46. failOnError: true,
  47. ...pluginOptions,
  48. ...(pluginOptions.quiet ? {
  49. emitError: true,
  50. emitWarning: false
  51. } : {})
  52. }; // @ts-ignore
  53. (0, _schemaUtils.validate)(_options.default, options, {
  54. name: 'Stylelint Webpack Plugin',
  55. baseDataPath: 'options'
  56. });
  57. return options;
  58. }
  59. /**
  60. * @param {Options} pluginOptions
  61. * @returns {Partial<StylelintOptions>}
  62. */
  63. function getStylelintOptions(pluginOptions) {
  64. const stylelintOptions = { ...pluginOptions
  65. }; // Keep the files and formatter option because it is common to both the plugin and Stylelint.
  66. const {
  67. files,
  68. formatter,
  69. ...stylelintOnlyOptions
  70. } = _options.default.properties; // No need to guard the for-in because schema.properties has hardcoded keys.
  71. // eslint-disable-next-line guard-for-in
  72. for (const option in stylelintOnlyOptions) {
  73. // @ts-ignore
  74. delete stylelintOptions[option];
  75. }
  76. return stylelintOptions;
  77. }