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 { 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 */ /** A full HTTP path to a JavaScript file which adds a function `customTSWorkerFactory` to the self inside a web-worker */
customWorkerPath?: string; customWorkerPath?: string;
} }

View file

@ -13,6 +13,8 @@ import {
} from './monaco.contribution'; } from './monaco.contribution';
import { Uri, worker } from '../../fillers/monaco-editor-core'; 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. * 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. * 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 _ctx: worker.IWorkerContext;
private _extraLibs: IExtraLibs = Object.create(null); private _extraLibs: IExtraLibs = Object.create(null);
private _languageService = ts.createLanguageService(this); private _languageService = tsImpl.createLanguageService(this);
private _compilerOptions: ts.CompilerOptions; private _compilerOptions: ts.CompilerOptions;
private _inlayHintsOptions?: ts.InlayHintsOptions; private _inlayHintsOptions?: ts.InlayHintsOptions;
@ -462,6 +464,7 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, ITypeScriptWork
export interface ICreateData { export interface ICreateData {
compilerOptions: ts.CompilerOptions; compilerOptions: ts.CompilerOptions;
extraLibs: IExtraLibs; extraLibs: IExtraLibs;
customTypeScriptPath?: string;
customWorkerPath?: string; customWorkerPath?: string;
inlayHintsOptions?: ts.InlayHintsOptions; inlayHintsOptions?: ts.InlayHintsOptions;
} }
@ -482,6 +485,23 @@ declare global {
export function create(ctx: worker.IWorkerContext, createData: ICreateData): TypeScriptWorker { export function create(ctx: worker.IWorkerContext, createData: ICreateData): TypeScriptWorker {
let TSWorkerClass = 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 (createData.customWorkerPath) {
if (typeof importScripts === 'undefined') { if (typeof importScripts === 'undefined') {
console.warn( 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: { createData: {
compilerOptions: this._defaults.getCompilerOptions(), compilerOptions: this._defaults.getCompilerOptions(),
extraLibs: this._defaults.getExtraLibs(), extraLibs: this._defaults.getExtraLibs(),
customTypeScriptPath: this._defaults.workerOptions.customTypeScriptPath,
customWorkerPath: this._defaults.workerOptions.customWorkerPath, customWorkerPath: this._defaults.workerOptions.customWorkerPath,
inlayHintsOptions: this._defaults.inlayHintsOptions inlayHintsOptions: this._defaults.inlayHintsOptions
} }