Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions extensions/vscode/schemas/vue-tsconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
],
"markdownDescription": "Target version of Vue."
},
"lib": {
"default": "vue",
"markdownDescription": "Specify module name for import regular types."
},
"globalTypesPath": {
"type": "string",
"markdownDescription": "Path to the global types file. Manual configuration is required when `node_modules` does not exist in the environment."
},
"extensions": {
"type": "array",
"default": [".vue"],
Expand All @@ -31,10 +39,6 @@
"default": [".html"],
"markdownDescription": "Valid file extensions that should be considered as regular PetiteVue SFC."
},
"lib": {
"default": "vue",
"markdownDescription": "Specify module name for import regular types."
},
"jsxSlots": {
"type": "boolean",
"default": false,
Expand Down
46 changes: 9 additions & 37 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function createCheckerByJsonConfigBase(
rootDir = rootDir.replace(windowsPathReg, '/');
return baseCreate(
ts,
() => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json, undefined, true),
() => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json),
checkerOptions,
rootDir,
path.join(rootDir, 'jsconfig.json.global.vue'),
Expand All @@ -42,7 +42,7 @@ export function createCheckerBase(
tsconfig = tsconfig.replace(windowsPathReg, '/');
return baseCreate(
ts,
() => vue.createParsedCommandLine(ts, ts.sys, tsconfig, true),
() => vue.createParsedCommandLine(ts, ts.sys, tsconfig),
checkerOptions,
path.dirname(tsconfig),
tsconfig + '.global.vue',
Expand All @@ -63,6 +63,13 @@ export function baseCreate(
let fileNames = new Set(commandLine.fileNames.map(path => path.replace(windowsPathReg, '/')));
let projectVersion = 0;

if (commandLine.vueOptions.globalTypesPath) {
ts.sys.writeFile(
commandLine.vueOptions.globalTypesPath,
vue.generateGlobalTypes(commandLine.vueOptions),
);
}

const projectHost: TypeScriptProjectHost = {
getCurrentDirectory: () => rootPath,
getProjectVersion: () => projectVersion.toString(),
Expand Down Expand Up @@ -136,41 +143,6 @@ export function baseCreate(
const { languageServiceHost } = createLanguageServiceHost(ts, ts.sys, language, s => s, projectHost);
const tsLs = ts.createLanguageService(languageServiceHost);

const directoryExists = languageServiceHost.directoryExists?.bind(languageServiceHost);
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost);
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost);
const globalTypesName = vue.getGlobalTypesFileName(commandLine.vueOptions);
const globalTypesContents = `// @ts-nocheck\nexport {};\n` + vue.generateGlobalTypes(commandLine.vueOptions);
const globalTypesSnapshot: ts.IScriptSnapshot = {
getText: (start, end) => globalTypesContents.slice(start, end),
getLength: () => globalTypesContents.length,
getChangeRange: () => undefined,
};
if (directoryExists) {
languageServiceHost.directoryExists = path => {
if (path.endsWith('.vue-global-types')) {
return true;
}
return directoryExists(path);
};
}
languageServiceHost.fileExists = path => {
if (
path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)
) {
return true;
}
return fileExists(path);
};
languageServiceHost.getScriptSnapshot = path => {
if (
path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)
) {
return globalTypesSnapshot;
}
return getScriptSnapshot(path);
};

if (checkerOptions.forceUseTs) {
const getScriptKind = languageServiceHost.getScriptKind?.bind(languageServiceHost);
languageServiceHost.getScriptKind = fileName => {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function generateGlobalTypes({
const fnPropsType = `(T extends { $props: infer Props } ? Props : {})${
checkUnknownProps ? '' : ' & Record<string, unknown>'
}`;
let text = ``;
let text = `// @ts-nocheck\nexport {};\n`;
if (target < 3.5) {
text += `
; declare module '${lib}' {
Expand Down
21 changes: 8 additions & 13 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { ScriptRanges } from '../../parsers/scriptRanges';
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc, VueCompilerOptions } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { generateGlobalTypes, getGlobalTypesFileName } from '../globalTypes';
import type { TemplateCodegenContext } from '../template/context';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateComponentSelf } from './componentSelf';
Expand All @@ -25,27 +24,26 @@ export interface ScriptCodegenOptions {
templateCodegen: TemplateCodegenContext & { codes: Code[] } | undefined;
destructuredPropNames: Set<string>;
templateRefNames: Set<string>;
appendGlobalTypes: boolean;
}

export function* generateScript(options: ScriptCodegenOptions): Generator<Code, ScriptCodegenContext> {
const ctx = createScriptCodegenContext(options);

if (options.vueCompilerOptions.__setupedGlobalTypes) {
const globalTypes = options.vueCompilerOptions.__setupedGlobalTypes;
if (typeof globalTypes === 'object') {
let relativePath = path.relative(path.dirname(options.fileName), globalTypes.absolutePath);
if (options.vueCompilerOptions.globalTypesPath) {
const globalTypesPath = options.vueCompilerOptions.globalTypesPath;
if (path.isAbsolute(globalTypesPath)) {
let relativePath = path.relative(path.dirname(options.fileName), globalTypesPath);
if (
relativePath !== globalTypes.absolutePath && !relativePath.startsWith('./') && !relativePath.startsWith('../')
relativePath !== globalTypesPath
&& !relativePath.startsWith('./')
&& !relativePath.startsWith('../')
) {
relativePath = './' + relativePath;
}
yield `/// <reference types="${relativePath}" />${newLine}`;
}
else {
yield `/// <reference types=".vue-global-types/${
getGlobalTypesFileName(options.vueCompilerOptions)
}" />${newLine}`;
yield `/// <reference types="${globalTypesPath}" />${newLine}`;
}
}
else {
Expand Down Expand Up @@ -165,9 +163,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
}

yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
if (options.appendGlobalTypes) {
yield generateGlobalTypes(options.vueCompilerOptions);
}

if (options.sfc.scriptSetup) {
yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, codeFeatures.verification];
Expand Down
11 changes: 1 addition & 10 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const tsCodegen = new WeakMap<Sfc, ReturnType<typeof createTsx>>();
const validLangs = new Set(['js', 'jsx', 'ts', 'tsx']);

const plugin: VueLanguagePlugin = ctx => {
let appendedGlobalTypes = false;

return {
version: 2.1,

Expand Down Expand Up @@ -46,12 +44,7 @@ const plugin: VueLanguagePlugin = ctx => {

function useCodegen(fileName: string, sfc: Sfc) {
if (!tsCodegen.has(sfc)) {
let appendGlobalTypes = false;
if (!ctx.vueCompilerOptions.__setupedGlobalTypes && !appendedGlobalTypes) {
appendGlobalTypes = true;
appendedGlobalTypes = true;
}
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx, appendGlobalTypes));
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx));
}
return tsCodegen.get(sfc)!;
}
Expand All @@ -63,7 +56,6 @@ function createTsx(
fileName: string,
sfc: Sfc,
ctx: Parameters<VueLanguagePlugin>[0],
appendGlobalTypes: boolean,
) {
const ts = ctx.modules.typescript;

Expand Down Expand Up @@ -231,7 +223,6 @@ function createTsx(
templateCodegen: getGeneratedTemplate(),
destructuredPropNames: getSetupDestructuredPropNames(),
templateRefNames: getSetupTemplateRefNames(),
appendGlobalTypes,
});

let current = codegen.next();
Expand Down
6 changes: 1 addition & 5 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Code = Segment<VueCodeInformation>;
export interface VueCompilerOptions {
target: number;
lib: string;
globalTypesPath?: string;
extensions: string[];
vitePressExtensions: string[];
petiteVueExtensions: string[];
Expand Down Expand Up @@ -72,11 +73,6 @@ export interface VueCompilerOptions {
string,
Record<string, boolean | Record<string, string> | Record<string, string>[]>
>;

// internal
__setupedGlobalTypes?: true | {
absolutePath: string;
};
}

export const validVersions = [2, 2.1] as const;
Expand Down
Loading
Loading