@@ -10,7 +10,9 @@ const { arrify, parseFiles, parseFoldersToGlobs } = require('./utils');
1010/** @typedef {import('./options').Options } Options */
1111
1212const ESLINT_PLUGIN = 'ESLintWebpackPlugin' ;
13- let counter = 0 ;
13+ const DEFAULT_FOLDER_TO_EXCLUDE = '**/node_modules/**' ;
14+
15+ let compilerId = 0 ;
1416
1517class ESLintWebpackPlugin {
1618 /**
@@ -29,26 +31,30 @@ class ESLintWebpackPlugin {
2931 apply ( compiler ) {
3032 // Generate key for each compilation,
3133 // this differentiates one from the other when being cached.
32- this . key = compiler . name || `${ this . key } _${ ( counter += 1 ) } ` ;
34+ this . key = compiler . name || `${ this . key } _${ ( compilerId += 1 ) } ` ;
35+
36+ const excludedFiles = parseFiles (
37+ this . options . exclude || [ ] ,
38+ this . getContext ( compiler )
39+ ) ;
40+ const resourceQueries = arrify ( this . options . resourceQueryExclude || [ ] ) ;
41+ const excludedResourceQueries = resourceQueries . map ( ( item ) =>
42+ item instanceof RegExp ? item : new RegExp ( item )
43+ ) ;
3344
3445 const options = {
3546 ...this . options ,
36- exclude : parseFiles (
37- this . options . exclude || [ ] ,
38- this . getContext ( compiler )
39- ) ,
47+ exclude : excludedFiles ,
48+ resourceQueryExclude : excludedResourceQueries ,
4049 extensions : arrify ( this . options . extensions ) ,
41- resourceQueryExclude : arrify ( this . options . resourceQueryExclude || [ ] ) . map (
42- ( item ) => ( item instanceof RegExp ? item : new RegExp ( item ) )
43- ) ,
4450 files : parseFiles ( this . options . files || '' , this . getContext ( compiler ) ) ,
4551 } ;
4652
53+ const foldersToExclude = this . options . exclude
54+ ? options . exclude
55+ : DEFAULT_FOLDER_TO_EXCLUDE ;
56+ const exclude = parseFoldersToGlobs ( foldersToExclude ) ;
4757 const wanted = parseFoldersToGlobs ( options . files , options . extensions ) ;
48- const exclude = parseFoldersToGlobs (
49- this . options . exclude ? options . exclude : '**/node_modules/**' ,
50- [ ]
51- ) ;
5258
5359 // If `lintDirtyModulesOnly` is disabled,
5460 // execute the linter on the build
@@ -58,15 +64,15 @@ class ESLintWebpackPlugin {
5864 ) ;
5965 }
6066
61- let isFirstRun = this . options . lintDirtyModulesOnly ;
67+ let hasCompilerRunByDirtyModule = this . options . lintDirtyModulesOnly ;
68+
6269 compiler . hooks . watchRun . tapPromise ( this . key , ( c ) => {
63- if ( isFirstRun ) {
64- isFirstRun = false ;
70+ if ( ! hasCompilerRunByDirtyModule )
71+ return this . run ( c , options , wanted , exclude ) ;
6572
66- return Promise . resolve ( ) ;
67- }
73+ hasCompilerRunByDirtyModule = false ;
6874
69- return this . run ( c , options , wanted , exclude ) ;
75+ return Promise . resolve ( ) ;
7076 } ) ;
7177 }
7278
@@ -77,13 +83,12 @@ class ESLintWebpackPlugin {
7783 * @param {string[] } exclude
7884 */
7985 async run ( compiler , options , wanted , exclude ) {
80- // Do not re-hook
81- if (
82- // @ts -ignore
83- compiler . hooks . compilation . taps . find ( ( { name } ) => name === this . key )
84- ) {
85- return ;
86- }
86+ // @ts -ignore
87+ const isCompilerHooked = compiler . hooks . compilation . taps . find (
88+ ( { name } ) => name === this . key
89+ ) ;
90+
91+ if ( isCompilerHooked ) return ;
8792
8893 compiler . hooks . compilation . tap ( this . key , ( compilation ) => {
8994 /** @type {import('./linter').Linter } */
@@ -106,30 +111,27 @@ class ESLintWebpackPlugin {
106111 // @ts -ignore
107112 // Add the file to be linted
108113 compilation . hooks . succeedModule . tap ( this . key , ( { resource } ) => {
109- if ( resource ) {
110- const [ file , query ] = resource . split ( '?' ) ;
111-
112- if (
113- file &&
114- ! files . includes ( file ) &&
115- isMatch ( file , wanted , { dot : true } ) &&
116- ! isMatch ( file , exclude , { dot : true } ) &&
117- options . resourceQueryExclude . every ( ( reg ) => ! reg . test ( query ) )
118- ) {
119- files . push ( file ) ;
120-
121- if ( threads > 1 ) {
122- lint ( file ) ;
123- }
124- }
114+ if ( ! resource ) return ;
115+
116+ const [ file , query ] = resource . split ( '?' ) ;
117+ const isFileNotListed = file && ! files . includes ( file ) ;
118+ const isFileWanted =
119+ isMatch ( file , wanted , { dot : true } ) &&
120+ ! isMatch ( file , exclude , { dot : true } ) ;
121+ const isQueryNotExclude = options . resourceQueryExclude . every (
122+ ( reg ) => ! reg . test ( query )
123+ ) ;
124+
125+ if ( isFileNotListed && isFileWanted && isQueryNotExclude ) {
126+ files . push ( file ) ;
127+
128+ if ( threads > 1 ) lint ( file ) ;
125129 }
126130 } ) ;
127131
128132 // Lint all files added
129133 compilation . hooks . finishModules . tap ( this . key , ( ) => {
130- if ( files . length > 0 && threads <= 1 ) {
131- lint ( files ) ;
132- }
134+ if ( files . length > 0 && threads <= 1 ) lint ( files ) ;
133135 } ) ;
134136
135137 // await and interpret results
@@ -141,22 +143,20 @@ class ESLintWebpackPlugin {
141143 if ( warnings && ! options . failOnWarning ) {
142144 // @ts -ignore
143145 compilation . warnings . push ( warnings ) ;
144- } else if ( warnings && options . failOnWarning ) {
146+ } else if ( warnings ) {
145147 // @ts -ignore
146148 compilation . errors . push ( warnings ) ;
147149 }
148150
149- if ( errors && options . failOnError ) {
150- // @ts -ignore
151- compilation . errors . push ( errors ) ;
152- } else if ( errors && ! options . failOnError ) {
151+ if ( errors && ! options . failOnError ) {
153152 // @ts -ignore
154153 compilation . warnings . push ( errors ) ;
154+ } else if ( errors ) {
155+ // @ts -ignore
156+ compilation . errors . push ( errors ) ;
155157 }
156158
157- if ( generateReportAsset ) {
158- await generateReportAsset ( compilation ) ;
159- }
159+ if ( generateReportAsset ) await generateReportAsset ( compilation ) ;
160160 }
161161 } ) ;
162162 }
@@ -167,15 +167,14 @@ class ESLintWebpackPlugin {
167167 * @returns {string }
168168 */
169169 getContext ( compiler ) {
170- if ( ! this . options . context ) {
171- return String ( compiler . options . context ) ;
172- }
170+ const compilerContext = String ( compiler . options . context ) ;
171+ const optionContext = this . options . context ;
173172
174- if ( ! isAbsolute ( this . options . context ) ) {
175- return join ( String ( compiler . options . context ) , this . options . context ) ;
176- }
173+ if ( ! optionContext ) return compilerContext ;
174+
175+ if ( isAbsolute ( optionContext ) ) return optionContext ;
177176
178- return this . options . context ;
177+ return join ( compilerContext , optionContext ) ;
179178 }
180179}
181180
0 commit comments