Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate global types file into node_modules/.vue-global-types #4752

Merged
merged 5 commits into from
Aug 30, 2024
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
13 changes: 11 additions & 2 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,27 @@ 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 = `__global_types_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`;
const globalTypesName = `${commandLine.vueOptions.lib}_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`;
const snapshots = new Map<string, ts.IScriptSnapshot>();
if (directoryExists) {
languageServiceHost.directoryExists = path => {
if (path.endsWith('.vue-global-types')) {
return true;
}
return directoryExists(path);
};
}
languageServiceHost.fileExists = path => {
if (path.endsWith(globalTypesName)) {
return true;
}
return fileExists(path);
};
languageServiceHost.getScriptSnapshot = path => {
if (path.endsWith(globalTypesName)) {
if (path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)) {
if (!snapshots.has(path)) {
const contents = vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates);
snapshots.set(path, {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface ScriptCodegenOptions {
export function* generateScript(options: ScriptCodegenOptions): Generator<Code, ScriptCodegenContext> {
const ctx = createScriptCodegenContext(options);

yield `/// <reference types="${options.vueCompilerOptions.lib}/dist/__global_types_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;

if (options.sfc.script?.src) {
yield* generateSrc(options.sfc.script, options.sfc.script.src);
Expand Down
13 changes: 11 additions & 2 deletions packages/language-server/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,27 @@ export function initialize(
project.vue = { compilerOptions: vueCompilerOptions };

if (project.typescript) {
const globalTypesName = `__global_types_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
const globalTypesName = `${vueCompilerOptions.lib}_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
const directoryExists = project.typescript.languageServiceHost.directoryExists?.bind(project.typescript.languageServiceHost);
const fileExists = project.typescript.languageServiceHost.fileExists.bind(project.typescript.languageServiceHost);
const getScriptSnapshot = project.typescript.languageServiceHost.getScriptSnapshot.bind(project.typescript.languageServiceHost);
const snapshots = new Map<string, ts.IScriptSnapshot>();
if (directoryExists) {
project.typescript.languageServiceHost.directoryExists = path => {
if (path.endsWith('.vue-global-types')) {
return true;
}
return directoryExists!(path);
};
}
project.typescript.languageServiceHost.fileExists = path => {
if (path.endsWith(globalTypesName)) {
return true;
}
return fileExists(path);
};
project.typescript.languageServiceHost.getScriptSnapshot = path => {
if (path.endsWith(globalTypesName)) {
if (path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)) {
if (!snapshots.has(path)) {
const contents = generateGlobalTypes(vueCompilerOptions.lib, vueCompilerOptions.target, vueCompilerOptions.strictTemplates);
snapshots.set(path, {
Expand Down
14 changes: 10 additions & 4 deletions packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { runTsc } from '@volar/typescript/lib/quickstart/runTsc';
import * as path from 'path';
import * as vue from '@vue/language-core';

const windowsPathReg = /\\/g;
Expand All @@ -22,12 +23,17 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
&& runExtensions.every(ext => allExtensions.includes(ext))
) {
try {
const rootDir = typeof configFilePath === 'string'
let dir = typeof configFilePath === 'string'
? configFilePath
: options.host?.getCurrentDirectory() ?? ts.sys.getCurrentDirectory();
const libDir = require.resolve(`${vueOptions.lib}/package.json`, { paths: [rootDir] })
.slice(0, -'package.json'.length);
const globalTypesPath = `${libDir}dist/__global_types_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`;
while (!ts.sys.directoryExists(path.resolve(dir, 'node_modules'))) {
const parentDir = path.resolve(dir, '..');
if (dir === parentDir) {
throw 0;
}
dir = parentDir;
}
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);
} catch { }
Expand Down
13 changes: 9 additions & 4 deletions packages/tsc/tests/dts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ describe('vue-tsc-dts', () => {
: vue.resolveVueCompilerOptions({ extensions: ['.vue', '.cext'] });

try {
const rootDir = typeof configFilePath === 'string'
let dir = typeof configFilePath === 'string'
? configFilePath
: options.host?.getCurrentDirectory() ?? ts.sys.getCurrentDirectory();
const libDir = require.resolve(`${vueOptions.lib}/package.json`, { paths: [rootDir] })
.slice(0, -'package.json'.length);
const globalTypesPath = `${libDir}dist/__global_types_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`;
while (!ts.sys.directoryExists(path.resolve(dir, 'node_modules'))) {
const parentDir = path.resolve(dir, '..');
if (dir === parentDir) {
throw 0;
}
dir = parentDir;
}
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);
} catch { }
Expand Down
13 changes: 10 additions & 3 deletions packages/typescript-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createLanguageServicePlugin } from '@volar/typescript/lib/quickstart/createLanguageServicePlugin';
import * as path from 'path';
import * as vue from '@vue/language-core';
import { proxyLanguageServiceForVue } from './lib/common';
import { startNamedPipeServer } from './lib/server';
Expand Down Expand Up @@ -61,9 +62,15 @@ const plugin: ts.server.PluginModuleFactory = mods => {
const options = vueCompilerOptions.get(proj);
if (updateLevel >= 1 && options) {
try {
const libDir = require.resolve(`${options.lib}/package.json`, { paths: [proj.getCurrentDirectory()] })
.slice(0, -'package.json'.length);
const globalTypesPath = `${libDir}dist/__global_types_${options.target}_${options.strictTemplates}.d.ts`;
let dir = proj.getCurrentDirectory();
while (!proj.directoryExists(path.resolve(dir, 'node_modules'))) {
const parentDir = path.resolve(dir, '..');
if (dir === parentDir) {
throw 0;
}
dir = parentDir;
}
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);
} catch { }
Expand Down