Skip to content

Commit f498667

Browse files
refactor(language-core): replace dynamic types generation with static files (#5872)
1 parent 9ad178a commit f498667

24 files changed

Lines changed: 258 additions & 360 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_modules
33
*.tsbuildinfo
44
*.vsix
55

6+
extensions/vscode/types
67
extensions/vscode/out
78
extensions/vscode/tests/embeddedGrammars/*.tmLanguage.json
89
packages/*/*.d.ts

extensions/vscode/rolldown.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ export default defineConfig({
2929
fs.rmSync(path.resolve(__dirname, './dist'), { recursive: true, force: true });
3030
},
3131
},
32+
{
33+
name: 'copy-types',
34+
buildEnd() {
35+
const sourceDir = path.resolve(__dirname, '../../packages/language-core/types');
36+
const targetDir = path.resolve(__dirname, './types');
37+
fs.rmSync(targetDir, { recursive: true, force: true });
38+
fs.cpSync(sourceDir, targetDir, { recursive: true });
39+
},
40+
},
3241
{
3342
name: 'umd2esm',
3443
resolveId: {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
"default": "vue",
2121
"markdownDescription": "Specify module name for import regular types."
2222
},
23-
"globalTypesPath": {
23+
"typesRoot": {
2424
"type": "string",
25-
"markdownDescription": "Path to the global types file. Manual configuration is required when `node_modules` does not exist in the environment."
25+
"default": "@vue/language-core/types",
26+
"markdownDescription": "Root path for helper type definitions. See: https://github.com/vuejs/language-tools/pull/5872"
2627
},
2728
"extensions": {
2829
"type": "array",

packages/component-meta/lib/base.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ function baseCreate(
157157
let fileNamesSet = new Set(fileNames.map(path => path.replace(windowsPathReg, '/')));
158158
let projectVersion = 0;
159159

160-
vueOptions.globalTypesPath = core.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
161-
162160
const projectHost: TypeScriptProjectHost = {
163161
getCurrentDirectory: () => rootPath,
164162
getProjectVersion: () => projectVersion.toString(),
@@ -267,7 +265,6 @@ function baseCreate(
267265
},
268266
reload() {
269267
[{ vueOptions, options, projectReferences }, fileNames] = getConfigAndFiles();
270-
vueOptions.globalTypesPath = core.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
271268
fileNamesSet = new Set(fileNames.map(path => path.replace(windowsPathReg, '/')));
272269
this.clearCache();
273270
},

packages/language-core/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './lib/codegen/globalTypes';
21
export * from './lib/codegen/template';
32
export * from './lib/compilerOptions';
43
export * from './lib/languagePlugin';

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

Lines changed: 0 additions & 184 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const setup = '__VLS_setup';
1111
export const components = '__VLS_components';
1212
export const directives = '__VLS_directives';
1313
export const intrinsics = '__VLS_intrinsics';
14-
export const placeholder = '__VLS_placeholder';
1514
export const _export = '__VLS_export';
1615

1716
export const ModelProps = '__VLS_ModelProps';
@@ -27,5 +26,3 @@ export const Emit = '__VLS_Emit';
2726
export const SetupExposed = '__VLS_SetupExposed';
2827
export const PublicProps = '__VLS_PublicProps';
2928
export const StyleModules = '__VLS_StyleModules';
30-
31-
export const PROPS_FALLBACK = '__VLS_PROPS_FALLBACK';

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,31 @@ function* generateScriptWithExportDefault(
234234
yield* generateSfcBlockSection(script, exportDefault.end, script.content.length, codeFeatures.all);
235235
}
236236

237-
function* generateGlobalTypesReference(vueCompilerOptions: VueCompilerOptions, fileName: string): Generator<Code> {
238-
const globalTypesPath = vueCompilerOptions.globalTypesPath(fileName);
239-
if (!globalTypesPath) {
240-
yield `/* placeholder */${newLine}`;
241-
}
242-
else if (path.isAbsolute(globalTypesPath)) {
243-
let relativePath = path.relative(path.dirname(fileName), globalTypesPath);
237+
function* generateGlobalTypesReference(
238+
{ typesRoot, lib, target, checkUnknownProps }: VueCompilerOptions,
239+
fileName: string,
240+
): Generator<Code> {
241+
let typesPath: string;
242+
if (path.isAbsolute(typesRoot)) {
243+
let relativePath = path.relative(path.dirname(fileName), typesRoot);
244244
if (
245-
relativePath !== globalTypesPath
245+
relativePath !== typesRoot
246246
&& !relativePath.startsWith('./')
247247
&& !relativePath.startsWith('../')
248248
) {
249249
relativePath = './' + relativePath;
250250
}
251-
yield `/// <reference types="${relativePath}" />${newLine}`;
251+
typesPath = relativePath;
252252
}
253253
else {
254-
yield `/// <reference types="${globalTypesPath}" />${newLine}`;
254+
typesPath = typesRoot;
255+
}
256+
yield `/// <reference types=${JSON.stringify(typesPath + '/template-helpers.d.ts')} />${newLine}`;
257+
if (!checkUnknownProps) {
258+
yield `/// <reference types=${JSON.stringify(typesPath + '/props-fallback.d.ts')} />${newLine}`;
259+
}
260+
if (lib === 'vue' && target < 3.5) {
261+
yield `/// <reference types=${JSON.stringify(typesPath + '/vue-3.4-shims.d.ts')} />${newLine}`;
255262
}
256263
}
257264

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ export function* generateGeneric(
9898
if (propTypes.length) {
9999
yield ` & ${ctx.localTypes.PrettifyLocal}<${propTypes.join(` & `)}>`;
100100
}
101-
if (!vueCompilerOptions.checkUnknownProps) {
102-
yield ` & (typeof globalThis extends { ${names.PROPS_FALLBACK}: infer P } ? P : {})`;
103-
}
104-
yield endOfLine;
101+
yield ` & (typeof globalThis extends { __VLS_PROPS_FALLBACK: infer P } ? P : {})${endOfLine}`;
105102
yield ` expose: (exposed: `;
106103
yield scriptSetupRanges.defineExpose
107104
? `import('${vueCompilerOptions.lib}').ShallowUnwrapRef<typeof ${names.exposed}>`
@@ -219,7 +216,7 @@ export function* generateSetupFunction(
219216
yield `])`;
220217
}),
221218
replace(arg.start, arg.end, function*() {
222-
yield names.placeholder;
219+
yield `{} as any`;
223220
}),
224221
);
225222
}
@@ -253,7 +250,7 @@ export function* generateSetupFunction(
253250
yield `(`;
254251
}),
255252
insert(callExp.end, function*() {
256-
yield ` as __VLS_UseTemplateRef<`;
253+
yield ` as Readonly<import('${options.vueCompilerOptions.lib}').ShallowRef<`;
257254
if (arg) {
258255
yield names.TemplateRefs;
259256
yield `[`;
@@ -263,13 +260,13 @@ export function* generateSetupFunction(
263260
else {
264261
yield `unknown`;
265262
}
266-
yield `>)`;
263+
yield ` | null>>)`;
267264
}),
268265
);
269266
if (arg) {
270267
transforms.push(
271268
replace(arg.start, arg.end, function*() {
272-
yield names.placeholder;
269+
yield `{} as any`;
273270
}),
274271
);
275272
}

0 commit comments

Comments
 (0)