Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: threads multi-compiler #69

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/getESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ export function loadESLint(options) {
}

/**
* @param {string} key
* @param {number} poolSize
* @param {Options} options
* @returns {Linter}
*/
export function loadESLintThreaded(poolSize, options) {
const key = getCacheKey(options);
export function loadESLintThreaded(key, poolSize, options) {
const cacheKey = getCacheKey(key, options);
const { eslintPath = 'eslint' } = options;
const source = require.resolve('./worker');
const workerOptions = {
Expand All @@ -74,7 +75,7 @@ export function loadESLintThreaded(poolSize, options) {
(worker && (await worker.lintFiles(files))) ||
/* istanbul ignore next */ [],
cleanup: async () => {
cache[key] = local;
cache[cacheKey] = local;
context.lintFiles = (files) => local.lintFiles(files);
if (worker) {
worker.end();
Expand All @@ -87,10 +88,11 @@ export function loadESLintThreaded(poolSize, options) {
}

/**
* @param {string} key
* @param {Options} options
* @returns {Linter}
*/
export default function getESLint({ threads, ...options }) {
export default function getESLint(key, { threads, ...options }) {
const max =
typeof threads !== 'number'
? threads
Expand All @@ -99,18 +101,19 @@ export default function getESLint({ threads, ...options }) {
: /* istanbul ignore next */
threads;

const key = getCacheKey({ threads, ...options });
if (!cache[key]) {
cache[key] =
max > 1 ? loadESLintThreaded(max, options) : loadESLint(options);
const cacheKey = getCacheKey(key, { threads, ...options });
if (!cache[cacheKey]) {
cache[cacheKey] =
max > 1 ? loadESLintThreaded(key, max, options) : loadESLint(options);
}
return cache[key];
return cache[cacheKey];
}

/**
* @param {string} key
* @param {Options} options
* @returns {string}
*/
function getCacheKey(options) {
return JSON.stringify(options, jsonStringifyReplacerSortKeys);
function getCacheKey(key, options) {
return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys);
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class ESLintWebpackPlugin {
* @param {Options} options
*/
constructor(options = {}) {
this.key = Math.random().toString();
ricardogobbosouza marked this conversation as resolved.
Show resolved Hide resolved
this.options = getOptions(options);
this.run = this.run.bind(this);
}
Expand Down Expand Up @@ -72,7 +73,7 @@ export class ESLintWebpackPlugin {
let report;

try {
({ lint, report } = linter(options, compilation));
({ lint, report } = linter(this.key, options, compilation));
} catch (e) {
compilation.errors.push(e);
return;
Expand Down
5 changes: 3 additions & 2 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import getESLint from './getESLint';
const resultStorage = new WeakMap();

/**
* @param {string} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{lint: Linter, report: Reporter}}
*/
export default function linter(options, compilation) {
export default function linter(key, options, compilation) {
/** @type {ESLint} */
let eslint;

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

try {
({ eslint, lintFiles, cleanup } = getESLint(options));
({ eslint, lintFiles, cleanup } = getESLint(key, options));
} catch (e) {
throw new ESLintError(e.message);
}
Expand Down
4 changes: 2 additions & 2 deletions test/threads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { loadESLint, loadESLintThreaded } from '../src/getESLint';
describe('Threading', () => {
test('Threaded interface should look like non-threaded interface', async () => {
const single = loadESLint({});
const threaded = loadESLintThreaded(1, {});
const threaded = loadESLintThreaded('foo', 1, {});
for (const key of Object.keys(single)) {
expect(typeof single[key]).toEqual(typeof threaded[key]);
}
Expand All @@ -21,7 +21,7 @@ describe('Threading', () => {
});

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