stylelint-webpack-plugin 2.3.2 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,144 +1,149 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _path = require("path");
9
-
10
- var _arrify = _interopRequireDefault(require("arrify"));
11
-
12
- var _globby = _interopRequireDefault(require("globby"));
13
-
14
- var _micromatch = require("micromatch");
15
-
16
- var _options = require("./options");
17
-
18
- var _linter = _interopRequireDefault(require("./linter"));
19
-
20
- var _utils = require("./utils");
21
-
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- // @ts-ignore
25
- // @ts-ignore
3
+ const {
4
+ isAbsolute,
5
+ join
6
+ } = require('path');
7
+ const arrify = require('arrify');
8
+ const globby = require('globby');
9
+ const {
10
+ isMatch
11
+ } = require('micromatch');
12
+ const {
13
+ getOptions
14
+ } = require('./options');
15
+ const linter = require('./linter');
16
+ const {
17
+ parseFiles,
18
+ parseFoldersToGlobs
19
+ } = require('./utils');
26
20
 
27
21
  /** @typedef {import('webpack').Compiler} Compiler */
28
-
29
22
  /** @typedef {import('webpack').Module} Module */
30
-
31
23
  /** @typedef {import('./options').Options} Options */
32
-
33
24
  /** @typedef {Partial<{timestamp:number} | number>} FileSystemInfoEntry */
25
+
34
26
  const STYLELINT_PLUGIN = 'StylelintWebpackPlugin';
35
27
  let counter = 0;
36
-
37
28
  class StylelintWebpackPlugin {
38
- /**
39
- * @param {Options} options
29
+ /**
30
+ * @param {Options} options
40
31
  */
41
32
  constructor(options = {}) {
42
33
  this.key = STYLELINT_PLUGIN;
43
- this.options = (0, _options.getOptions)(options);
34
+ this.options = getOptions(options);
44
35
  this.run = this.run.bind(this);
45
36
  this.startTime = Date.now();
46
- /** @type {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} */
47
37
 
38
+ /** @type {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} */
48
39
  this.prevTimestamps = new Map();
49
40
  }
50
- /**
51
- * @param {Compiler} compiler
52
- * @returns {void}
53
- */
54
-
55
41
 
42
+ /**
43
+ * @param {Compiler} compiler
44
+ * @returns {void}
45
+ */
56
46
  apply(compiler) {
57
47
  // Generate key for each compilation,
58
48
  // this differentiates one from the other when being cached.
59
- this.key = compiler.name || `${this.key}_${counter += 1}`; // If `lintDirtyModulesOnly` is disabled,
60
- // execute the linter on the build
49
+ this.key = compiler.name || `${this.key}_${counter += 1}`;
50
+ const context = this.getContext(compiler);
51
+ const excludeDefault = ['**/node_modules/**', String(compiler.options.output.path)];
52
+ const options = {
53
+ ...this.options,
54
+ context,
55
+ exclude: parseFiles(this.options.exclude || excludeDefault, context),
56
+ extensions: arrify(this.options.extensions),
57
+ files: parseFiles(this.options.files || '', context)
58
+ };
59
+ const wanted = parseFoldersToGlobs(options.files, options.extensions);
60
+ const exclude = parseFoldersToGlobs(options.exclude);
61
61
 
62
+ // If `lintDirtyModulesOnly` is disabled,
63
+ // execute the linter on the build
62
64
  if (!this.options.lintDirtyModulesOnly) {
63
- compiler.hooks.run.tapPromise(this.key, this.run);
65
+ compiler.hooks.run.tapPromise(this.key, c => this.run(c, options, wanted, exclude));
64
66
  }
65
-
66
67
  let isFirstRun = this.options.lintDirtyModulesOnly;
67
68
  compiler.hooks.watchRun.tapPromise(this.key, c => {
68
69
  if (isFirstRun) {
69
70
  isFirstRun = false;
70
71
  return Promise.resolve();
71
72
  }
72
-
73
- return this.run(c);
73
+ return this.run(c, options, wanted, exclude);
74
74
  });
75
75
  }
76
- /**
77
- * @param {Compiler} compiler
78
- */
79
76
 
80
-
81
- async run(compiler) {
77
+ /**
78
+ * @param {Compiler} compiler
79
+ * @param {Options} options
80
+ * @param {string[]} wanted
81
+ * @param {string[]} exclude
82
+ */
83
+ async run(compiler, options, wanted, exclude) {
82
84
  // Do not re-hook
83
-
84
85
  /* istanbul ignore if */
85
- if ( // @ts-ignore
86
+ if (
87
+ // @ts-ignore
86
88
  compiler.hooks.thisCompilation.taps.find(({
87
89
  name
88
90
  }) => name === this.key)) {
89
91
  return;
90
92
  }
91
-
92
- const context = this.getContext(compiler);
93
- const options = { ...this.options,
94
- exclude: (0, _utils.parseFiles)(this.options.exclude || ['**/node_modules/**', compiler.options.output.path], context),
95
- extensions: (0, _arrify.default)(this.options.extensions),
96
- files: (0, _utils.parseFiles)(this.options.files || '', context)
97
- };
98
- const wanted = (0, _utils.parseFoldersToGlobs)(options.files, options.extensions);
99
- const exclude = (0, _utils.parseFoldersToGlobs)(options.exclude);
100
93
  compiler.hooks.thisCompilation.tap(this.key, compilation => {
94
+ /** @type {import('./getStylelint').Stylelint} */
95
+ let stylelint;
96
+
101
97
  /** @type {import('./linter').Linter} */
102
98
  let lint;
103
- /** @type {import('./linter').Reporter} */
104
99
 
100
+ /** @type {import('./linter').isPathIgnored} */
101
+ let isPathIgnored;
102
+
103
+ /** @type {import('./linter').Reporter} */
105
104
  let report;
106
- /** @type number */
107
105
 
106
+ /** @type number */
108
107
  let threads;
109
-
110
108
  try {
111
109
  ({
110
+ stylelint,
112
111
  lint,
112
+ isPathIgnored,
113
113
  report,
114
114
  threads
115
- } = (0, _linter.default)(this.key, options, compilation));
115
+ } = linter(this.key, options, compilation));
116
116
  } catch (e) {
117
117
  compilation.errors.push(e);
118
118
  return;
119
119
  }
120
-
121
- compilation.hooks.finishModules.tap(this.key, () => {
122
- const files = this.getFiles(compiler, wanted, exclude);
123
-
120
+ compilation.hooks.finishModules.tapPromise(this.key, async () => {
121
+ /** @type {string[]} */
122
+ // @ts-ignore
123
+ const files = (await Promise.all(this.getFiles(compiler, wanted, exclude).map(async ( /** @type {string} */file) => {
124
+ try {
125
+ return (await isPathIgnored(stylelint, file)) ? false : file;
126
+ } catch (e) {
127
+ return file;
128
+ }
129
+ }))).filter(file => file !== false);
124
130
  if (threads > 1) {
125
131
  for (const file of files) {
126
- lint((0, _utils.parseFiles)(file, context));
132
+ lint(parseFiles(file, options.context || ''));
127
133
  }
128
134
  } else if (files.length > 0) {
129
- lint((0, _utils.parseFiles)(files, context));
135
+ lint(parseFiles(files, options.context || ''));
130
136
  }
131
- }); // await and interpret results
137
+ });
132
138
 
139
+ // await and interpret results
133
140
  compilation.hooks.additionalAssets.tapPromise(this.key, processResults);
134
-
135
141
  async function processResults() {
136
142
  const {
137
143
  errors,
138
144
  warnings,
139
145
  generateReportAsset
140
146
  } = await report();
141
-
142
147
  if (warnings && !options.failOnWarning) {
143
148
  // @ts-ignore
144
149
  compilation.warnings.push(warnings);
@@ -146,7 +151,6 @@ class StylelintWebpackPlugin {
146
151
  // @ts-ignore
147
152
  compilation.errors.push(warnings);
148
153
  }
149
-
150
154
  if (errors && options.failOnError) {
151
155
  // @ts-ignore
152
156
  compilation.errors.push(errors);
@@ -154,115 +158,99 @@ class StylelintWebpackPlugin {
154
158
  // @ts-ignore
155
159
  compilation.warnings.push(errors);
156
160
  }
157
-
158
161
  if (generateReportAsset) {
159
162
  await generateReportAsset(compilation);
160
163
  }
161
164
  }
162
165
  });
163
166
  }
164
- /**
165
- *
166
- * @param {Compiler} compiler
167
- * @returns {string}
168
- */
169
-
170
167
 
168
+ /**
169
+ *
170
+ * @param {Compiler} compiler
171
+ * @returns {string}
172
+ */
171
173
  getContext(compiler) {
172
174
  if (!this.options.context) {
173
175
  return String(compiler.options.context);
174
176
  }
175
-
176
- if (!(0, _path.isAbsolute)(this.options.context)) {
177
- return (0, _path.join)(String(compiler.options.context), this.options.context);
177
+ if (!isAbsolute(this.options.context)) {
178
+ return join(String(compiler.options.context), this.options.context);
178
179
  }
179
-
180
180
  return this.options.context;
181
181
  }
182
- /**
183
- * @param {Compiler} compiler
184
- * @param {string[]} wanted
185
- * @param {string[]} exclude
186
- * @returns {string[]}
182
+
183
+ /**
184
+ * @param {Compiler} compiler
185
+ * @param {string[]} wanted
186
+ * @param {string[]} exclude
187
+ * @returns {string[]}
187
188
  */
188
189
  // eslint-disable-next-line no-unused-vars
189
-
190
-
191
190
  getFiles(compiler, wanted, exclude) {
192
191
  // webpack 5
193
192
  if (compiler.modifiedFiles) {
194
- return Array.from(compiler.modifiedFiles).filter(file => (0, _micromatch.isMatch)(file, wanted, {
193
+ return Array.from(compiler.modifiedFiles).filter(file => isMatch(file, wanted, {
195
194
  dot: true
196
- }) && !(0, _micromatch.isMatch)(file, exclude, {
195
+ }) && !isMatch(file, exclude, {
197
196
  dot: true
198
197
  }));
199
- } // webpack 4
198
+ }
200
199
 
200
+ // webpack 4
201
201
  /* istanbul ignore next */
202
-
203
-
204
202
  if (compiler.fileTimestamps && compiler.fileTimestamps.size > 0) {
205
- return this.getChangedFiles(compiler.fileTimestamps).filter(file => (0, _micromatch.isMatch)(file, wanted, {
203
+ return this.getChangedFiles(compiler.fileTimestamps).filter(file => isMatch(file, wanted, {
206
204
  dot: true
207
- }) && !(0, _micromatch.isMatch)(file, exclude, {
205
+ }) && !isMatch(file, exclude, {
208
206
  dot: true
209
207
  }));
210
208
  }
211
-
212
- return _globby.default.sync(wanted, {
209
+ return globby.sync(wanted, {
213
210
  dot: true,
214
211
  ignore: exclude
215
212
  });
216
213
  }
217
- /**
218
- * @param {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} fileTimestamps
219
- * @returns {string[]}
220
- */
221
214
 
215
+ /**
216
+ * @param {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} fileTimestamps
217
+ * @returns {string[]}
218
+ */
222
219
  /* istanbul ignore next */
223
-
224
-
225
220
  getChangedFiles(fileTimestamps) {
226
- /**
227
- * @param {null | FileSystemInfoEntry | "ignore" | undefined} fileSystemInfoEntry
228
- * @returns {Partial<number>}
221
+ /**
222
+ * @param {null | FileSystemInfoEntry | "ignore" | undefined} fileSystemInfoEntry
223
+ * @returns {Partial<number>}
229
224
  */
230
225
  const getTimestamps = fileSystemInfoEntry => {
231
226
  // @ts-ignore
232
227
  if (fileSystemInfoEntry && fileSystemInfoEntry.timestamp) {
233
228
  // @ts-ignore
234
229
  return fileSystemInfoEntry.timestamp;
235
- } // @ts-ignore
236
-
230
+ }
237
231
 
232
+ // @ts-ignore
238
233
  return fileSystemInfoEntry;
239
234
  };
240
- /**
241
- * @param {string} filename
242
- * @param {null | FileSystemInfoEntry | "ignore" | undefined} fileSystemInfoEntry
243
- * @returns {boolean}
244
- */
245
-
246
235
 
236
+ /**
237
+ * @param {string} filename
238
+ * @param {null | FileSystemInfoEntry | "ignore" | undefined} fileSystemInfoEntry
239
+ * @returns {boolean}
240
+ */
247
241
  const hasFileChanged = (filename, fileSystemInfoEntry) => {
248
242
  const prevTimestamp = getTimestamps(this.prevTimestamps.get(filename));
249
243
  const timestamp = getTimestamps(fileSystemInfoEntry);
250
244
  return (prevTimestamp || this.startTime) < (timestamp || Infinity);
251
245
  };
252
-
253
246
  const changedFiles = [];
254
-
255
247
  for (const [filename, timestamp] of fileTimestamps.entries()) {
256
248
  if (hasFileChanged(filename, timestamp)) {
257
249
  changedFiles.push(filename);
258
250
  }
259
251
  }
260
-
261
252
  this.prevTimestamps = fileTimestamps;
262
253
  return changedFiles;
263
254
  }
264
-
265
255
  }
266
-
267
- var _default = StylelintWebpackPlugin;
268
- exports.default = _default;
256
+ module.exports = StylelintWebpackPlugin;