Many ways of AMD compilation

Topics: General, Language Specification
Feb 3, 2014 at 7:15 PM
Ok this is out first project we truly started using typescript for production and development process. we are about to use Durandal to build our SPA application but some things are not obvious for us. for example if a have a Typescript file

Main.ts content of which is as following
import _app = require('Config')

export module App {
   export class Shell {

       public search = () => {
           var data = new _app.AppData.AppConfig();
        }
       public activate = () => {
       }
    }
}
this compiles to
define(["require", "exports", 'Config'], function(require, exports, _app) {
    (function (App) {
        var Shell = (function () {
            function Shell() {
                this.search = function () {
                    var data = new _app.AppData.AppConfig();
                };
                this.activate = function () {
                };
            }
            return Shell;
        })();
        App.Shell = Shell;
    })(exports.App || (exports.App = {}));
    var App = exports.App;
});
how ever according to new typescript specifications I do not need modules and I can have class per file and create exports in this way.
import _app = require('Config')
class Shell {
     public search = () => {
         var data = new _app.AppData.AppConfig();
     }
     public activate = () => {

     }
}
export = Shell;
it compiles to
define(["require", "exports", 'Config'], function(require, exports, _app) {
    var Shell = (function () {
        function Shell() {
            this.search = function () {
                var data = new _app.AppData.AppConfig();
            };
            this.activate = function () {
            };
        }
        return Shell;
    })();

    
    return Shell;
});
what is the different here when using module keyword and end export? what if I really want to organize my DomainObjets in namespaces and use Modules for that and what it there exports keyword in every generated js file with define function?

I think typescript is really enterprise level way of developing apps but all those unknown code generations makes me thing there is something more we do not understand at the moment.
Feb 4, 2014 at 7:31 PM
I don't think that you need internal modules inside external modules.
Just use the directory structure to namespace the AMD modules.

I have written a little introduction to TypeScript's module system. Maybe it can clarify some things for you.
http://blog.oio.de/2014/01/31/an-introduction-to-typescript-module-system/
Feb 4, 2014 at 8:57 PM
ok thats a very good starting point but can u explain what is that exports keyword define(["require", "exports"]) I do not need this at all is it some typescript compiler bug I am using AMD why do I need exports at all?
Feb 5, 2014 at 1:44 PM
@taicodev You don't need to worry about require and exports.

They're automatically added by tsc and they're used by require.js (there are three special/magic dependencies). exports is used if your file contains "export function" or "export class" statements (not export = ).

See details here: https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#wiki-magic

PromDoe is right - it's better to use file/folder hierarchy for organizing dependencies when you're using external modules.
Marked as answer by taicodev on 2/5/2014 at 11:35 AM
Feb 5, 2014 at 6:36 PM
ok this is what I was looking for but is there any way I could disable that export keyword when modules are generated? any way this is really good answer and exactly what I was looking for. thank u very much.
Feb 6, 2014 at 2:32 PM
@taicodev You don't need to worry about exports.

You do not need to disable it.

Just stop looking at the generated source because it will work just fine - trust me.

TypeScript compiler could omit exports when it's not needed (just like in your case). But it doesn't do that because the extra exports do not make any difference for require.js.

So once again - stop worrying about extra exports! It is perfectly fine and OK.