Skip to content

Commit

Permalink
chore: adjust early exit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 3, 2024
1 parent 2066010 commit 04d3e47
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<string>();
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);
};
Expand All @@ -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,
Expand Down

0 comments on commit 04d3e47

Please sign in to comment.