@@ -19,13 +19,19 @@ import { readFileSync } from 'fs';
1919import { URL } from 'url' ;
2020
2121let logger : Logger ;
22+ let pluginInfo : ts_module . server . PluginCreateInfo ;
2223
2324type DenoPluginConfig = {
2425 enable : boolean ;
2526 importmap ?: string ;
27+ tsconfig ?: string ;
2628 dtsPath ?: string ;
2729} ;
2830
31+ const config : DenoPluginConfig = {
32+ enable : true ,
33+ } ;
34+
2935module . exports = function init (
3036 { typescript } : { typescript : typeof ts_module } ,
3137) {
@@ -68,32 +74,24 @@ module.exports = function init(
6874 logger = Logger . forPlugin ( info ) ;
6975 logger . info ( "plugin created." ) ;
7076
77+ pluginInfo = info ;
7178 const tsLs = info . languageService ;
7279 const tsLsHost = info . languageServiceHost ;
7380 const project = info . project ;
74- const config : DenoPluginConfig = {
75- enable : true ,
76- ...info . config ,
77- } ;
81+
82+ Object . assign ( config , info . config ) ;
7883
7984 if ( ! config . enable ) {
8085 logger . info ( "plugin disabled." ) ;
8186 return tsLs ;
8287 }
8388
84- logger . info ( 'config:\n' + JSON . stringify ( config , null , ' ' ) ) ;
85-
8689 const projectDirectory = project . getCurrentDirectory ( ) ;
8790 // TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
8891 process . chdir ( projectDirectory ) ;
8992
9093 let parsedImportMap : ImportMaps | null = null ;
9194
92- if ( config . importmap != null ) {
93- logger . info ( 'use import maps: ' + config . importmap ) ;
94- parsedImportMap = parseImportMapFromFile ( projectDirectory , config . importmap ) ;
95- }
96-
9795 const resolveTypeReferenceDirectives =
9896 tsLsHost . resolveTypeReferenceDirectives ;
9997
@@ -104,6 +102,7 @@ module.exports = function init(
104102 redirectedReference : ts_module . ResolvedProjectReference | undefined ,
105103 options : ts_module . CompilerOptions ,
106104 ) : ( ts_module . ResolvedTypeReferenceDirective | undefined ) [ ] => {
105+
107106 const ret = resolveTypeReferenceDirectives . call (
108107 tsLsHost ,
109108 typeDirectiveNames ,
@@ -112,6 +111,11 @@ module.exports = function init(
112111 options ,
113112 ) ;
114113
114+ if ( ! config . enable ) {
115+ logger . info ( "plugin disabled." ) ;
116+ return ret ;
117+ }
118+
115119 return ret ;
116120 } ;
117121 }
@@ -123,9 +127,19 @@ module.exports = function init(
123127 tsLsHost . resolveModuleNames = (
124128 moduleNames : string [ ] ,
125129 containingFile : string ,
130+ ...rest
126131 ) => {
132+ if ( ! config . enable ) {
133+ logger . info ( "plugin disabled." ) ;
134+ return resolveModuleNames . call ( tsLsHost , moduleNames , containingFile , ...rest ) ;
135+ }
136+
127137 const resolvedModules : ( ResolvedModuleFull | undefined ) [ ] = [ ] ;
128138
139+ if ( config . importmap != null ) {
140+ parsedImportMap = parseImportMapFromFile ( projectDirectory , config . importmap ) ;
141+ }
142+
129143 // try resolve typeReferenceDirectives
130144 for ( let moduleName of moduleNames ) {
131145 if ( parsedImportMap !== null ) {
@@ -190,6 +204,10 @@ module.exports = function init(
190204 info . languageServiceHost . getCompilationSettings ;
191205
192206 info . languageServiceHost . getCompilationSettings = ( ) => {
207+ if ( ! config . enable ) {
208+ return getCompilationSettings . call ( tsLsHost ) ;
209+ }
210+
193211 const projectConfig = getCompilationSettings . call (
194212 info . languageServiceHost ,
195213 ) ;
@@ -203,6 +221,10 @@ module.exports = function init(
203221
204222 const getScriptFileNames = info . languageServiceHost . getScriptFileNames ! ;
205223 info . languageServiceHost . getScriptFileNames = ( ) => {
224+ if ( ! config . enable ) {
225+ return getScriptFileNames . call ( tsLsHost ) ;
226+ }
227+
206228 const scriptFileNames = getScriptFileNames . call (
207229 info . languageServiceHost ,
208230 ) ;
@@ -240,6 +262,10 @@ module.exports = function init(
240262 preferences ,
241263 ) ;
242264
265+ if ( ! config . enable ) {
266+ return details ;
267+ }
268+
243269 if ( details ) {
244270 if ( details . codeActions && details . codeActions . length ) {
245271 for ( const ca of details . codeActions ) {
@@ -263,6 +289,10 @@ module.exports = function init(
263289 function getSemanticDiagnostics ( filename : string ) {
264290 const diagnostics = tsLs . getSemanticDiagnostics ( filename ) ;
265291
292+ if ( ! config . enable ) {
293+ return diagnostics ;
294+ }
295+
266296 // ref: https://github.com/denoland/deno/blob/da8cb408c878aa6e90542e26173f1f14b5254d29/cli/js/compiler/util.ts#L262
267297 const ignoredDiagnostics = [
268298 // TS2306: File 'file:///Users/rld/src/deno/cli/tests/subdir/amd_like.js' is
@@ -311,6 +341,13 @@ module.exports = function init(
311341
312342 return proxy ;
313343 } ,
344+
345+ onConfigurationChanged ( c : DenoPluginConfig ) {
346+ Object . assign ( config , c ) ;
347+ pluginInfo . project . markAsDirty ( ) ;
348+ pluginInfo . project . refreshDiagnostics ( ) ;
349+ pluginInfo . project . updateGraph ( ) ;
350+ }
314351 } ;
315352} ;
316353
0 commit comments