Domanda

I am trying to run the TypeScript compiler from my Java application. To start, I am trying to figure out, whether I can run the compiler from command-line without Node.js:

$ jsc tsc.js

But this way I don't get any errors, nor help.

$ jsc tsc.js myscript.ts

Will get me nowhere.

It is easy to run js code directly from java (and I am hoping to run the compiler in this way), but is it possible to run TypeScript compiler without node.js?

EDIT:

I confirm the same behaviour with rhino.

È stato utile?

Soluzione 2

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top