Skip to content

Commit

Permalink
Merge branch 'master' into fix/call-template-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 31, 2024
2 parents 153c9c5 + af7fc39 commit 77b75f4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export function baseCreate(
const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
ts,
projectHost.getCompilationSettings(),
commandLine.vueOptions,
{
...commandLine.vueOptions,
__setupedGlobalTypes: () => true,
},
id => id
);
const language = vue.createLanguage(
Expand Down
11 changes: 10 additions & 1 deletion packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createScriptCodegenContext, ScriptCodegenContext } from './context';
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
import { generateSrc } from './src';
import { generateTemplate } from './template';
import { generateGlobalTypes } from '../globalTypes';

export const codeFeatures = {
all: {
Expand Down Expand Up @@ -51,7 +52,12 @@ export interface ScriptCodegenOptions {
export function* generateScript(options: ScriptCodegenOptions): Generator<Code, ScriptCodegenContext> {
const ctx = createScriptCodegenContext(options);

yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
if (options.vueCompilerOptions.__setupedGlobalTypes?.()) {
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
}
else {
yield `/* placeholder */`;
}

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

if (options.sfc.scriptSetup) {
yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, codeFeatures.verification];
Expand Down
1 change: 1 addition & 0 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface VueCompilerOptions {
experimentalModelPropName: Record<string, Record<string, boolean | Record<string, string> | Record<string, string>[]>>;

// internal
__setupedGlobalTypes?: () => boolean;
__test?: boolean;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/language-server/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export function initialize(
createVueLanguagePlugin(
ts,
compilerOptions,
vueCompilerOptions,
{
...vueCompilerOptions,
__setupedGlobalTypes: () => true,
},
s => uriConverter.asFileName(s)
),
],
Expand Down
7 changes: 6 additions & 1 deletion packages/tsc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
runExtensions.length === allExtensions.length
&& runExtensions.every(ext => allExtensions.includes(ext))
) {
let setupedGlobalTypes = false;
try {
let dir = typeof configFilePath === 'string'
? configFilePath
Expand All @@ -36,12 +37,16 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${vueOptions.lib}_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`);
const globalTypesContents = vue.generateGlobalTypes(vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
ts.sys.writeFile(globalTypesPath, globalTypesContents);
setupedGlobalTypes = true;
} catch { }

const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
ts,
options.options,
vueOptions,
{
...vueOptions,
__setupedGlobalTypes: () => setupedGlobalTypes,
},
id => id
);
return { languagePlugins: [vueLanguagePlugin] };
Expand Down
8 changes: 7 additions & 1 deletion packages/tsc/tests/dts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('vue-tsc-dts', () => {
? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
: vue.resolveVueCompilerOptions({ extensions: ['.vue', '.cext'] });

let setupedGlobalTypes = false;

try {
let dir = typeof configFilePath === 'string'
? configFilePath
Expand All @@ -45,12 +47,16 @@ describe('vue-tsc-dts', () => {
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${vueOptions.lib}_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`);
const globalTypesContents = vue.generateGlobalTypes(vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
ts.sys.writeFile(globalTypesPath, globalTypesContents);
setupedGlobalTypes = true;
} catch { }

const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
ts,
options.options,
vueOptions,
{
...vueOptions,
__setupedGlobalTypes: () => setupedGlobalTypes,
},
id => id
);
return [vueLanguagePlugin];
Expand Down
8 changes: 6 additions & 2 deletions packages/typescript-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import { startNamedPipeServer } from './lib/server';
import type * as ts from 'typescript';

const windowsPathReg = /\\/g;

const vueCompilerOptions = new WeakMap<ts.server.Project, vue.VueCompilerOptions>();
const setupedProjects = new WeakSet<ts.server.Project>();

const basePlugin = createLanguageServicePlugin(
(ts, info) => {
const vueOptions = getVueCompilerOptions();
const languagePlugin = vue.createVueLanguagePlugin<string>(
ts,
info.languageServiceHost.getCompilationSettings(),
vueOptions,
{
...vueOptions,
__setupedGlobalTypes: () => setupedProjects.has(info.project),
},
id => id
);

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

0 comments on commit 77b75f4

Please sign in to comment.