index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 _globby = _interopRequireDefault(require("globby"));
  9. var _micromatch = require("micromatch");
  10. var _options = require("./options");
  11. var _linter = _interopRequireDefault(require("./linter"));
  12. var _utils = require("./utils");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. // @ts-ignore
  15. // @ts-ignore
  16. /** @typedef {import('webpack').Compiler} Compiler */
  17. /** @typedef {import('webpack').Module} Module */
  18. /** @typedef {import('./options').Options} Options */
  19. /** @typedef {Partial<{timestamp:number} | number>} FileSystemInfoEntry */
  20. const STYLELINT_PLUGIN = 'StylelintWebpackPlugin';
  21. let counter = 0;
  22. class StylelintWebpackPlugin {
  23. /**
  24. * @param {Options} options
  25. */
  26. constructor(options = {}) {
  27. this.key = STYLELINT_PLUGIN;
  28. this.options = (0, _options.getOptions)(options);
  29. this.run = this.run.bind(this);
  30. this.startTime = Date.now();
  31. /** @type {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} */
  32. this.prevTimestamps = new Map();
  33. }
  34. /**
  35. * @param {Compiler} compiler
  36. * @returns {void}
  37. */
  38. apply(compiler) {
  39. // Generate key for each compilation,
  40. // this differentiates one from the other when being cached.
  41. this.key = compiler.name || `${this.key}_${counter += 1}`; // If `lintDirtyModulesOnly` is disabled,
  42. // execute the linter on the build
  43. if (!this.options.lintDirtyModulesOnly) {
  44. compiler.hooks.run.tapPromise(this.key, this.run);
  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);
  53. });
  54. }
  55. /**
  56. * @param {Compiler} compiler
  57. */
  58. async run(compiler) {
  59. // Do not re-hook
  60. /* istanbul ignore if */
  61. if ( // @ts-ignore
  62. compiler.hooks.thisCompilation.taps.find(({
  63. name
  64. }) => name === this.key)) {
  65. return;
  66. }
  67. const context = this.getContext(compiler);
  68. const options = { ...this.options,
  69. exclude: (0, _utils.parseFiles)(this.options.exclude || ['**/node_modules/**', compiler.options.output.path], context),
  70. extensions: (0, _arrify.default)(this.options.extensions),
  71. files: (0, _utils.parseFiles)(this.options.files || '', context)
  72. };
  73. const wanted = (0, _utils.parseFoldersToGlobs)(options.files, options.extensions);
  74. const exclude = (0, _utils.parseFoldersToGlobs)(options.exclude);
  75. compiler.hooks.thisCompilation.tap(this.key, compilation => {
  76. /** @type {import('./linter').Linter} */
  77. let lint;
  78. /** @type {import('./linter').Reporter} */
  79. let report;
  80. /** @type number */
  81. let threads;
  82. try {
  83. ({
  84. lint,
  85. report,
  86. threads
  87. } = (0, _linter.default)(this.key, options, compilation));
  88. } catch (e) {
  89. compilation.errors.push(e);
  90. return;
  91. }
  92. compilation.hooks.finishModules.tap(this.key, () => {
  93. const files = this.getFiles(compiler, wanted, exclude);
  94. if (threads > 1) {
  95. for (const file of files) {
  96. lint((0, _utils.parseFiles)(file, context));
  97. }
  98. } else if (files.length > 0) {
  99. lint((0, _utils.parseFiles)(files, context));
  100. }
  101. }); // await and interpret results
  102. compilation.hooks.additionalAssets.tapPromise(this.key, processResults);
  103. async function processResults() {
  104. const {
  105. errors,
  106. warnings,
  107. generateReportAsset
  108. } = await report();
  109. if (warnings && !options.failOnWarning) {
  110. // @ts-ignore
  111. compilation.warnings.push(warnings);
  112. } else if (warnings && options.failOnWarning) {
  113. // @ts-ignore
  114. compilation.errors.push(warnings);
  115. }
  116. if (errors && options.failOnError) {
  117. // @ts-ignore
  118. compilation.errors.push(errors);
  119. } else if (errors && !options.failOnError) {
  120. // @ts-ignore
  121. compilation.warnings.push(errors);
  122. }
  123. if (generateReportAsset) {
  124. await generateReportAsset(compilation);
  125. }
  126. }
  127. });
  128. }
  129. /**
  130. *
  131. * @param {Compiler} compiler
  132. * @returns {string}
  133. */
  134. getContext(compiler) {
  135. if (!this.options.context) {
  136. return String(compiler.options.context);
  137. }
  138. if (!(0, _path.isAbsolute)(this.options.context)) {
  139. return (0, _path.join)(String(compiler.options.context), this.options.context);
  140. }
  141. return this.options.context;
  142. }
  143. /**
  144. * @param {Compiler} compiler
  145. * @param {string[]} wanted
  146. * @param {string[]} exclude
  147. * @returns {string[]}
  148. */
  149. // eslint-disable-next-line no-unused-vars
  150. getFiles(compiler, wanted, exclude) {
  151. // webpack 5
  152. if (compiler.modifiedFiles) {
  153. return Array.from(compiler.modifiedFiles).filter(file => (0, _micromatch.isMatch)(file, wanted, {
  154. dot: true
  155. }) && !(0, _micromatch.isMatch)(file, exclude, {
  156. dot: true
  157. }));
  158. } // webpack 4
  159. /* istanbul ignore next */
  160. if (compiler.fileTimestamps && compiler.fileTimestamps.size > 0) {
  161. return this.getChangedFiles(compiler.fileTimestamps).filter(file => (0, _micromatch.isMatch)(file, wanted, {
  162. dot: true
  163. }) && !(0, _micromatch.isMatch)(file, exclude, {
  164. dot: true
  165. }));
  166. }
  167. return _globby.default.sync(wanted, {
  168. dot: true,
  169. ignore: exclude
  170. });
  171. }
  172. /**
  173. * @param {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} fileTimestamps
  174. * @returns {string[]}
  175. */
  176. /* istanbul ignore next */
  177. getChangedFiles(fileTimestamps) {
  178. /**
  179. * @param {null | FileSystemInfoEntry | "ignore" | undefined} fileSystemInfoEntry
  180. * @returns {Partial<number>}
  181. */
  182. const getTimestamps = fileSystemInfoEntry => {
  183. // @ts-ignore
  184. if (fileSystemInfoEntry && fileSystemInfoEntry.timestamp) {
  185. // @ts-ignore
  186. return fileSystemInfoEntry.timestamp;
  187. } // @ts-ignore
  188. return fileSystemInfoEntry;
  189. };
  190. /**
  191. * @param {string} filename
  192. * @param {null | FileSystemInfoEntry | "ignore" | undefined} fileSystemInfoEntry
  193. * @returns {boolean}
  194. */
  195. const hasFileChanged = (filename, fileSystemInfoEntry) => {
  196. const prevTimestamp = getTimestamps(this.prevTimestamps.get(filename));
  197. const timestamp = getTimestamps(fileSystemInfoEntry);
  198. return (prevTimestamp || this.startTime) < (timestamp || Infinity);
  199. };
  200. const changedFiles = [];
  201. for (const [filename, timestamp] of fileTimestamps.entries()) {
  202. if (hasFileChanged(filename, timestamp)) {
  203. changedFiles.push(filename);
  204. }
  205. }
  206. this.prevTimestamps = fileTimestamps;
  207. return changedFiles;
  208. }
  209. }
  210. var _default = StylelintWebpackPlugin;
  211. exports.default = _default;