index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = require("path");
  7. var _arrify = _interopRequireDefault(require("arrify"));
  8. var _micromatch = require("micromatch");
  9. var _options = require("./options");
  10. var _linter = _interopRequireDefault(require("./linter"));
  11. var _utils = require("./utils");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. // @ts-ignore
  14. /** @typedef {import('webpack').Compiler} Compiler */
  15. /** @typedef {import('./options').Options} Options */
  16. const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
  17. let counter = 0;
  18. class ESLintWebpackPlugin {
  19. /**
  20. * @param {Options} options
  21. */
  22. constructor(options = {}) {
  23. this.key = ESLINT_PLUGIN;
  24. this.options = (0, _options.getOptions)(options);
  25. this.run = this.run.bind(this);
  26. }
  27. /**
  28. * @param {Compiler} compiler
  29. * @returns {void}
  30. */
  31. apply(compiler) {
  32. // Generate key for each compilation,
  33. // this differentiates one from the other when being cached.
  34. this.key = compiler.name || `${this.key}_${counter += 1}`;
  35. const options = { ...this.options,
  36. exclude: (0, _utils.parseFiles)(this.options.exclude || [], this.getContext(compiler)),
  37. extensions: (0, _arrify.default)(this.options.extensions),
  38. files: (0, _utils.parseFiles)(this.options.files || '', this.getContext(compiler))
  39. };
  40. const wanted = (0, _utils.parseFoldersToGlobs)(options.files, options.extensions);
  41. const exclude = (0, _utils.parseFoldersToGlobs)(this.options.exclude ? options.exclude : '**/node_modules/**', []); // If `lintDirtyModulesOnly` is disabled,
  42. // execute the linter on the build
  43. if (!this.options.lintDirtyModulesOnly) {
  44. compiler.hooks.run.tapPromise(this.key, c => this.run(c, options, wanted, exclude));
  45. }
  46. let isFirstRun = this.options.lintDirtyModulesOnly;
  47. compiler.hooks.watchRun.tapPromise(this.key, c => {
  48. if (isFirstRun) {
  49. isFirstRun = false;
  50. return Promise.resolve();
  51. }
  52. return this.run(c, options, wanted, exclude);
  53. });
  54. }
  55. /**
  56. * @param {Compiler} compiler
  57. * @param {Options} options
  58. * @param {string[]} wanted
  59. * @param {string[]} exclude
  60. */
  61. async run(compiler, options, wanted, exclude) {
  62. // Do not re-hook
  63. if ( // @ts-ignore
  64. compiler.hooks.thisCompilation.taps.find(({
  65. name
  66. }) => name === this.key)) {
  67. return;
  68. }
  69. compiler.hooks.thisCompilation.tap(this.key, compilation => {
  70. /** @type {import('./linter').Linter} */
  71. let lint;
  72. /** @type {import('./linter').Reporter} */
  73. let report;
  74. /** @type number */
  75. let threads;
  76. try {
  77. ({
  78. lint,
  79. report,
  80. threads
  81. } = (0, _linter.default)(this.key, options, compilation));
  82. } catch (e) {
  83. compilation.errors.push(e);
  84. return;
  85. }
  86. /** @type {string[]} */
  87. const files = []; // @ts-ignore
  88. // Add the file to be linted
  89. compilation.hooks.succeedModule.tap(this.key, ({
  90. resource
  91. }) => {
  92. if (resource) {
  93. const [file] = resource.split('?');
  94. if (file && !files.includes(file) && (0, _micromatch.isMatch)(file, wanted, {
  95. dot: true
  96. }) && !(0, _micromatch.isMatch)(file, exclude, {
  97. dot: true
  98. })) {
  99. files.push(file);
  100. if (threads > 1) {
  101. lint(file);
  102. }
  103. }
  104. }
  105. }); // Lint all files added
  106. compilation.hooks.finishModules.tap(this.key, () => {
  107. if (files.length > 0 && threads <= 1) {
  108. lint(files);
  109. }
  110. }); // await and interpret results
  111. compilation.hooks.additionalAssets.tapPromise(this.key, processResults);
  112. async function processResults() {
  113. const {
  114. errors,
  115. warnings,
  116. generateReportAsset
  117. } = await report();
  118. if (warnings && !options.failOnWarning) {
  119. // @ts-ignore
  120. compilation.warnings.push(warnings);
  121. } else if (warnings && options.failOnWarning) {
  122. // @ts-ignore
  123. compilation.errors.push(warnings);
  124. }
  125. if (errors && options.failOnError) {
  126. // @ts-ignore
  127. compilation.errors.push(errors);
  128. } else if (errors && !options.failOnError) {
  129. // @ts-ignore
  130. compilation.warnings.push(errors);
  131. }
  132. if (generateReportAsset) {
  133. await generateReportAsset(compilation);
  134. }
  135. }
  136. });
  137. }
  138. /**
  139. *
  140. * @param {Compiler} compiler
  141. * @returns {string}
  142. */
  143. getContext(compiler) {
  144. if (!this.options.context) {
  145. return String(compiler.options.context);
  146. }
  147. if (!(0, _path.isAbsolute)(this.options.context)) {
  148. return (0, _path.join)(String(compiler.options.context), this.options.context);
  149. }
  150. return this.options.context;
  151. }
  152. }
  153. var _default = ESLintWebpackPlugin;
  154. exports.default = _default;