Skip to content

Commit 9ad6714

Browse files
feat(language-core): introduce globalTypesPath option for non-npm like environment (#5505)
Co-authored-by: Johnson Chu <[email protected]>
1 parent 5df8a9b commit 9ad6714

File tree

13 files changed

+145
-141
lines changed

13 files changed

+145
-141
lines changed

extensions/vscode/schemas/vue-tsconfig.schema.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
],
1717
"markdownDescription": "Target version of Vue."
1818
},
19+
"lib": {
20+
"default": "vue",
21+
"markdownDescription": "Specify module name for import regular types."
22+
},
23+
"globalTypesPath": {
24+
"type": "string",
25+
"markdownDescription": "Path to the global types file. Manual configuration is required when `node_modules` does not exist in the environment."
26+
},
1927
"extensions": {
2028
"type": "array",
2129
"default": [".vue"],
@@ -31,10 +39,6 @@
3139
"default": [".html"],
3240
"markdownDescription": "Valid file extensions that should be considered as regular PetiteVue SFC."
3341
},
34-
"lib": {
35-
"default": "vue",
36-
"markdownDescription": "Specify module name for import regular types."
37-
},
3842
"jsxSlots": {
3943
"type": "boolean",
4044
"default": false,

packages/component-meta/lib/base.ts

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function createCheckerByJsonConfigBase(
2727
rootDir = rootDir.replace(windowsPathReg, '/');
2828
return baseCreate(
2929
ts,
30-
() => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json, undefined, true),
30+
() => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json),
3131
checkerOptions,
3232
rootDir,
3333
path.join(rootDir, 'jsconfig.json.global.vue'),
@@ -42,7 +42,7 @@ export function createCheckerBase(
4242
tsconfig = tsconfig.replace(windowsPathReg, '/');
4343
return baseCreate(
4444
ts,
45-
() => vue.createParsedCommandLine(ts, ts.sys, tsconfig, true),
45+
() => vue.createParsedCommandLine(ts, ts.sys, tsconfig),
4646
checkerOptions,
4747
path.dirname(tsconfig),
4848
tsconfig + '.global.vue',
@@ -63,6 +63,13 @@ export function baseCreate(
6363
let fileNames = new Set(commandLine.fileNames.map(path => path.replace(windowsPathReg, '/')));
6464
let projectVersion = 0;
6565

66+
if (commandLine.vueOptions.globalTypesPath) {
67+
ts.sys.writeFile(
68+
commandLine.vueOptions.globalTypesPath,
69+
vue.generateGlobalTypes(commandLine.vueOptions),
70+
);
71+
}
72+
6673
const projectHost: TypeScriptProjectHost = {
6774
getCurrentDirectory: () => rootPath,
6875
getProjectVersion: () => projectVersion.toString(),
@@ -136,41 +143,6 @@ export function baseCreate(
136143
const { languageServiceHost } = createLanguageServiceHost(ts, ts.sys, language, s => s, projectHost);
137144
const tsLs = ts.createLanguageService(languageServiceHost);
138145

139-
const directoryExists = languageServiceHost.directoryExists?.bind(languageServiceHost);
140-
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost);
141-
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost);
142-
const globalTypesName = vue.getGlobalTypesFileName(commandLine.vueOptions);
143-
const globalTypesContents = `// @ts-nocheck\nexport {};\n` + vue.generateGlobalTypes(commandLine.vueOptions);
144-
const globalTypesSnapshot: ts.IScriptSnapshot = {
145-
getText: (start, end) => globalTypesContents.slice(start, end),
146-
getLength: () => globalTypesContents.length,
147-
getChangeRange: () => undefined,
148-
};
149-
if (directoryExists) {
150-
languageServiceHost.directoryExists = path => {
151-
if (path.endsWith('.vue-global-types')) {
152-
return true;
153-
}
154-
return directoryExists(path);
155-
};
156-
}
157-
languageServiceHost.fileExists = path => {
158-
if (
159-
path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)
160-
) {
161-
return true;
162-
}
163-
return fileExists(path);
164-
};
165-
languageServiceHost.getScriptSnapshot = path => {
166-
if (
167-
path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)
168-
) {
169-
return globalTypesSnapshot;
170-
}
171-
return getScriptSnapshot(path);
172-
};
173-
174146
if (checkerOptions.forceUseTs) {
175147
const getScriptKind = languageServiceHost.getScriptKind?.bind(languageServiceHost);
176148
languageServiceHost.getScriptKind = fileName => {

packages/language-core/lib/codegen/globalTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function generateGlobalTypes({
2727
const fnPropsType = `(T extends { $props: infer Props } ? Props : {})${
2828
checkUnknownProps ? '' : ' & Record<string, unknown>'
2929
}`;
30-
let text = ``;
30+
let text = `// @ts-nocheck\nexport {};\n`;
3131
if (target < 3.5) {
3232
text += `
3333
; declare module '${lib}' {

packages/language-core/lib/codegen/script/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { ScriptRanges } from '../../parsers/scriptRanges';
44
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
55
import type { Code, Sfc, VueCompilerOptions } from '../../types';
66
import { codeFeatures } from '../codeFeatures';
7-
import { generateGlobalTypes, getGlobalTypesFileName } from '../globalTypes';
87
import type { TemplateCodegenContext } from '../template/context';
98
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
109
import { generateComponentSelf } from './componentSelf';
@@ -25,27 +24,26 @@ export interface ScriptCodegenOptions {
2524
templateCodegen: TemplateCodegenContext & { codes: Code[] } | undefined;
2625
destructuredPropNames: Set<string>;
2726
templateRefNames: Set<string>;
28-
appendGlobalTypes: boolean;
2927
}
3028

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

34-
if (options.vueCompilerOptions.__setupedGlobalTypes) {
35-
const globalTypes = options.vueCompilerOptions.__setupedGlobalTypes;
36-
if (typeof globalTypes === 'object') {
37-
let relativePath = path.relative(path.dirname(options.fileName), globalTypes.absolutePath);
32+
if (options.vueCompilerOptions.globalTypesPath) {
33+
const globalTypesPath = options.vueCompilerOptions.globalTypesPath;
34+
if (path.isAbsolute(globalTypesPath)) {
35+
let relativePath = path.relative(path.dirname(options.fileName), globalTypesPath);
3836
if (
39-
relativePath !== globalTypes.absolutePath && !relativePath.startsWith('./') && !relativePath.startsWith('../')
37+
relativePath !== globalTypesPath
38+
&& !relativePath.startsWith('./')
39+
&& !relativePath.startsWith('../')
4040
) {
4141
relativePath = './' + relativePath;
4242
}
4343
yield `/// <reference types="${relativePath}" />${newLine}`;
4444
}
4545
else {
46-
yield `/// <reference types=".vue-global-types/${
47-
getGlobalTypesFileName(options.vueCompilerOptions)
48-
}" />${newLine}`;
46+
yield `/// <reference types="${globalTypesPath}" />${newLine}`;
4947
}
5048
}
5149
else {
@@ -165,9 +163,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
165163
}
166164

167165
yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
168-
if (options.appendGlobalTypes) {
169-
yield generateGlobalTypes(options.vueCompilerOptions);
170-
}
171166

172167
if (options.sfc.scriptSetup) {
173168
yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, codeFeatures.verification];

packages/language-core/lib/plugins/vue-tsx.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export const tsCodegen = new WeakMap<Sfc, ReturnType<typeof createTsx>>();
1515
const validLangs = new Set(['js', 'jsx', 'ts', 'tsx']);
1616

1717
const plugin: VueLanguagePlugin = ctx => {
18-
let appendedGlobalTypes = false;
19-
2018
return {
2119
version: 2.1,
2220

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

4745
function useCodegen(fileName: string, sfc: Sfc) {
4846
if (!tsCodegen.has(sfc)) {
49-
let appendGlobalTypes = false;
50-
if (!ctx.vueCompilerOptions.__setupedGlobalTypes && !appendedGlobalTypes) {
51-
appendGlobalTypes = true;
52-
appendedGlobalTypes = true;
53-
}
54-
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx, appendGlobalTypes));
47+
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx));
5548
}
5649
return tsCodegen.get(sfc)!;
5750
}
@@ -63,7 +56,6 @@ function createTsx(
6356
fileName: string,
6457
sfc: Sfc,
6558
ctx: Parameters<VueLanguagePlugin>[0],
66-
appendGlobalTypes: boolean,
6759
) {
6860
const ts = ctx.modules.typescript;
6961

@@ -231,7 +223,6 @@ function createTsx(
231223
templateCodegen: getGeneratedTemplate(),
232224
destructuredPropNames: getSetupDestructuredPropNames(),
233225
templateRefNames: getSetupTemplateRefNames(),
234-
appendGlobalTypes,
235226
});
236227

237228
let current = codegen.next();

packages/language-core/lib/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type Code = Segment<VueCodeInformation>;
2525
export interface VueCompilerOptions {
2626
target: number;
2727
lib: string;
28+
globalTypesPath?: string;
2829
extensions: string[];
2930
vitePressExtensions: string[];
3031
petiteVueExtensions: string[];
@@ -72,11 +73,6 @@ export interface VueCompilerOptions {
7273
string,
7374
Record<string, boolean | Record<string, string> | Record<string, string>[]>
7475
>;
75-
76-
// internal
77-
__setupedGlobalTypes?: true | {
78-
absolutePath: string;
79-
};
8076
}
8177

8278
export const validVersions = [2, 2.1] as const;

0 commit comments

Comments
 (0)