Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Typescript Compiler from C# #2565

Closed
mike-ward opened this issue Mar 31, 2015 · 8 comments
Closed

Call Typescript Compiler from C# #2565

mike-ward opened this issue Mar 31, 2015 · 8 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@mike-ward
Copy link

Create nuget module to invokeTypescript compiler in C# to compile Typscript code specified in C# strings.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Mar 31, 2015
@RyanCavanaugh
Copy link
Member

Someone else can do this.

@mike-ward
Copy link
Author

Um...

I would have expected something like:

            var scType = Type.GetTypeFromProgID("MSScriptControl.ScriptControl.1");
            dynamic sc = Activator.CreateInstance(scType);
            sc.Language = "JavaScript";
            sc.AddCode(tsc.js.text);

to work. But it doesn't. Guess that, "Someone else" has his work cut out for him.

@DanielRosenwasser
Copy link
Member

Not really. It's not entirely trivial, but it's decently easy to use Chakra/JsRT from C# with the following resource. You can check out the C# version of the sample app which is Apache 2.0 licensed.

@DanielRosenwasser
Copy link
Member

Also you can use Edge.js

@DickvdBrink
Copy link
Contributor

I did something like that here: https://github.com/DickvdBrink/TypeScriptSyntaxVisualizer/blob/master/TypeScriptSyntaxVisualizer/TypeScript/TypeScriptContext.cs
Note: It is really old and won't work with the latest TS anymore

@mike-ward
Copy link
Author

OK, just to close the loop on this one, I got this working by removing the last line in tsc.js where is invokes the command line processling.

I then created the following JS code:

var tsc = function(input) {
  var opts = {};
  var host = ts.createCompilerHost(opts);

  var output = '';
  host.writeFile = function (filename, text) { output += text; };
  host.getSourceFile = function() { return ts.createSourceFile('textsource', input, 0, '0'); }
  host.getDefaultLibFilename = function () { return ''; }
  host.getCurrentDirectory = function () { return '/' }
  host.useCaseSensitiveFileNames = function () { return false; }
  host.getCanonicalFileName = function (text) { return text; }
  host.getNewLine = function () { return '\n'; }

  var prog = ts.createProgram(['textsource'], opts, host);
  prog.getDiagnostics();
  var checker = prog.getTypeChecker(true);
  checker.emitFiles();
  return output;
}

var typescriptout = tsc(typescriptin);

and finally the C# code to invoke it:

    public static string Compile(string text)
    {
        try
        {
            var engine = new Engine();
            engine.SetValue("typescriptin", text);
            engine.Execute(Resources.TypeScript);
            engine.Execute(Resources.tsc);
            var js = engine.GetValue("typescriptout").AsString();
            return js;
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

I'm sure I missed a few things but there you have it. It of course relies on knowing something about the inner workings which means it will likely break in the next version. Maybe a method could be added in some future version that takes strings instead of files?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 1, 2015

I would highly recommend using the compiler API for these scenarios instead of just replacing strings in the compiler.

here is how to use the compiler API: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API

And starting from next release, you should have a new API for string -> string transformation for modules, see issue #2499 for more details.

@mike-ward
Copy link
Author

Awesome. Just what I needed.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants