From 04d3e475f02055a93c0eb9f7f17e1d346f721b01 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Wed, 3 Apr 2024 15:46:14 +0800 Subject: [PATCH] chore: adjust early exit logic --- packages/tsc/index.ts | 44 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/packages/tsc/index.ts b/packages/tsc/index.ts index ef04ee560c..759980b6ca 100644 --- a/packages/tsc/index.ts +++ b/packages/tsc/index.ts @@ -1,6 +1,5 @@ import { runTsc } from '@volar/typescript/lib/quickstart/runTsc'; import * as vue from '@vue/language-core'; -import * as path from 'path' const windowsPathReg = /\\/g; @@ -17,11 +16,22 @@ export function run() { 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); + const getCanonicalFileName = options.host?.useCaseSensitiveFileNames?.() + ? (fileName: string) => fileName + : (fileName: string) => fileName.toLowerCase(); + const canonicalRootFileNames = new Set( + options.rootNames + .map(rootName => rootName.replace(windowsPathReg, '/')) + .map(getCanonicalFileName) + ); + const canonicalGlobalTypesHolderFileNames = new Set(); options.host!.writeFile = (fileName, contents, ...args) => { - if (globalTypesBaseName && fileName.toLowerCase().endsWith(`${globalTypesBaseName}.d.ts`)) { - return writeFile(fileName, removeEmitGlobalTypes(contents), ...args); + if ( + fileName.endsWith('.d.ts') + && canonicalGlobalTypesHolderFileNames.has(getCanonicalFileName(fileName.replace(windowsPathReg, '/')).slice(0, -5)) + ) { + contents = removeEmitGlobalTypes(contents); } return writeFile(fileName, contents, ...args); }; @@ -33,28 +43,12 @@ export function run() { ts, id => id, fileName => { - const rootFileNames = options.rootNames.map(rootName => rootName.replace(windowsPathReg, '/')); - if (globalTypesBaseName) { - return globalTypesBaseName === path.basename(fileName).toLowerCase(); - } - - if (options.host?.useCaseSensitiveFileNames?.()) { - if (rootFileNames.includes(fileName)) { - globalTypesBaseName = path.basename(fileName).toLowerCase(); - return true - }; - return false; - } - else { - const lowerFileName = fileName.toLowerCase(); - for (const rootFile of rootFileNames) { - if (rootFile.toLowerCase() === lowerFileName) { - globalTypesBaseName = path.basename(lowerFileName); - return true; - } - } - return false; + fileName = getCanonicalFileName(fileName); + canonicalGlobalTypesHolderFileNames.add(fileName); + if (canonicalRootFileNames.has(fileName)) { + return true; } + return false; }, options.options, vueOptions,