From b685836f3247fc03841b2da47d9166429747cfa8 Mon Sep 17 00:00:00 2001 From: Blake Newman Date: Wed, 3 Apr 2024 00:30:07 +0100 Subject: [PATCH] perf(tsc): store the global types basename Store the global types basename to only apply the removal of global emits when needed. Otherwise this will cause unnessary searches of file contents which is slow with large amount of emits. --- packages/tsc/index.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/tsc/index.ts b/packages/tsc/index.ts index db0b354fa2..05619efbbf 100644 --- a/packages/tsc/index.ts +++ b/packages/tsc/index.ts @@ -1,5 +1,7 @@ import { runTsc } from '@volar/typescript/lib/quickstart/runTsc'; import * as vue from '@vue/language-core'; +import * as path from 'path' +import type * as ts from 'typescript'; const windowsPathReg = /\\/g; @@ -11,15 +13,18 @@ export function run() { const main = () => runTsc( require.resolve('typescript/lib/tsc'), runExtensions, - (ts, options) => { + (ts, options: ts.CreateProgramOptions) => { const { configFilePath } = options.options; const vueOptions = typeof configFilePath === 'string' ? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions : vue.resolveVueCompilerOptions({}); + let globalTypesBaseName: undefined | string; const writeFile = options.host!.writeFile.bind(options.host); - options.host!.writeFile = (fileName, data, ...args) => { - data = removeEmitGlobalTypes(data); - writeFile(fileName, data, ...args); + options.host!.writeFile = (fileName, contents, ...args) => { + if (globalTypesBaseName && fileName.toLowerCase().endsWith(`${globalTypesBaseName}.d.ts`)) { + return writeFile(fileName, removeEmitGlobalTypes(contents), ...args); + } + return writeFile(fileName, contents, ...args); }; if ( runExtensions.length === vueOptions.extensions.length @@ -30,13 +35,22 @@ export function run() { id => id, fileName => { const rootFileNames = options.rootNames.map(rootName => rootName.replace(windowsPathReg, '/')); + if (globalTypesBaseName) { + return globalTypesBaseName === path.basename(fileName).toLowerCase(); + } + if (options.host?.useCaseSensitiveFileNames?.()) { - return rootFileNames.includes(fileName); + if (rootFileNames.includes(fileName)) { + globalTypesBaseName = path.basename(fileName).toLowerCase(); + return true + }; + return false; } else { const lowerFileName = fileName.toLowerCase(); - for (const rootFileName of rootFileNames) { - if (rootFileName.toLowerCase() === lowerFileName) { + for (const rootFile of rootFileNames) { + if (rootFile.toLowerCase() === lowerFileName) { + globalTypesBaseName = path.basename(lowerFileName); return true; } }