Skip to content

Commit af7fc39

Browse files
committed
fix(typescript-plugin): compatible with Yarn PnP
close #4751
1 parent 923f835 commit af7fc39

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

packages/component-meta/lib/base.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ export function baseCreate(
8686
const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
8787
ts,
8888
projectHost.getCompilationSettings(),
89-
commandLine.vueOptions,
89+
{
90+
...commandLine.vueOptions,
91+
__setupedGlobalTypes: () => true,
92+
},
9093
id => id
9194
);
9295
const language = vue.createLanguage(

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createScriptCodegenContext, ScriptCodegenContext } from './context';
99
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
1010
import { generateSrc } from './src';
1111
import { generateTemplate } from './template';
12+
import { generateGlobalTypes } from '../globalTypes';
1213

1314
export const codeFeatures = {
1415
all: {
@@ -51,7 +52,12 @@ export interface ScriptCodegenOptions {
5152
export function* generateScript(options: ScriptCodegenOptions): Generator<Code, ScriptCodegenContext> {
5253
const ctx = createScriptCodegenContext(options);
5354

54-
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
55+
if (options.vueCompilerOptions.__setupedGlobalTypes?.()) {
56+
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
57+
}
58+
else {
59+
yield `/* placeholder */`;
60+
}
5561

5662
if (options.sfc.script?.src) {
5763
yield* generateSrc(options.sfc.script, options.sfc.script.src);
@@ -136,6 +142,9 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
136142
yield `type __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements${endOfLine}`;
137143
}
138144
yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
145+
if (!options.vueCompilerOptions.__setupedGlobalTypes?.()) {
146+
yield generateGlobalTypes(options.vueCompilerOptions.lib, options.vueCompilerOptions.target, options.vueCompilerOptions.strictTemplates);
147+
}
139148

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

packages/language-core/lib/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface VueCompilerOptions {
5555
experimentalModelPropName: Record<string, Record<string, boolean | Record<string, string> | Record<string, string>[]>>;
5656

5757
// internal
58+
__setupedGlobalTypes?: () => boolean;
5859
__test?: boolean;
5960
}
6061

packages/language-server/lib/initialize.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export function initialize(
4444
createVueLanguagePlugin(
4545
ts,
4646
compilerOptions,
47-
vueCompilerOptions,
47+
{
48+
...vueCompilerOptions,
49+
__setupedGlobalTypes: () => true,
50+
},
4851
s => uriConverter.asFileName(s)
4952
),
5053
],

packages/tsc/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
2222
runExtensions.length === allExtensions.length
2323
&& runExtensions.every(ext => allExtensions.includes(ext))
2424
) {
25+
let setupedGlobalTypes = false;
2526
try {
2627
let dir = typeof configFilePath === 'string'
2728
? configFilePath
@@ -36,12 +37,16 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
3637
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${vueOptions.lib}_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`);
3738
const globalTypesContents = vue.generateGlobalTypes(vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
3839
ts.sys.writeFile(globalTypesPath, globalTypesContents);
40+
setupedGlobalTypes = true;
3941
} catch { }
4042

4143
const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
4244
ts,
4345
options.options,
44-
vueOptions,
46+
{
47+
...vueOptions,
48+
__setupedGlobalTypes: () => setupedGlobalTypes,
49+
},
4550
id => id
4651
);
4752
return { languagePlugins: [vueLanguagePlugin] };

packages/tsc/tests/dts.spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ describe('vue-tsc-dts', () => {
3131
? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
3232
: vue.resolveVueCompilerOptions({ extensions: ['.vue', '.cext'] });
3333

34+
let setupedGlobalTypes = false;
35+
3436
try {
3537
let dir = typeof configFilePath === 'string'
3638
? configFilePath
@@ -45,12 +47,16 @@ describe('vue-tsc-dts', () => {
4547
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${vueOptions.lib}_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`);
4648
const globalTypesContents = vue.generateGlobalTypes(vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
4749
ts.sys.writeFile(globalTypesPath, globalTypesContents);
50+
setupedGlobalTypes = true;
4851
} catch { }
4952

5053
const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
5154
ts,
5255
options.options,
53-
vueOptions,
56+
{
57+
...vueOptions,
58+
__setupedGlobalTypes: () => setupedGlobalTypes,
59+
},
5460
id => id
5561
);
5662
return [vueLanguagePlugin];

packages/typescript-plugin/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import { startNamedPipeServer } from './lib/server';
66
import type * as ts from 'typescript';
77

88
const windowsPathReg = /\\/g;
9-
109
const vueCompilerOptions = new WeakMap<ts.server.Project, vue.VueCompilerOptions>();
10+
const setupedProjects = new WeakSet<ts.server.Project>();
1111

1212
const basePlugin = createLanguageServicePlugin(
1313
(ts, info) => {
1414
const vueOptions = getVueCompilerOptions();
1515
const languagePlugin = vue.createVueLanguagePlugin<string>(
1616
ts,
1717
info.languageServiceHost.getCompilationSettings(),
18-
vueOptions,
18+
{
19+
...vueOptions,
20+
__setupedGlobalTypes: () => setupedProjects.has(info.project),
21+
},
1922
id => id
2023
);
2124

@@ -73,6 +76,7 @@ const plugin: ts.server.PluginModuleFactory = mods => {
7376
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${options.lib}_${options.target}_${options.strictTemplates}.d.ts`);
7477
const globalTypesContents = vue.generateGlobalTypes(options.lib, options.target, options.strictTemplates);
7578
proj.writeFile(globalTypesPath, globalTypesContents);
79+
setupedProjects.add(proj);
7680
} catch { }
7781
}
7882
return pluginModule.getExternalFiles?.(proj, updateLevel) ?? [];

0 commit comments

Comments
 (0)