The AMD output of traceur has me a bit confused. The return value looks a lot like an ES6 class - shouldn't this be converting all the way to ES5? FWIW, my options (to traceur) are:
{
modules: "amd",
outputLanguage: 'es5'
}
define([], function() { | |
"use strict"; | |
var myVar = {}; | |
var $__default = myVar; | |
return { | |
get default() { | |
return $__default; | |
}, | |
__esModule: true | |
}; | |
}); |
var myVar = {}; | |
export default myVar; |
@eventualbuddha: ES6 and (node flavored) CJS both have different cyclic dependency strategies so
ES6 cyclic dependencies that don't work in CJS
//even.js
import { odd } from './odd'
export var counter = 0;
export function even(n) {
counter++;
return n == 0 || odd(n - 1);
}
//odd.js
import { even } from './even';
export function odd(n) {
return n != 0 && even(n - 1);
}
//app.js
import { even } from './even';
even(); //this works
CJS circular dependencies that don't work in ES6
//a.js
exports.obj = {};
var b = require('./b');
b.something();
//b.js
var a = require('./a');
a.obj.val = 'asdf';
exports.something = function() {};
//app.js
require('./a'); // this doesn't throw
Agreed with @eventualbuddha, that example would work because of partially loaded modules.
Modules are cached after the first time they are loaded. This means (among other things) that every call to
require('foo')
will get exactly the same object returned, if it would resolve to the same file. Multiple calls torequire('foo')
may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.
edit: This was posted way after composing it, and a few comments were posted before it.
@eventualbuddha You may be right. I’ll have to give this more thought (which will take a while – busy ATM).