Path: blob/main/build/lib/mangle/staticLanguageServiceHost.ts
3520 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import ts from 'typescript';6import path from 'path';78export class StaticLanguageServiceHost implements ts.LanguageServiceHost {910private readonly _cmdLine: ts.ParsedCommandLine;11private readonly _scriptSnapshots: Map<string, ts.IScriptSnapshot> = new Map();1213constructor(readonly projectPath: string) {14const existingOptions: Partial<ts.CompilerOptions> = {};15const parsed = ts.readConfigFile(projectPath, ts.sys.readFile);16if (parsed.error) {17throw parsed.error;18}19this._cmdLine = ts.parseJsonConfigFileContent(parsed.config, ts.sys, path.dirname(projectPath), existingOptions);20if (this._cmdLine.errors.length > 0) {21throw parsed.error;22}23}24getCompilationSettings(): ts.CompilerOptions {25return this._cmdLine.options;26}27getScriptFileNames(): string[] {28return this._cmdLine.fileNames;29}30getScriptVersion(_fileName: string): string {31return '1';32}33getProjectVersion(): string {34return '1';35}36getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {37let result: ts.IScriptSnapshot | undefined = this._scriptSnapshots.get(fileName);38if (result === undefined) {39const content = ts.sys.readFile(fileName);40if (content === undefined) {41return undefined;42}43result = ts.ScriptSnapshot.fromString(content);44this._scriptSnapshots.set(fileName, result);45}46return result;47}48getCurrentDirectory(): string {49return path.dirname(this.projectPath);50}51getDefaultLibFileName(options: ts.CompilerOptions): string {52return ts.getDefaultLibFilePath(options);53}54directoryExists = ts.sys.directoryExists;55getDirectories = ts.sys.getDirectories;56fileExists = ts.sys.fileExists;57readFile = ts.sys.readFile;58readDirectory = ts.sys.readDirectory;59// this is necessary to make source references work.60realpath = ts.sys.realpath;61}626364