Skip to content

Commit

Permalink
feat: generate global types file into node_modules/.vue
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Aug 29, 2024
1 parent d18cade commit 76056cf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function baseCreate(

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 = `global_types_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`;
const snapshots = new Map<string, ts.IScriptSnapshot>();
languageServiceHost.fileExists = path => {
if (path.endsWith(globalTypesName)) {
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.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;

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

if (project.typescript) {
const globalTypesName = `__global_types_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
const globalTypesName = `global_types_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
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>();
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.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.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.target}_${options.strictTemplates}.d.ts`);
const globalTypesContents = vue.generateGlobalTypes(options.lib, options.target, options.strictTemplates);
proj.writeFile(globalTypesPath, globalTypesContents);
} catch { }
Expand Down

0 comments on commit 76056cf

Please sign in to comment.