To achieve this, you can develop your custom implementation of ts.CompilerHost
:
const customCompilerHost: ts.CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void) => {
// Your logic to obtain the source files goes here
},
getDefaultLibFileName: (defaultLibOptions: ts.CompilerOptions) => "/" + ts.getDefaultLibFileName(defaultLibOptions),
writeFile: () => {}, // No action performed
getCurrentDirectory: () => "/",
getDirectories: (path: string) => [],
fileExists: (fileName: string) => { /* Custom implementation */ },
readFile: (fileName: string) => { /* Custom implementation */ },
getCanonicalFileName: (fileName: string) => fileName,
useCaseSensitiveFileNames: () => true,
getNewLine: () => "\n",
getEnvironmentVariable: () => "" // No operation
};
If you prefer a simpler solution, you may consider using my library @ts-morph/bootstrap, which is designed to function in browsers and load all necessary lib declaration files for you. Should any issues arise, please notify me by creating an issue on the repository. When utilizing it online, specify the usage of an in-memory file system:
import { Project, ts } from "@ts-morph/bootstrap";
const project = new Project({ useInMemoryFileSystem: true });
// Carry out actions with the project...