import as annotation and --module none

Topics: General
May 22, 2014 at 10:31 AM
Take a look at this very simple example:
import _path = require("path");
var path: typeof _path = require("path");
if you try to compile this with options: module set to none and combine to single js, you get error:

error TS5037: Build: Cannot compile external modules unless the '--module' flag is provided.

But there is no external modules here, because import is used only as annotation, so this should be compiled just fine.

May 23, 2014 at 8:36 PM
The 'import' is what turns it into an external module, even if it's ultimately compiled out. We could consider changing that, but I'm not clear on what the usage scenario that's currently blocked. What would you need to use this for?
Jun 5, 2014 at 7:35 AM
In short, releasing this lock will allow me to compile a project to single js file and then use this file as amd/commonjs external module.

Currently there is no support to create single js file and use external modules. To achieve this you must set –module to none, compile to single js file and remove all top export directives. After compiling you change output js file so that you manually add code to export modules. In simple case when commonjs library has single root module, you just add something like this at the end:
exports.rootmodule = rootmodule;
On the import side you then use:
var __ rootmodule: typeof rootmodule = require("./rootmodule"). rootmodule;
Problem is that this statements can’t be used in all situations, for example it doesn’t work if you try to require path module in node. So you end up without type information. If you enable import as type annotation when –module is set to none you will be able to use:
import _path = require("path");
var path: typeof _path = require("path");
For me this will be very helpful. In big project it is really difficult to maintain all those import paths. For example just renaming/moving file to other location may require a lot of changes in your code. Also I don’t feel very comfortable if I have to deploy 1000+ files, which should be no more than 10 js libraries. Also using single file for more classes it is not an option, I have tried this and is totally unmaintainable.

It would be great if tsc will allow to compile to single file with commonjs/amd support. Of course because project(all files) will have single js module output, you can force that all import/export directive must be used inside _references.ts.

thanks again