Last active
February 12, 2024 00:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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