node ; api;
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.
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.
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. 😁
Most helpful comment
You can use the TypeScript compiler to transform .ts files to .js files.
Call that from node (subbing in the file names you actually want), then do this:
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.