@@ -3,7 +3,7 @@ import { isAbsolute, join } from 'path';
33// @ts -ignore
44import arrify from 'arrify' ;
55// @ts -ignore
6- import fastGlob from 'fast-glob ' ;
6+ import globby from 'globby ' ;
77import { isMatch } from 'micromatch' ;
88
99import { getOptions } from './options' ;
@@ -73,16 +73,19 @@ class StylelintWebpackPlugin {
7373 const context = this . getContext ( compiler ) ;
7474 const options = {
7575 ...this . options ,
76- exclude : parseFiles ( this . options . exclude || [ ] , context ) ,
76+ exclude : parseFiles (
77+ this . options . exclude || [
78+ '**/node_modules/**' ,
79+ compiler . options . output . path ,
80+ ] ,
81+ context
82+ ) ,
7783 extensions : arrify ( this . options . extensions ) ,
7884 files : parseFiles ( this . options . files || '' , context ) ,
7985 } ;
8086
8187 const wanted = parseFoldersToGlobs ( options . files , options . extensions ) ;
82- const exclude = parseFoldersToGlobs (
83- this . options . exclude ? options . exclude : '**/node_modules/**' ,
84- [ ]
85- ) ;
88+ const exclude = parseFoldersToGlobs ( options . exclude ) ;
8689
8790 compiler . hooks . thisCompilation . tap ( this . key , ( compilation ) => {
8891 /** @type {import('./linter').Linter } */
@@ -99,30 +102,14 @@ class StylelintWebpackPlugin {
99102 return ;
100103 }
101104
102- /** @type {string[] } */
103- const files = [ ] ;
104-
105- // Add the file to be linted
106- compilation . hooks . succeedModule . tap ( this . key , ( module ) => {
107- const filteredFiles = this . getFiles ( wanted , compiler , module ) . filter (
108- ( file ) =>
109- ! files . includes ( file ) &&
110- isMatch ( file , wanted , { dot : true } ) &&
111- ! isMatch ( file , exclude , { dot : true } )
112- ) ;
113-
114- for ( const file of filteredFiles ) {
115- files . push ( file ) ;
105+ compilation . hooks . finishModules . tap ( this . key , ( ) => {
106+ const files = this . getFiles ( compiler , wanted , exclude ) ;
116107
117- if ( threads > 1 ) {
108+ if ( threads > 1 ) {
109+ for ( const file of files ) {
118110 lint ( parseFiles ( file , context ) ) ;
119111 }
120- }
121- } ) ;
122-
123- // Lint all files added
124- compilation . hooks . finishModules . tap ( this . key , ( ) => {
125- if ( files . length > 0 && threads <= 1 ) {
112+ } else if ( files . length > 0 ) {
126113 lint ( parseFiles ( files , context ) ) ;
127114 }
128115 } ) ;
@@ -174,60 +161,33 @@ class StylelintWebpackPlugin {
174161 }
175162
176163 /**
177- * @param {string[] } glob
178164 * @param {Compiler } compiler
179- * @param {Module } module
165+ * @param {string[] } wanted
166+ * @param {string[] } exclude
180167 * @returns {string[] }
181168 */
182169 // eslint-disable-next-line no-unused-vars
183- getFiles ( glob , compiler , module ) {
184- // TODO: how to get module dependencies on css files?
185- // maybe implemented on next major version v3
186- // Temporaly lint all css files on start webpack
187- // on watch lint only modified files
188- // on webpack 5 not safe to use `module.buildInfo.snapshot`
189- // on webpack `module.buildInfo.fileDependencies` not working correclty
190-
191- // webpack 5
192- /*
193- if (
194- module.buildInfo &&
195- module.buildInfo.snapshot &&
196- module.buildInfo.snapshot.fileTimestamps
197- ) {
198- files = this.getChangedFiles(module.buildInfo.snapshot.fileTimestamps);
199- }
200-
201- // webpack 4
202- else if (module.buildInfo && module.buildInfo.fileDependencies) {
203- files = Array.from(module.buildInfo.fileDependencies);
204-
205- if (compiler.fileTimestamps && compiler.fileTimestamps.size > 0) {
206- const fileDependencies = new Map();
207-
208- for (const [filename, timestamp] of compiler.fileTimestamps.entries()) {
209- if (files.includes(filename)) {
210- fileDependencies.set(filename, timestamp);
211- }
212- }
213-
214- files = this.getChangedFiles(fileDependencies);
215- }
216- }
217- */
218-
170+ getFiles ( compiler , wanted , exclude ) {
219171 // webpack 5
220172 if ( compiler . modifiedFiles ) {
221- return Array . from ( compiler . modifiedFiles ) ;
173+ return Array . from ( compiler . modifiedFiles ) . filter (
174+ ( file ) =>
175+ isMatch ( file , wanted , { dot : true } ) &&
176+ ! isMatch ( file , exclude , { dot : true } )
177+ ) ;
222178 }
223179
224180 // webpack 4
225181 /* istanbul ignore next */
226182 if ( compiler . fileTimestamps && compiler . fileTimestamps . size > 0 ) {
227- return this . getChangedFiles ( compiler . fileTimestamps ) ;
183+ return this . getChangedFiles ( compiler . fileTimestamps ) . filter (
184+ ( file ) =>
185+ isMatch ( file , wanted , { dot : true } ) &&
186+ ! isMatch ( file , exclude , { dot : true } )
187+ ) ;
228188 }
229189
230- return fastGlob . sync ( glob , { dot : true } ) ;
190+ return globby . sync ( wanted , { dot : true , ignore : exclude } ) ;
231191 }
232192
233193 /**
0 commit comments