diff --git a/src/language/typescript/monaco.contribution.ts b/src/language/typescript/monaco.contribution.ts index 4220bcfb..5de24b2e 100644 --- a/src/language/typescript/monaco.contribution.ts +++ b/src/language/typescript/monaco.contribution.ts @@ -164,6 +164,8 @@ export interface DiagnosticsOptions { } export interface WorkerOptions { + /** A full HTTP path to a typescript.js file that will define a global `ts` which the worker will use */ + customTypeScriptPath?: string; /** A full HTTP path to a JavaScript file which adds a function `customTSWorkerFactory` to the self inside a web-worker */ customWorkerPath?: string; } diff --git a/src/language/typescript/tsWorker.ts b/src/language/typescript/tsWorker.ts index 51882423..43e78573 100644 --- a/src/language/typescript/tsWorker.ts +++ b/src/language/typescript/tsWorker.ts @@ -13,6 +13,8 @@ import { } from './monaco.contribution'; import { Uri, worker } from '../../fillers/monaco-editor-core'; +let tsImpl: typeof ts = ts; + /** * Loading a default lib as a source file will mess up TS completely. * So our strategy is to hide such a text model from TS. @@ -36,7 +38,7 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, ITypeScriptWork private _ctx: worker.IWorkerContext; private _extraLibs: IExtraLibs = Object.create(null); - private _languageService = ts.createLanguageService(this); + private _languageService = tsImpl.createLanguageService(this); private _compilerOptions: ts.CompilerOptions; private _inlayHintsOptions?: ts.InlayHintsOptions; @@ -462,6 +464,7 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, ITypeScriptWork export interface ICreateData { compilerOptions: ts.CompilerOptions; extraLibs: IExtraLibs; + customTypeScriptPath?: string; customWorkerPath?: string; inlayHintsOptions?: ts.InlayHintsOptions; } @@ -482,6 +485,23 @@ declare global { export function create(ctx: worker.IWorkerContext, createData: ICreateData): TypeScriptWorker { let TSWorkerClass = TypeScriptWorker; + if (createData.customTypeScriptPath) { + if (typeof importScripts === 'undefined') { + console.warn( + 'Monaco is not using webworkers for background tasks, and that is needed to support the customTypeScriptPath flag' + ); + } else { + (globalThis).ts = undefined; + self.importScripts(createData.customTypeScriptPath); + if (!(globalThis).ts) { + console.warn( + 'No global `ts` defined after importing custom TypeScript, using builtin TypeScript!' + ); + } else { + tsImpl = (globalThis).ts; + } + } + } if (createData.customWorkerPath) { if (typeof importScripts === 'undefined') { console.warn( @@ -497,7 +517,7 @@ export function create(ctx: worker.IWorkerContext, createData: ICreateData): Typ ); } - TSWorkerClass = workerFactoryFunc(TypeScriptWorker, ts, libFileMap); + TSWorkerClass = workerFactoryFunc(TypeScriptWorker, tsImpl, libFileMap); } } diff --git a/src/language/typescript/workerManager.ts b/src/language/typescript/workerManager.ts index 8ae5a78f..7f7cd2a6 100644 --- a/src/language/typescript/workerManager.ts +++ b/src/language/typescript/workerManager.ts @@ -70,6 +70,7 @@ export class WorkerManager { createData: { compilerOptions: this._defaults.getCompilerOptions(), extraLibs: this._defaults.getExtraLibs(), + customTypeScriptPath: this._defaults.workerOptions.customTypeScriptPath, customWorkerPath: this._defaults.workerOptions.customWorkerPath, inlayHintsOptions: this._defaults.inlayHintsOptions }