Skip to content

Commit

Permalink
fix: lint modules that are cached with webpack's filesystem cache
Browse files Browse the repository at this point in the history
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 webpack-contrib#130
  • Loading branch information
facugaich committed Dec 28, 2022
1 parent 790a1cb commit cfb2ec8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const linter = require('./linter');
const { arrify, parseFiles, parseFoldersToGlobs } = require('./utils');

/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('webpack').Module} Module */
/** @typedef {import('webpack').NormalModule} NormalModule */
/** @typedef {import('./options').Options} Options */

const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
Expand Down Expand Up @@ -103,9 +105,16 @@ class ESLintWebpackPlugin {
/** @type {string[]} */
const files = [];

// @ts-ignore
// Add the file to be linted
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
compilation.hooks.succeedModule.tap(this.key, addFile);
compilation.hooks.stillValidModule.tap(this.key, addFile);

/**
* @param {Module} module
*/
function addFile(module) {
const { resource } = /** @type {NormalModule} */ (module);

if (resource) {
const [file, query] = resource.split('?');

Expand All @@ -123,7 +132,7 @@ class ESLintWebpackPlugin {
}
}
}
});
}

// Lint all files added
compilation.hooks.finishModules.tap(this.key, () => {
Expand Down
49 changes: 49 additions & 0 deletions test/cached.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { join } from 'path';

import { removeSync } from 'fs-extra';

import webpack from 'webpack';

import conf from './utils/conf';

describe('error (cached module)', () => {
const cacheLocation = join(__dirname, 'cache');

beforeEach(() => {
removeSync(cacheLocation);
});

afterAll(() => {
removeSync(cacheLocation);
});

it('should return error even if module is cached', (done) => {
const config = conf('error');
config.cache = {
type: 'filesystem',
idleTimeout: 0,
idleTimeoutAfterLargeChanges: 0,
idleTimeoutForInitialStore: 0,
cacheLocation,
};

const c1 = webpack(config);

c1.run((err1, stats1) => {
expect(err1).toBeNull();
expect(stats1.hasWarnings()).toBe(false);
expect(stats1.hasErrors()).toBe(true);

c1.close(() => {
const c2 = webpack(config);
c2.run((err2, stats2) => {
expect(err2).toBeNull();
expect(stats2.hasWarnings()).toBe(false);
expect(stats2.hasErrors()).toBe(true);

done();
});
});
});
});
});
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ declare class ESLintWebpackPlugin {
getContext(compiler: Compiler): string;
}
declare namespace ESLintWebpackPlugin {
export { Compiler, Options };
export { Compiler, Module, NormalModule, Options };
}
type Compiler = import('webpack').Compiler;
type Options = import('./options').Options;
type Module = import('webpack').Module;
type NormalModule = import('webpack').NormalModule;

0 comments on commit cfb2ec8

Please sign in to comment.