Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Last active February 12, 2024 00:36
Show Gist options
  • Save unscriptable/5204641 to your computer and use it in GitHub Desktop.
Save unscriptable/5204641 to your computer and use it in GitHub Desktop.
async module "factory" evaluation for inline module declarations. I want to ensure that our AMD+CJS tools have consistent behavior with ES6.
<script>
var flag;
// this variable is the interesting bit
flag = "initial value";
define('unicorn', function (require, exports) {
var rainbow = require('rainbow');
exports.squeeze = function () { return rainbow(flag); };
// modify flag when factory evaluates
flag = "modified value";
});
// this should log "initial value" if module evaluates JIT
console.log(flag);
// import "unicorn"
define('puppies-and-kittens' function (require, exports) {
var playmate = require('unicorn');
playmate.squeeze();
});
// this should log "modified value" now
console.log(flag);
</script>
<script>
var flag;
// this variable is the interesting bit
flag = 'initial value';
module "unicorn" {
import { rainbow } from "rainbow";
export function squeeze () { return rainbow(flag); }
// modify flag when "factory" evaluates
flag = "modified value";
}
// this should log "initial value" if module evaluates JIT
console.log(flag);
// import "unicorn"
module "puppies-and-kittens" {
import { squeeze: playmate } from 'unicorn';
squeeze();
}
// this should log "modified value" now
console.log(flag);
</script>
@dherman
Copy link

dherman commented May 1, 2013

Hi Jon,

I'm pretty sure different AMD loaders do things in different orders. Yehuda and I looked into this and it doesn't seem like any of the ones we saw do this in the order your comments suggest. ES6 will probably not exactly agree with existing AMD loaders on the order of execution, but given that AMD doesn't seem to mandate a particular order, I'd say you're already writing risky, unportable code by depending on a particular order of execution.

Dave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment