Can we have the ability to name the AMD modules ?

Topics: General, Language Specification
Jul 26, 2013 at 12:35 AM
Edited Jul 26, 2013 at 1:13 AM
When the typescript classes are built using AMD modules, you cannot define the module name. This means that you rely either the path, a require config and diligence of developer to import consistently.

For example...
// File: app/classes/Timer.ts

class Timer {
}

export = Timer;
// File: app/classes/Greeter.ts
import Timer = require("Timer");

class Greeter extends Timer {
}
In the above code because Timer and Greeter files are in the same folder, the compiler can locate the import. You can also replace the import with
import Timer = require("app/classes/Timer");
Now it is a fully qualified path, and this still works.
However there is a difference in the define statement generated
// Timer.ts
import Timer = require("Timer");

// Timer.js
define(["require", "exports", 'Timer'], function(require, exports, __Timer__) {
...
});
// Greeter.ts
import Timer = require("app/classes/Timer");

// Greeter.js
define(["require", "exports", 'app/classes/Timer'], function(require, exports, __Timer__) {
...
});
You can see that the name of the dependency has changed.

In both cases, the Timer module and Greeter module do not have a name defined so you will be relying on a implicit path or an explicit require config paths find the module.

This situation is completely broken if you then combine javascript files ( for example using ASP.NET MVC bundles ), you no longer have any way of locating these modules as each module has no name and the combined javascript file will contain many modules.

We need a way of being able to name each module on the export statement, or the compiler always uses the full path to the typescript file as the module name.
Coordinator
Jul 29, 2013 at 2:04 PM
There's a feature request for this you may want to upvote: https://typescript.codeplex.com/workitem/285
Developer
Jul 31, 2013 at 9:54 PM
The AMD convention is to author anonymous modules and then use tools like r.js from RequireJS to combine the individual modules. This could conceivably be a feature of the TypeScript compiler (as could minification, for example), but no such plans for 1.0.
Aug 1, 2013 at 10:12 PM
Edited Aug 1, 2013 at 10:40 PM
I think that the inconsistency of the import name is much more of a problem than the ability to combine.

The TS compiler should ALWAYS name the module, and it should be full path. If that means only 1 class per file then so be it ( but you can only have 1 export statement per file anyway ).

Importing the module should also always refer to the full path in the definition. According to RequireJS, the AMD loader will name the module internally anyway

http://requirejs.org/docs/whyamd.html#namedmodules