meikeric / NTypeScript

A C# wrapper for TypeScript - Compile TypeScript from C#, embed the TypeScript compiler in your .NET apps

Home Page:https://0xfireball.github.io/NTypeScript/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NTypeScript / TypeScript.NET

A C#/.NET wrapper for TypeScript

License: MIT

Features

  • Compile TypeScript from C#
  • Execute compiled JavaScript with built-in JurassicJS Engine
  • Asynchronous API
  • Can run latest TypeScript version

Usage

        private static async Task MainAsync()
        {
            //Create a new Compiler instance (it has yet to be initialized)
            var compiler = new TypeScriptCompiler();
            Console.WriteLine("Compiling compiler...");

            //InitializeCompilerAsync() loads the compiler from a resource
            //and compiles it. This is relatively resource expensive; it can
            //take a few seconds to compile the TypeScript compiler and
            //quite a few MB of RAM.
            //Right now, NTypeScript uses JurassicJS as its engine, because
            //Jurassic compiles to IL code.
            //Once the compiler is compiled, the compiler can be used to compile
            //TypeScript.
            await compiler.InitializeCompilerAsync();
            string helloWorldScript = @"
class RandomProgram {
    sayHello() {
        console.log(""Hello, World!"");
    }
}
let myProgram = new RandomProgram();
myProgram.sayHello();
";
            Console.WriteLine("Compiling Hello World script...");
            
            //This will compile the script with the compiler that was
            //compiled earlier. This isn't too resource intensive, but
            //takes about the same amount of time as the normal TypeScript
            //compiler running in Node.JS
            var transpiledHelloWorldScript = await compiler.CompileAsync(helloWorldScript);
            Console.WriteLine(transpiledHelloWorldScript);

            //Run the script
            var jsExecutor = new JavaScriptExecutor();
            jsExecutor.EnableConsoleApi();
            await jsExecutor.ExecuteAsync(transpiledHelloWorldScript);
        }

Copyright (c) 2016 Nihal Talur, 0xFireball, IridiumIon Software

About

A C# wrapper for TypeScript - Compile TypeScript from C#, embed the TypeScript compiler in your .NET apps

https://0xfireball.github.io/NTypeScript/

License:MIT License


Languages

Language:JavaScript 99.8%Language:C# 0.2%