Use real maps for monaco-api/treeshaking

Working on understanding this code and trying to modernize it a little
This commit is contained in:
Matt Bierner
2025-10-07 16:56:14 -07:00
parent 45d6e6dc78
commit 68fb92d6e0
4 changed files with 68 additions and 70 deletions

View File

@@ -652,10 +652,10 @@ export class DeclarationResolver {
);
}
const fileContents = this._fsProvider.readFileSync(moduleId, fileName).toString();
const fileMap: IFileMap = {
'file.ts': fileContents
};
const service = this.ts.createLanguageService(new TypeScriptLanguageServiceHost(this.ts, {}, fileMap, {}));
const fileMap: IFileMap = new Map([
['file.ts', fileContents]
]);
const service = this.ts.createLanguageService(new TypeScriptLanguageServiceHost(this.ts, new Map(), fileMap, {}));
const text = service.getEmitOutput('file.ts', true, true).outputFiles[0].text;
return new CacheEntry(
this.ts.createSourceFile(fileName, text, this.ts.ScriptTarget.ES5),
@@ -670,10 +670,8 @@ export function run3(resolver: DeclarationResolver): IMonacoDeclarationResult |
}
interface ILibMap { [libName: string]: string }
interface IFileMap { [fileName: string]: string }
type ILibMap = Map</*libName*/ string, string>;
type IFileMap = Map</*fileName*/ string, string>;
class TypeScriptLanguageServiceHost implements ts.LanguageServiceHost {
@@ -695,11 +693,10 @@ class TypeScriptLanguageServiceHost implements ts.LanguageServiceHost {
return this._compilerOptions;
}
getScriptFileNames(): string[] {
return (
([] as string[])
.concat(Object.keys(this._libs))
.concat(Object.keys(this._files))
);
return [
...this._libs.keys(),
...this._files.keys(),
];
}
getScriptVersion(_fileName: string): string {
return '1';
@@ -708,10 +705,10 @@ class TypeScriptLanguageServiceHost implements ts.LanguageServiceHost {
return '1';
}
getScriptSnapshot(fileName: string): ts.IScriptSnapshot {
if (this._files.hasOwnProperty(fileName)) {
return this._ts.ScriptSnapshot.fromString(this._files[fileName]);
} else if (this._libs.hasOwnProperty(fileName)) {
return this._ts.ScriptSnapshot.fromString(this._libs[fileName]);
if (this._files.has(fileName)) {
return this._ts.ScriptSnapshot.fromString(this._files.get(fileName)!);
} else if (this._libs.has(fileName)) {
return this._ts.ScriptSnapshot.fromString(this._libs.get(fileName)!);
} else {
return this._ts.ScriptSnapshot.fromString('');
}
@@ -729,10 +726,10 @@ class TypeScriptLanguageServiceHost implements ts.LanguageServiceHost {
return fileName === this.getDefaultLibFileName(this._compilerOptions);
}
readFile(path: string, _encoding?: string): string | undefined {
return this._files[path] || this._libs[path];
return this._files.get(path) || this._libs.get(path);
}
fileExists(path: string): boolean {
return path in this._files || path in this._libs;
return this._files.has(path) || this._libs.has(path);
}
}