34

Closed

NPM module should export the TypeScriptCompiler

description

I can't find a good way to programmatically compile TypeScript using the NPM module. What I'd like to do is the following.
npm install typescript
var TypeScript = require('typescript');
var compiler = new TypeScript.TypeScriptCompiler();
// ... do something with the compiler here
Closed Jul 28, 2014 at 10:18 PM by jonturner
As part of our move to GitHub, we're closing our CodePlex suggestions and asking that people move them to the GitHub issue tracker for further discussion. Some feature requests may already be active on GitHub, so please make sure to look for an existing issue before filing a new one.

You can find our GitHub issue tracker here:
https://github.com/microsoft/typeScript/issues

comments

jpoehls wrote Oct 4, 2012 at 3:21 PM

Ideally TypeScript would provide an interface similar to the coffee-script compiler module which works like this:

var coffee = require('coffee-script');
coffee.compile("a = 1");
//=> '(function() {\n var a;\n\n a = 1;\n\n}).call(this);\n'

orefalo wrote Oct 4, 2012 at 8:46 PM

+10 on this one. This interface is required to smoothly integrate TS with a node asset manager...

edwinyzh wrote Oct 7, 2012 at 6:22 AM

+1! I vote this as the developer of LIVEditor (live code editor for the web, at http://liveditor.com), since I guess LIVEditor will mostly like support TypeScript in the future.

clausreinke wrote Dec 1, 2012 at 8:59 PM

Not just the compiler, also the language services, please.

'typescript.js' and friends are included in the npm package, but do not export anything.
They cannot export anything because doing so would drop them out of the global module, preventing them from other parts of the compiler, which also rely on the global module and reference path includes.

So it seems that a pre-requisite for this ticket would be to modularize the compiler (and language services), which would also be a great driver for testing and improving the external module system support.

related issue: http://typescript.codeplex.com/workitem/521

RyanCavanaugh wrote Jan 29, 2013 at 12:08 AM

Changing to suggestion assigned to Jonathan

jonturner wrote Mar 7, 2013 at 9:53 PM

We're looking into language extensions that will make it much easier to export the compiler. In 0.9.0, we're planning a way of saying "export = <symbol>" which would allow you to export the internal symbol/module that represents the compiler.

Doing so would be experimental in that the compiler API may change. We have not fully worked out all the scenarios to build the proper API, yet.

potmo wrote Mar 9, 2014 at 9:45 PM

Any updates on this?

mblund wrote May 22, 2014 at 10:20 PM

Any news on this one? I'm trying to use it like the coffee-script example in the description to create a asset compiler for sbt-web. Just like https://github.com/sbt/sbt-coffeescript/blob/master/src/main/resources/coffee.js

Arnavion wrote May 24, 2014 at 12:07 AM

It is currently possible to do this using the vm module. Use fs.readFile() to read typescript.js from ./node_modules/typescript/bin, then add module.exports = TypeScript; at the end and run this via vm.runInNewContext().

See my compiler wrapper lines 29 to 42. Something similar is done by grunt-typescript.

The exported TypeScript object will have the TypeScriptCompiler available to use. There is no documentation on how to use TypeScriptCompiler, but you can look at how BatchCompiler does it in tsc.js - BatchCompiler is what runs when you invoke tsc on the shell and internally uses TypeScriptCompiler.

The caveat is that the npm package does not contain the language service, so if you need that, you'd have to build typescript yourself and then check it in along with your code.

Of course, this is all a hack while we wait for the TS team to actually export the compiler from typescript.js and document the compiler API. Some of us have been hacking at extending the compiler in #typescript on Freenode so you're welcome to join and ask.

mblund wrote May 25, 2014 at 5:32 PM

Thanks Arnavion! Great examples and links. I manage to get the TypeScript reference and will dive into the compile 'API' now.