Skip to content

Commit

Permalink
fix: remove global placeholder file
Browse files Browse the repository at this point in the history
remove global placeholder fake file logic, as adding a fake file will cause
composite projects to always rebuild as the fake file is not
in the root files for the project build.

it's not viable to create a fake file to host the global types,
due to the fact tsc composite projects can't work in this format
without the file actually existing.
  • Loading branch information
blake-newman committed Apr 2, 2024
1 parent 5e521f2 commit f2b373a
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 122 deletions.
14 changes: 0 additions & 14 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,6 @@ export function baseCreate(
const vueLanguagePlugin = vue.createVueLanguagePlugin(
ts,
id => id,
fileName => {
if (ts.sys.useCaseSensitiveFileNames) {
return host.getScriptFileNames().includes(fileName) ?? false;
}
else {
const lowerFileName = fileName.toLowerCase();
for (const rootFile of host.getScriptFileNames()) {
if (rootFile.toLowerCase() === lowerFileName) {
return true;
}
}
return false;
}
},
host.getCompilationSettings(),
vueCompilerOptions,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/generators/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getSlotsPropertyName } from '../utils/shared';
export function generateGlobalTypes(vueCompilerOptions: VueCompilerOptions) {
const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${vueCompilerOptions.strictTemplates ? '' : ' & Record<string, unknown>'}`;
return `
; declare global {
;
// @ts-ignore
type __VLS_IntrinsicElements = __VLS_PickNotAny<import('vue/jsx-runtime').JSX.IntrinsicElements, __VLS_PickNotAny<JSX.IntrinsicElements, Record<string, any>>>;
// @ts-ignore
Expand Down Expand Up @@ -125,5 +125,5 @@ type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal<
>
>;
type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {};
}`;
`;
};
4 changes: 1 addition & 3 deletions packages/language-core/lib/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ export function* generate(
yield* generateScriptContentBeforeExportDefault();
yield* generateScriptSetupAndTemplate();
yield* generateScriptContentAfterExportDefault();
if (globalTypesHolder === fileName) {
yield _(generateGlobalTypes(vueCompilerOptions));
}
yield _(generateGlobalTypes(vueCompilerOptions));
yield* generateLocalHelperTypes();
yield _(`\ntype __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements;\n`);

Expand Down
24 changes: 6 additions & 18 deletions packages/language-core/lib/languageModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { VueGeneratedCode } from './virtualFile/vueFile';
import * as CompilerDOM from '@vue/compiler-dom';
import * as CompilerVue2 from './utils/vue2TemplateCompiler';

const normalFileRegistries: {
const fileRegistries: {
key: string;
plugins: VueLanguagePlugin[];
files: Map<string, VueGeneratedCode>;
}[] = [];
const holderFileRegistries: typeof normalFileRegistries = [];

function getVueFileRegistry(isGlobalTypesHolder: boolean, key: string, plugins: VueLanguagePlugin[]) {
const fileRegistries = isGlobalTypesHolder ? holderFileRegistries : normalFileRegistries;
function getVueFileRegistry(key: string, plugins: VueLanguagePlugin[]) {
let fileRegistry = fileRegistries.find(r =>
r.key === key
&& r.plugins.length === plugins.length
Expand Down Expand Up @@ -51,7 +49,6 @@ function getFileRegistryKey(
export function createVueLanguagePlugin(
ts: typeof import('typescript'),
getFileName: (fileId: string) => string,
isValidGlobalTypesHolder: (fileName: string) => boolean,
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
codegenStack: boolean = false,
Expand All @@ -70,7 +67,6 @@ export function createVueLanguagePlugin(
compilerOptions,
vueCompilerOptions,
codegenStack,
globalTypesHolder: undefined,
};
const plugins = getDefaultVueLanguagePlugins(pluginContext);

Expand All @@ -85,10 +81,10 @@ export function createVueLanguagePlugin(
createVirtualCode(fileId, languageId, snapshot) {
if (allowLanguageIds.has(languageId)) {
const fileName = getFileName(fileId);
if (!pluginContext.globalTypesHolder && isValidGlobalTypesHolder(fileName)) {
pluginContext.globalTypesHolder = fileName;
}
const fileRegistry = getFileRegistry(pluginContext.globalTypesHolder === fileName);
const fileRegistry = getVueFileRegistry(
getFileRegistryKey(compilerOptions, vueCompilerOptions, plugins),
vueCompilerOptions.plugins,
);
const code = fileRegistry.get(fileId);
if (code) {
code.update(snapshot);
Expand Down Expand Up @@ -160,12 +156,4 @@ export function createVueLanguagePlugin(
},
},
};

function getFileRegistry(isGlobalTypesHolder: boolean) {
return getVueFileRegistry(
isGlobalTypesHolder,
getFileRegistryKey(compilerOptions, vueCompilerOptions, plugins),
vueCompilerOptions.plugins,
);
}
}
1 change: 0 additions & 1 deletion packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export type VueLanguagePlugin = (ctx: {
compilerOptions: ts.CompilerOptions;
vueCompilerOptions: VueCompilerOptions;
codegenStack: boolean;
globalTypesHolder: string | undefined;
}) => {
version: typeof pluginVersion;
name?: string;
Expand Down
14 changes: 0 additions & 14 deletions packages/language-server/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,6 @@ connection.onInitialize(async params => {
const vueLanguagePlugin = createVueLanguagePlugin(
tsdk.typescript,
serviceEnv.typescript!.uriToFileName,
fileName => {
if (projectContext.typescript?.sys.useCaseSensitiveFileNames ?? false) {
return projectContext.typescript?.host.getScriptFileNames().includes(fileName) ?? false;
}
else {
const lowerFileName = fileName.toLowerCase();
for (const rootFile of projectContext.typescript?.host.getScriptFileNames() ?? []) {
if (rootFile.toLowerCase() === lowerFileName) {
return true;
}
}
return false;
}
},
commandLine?.options ?? {},
vueOptions,
options.codegenStack,
Expand Down
14 changes: 0 additions & 14 deletions packages/language-service/tests/utils/createTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ function createTester(rootUri: string) {
const vueLanguagePlugin = createVueLanguagePlugin(
ts,
serviceEnv.typescript!.uriToFileName,
fileName => {
if (ts.sys.useCaseSensitiveFileNames) {
return projectHost.getScriptFileNames().includes(fileName);
}
else {
const lowerFileName = fileName.toLowerCase();
for (const rootFile of projectHost.getScriptFileNames()) {
if (rootFile.toLowerCase() === lowerFileName) {
return true;
}
}
return false;
}
},
parsedCommandLine.options,
parsedCommandLine.vueOptions,
);
Expand Down
1 change: 0 additions & 1 deletion packages/language-service/tests/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const resolvedVueOptions = resolveVueCompilerOptions({});
const vueLanguagePlugin = createVueLanguagePlugin(
ts,
fileId => formatter.env.typescript!.uriToFileName(fileId),
() => false,
{},
resolvedVueOptions,
);
Expand Down
37 changes: 1 addition & 36 deletions packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { runTsc } from '@volar/typescript/lib/quickstart/runTsc';
import * as vue from '@vue/language-core';
import type * as ts from 'typescript';
import path from 'path';

const windowsPathReg = /\\/g;

Expand All @@ -17,15 +18,13 @@ export function run() {
const vueOptions = typeof configFilePath === 'string'
? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
: vue.resolveVueCompilerOptions({});
const fakeGlobalTypesHolder = createFakeGlobalTypesHolder(options);
if (
runExtensions.length === vueOptions.extensions.length
&& runExtensions.every(ext => vueOptions.extensions.includes(ext))
) {
const vueLanguagePlugin = vue.createVueLanguagePlugin(
ts,
id => id,
fileName => fileName === fakeGlobalTypesHolder,
options.options,
vueOptions,
false,
Expand Down Expand Up @@ -53,37 +52,3 @@ export function run() {
}
}
}

export function createFakeGlobalTypesHolder(options: ts.CreateProgramOptions) {
const firstVueFile = options.rootNames.find(fileName => fileName.endsWith('.vue'));
if (firstVueFile) {
const fakeFileName = firstVueFile + '__VLS_globalTypes.vue';

(options.rootNames as string[]).push(fakeFileName);

const fileExists = options.host!.fileExists.bind(options.host);
const readFile = options.host!.readFile.bind(options.host);
const writeFile = options.host!.writeFile.bind(options.host);

options.host!.fileExists = fileName => {
if (fileName.endsWith('__VLS_globalTypes.vue')) {
return true;
}
return fileExists(fileName);
};
options.host!.readFile = fileName => {
if (fileName.endsWith('__VLS_globalTypes.vue')) {
return '<script setup lang="ts"></script>';
}
return readFile(fileName);
};
options.host!.writeFile = (fileName, ...args) => {
if (fileName.endsWith('__VLS_globalTypes.vue.d.ts')) {
return;
}
return writeFile(fileName, ...args);
};

return fakeFileName.replace(windowsPathReg, '/');
}
}
5 changes: 0 additions & 5 deletions packages/tsc/tests/dts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('vue-tsc-dts', () => {
rootNames: readFilesRecursive(workspace),
options: compilerOptions
};
const fakeGlobalTypesHolder = createFakeGlobalTypesHolder(options);

let vueExts: string[] = [];
const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts, options) => {
Expand All @@ -36,7 +35,6 @@ describe('vue-tsc-dts', () => {
const vueLanguagePlugin = vue.createVueLanguagePlugin(
ts,
id => id,
fileName => fileName === fakeGlobalTypesHolder,
options.options,
vueOptions,
false,
Expand All @@ -52,9 +50,6 @@ describe('vue-tsc-dts', () => {

for (const intputFile of options.rootNames) {

if (intputFile.endsWith('__VLS_globalTypes.vue'))
continue;

const expectedOutputFile = intputFile.endsWith('.ts')
? intputFile.slice(0, -'.ts'.length) + '.d.ts'
: intputFile.endsWith('.tsx')
Expand Down
14 changes: 0 additions & 14 deletions packages/typescript-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ function createLanguageServicePlugin(): ts.server.PluginModuleFactory {
const languagePlugin = vue.createVueLanguagePlugin(
ts,
id => id,
fileName => {
if (info.languageServiceHost.useCaseSensitiveFileNames?.() ?? false) {
return externalFiles.get(info.project)?.has(fileName) ?? false;
}
else {
const lowerFileName = fileName.toLowerCase();
for (const externalFile of externalFiles.get(info.project) ?? []) {
if (externalFile.toLowerCase() === lowerFileName) {
return true;
}
}
return false;
}
},
info.languageServiceHost.getCompilationSettings(),
vueOptions,
);
Expand Down

0 comments on commit f2b373a

Please sign in to comment.