Run TypeScript compiler from Java

偶尔善良 提交于 2019-12-04 10:49:06

Looking at the source code, the tsc command invokes a JS script tsc.js, which has 2 backends: Node.js and Windows Scripting Host. If any other JavaScript server supports reading and writing to a file system (like Rhino with RingoJS), it should be able to run the TypeScript compiler tsc.js.

Moreover, there is a fork of TypeScript compiler which claims to directly run on Rhino. So you could invoke Rhino directly from Java, without installing node.js.

I have a project, Typescript4j that does precisely this.

It runs the Typescript compiler wrapped within Rhino.

I'm using it successfully within Bakehouse, and a non-trivial Typescript application.

what you want to do is jsc node_modules/typescript/lib/tsc.js file1.ts, but unfortunately that won't work with engines different than node.js.

What will work (or at least works in the browser), is using TypeScript Compiler API, instead of trying to use the CLI (you will have to program). In the browser, you do this by loading the file node_modules/typescript/typescript.js and then you have access to the compiler API (https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API) via the global ts. Here you have an example of how to transpile a ts string to js using the compiler API: https://typescript-api-playground.glitch.me/#example=Transpiling-a-single-file

Good luck

The TypeScript compiler is implemented in TypeScript, and can be used in any JavaScript host.

You may need to specify the full path to tsc.js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!