From ae15b6d6fd283a1eb6f9e2329df2c209d5a9e3cd Mon Sep 17 00:00:00 2001 From: azu Date: Fri, 3 Jan 2025 16:58:01 +0900 Subject: [PATCH] perf: cache ESLint instance for config --- src/textlint-rule-eslint.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/textlint-rule-eslint.ts b/src/textlint-rule-eslint.ts index eb29d7e..3df8a07 100644 --- a/src/textlint-rule-eslint.ts +++ b/src/textlint-rule-eslint.ts @@ -26,6 +26,22 @@ export type Options = { configFile: string; langs: string[]; }; +const createESLint = () => { + const cache = new Map(); + return function getESLint(esLintConfigFilePath: string) { + const cachedESLint = cache.get(esLintConfigFilePath); + if (cachedESLint) { + return cachedESLint; + } + const engine = new ESLint({ + overrideConfigFile: esLintConfigFilePath, + ignore: false + }); + cache.set(esLintConfigFilePath, engine); + return engine; + }; +}; +const getESLint = createESLint(); const reporter: TextlintRuleModule = (context, options) => { const { Syntax, RuleError, report, fixer, getSource, locator } = context; if (!options) { @@ -37,10 +53,7 @@ const reporter: TextlintRuleModule = (context, options) => { const availableLang = options.langs || defaultOptions.langs; const textlintRCDir = getConfigBaseDir(context); const esLintConfigFilePath = textlintRCDir ? path.resolve(textlintRCDir, options.configFile) : options.configFile; - const engine = new ESLint({ - overrideConfigFile: esLintConfigFilePath, - ignore: false - }); + const engine = getESLint(esLintConfigFilePath); return { async [Syntax.CodeBlock](node) { if (!node.lang) {