From b8ae905a9ece4aa375ea41db941dbf36be2fdd08 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Fri, 28 Apr 2023 01:54:54 +0800 Subject: [PATCH] feat: vueCompilerOptions.lib close #2722 --- CHANGELOG.md | 1 + .../schemas/vue-tsconfig.schema.json | 4 ++ .../src/generators/script.ts | 39 +++++++++---------- .../vue-language-core/src/languageModule.ts | 2 +- packages/vue-language-core/src/types.ts | 1 + .../src/utils/directorySharedTypes.ts | 11 ++---- .../vue-language-core/src/utils/shared.ts | 4 -- packages/vue-language-core/src/utils/ts.ts | 1 + 8 files changed, 30 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab947520e..d905c15d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- feat: support for specify vue library name by `vueCompilerOptions.lib` option ([#2730](https://github.com/johnsoncodehk/volar/issues/2730)) - fix: avoid `` type checking with string literal assignment ([#2725](https://github.com/johnsoncodehk/volar/issues/2725)) - fix: `` reporting false positive error when `strictTemplates` enabled ([#2726](https://github.com/johnsoncodehk/volar/issues/2726)) ([#2723](https://github.com/johnsoncodehk/volar/issues/2723)) - fix: error using custom directive: `Expected 2 arguments, but got 1.` ([#2730](https://github.com/johnsoncodehk/volar/issues/2730)) diff --git a/packages/vue-language-core/schemas/vue-tsconfig.schema.json b/packages/vue-language-core/schemas/vue-tsconfig.schema.json index adfa891f1..82944371b 100644 --- a/packages/vue-language-core/schemas/vue-tsconfig.schema.json +++ b/packages/vue-language-core/schemas/vue-tsconfig.schema.json @@ -19,6 +19,10 @@ "default": [ ".vue" ], "markdownDescription": "Valid file extensions that should be considered as regular Vue SFC, please note that you should not use this option separately for additional file extensions IDE support, see https://github.com/johnsoncodehk/volar/tree/master/packages/vscode-vue/README.md#custom-file-extensions." }, + "lib": { + "default": "", + "markdownDescription": "Specify module name for import regular types. (If empty, will use `@vue/runtime-dom` for target < 2.7, `vue` for target >= 2.7)" + }, "jsxSlots": { "type": "boolean", "default": false, diff --git a/packages/vue-language-core/src/generators/script.ts b/packages/vue-language-core/src/generators/script.ts index ce2220d54..09ab22aa2 100644 --- a/packages/vue-language-core/src/generators/script.ts +++ b/packages/vue-language-core/src/generators/script.ts @@ -11,7 +11,7 @@ import type { ScriptSetupRanges } from '../parsers/scriptSetupRanges'; import { collectCssVars, collectStyleCssClasses } from '../plugins/vue-tsx'; import { Sfc } from '../types'; import type { VueCompilerOptions } from '../types'; -import { getSlotsPropertyName, getVueLibraryName } from '../utils/shared'; +import { getSlotsPropertyName } from '../utils/shared'; import { walkInterpolationFragment } from '../utils/transform'; import * as sharedTypes from '../utils/directorySharedTypes'; import * as muggle from 'muggle-string'; @@ -72,7 +72,6 @@ export function generate( //#endregion const bypassDefineComponent = lang === 'js' || lang === 'jsx'; - const vueLibName = getVueLibraryName(vueCompilerOptions.target); const usedHelperTypes = { DefinePropsToOptions: false, mergePropDefaults: false, @@ -116,11 +115,11 @@ export function generate( let usedPrettify = false; if (usedHelperTypes.DefinePropsToOptions) { if (compilerOptions.exactOptionalPropertyTypes) { - codes.push(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueLibName}').PropType } : { type: import('${vueLibName}').PropType, required: true } };\n`); + codes.push(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueCompilerOptions.lib}').PropType } : { type: import('${vueCompilerOptions.lib}').PropType, required: true } };\n`); } else { codes.push(`type __VLS_NonUndefinedable = T extends undefined ? never : T;\n`); - codes.push(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable> } : { type: import('${vueLibName}').PropType, required: true } };\n`); + codes.push(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable> } : { type: import('${vueCompilerOptions.lib}').PropType, required: true } };\n`); } } if (usedHelperTypes.mergePropDefaults) { @@ -317,9 +316,9 @@ export function generate( codes.push('(\n'); codes.push( `__VLS_props: typeof __VLS_setup['props']`, - `& import('${vueLibName}').VNodeProps`, - `& import('${vueLibName}').AllowedComponentProps`, - `& import('${vueLibName}').ComponentCustomProps,\n`, + `& import('${vueCompilerOptions.lib}').VNodeProps`, + `& import('${vueCompilerOptions.lib}').AllowedComponentProps`, + `& import('${vueCompilerOptions.lib}').ComponentCustomProps,\n`, ); codes.push(`__VLS_ctx?: Pick,\n`); codes.push('__VLS_setup = (() => {\n'); @@ -437,7 +436,7 @@ export function generate( codes.push('emit: typeof __VLS_emit'); codes.push('};\n'); codes.push('})(),\n'); - codes.push(`) => ({} as import('${vueLibName}').VNode & { __ctx?: typeof __VLS_setup }))`); + codes.push(`) => ({} as import('${vueCompilerOptions.lib}').VNode & { __ctx?: typeof __VLS_setup }))`); } else if (!sfc.script) { // no script block, generate script setup code at root @@ -484,20 +483,20 @@ export function generate( const definePropProposalB = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition'; if (vueCompilerOptions.target >= 3.3) { - codes.push(`const { defineProps, defineEmits, defineExpose, defineOptions, defineSlots, defineModel, withDefaults } = await import('${vueLibName}');\n`); + codes.push(`const { defineProps, defineEmits, defineExpose, defineOptions, defineSlots, defineModel, withDefaults } = await import('${vueCompilerOptions.lib}');\n`); } if (definePropProposalA) { codes.push(` -declare function defineProp(name: string, options: { required: true } & Record): import('${vueLibName}').ComputedRef; -declare function defineProp(name: string, options: { default: any } & Record): import('${vueLibName}').ComputedRef; -declare function defineProp(name?: string, options?: any): import('${vueLibName}').ComputedRef; +declare function defineProp(name: string, options: { required: true } & Record): import('${vueCompilerOptions.lib}').ComputedRef; +declare function defineProp(name: string, options: { default: any } & Record): import('${vueCompilerOptions.lib}').ComputedRef; +declare function defineProp(name?: string, options?: any): import('${vueCompilerOptions.lib}').ComputedRef; `.trim() + '\n'); } if (definePropProposalB) { codes.push(` -declare function defineProp(value: T | (() => T), required?: boolean, rest?: any): import('${vueLibName}').ComputedRef; -declare function defineProp(value: T | (() => T) | undefined, required: true, rest?: any): import('${vueLibName}').ComputedRef; -declare function defineProp(value?: T | (() => T), required?: boolean, rest?: any): import('${vueLibName}').ComputedRef; +declare function defineProp(value: T | (() => T), required?: boolean, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef; +declare function defineProp(value: T | (() => T) | undefined, required: true, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef; +declare function defineProp(value?: T | (() => T), required?: boolean, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef; `.trim() + '\n'); } @@ -519,7 +518,7 @@ declare function defineProp(value?: T | (() => T), required?: boolean, rest?: codes.push(`{\n`); } else { - codes.push(`const __VLS_publicComponent = (await import('${vueLibName}')).defineComponent({\n`); + codes.push(`const __VLS_publicComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`); } if (scriptSetupRanges.defineProp.length) { @@ -552,10 +551,10 @@ declare function defineProp(value?: T | (() => T), required?: boolean, rest?: } if (defineProp.required) { - codes.push(`{ required: true, type: import('${vueLibName}').PropType<${type}> },\n`); + codes.push(`{ required: true, type: import('${vueCompilerOptions.lib}').PropType<${type}> },\n`); } else { - codes.push(`import('${vueLibName}').PropType<${type}>,\n`); + codes.push(`import('${vueCompilerOptions.lib}').PropType<${type}>,\n`); } } codes.push(`},\n`); @@ -714,7 +713,7 @@ declare function defineProp(value?: T | (() => T), required?: boolean, rest?: if (sfc.scriptSetup && scriptSetupRanges) { - codes.push(`const __VLS_internalComponent = (await import('${vueLibName}')).defineComponent({\n`); + codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`); codes.push(`setup() {\n`); codes.push(`return {\n`); // fill ctx from props @@ -776,7 +775,7 @@ declare function defineProp(value?: T | (() => T), required?: boolean, rest?: codes.push(`let __VLS_internalComponent!: typeof import('./${path.basename(fileName)}')['default'];\n`); } else { - codes.push(`const __VLS_internalComponent = (await import('${vueLibName}')).defineComponent({});\n`); + codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({});\n`); } } function generateExportOptions() { diff --git a/packages/vue-language-core/src/languageModule.ts b/packages/vue-language-core/src/languageModule.ts index 6627feb8d..91a146c50 100644 --- a/packages/vue-language-core/src/languageModule.ts +++ b/packages/vue-language-core/src/languageModule.ts @@ -19,7 +19,7 @@ export function createLanguageModules( compilerOptions, vueCompilerOptions, ); - const sharedTypesSnapshot = ts.ScriptSnapshot.fromString(sharedTypes.getTypesCode(vueCompilerOptions.target, vueCompilerOptions)); + const sharedTypesSnapshot = ts.ScriptSnapshot.fromString(sharedTypes.getTypesCode(vueCompilerOptions)); const languageModule: embedded.LanguageModule = { createFile(fileName, snapshot, languageId) { if ( diff --git a/packages/vue-language-core/src/types.ts b/packages/vue-language-core/src/types.ts index 88ff2f373..351545e68 100644 --- a/packages/vue-language-core/src/types.ts +++ b/packages/vue-language-core/src/types.ts @@ -18,6 +18,7 @@ export type RawVueCompilerOptions = Partial>>; export type Element = PickNotAny; @@ -47,7 +42,7 @@ export type GlobalComponents = & PickNotAny // @ts-ignore & PickNotAny - & Pick