11/* eslint-disable @typescript-eslint/no-require-imports */
2+
3+ import type { Configuration } from './types'
4+ import { compact } from '@zardoy/utils'
5+ import { get } from 'lodash'
6+
27// will be required from ./node_modules/typescript-essential-plugins/index.js
38const originalPluginFactory = require ( 'typescript-essential-plugins' )
49
510const plugin = ( ( context , { typescript : tsModule } = { } ) => {
611 if ( ! context ) throw new Error ( 'Not recieve context' )
712 const { typescript } = context
813 let configurationHost = context . env
9- if ( context [ 'configurationHost' ] ! ) configurationHost = context [ 'configurationHost' ] !
10- const patchConfig = config => {
14+ if ( context [ 'configurationHost' ] ) configurationHost = context [ 'configurationHost' ]
15+ configurationHost = configurationHost [ 'configurationHost' ] ?? configurationHost
16+ const mergeAndPatchConfig = ( generalConfig , vueConfig ) => {
17+ const mergedConfig = {
18+ ...generalConfig ,
19+ ...Object . fromEntries (
20+ Object . entries ( vueConfig ) . map ( ( [ key , value ] ) => {
21+ const getType = obj => {
22+ return Array . isArray ( obj ) ? 'array' : typeof obj === 'object' && obj !== null ? 'object' : undefined
23+ }
24+ const type = getType ( value )
25+ if ( ! type || vueConfig [ 'resetSettings' ] ?. includes ( key ) ) return [ key , value ]
26+ const generalConfigValue = get ( generalConfig , key )
27+ const generalValueType = getType ( generalConfigValue )
28+ if ( type !== generalValueType ) return [ key , value ]
29+ return [ key , generalValueType === 'object' ? { ...generalConfigValue , ...value } : [ ...generalConfigValue , ...value ] ]
30+ } ) ,
31+ ) ,
32+ }
33+
1134 return {
12- ...config ,
35+ ...mergedConfig ,
1336 _additionalPluginOptions : {
1437 pluginSpecificSyntaxServerConfigCheck : false ,
1538 } ,
16- enablePlugin : config . enableVueSupport ,
39+ enablePlugin : generalConfig . enableVueSupport ,
1740 }
1841 }
1942
@@ -22,12 +45,24 @@ const plugin = ((context, { typescript: tsModule } = {}) => {
2245 const plugin = originalPluginFactory ( {
2346 typescript : ts ,
2447 } )
25- // todo support vue-specific settings
2648 const originalLsMethods = { ...typescript . languageService }
2749
28- void configurationHost . getConfiguration ! < any > ( 'tsEssentialPlugins' ) . then ( _configuration => {
50+ const getResolvedUserConfig = async ( ) => {
51+ const regularConfig = await configurationHost . getConfiguration ! < any > ( 'tsEssentialPlugins' )
52+ const _vueSpecificConfig = await configurationHost . getConfiguration ! < any > ( '[vue]' )
53+ const vueSpecificConfig = Object . fromEntries (
54+ compact (
55+ Object . entries ( _vueSpecificConfig ) . map ( ( [ key , value ] ) =>
56+ key . startsWith ( 'tsEssentialPlugins' ) ? [ key . slice ( 'tsEssentialPlugins.' . length ) , value ] : undefined ,
57+ ) ,
58+ ) ,
59+ )
60+ const config : Configuration = mergeAndPatchConfig ( regularConfig , vueSpecificConfig )
61+ return config
62+ }
63+
64+ void getResolvedUserConfig ( ) . then ( async config => {
2965 // if (typescript.languageService[thisPluginMarker]) return
30- const config = patchConfig ( _configuration )
3166 if ( ! config . enablePlugin ) return
3267 const proxy = plugin . create ( {
3368 ...typescript ,
@@ -42,8 +77,7 @@ const plugin = ((context, { typescript: tsModule } = {}) => {
4277 } )
4378
4479 configurationHost . onDidChangeConfiguration ! ( ( ) => {
45- void configurationHost . getConfiguration ! < any > ( 'tsEssentialPlugins' ) . then ( config => {
46- config = patchConfig ( config )
80+ void getResolvedUserConfig ( ) . then ( config => {
4781 plugin . onConfigurationChanged ?.( config )
4882 // temporary workaround
4983 if ( ! config . enablePlugin ) {
0 commit comments