ceylon/ceylon-js — Issue #37: Overcomplicating the node module?
Author: chochos
Created: 2012-02-01 00:56:35+00:00
State: open
Labels: optimisation

The ceylon.language module is define like this in js:

````javascript
(function (define) {
define('ceylon.language', function (require, exports) {

//the Ceylon language module
...
exports.bla=bla;
});
}(typeof define==='function' && define.amd ?
define : function (id, factory) {
if (typeof exports!=='undefined') {
factory(require, exports);
} else {
throw "no module loader";
}
}));
````

What is all that stuff for? I was doing some tests for issue #36 and it turns out that if I remove all that stuff and just leave the code between "the Ceylon language module" up to the last `exports.bla`, it works perfectly in node.

Turns out this is the CommonJS format as described here http://wiki.commonjs.org/wiki/Modules/1.1.1#Module_Context

And to make it work in a browser, using require.js it's just a matter of adding a couple of lines of code:

````javascript
//this as first line of ceylon.language.js
define(function (require, exports, module) {
...
//this as the last line of the file
});
````

So... what is the idea behind all that extra stuff? Looks like the ability to just throw an exception if there is no module loader... but if there is no module loader, well loading this file will just result in an exception when it hits the first assign to `exports`.

I haven't pushed any of these changes to the repo because there might be a good reason I'm not aware of. But if having it work with node.js is OK and it leaves the file prepared for require.js (just need to add two lines) is more desirable, then I guess we should just remove all that unnecessary encapsulation...