Skip to content

Commit

Permalink
perf(tsc): store the global types basename
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
blake-newman committed Apr 2, 2024
1 parent 36fdab3 commit b685836
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -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;
}
}
Expand Down

0 comments on commit b685836

Please sign in to comment.