Loading the TypeScript compiler into ClearScript, “WScript is undefined”, impossible task?

前端 未结 3 1666
故里飘歌
故里飘歌 2021-02-09 12:46

I tried using ClearScript to load the TypeScript compiler in order to compile some basic TypeScript code.

Unfortunately, when executing the TypeScript compiler source I

3条回答
  •  不思量自难忘°
    2021-02-09 13:46

    After 4 years, using the TypeScript 2.7 Services at https://rawgit.com/Microsoft/TypeScript/master/lib/typescriptServices.js it's kind of a two-liner now:

    • initialize your favorite JS engine with this script
    • load the string contents of your *.ts resource
    • get a function reference of ts.transpile
    • feed it the type script as a string and get back a string with the transpiled source

    Using e.g. a ClearScript engine in C# with TS sourcecode in the string src this would be:

    dynamic transpile = engine.Evaluate("ts.transpile");
    var js = transpile(src);
    // now feed the string js into a different JS engine
    

提交回复
热议问题