Typescript: Is there some API for node to take '.ts' to '.js'? Not the bin for shell

Created on 29 Jun 2018  ·  3Comments  ·  Source: microsoft/TypeScript

Search Terms

node ; api;

Suggestion

Need some api for nodejs to take '.ts' to '.js', and return the '.js' content in string, so I can use the to do STH.
But I don't want to use the shell bin.

Use Cases

I want to take a ts preview , so I can see the '.js' file in moment , I think it's good for the ts-leaner.

Question

Most helpful comment

You can use the TypeScript compiler to transform .ts files to .js files.

> tsc myTsFile.ts --outFile myJsFile.js

Call that from node (subbing in the file names you actually want), then do this:

fs.readFile('myJsFile.js', function (err, data) {
    if (err) throw err;
    var jsFileText = data.toString();
    // Do something with the JS file text
});

Although if I understand your use-case correctly, you might be looking for the TypeScript Playground?

EDIT: Since the compiler is written in TypeScript, and thus compiles to JavaScript, you should just be able to invoke the compiler from Node directly.

All 3 comments

You can use the TypeScript compiler to transform .ts files to .js files.

> tsc myTsFile.ts --outFile myJsFile.js

Call that from node (subbing in the file names you actually want), then do this:

fs.readFile('myJsFile.js', function (err, data) {
    if (err) throw err;
    var jsFileText = data.toString();
    // Do something with the JS file text
});

Although if I understand your use-case correctly, you might be looking for the TypeScript Playground?

EDIT: Since the compiler is written in TypeScript, and thus compiles to JavaScript, you should just be able to invoke the compiler from Node directly.

You probably want to take a look at https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API if you need something more specialized than what the playground provides.

Specifically, we have a transpileModule function.

@JakeTunaley @DanielRosenwasser

THX. 😁

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbuckton picture rbuckton  ·  139Comments

disshishkov picture disshishkov  ·  224Comments

Gaelan picture Gaelan  ·  231Comments

tenry92 picture tenry92  ·  146Comments

yortus picture yortus  ·  157Comments