Add a customTypeScriptPath option which should point to a script which defines a global ts

This commit is contained in:
Alex Dima 2023-03-03 17:41:09 +01:00
parent f20e0c8d47
commit 3ccd487d9f
No known key found for this signature in database
GPG Key ID: 39563C1504FDD0C9
3 changed files with 25 additions and 2 deletions

View File

@ -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;
}

View File

@ -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 {
(<any>globalThis).ts = undefined;
self.importScripts(createData.customTypeScriptPath);
if (!(<any>globalThis).ts) {
console.warn(
'No global `ts` defined after importing custom TypeScript, using builtin TypeScript!'
);
} else {
tsImpl = (<any>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);
}
}

View File

@ -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
}