如何在浏览器中使用TypeScript编译器?
我正在尝试在浏览器中使用TypeScript编译器(例如Chrome): 上述代码失败,因为createCompilerHost在内部引用了未定义的ts.sys如何在浏览器中使用TypeScript编译器?,typescript,typescript-compiler-api,Typescript,Typescript Compiler Api,我正在尝试在浏览器中使用TypeScript编译器(例如Chrome): 上述代码失败,因为createCompilerHost在内部引用了未定义的ts.sys 如何确保ts.sys存在?我能自己模仿吗?分配ts.sys(ts.sys=…)失败,因为它是只读的。这可以通过创建自己的ts.CompilerHost实现来完成: const compilerHost: ts.CompilerHost = { getSourceFile: (fileName: string, languageV
如何确保ts.sys存在?我能自己模仿吗?分配ts.sys(ts.sys=…)失败,因为它是只读的。这可以通过创建自己的
ts.CompilerHost实现来完成:
const compilerHost: ts.CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void) => {
// you will need to implement a way to get source files here
},
getDefaultLibFileName: (defaultLibOptions: ts.CompilerOptions) => "/" + ts.getDefaultLibFileName(defaultLibOptions),
writeFile: () => {}, // do nothing
getCurrentDirectory: () => "/",
getDirectories: (path: string) => [],
fileExists: (fileName: string) => { /* implement this */ },
readFile: (fileName: string) => { /* implement this */ },
getCanonicalFileName: (fileName: string) => fileName,
useCaseSensitiveFileNames: () => true,
getNewLine: () => "\n",
getEnvironmentVariable: () => "" // do nothing
};
使用我的库可能更容易,因为它应该在浏览器中工作,并为您加载所有库声明文件。。。如果它不起作用,那么在回购协议的问题上让我知道。要在web上使用它,只需指定使用内存文件系统:
import { Project, ts } from "@ts-morph/bootstrap";
const project = new Project({ useInMemoryFileSystem: true });
// use project here...
import { Project, ts } from "@ts-morph/bootstrap";
const project = new Project({ useInMemoryFileSystem: true });
// use project here...