diff --git a/packages/component-meta/lib/base.ts b/packages/component-meta/lib/base.ts index 047ed8f423..e1d288b9eb 100644 --- a/packages/component-meta/lib/base.ts +++ b/packages/component-meta/lib/base.ts @@ -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(); languageServiceHost.fileExists = path => { if (path.endsWith(globalTypesName)) { diff --git a/packages/language-core/lib/codegen/script/index.ts b/packages/language-core/lib/codegen/script/index.ts index ee20660573..455d91155d 100644 --- a/packages/language-core/lib/codegen/script/index.ts +++ b/packages/language-core/lib/codegen/script/index.ts @@ -51,7 +51,7 @@ export interface ScriptCodegenOptions { export function* generateScript(options: ScriptCodegenOptions): Generator { const ctx = createScriptCodegenContext(options); - yield `/// ${newLine}`; + yield `/// ${newLine}`; if (options.sfc.script?.src) { yield* generateSrc(options.sfc.script, options.sfc.script.src); diff --git a/packages/language-server/lib/initialize.ts b/packages/language-server/lib/initialize.ts index d8cb42ec11..3681d94523 100644 --- a/packages/language-server/lib/initialize.ts +++ b/packages/language-server/lib/initialize.ts @@ -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(); diff --git a/packages/tsc/index.ts b/packages/tsc/index.ts index 34552d449e..9b45b4cb55 100644 --- a/packages/tsc/index.ts +++ b/packages/tsc/index.ts @@ -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; @@ -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 { } diff --git a/packages/tsc/tests/dts.spec.ts b/packages/tsc/tests/dts.spec.ts index ea25fb86f6..627e2946fa 100644 --- a/packages/tsc/tests/dts.spec.ts +++ b/packages/tsc/tests/dts.spec.ts @@ -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 { } diff --git a/packages/typescript-plugin/index.ts b/packages/typescript-plugin/index.ts index 32f5b4e040..b1a0046f8e 100644 --- a/packages/typescript-plugin/index.ts +++ b/packages/typescript-plugin/index.ts @@ -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'; @@ -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 { }