Skip to content

Commit 0cdd621

Browse files
fix: allow multiple instances (#92)
1 parent bb8750c commit 0cdd621

File tree

7 files changed

+81
-19
lines changed

7 files changed

+81
-19
lines changed

declarations/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare class ESLintWebpackPlugin {
77
* @param {Options} options
88
*/
99
constructor(options?: Options);
10+
key: string;
1011
options: import('./options').PluginOptions;
1112
/**
1213
* @param {Compiler} compiler
@@ -17,7 +18,6 @@ declare class ESLintWebpackPlugin {
1718
* @returns {void}
1819
*/
1920
apply(compiler: Compiler): void;
20-
key: string | undefined;
2121
/**
2222
*
2323
* @param {Compiler} compiler

src/cjs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const plugin = require('./index');
1+
const plugin = require('./');
22

33
module.exports = plugin.default;

src/index.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ESLintWebpackPlugin {
1919
* @param {Options} options
2020
*/
2121
constructor(options = {}) {
22+
this.key = ESLINT_PLUGIN;
2223
this.options = getOptions(options);
2324
this.run = this.run.bind(this);
2425
}
@@ -30,20 +31,20 @@ class ESLintWebpackPlugin {
3031
apply(compiler) {
3132
// Generate key for each compilation,
3233
// this differentiates one from the other when being cached.
33-
this.key = compiler.name || `${ESLINT_PLUGIN}_${(counter += 1)}`;
34+
this.key = compiler.name || `${this.key}_${(counter += 1)}`;
3435

3536
// If `lintDirtyModulesOnly` is disabled,
3637
// execute the linter on the build
3738
if (!this.options.lintDirtyModulesOnly) {
38-
compiler.hooks.run.tapPromise(ESLINT_PLUGIN, this.run);
39+
compiler.hooks.run.tapPromise(this.key, this.run);
3940
}
4041

4142
// TODO: Figure out want `compiler.watching` is and how to use it in Webpack5.
4243
// From my testing of compiler.watch() ... compiler.watching is always
4344
// undefined (webpack 4 doesn't define it either) I'm leaving it out
4445
// for now.
4546
let isFirstRun = this.options.lintDirtyModulesOnly;
46-
compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, (c) => {
47+
compiler.hooks.watchRun.tapPromise(this.key, (c) => {
4748
if (isFirstRun) {
4849
isFirstRun = false;
4950

@@ -61,10 +62,7 @@ class ESLintWebpackPlugin {
6162
// Do not re-hook
6263
if (
6364
// @ts-ignore
64-
compiler.hooks.thisCompilation.taps.find(
65-
// @ts-ignore
66-
({ name }) => name === ESLINT_PLUGIN
67-
)
65+
compiler.hooks.thisCompilation.taps.find(({ name }) => name === this.key)
6866
) {
6967
return;
7068
}
@@ -85,7 +83,7 @@ class ESLintWebpackPlugin {
8583
[]
8684
);
8785

88-
compiler.hooks.thisCompilation.tap(ESLINT_PLUGIN, (compilation) => {
86+
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
8987
/** @type {import('./linter').Linter} */
9088
let lint;
9189
/** @type {import('./linter').Reporter} */
@@ -103,7 +101,7 @@ class ESLintWebpackPlugin {
103101

104102
// @ts-ignore
105103
// Add the file to be linted
106-
compilation.hooks.succeedModule.tap(ESLINT_PLUGIN, ({ resource }) => {
104+
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
107105
if (resource) {
108106
const [file] = resource.split('?');
109107

@@ -119,17 +117,14 @@ class ESLintWebpackPlugin {
119117
});
120118

121119
// Lint all files added
122-
compilation.hooks.finishModules.tap(ESLINT_PLUGIN, () => {
120+
compilation.hooks.finishModules.tap(this.key, () => {
123121
if (files.length > 0) {
124122
lint(files);
125123
}
126124
});
127125

128126
// await and interpret results
129-
compilation.hooks.additionalAssets.tapPromise(
130-
ESLINT_PLUGIN,
131-
processResults
132-
);
127+
compilation.hooks.additionalAssets.tapPromise(this.key, processResults);
133128

134129
async function processResults() {
135130
const { errors, warnings, generateReportAsset } = await report();

test/empty.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { join } from 'path';
22

33
import webpack from 'webpack';
44

5-
import ESLintPlugin from '../src/index';
5+
import ESLintPlugin from '../src';
66

77
describe('empty', () => {
88
it('no error when no files matching', (done) => {
99
const compiler = webpack({
1010
context: join(__dirname, 'fixtures', 'empty'),
1111
mode: 'development',
12-
entry: '../index',
12+
entry: '../',
1313
plugins: [new ESLintPlugin()],
1414
});
1515

test/fixtures/multiple-entry.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./good');
2+
require('./error');

test/multiple-instances.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import ESLintPlugin from '../src';
2+
3+
import pack from './utils/pack';
4+
5+
describe('multiple instances', () => {
6+
it("should don't fail", (done) => {
7+
const compiler = pack(
8+
'multiple',
9+
{},
10+
{
11+
plugins: [
12+
new ESLintPlugin({ ignore: false, exclude: 'error.js' }),
13+
new ESLintPlugin({ ignore: false, exclude: 'error.js' }),
14+
],
15+
}
16+
);
17+
18+
compiler.run((err, stats) => {
19+
expect(err).toBeNull();
20+
expect(stats.hasWarnings()).toBe(false);
21+
expect(stats.hasErrors()).toBe(false);
22+
done();
23+
});
24+
});
25+
26+
it('should fail on first instance', (done) => {
27+
const compiler = pack(
28+
'multiple',
29+
{},
30+
{
31+
plugins: [
32+
new ESLintPlugin({ ignore: false, exclude: 'good.js' }),
33+
new ESLintPlugin({ ignore: false, exclude: 'error.js' }),
34+
],
35+
}
36+
);
37+
38+
compiler.run((err, stats) => {
39+
expect(err).toBeNull();
40+
expect(stats.hasWarnings()).toBe(false);
41+
expect(stats.hasErrors()).toBe(true);
42+
done();
43+
});
44+
});
45+
46+
it('should fail on second instance', (done) => {
47+
const compiler = pack(
48+
'multiple',
49+
{},
50+
{
51+
plugins: [
52+
new ESLintPlugin({ ignore: false, exclude: 'error.js' }),
53+
new ESLintPlugin({ ignore: false, exclude: 'good.js' }),
54+
],
55+
}
56+
);
57+
58+
compiler.run((err, stats) => {
59+
expect(err).toBeNull();
60+
expect(stats.hasWarnings()).toBe(false);
61+
expect(stats.hasErrors()).toBe(true);
62+
done();
63+
});
64+
});
65+
});

test/utils/conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'path';
22

3-
import ESLintPlugin from '../../src/index';
3+
import ESLintPlugin from '../../src';
44

55
export default (entry, pluginConf = {}, webpackConf = {}) => {
66
const testDir = join(__dirname, '..');

0 commit comments

Comments
 (0)