Skip to content

Commit cef4f74

Browse files
fix: threads multi-compiler (#69)
* fix: threads multi-compiler * fix: set key in apply
1 parent a67235f commit cef4f74

File tree

7 files changed

+41
-18
lines changed

7 files changed

+41
-18
lines changed

declarations/getESLint.d.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
1111
*/
1212
export function loadESLint(options: Options): Linter;
1313
/**
14+
* @param {string|undefined} key
1415
* @param {number} poolSize
1516
* @param {Options} options
1617
* @returns {Linter}
1718
*/
18-
export function loadESLintThreaded(poolSize: number, options: Options): Linter;
19+
export function loadESLintThreaded(
20+
key: string | undefined,
21+
poolSize: number,
22+
options: Options
23+
): Linter;
1924
/**
25+
* @param {string|undefined} key
2026
* @param {Options} options
2127
* @returns {Linter}
2228
*/
23-
export default function getESLint({ threads, ...options }: Options): Linter;
29+
export default function getESLint(
30+
key: string | undefined,
31+
{ threads, ...options }: Options
32+
): Linter;
2433
export type ESLint = import('eslint').ESLint;
2534
export type LintResult = import('eslint').ESLint.LintResult;
2635
export type Options = import('./options').PluginOptions &

declarations/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class ESLintWebpackPlugin {
1313
* @returns {void}
1414
*/
1515
apply(compiler: Compiler): void;
16+
key: string | undefined;
1617
/**
1718
*
1819
* @param {Compiler} compiler

declarations/linter.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/**
2+
* @param {string|undefined} key
23
* @param {Options} options
34
* @param {Compilation} compilation
45
* @returns {{lint: Linter, report: Reporter}}
56
*/
67
export default function linter(
8+
key: string | undefined,
79
options: Options,
810
compilation: Compilation
911
): {

src/getESLint.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ export function loadESLint(options) {
4646
}
4747

4848
/**
49+
* @param {string|undefined} key
4950
* @param {number} poolSize
5051
* @param {Options} options
5152
* @returns {Linter}
5253
*/
53-
export function loadESLintThreaded(poolSize, options) {
54-
const key = getCacheKey(options);
54+
export function loadESLintThreaded(key, poolSize, options) {
55+
const cacheKey = getCacheKey(key, options);
5556
const { eslintPath = 'eslint' } = options;
5657
const source = require.resolve('./worker');
5758
const workerOptions = {
@@ -74,7 +75,7 @@ export function loadESLintThreaded(poolSize, options) {
7475
(worker && (await worker.lintFiles(files))) ||
7576
/* istanbul ignore next */ [],
7677
cleanup: async () => {
77-
cache[key] = local;
78+
cache[cacheKey] = local;
7879
context.lintFiles = (files) => local.lintFiles(files);
7980
if (worker) {
8081
worker.end();
@@ -87,10 +88,11 @@ export function loadESLintThreaded(poolSize, options) {
8788
}
8889

8990
/**
91+
* @param {string|undefined} key
9092
* @param {Options} options
9193
* @returns {Linter}
9294
*/
93-
export default function getESLint({ threads, ...options }) {
95+
export default function getESLint(key, { threads, ...options }) {
9496
const max =
9597
typeof threads !== 'number'
9698
? threads
@@ -99,18 +101,19 @@ export default function getESLint({ threads, ...options }) {
99101
: /* istanbul ignore next */
100102
threads;
101103

102-
const key = getCacheKey({ threads, ...options });
103-
if (!cache[key]) {
104-
cache[key] =
105-
max > 1 ? loadESLintThreaded(max, options) : loadESLint(options);
104+
const cacheKey = getCacheKey(key, { threads, ...options });
105+
if (!cache[cacheKey]) {
106+
cache[cacheKey] =
107+
max > 1 ? loadESLintThreaded(key, max, options) : loadESLint(options);
106108
}
107-
return cache[key];
109+
return cache[cacheKey];
108110
}
109111

110112
/**
113+
* @param {string|undefined} key
111114
* @param {Options} options
112115
* @returns {string}
113116
*/
114-
function getCacheKey(options) {
115-
return JSON.stringify(options, jsonStringifyReplacerSortKeys);
117+
function getCacheKey(key, options) {
118+
return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys);
116119
}

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { parseFiles, parseFoldersToGlobs } from './utils';
1111
/** @typedef {import('./options').Options} Options */
1212

1313
const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
14+
let counter = 0;
1415

1516
export class ESLintWebpackPlugin {
1617
/**
@@ -26,6 +27,12 @@ export class ESLintWebpackPlugin {
2627
* @returns {void}
2728
*/
2829
apply(compiler) {
30+
// Generate key for each compilation,
31+
// this differentiates one from the other when being cached.
32+
this.key = compiler.name || `${ESLINT_PLUGIN}_${(counter += 1)}`;
33+
34+
// If `lintDirtyModulesOnly` is disabled,
35+
// execute the linter on the build
2936
if (!this.options.lintDirtyModulesOnly) {
3037
compiler.hooks.run.tapPromise(ESLINT_PLUGIN, this.run);
3138
}
@@ -81,7 +88,7 @@ export class ESLintWebpackPlugin {
8188
let report;
8289

8390
try {
84-
({ lint, report } = linter(options, compilation));
91+
({ lint, report } = linter(this.key, options, compilation));
8592
} catch (e) {
8693
compilation.errors.push(e);
8794
return;

src/linter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import getESLint from './getESLint';
2121
const resultStorage = new WeakMap();
2222

2323
/**
24+
* @param {string|undefined} key
2425
* @param {Options} options
2526
* @param {Compilation} compilation
2627
* @returns {{lint: Linter, report: Reporter}}
2728
*/
28-
export default function linter(options, compilation) {
29+
export default function linter(key, options, compilation) {
2930
/** @type {ESLint} */
3031
let eslint;
3132

@@ -41,7 +42,7 @@ export default function linter(options, compilation) {
4142
const crossRunResultStorage = getResultStorage(compilation);
4243

4344
try {
44-
({ eslint, lintFiles, cleanup } = getESLint(options));
45+
({ eslint, lintFiles, cleanup } = getESLint(key, options));
4546
} catch (e) {
4647
throw new ESLintError(e.message);
4748
}

test/threads.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { loadESLint, loadESLintThreaded } from '../src/getESLint';
66
describe('Threading', () => {
77
test('Threaded interface should look like non-threaded interface', async () => {
88
const single = loadESLint({});
9-
const threaded = loadESLintThreaded(1, {});
9+
const threaded = loadESLintThreaded('foo', 1, {});
1010
for (const key of Object.keys(single)) {
1111
expect(typeof single[key]).toEqual(typeof threaded[key]);
1212
}
@@ -21,7 +21,7 @@ describe('Threading', () => {
2121
});
2222

2323
test('Threaded should lint files', async () => {
24-
const threaded = loadESLintThreaded(1, { ignore: false });
24+
const threaded = loadESLintThreaded('bar', 1, { ignore: false });
2525
try {
2626
const [good, bad] = await Promise.all([
2727
threaded.lintFiles(join(__dirname, 'fixtures/good.js')),

0 commit comments

Comments
 (0)