I'm trying to compile a full typescript project using the typescript compiler. What I got so far is that the files specified in the tsconfig.json file are compiled and available. However, I also want to access the files on which the project files are dependent on, so files in node_modules. But only the ones that are used by the files specified in the tsconfig.json file. How can I do that?
import * as fs from "fs"
import * as ts from "typescript"
function compile(fileNames: string[], options: ts.CompilerOptions): void {
const createdFiles: { [key: string]: string } = {}
const host = ts.createCompilerHost(options);
host.writeFile = (fileName: string, contents: string) => {
console.log(">>", fileName)
createdFiles[fileName] = contents
}
const program = ts.createProgram(fileNames, options, host);
program.emit();
}
const parseConfigHost /* : any */ = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
readFile: (file: string) => fs.readFileSync(file, 'utf8'),
useCaseSensitiveFileNames: true,
}
const x = ts.parseJsonConfigFileContent(
ts.readConfigFile('./tsconfig.json', ts.sys.readFile),
parseConfigHost,
".",
)
compile(x.fileNames, x.options)