Skip to content

Commit a6f49ef

Browse files
fix: threads multi-compiler
1 parent f819203 commit a6f49ef

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

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} 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} 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} 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class ESLintWebpackPlugin {
1717
* @param {Options} options
1818
*/
1919
constructor(options = {}) {
20+
this.key = Math.random().toString();
2021
this.options = getOptions(options);
2122
this.run = this.run.bind(this);
2223
}
@@ -72,7 +73,7 @@ export class ESLintWebpackPlugin {
7273
let report;
7374

7475
try {
75-
({ lint, report } = linter(options, compilation));
76+
({ lint, report } = linter(this.key, options, compilation));
7677
} catch (e) {
7778
compilation.errors.push(e);
7879
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} 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)