Skip to content

Commit 92f25ec

Browse files
facugaichalexander-akaitricardogobbosouza
authored
fix: lint modules that are cached with webpack's filesystem cache (#197)
If webpack is setup with cache type `filesystem`, the `succeedModule` hook is not called for cached modules and no linting is run for them. Tap the `stillValidModule` hook to lint cached modules. Fixes #130 Co-authored-by: Alexander Akait <[email protected]> Co-authored-by: Ricardo Gobbo de Souza <[email protected]>
1 parent ebce9be commit 92f25ec

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const linter = require('./linter');
77
const { arrify, parseFiles, parseFoldersToGlobs } = require('./utils');
88

99
/** @typedef {import('webpack').Compiler} Compiler */
10+
/** @typedef {import('webpack').Module} Module */
11+
/** @typedef {import('webpack').NormalModule} NormalModule */
1012
/** @typedef {import('./options').Options} Options */
1113

1214
const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
@@ -108,9 +110,16 @@ class ESLintWebpackPlugin {
108110
/** @type {string[]} */
109111
const files = [];
110112

111-
// @ts-ignore
112113
// Add the file to be linted
113-
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
114+
compilation.hooks.succeedModule.tap(this.key, addFile);
115+
compilation.hooks.stillValidModule.tap(this.key, addFile);
116+
117+
/**
118+
* @param {Module} module
119+
*/
120+
function addFile(module) {
121+
const { resource } = /** @type {NormalModule} */ (module);
122+
114123
if (!resource) return;
115124

116125
const [file, query] = resource.split('?');
@@ -127,7 +136,7 @@ class ESLintWebpackPlugin {
127136

128137
if (threads > 1) lint(file);
129138
}
130-
});
139+
}
131140

132141
// Lint all files added
133142
compilation.hooks.finishModules.tap(this.key, () => {

test/cached.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { join } from 'path';
2+
3+
import { removeSync } from 'fs-extra';
4+
5+
import webpack from 'webpack';
6+
7+
import conf from './utils/conf';
8+
9+
describe('error (cached module)', () => {
10+
const cacheLocation = join(__dirname, 'cache');
11+
12+
beforeEach(() => {
13+
removeSync(cacheLocation);
14+
});
15+
16+
afterAll(() => {
17+
removeSync(cacheLocation);
18+
});
19+
20+
it('should return error even if module is cached', (done) => {
21+
const config = conf('error');
22+
config.cache = {
23+
type: 'filesystem',
24+
idleTimeout: 0,
25+
idleTimeoutAfterLargeChanges: 0,
26+
idleTimeoutForInitialStore: 0,
27+
cacheLocation,
28+
};
29+
30+
const c1 = webpack(config);
31+
32+
c1.run((err1, stats1) => {
33+
expect(err1).toBeNull();
34+
expect(stats1.hasWarnings()).toBe(false);
35+
expect(stats1.hasErrors()).toBe(true);
36+
37+
c1.close(() => {
38+
const c2 = webpack(config);
39+
c2.run((err2, stats2) => {
40+
expect(err2).toBeNull();
41+
expect(stats2.hasWarnings()).toBe(false);
42+
expect(stats2.hasErrors()).toBe(true);
43+
44+
done();
45+
});
46+
});
47+
});
48+
});
49+
});

types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ declare class ESLintWebpackPlugin {
3333
getContext(compiler: Compiler): string;
3434
}
3535
declare namespace ESLintWebpackPlugin {
36-
export { Compiler, Options };
36+
export { Compiler, Module, NormalModule, Options };
3737
}
3838
type Compiler = import('webpack').Compiler;
3939
type Options = import('./options').Options;
40+
type Module = import('webpack').Module;
41+
type NormalModule = import('webpack').NormalModule;

0 commit comments

Comments
 (0)