I realize I could answer myself with a simple test project (and I may, if no-one chimes in right away), but I couldn’t find the answer anywhere on SO or google, and it seems critical:
If I define an AMD module with require.js as such:
//a.js
define( ['stuff'], function (Stuff) {
return { thing: new Stuff() };
}
And then I use it in two different other modules as such:
// b.js
define( ['a'], function(a) {
// do something with a's stuff
});
// c.js
define( ['a'], function(a) {
//do something else with a's stuff
}
Does a‘s defining function get called (and therefore a new Stuff instantiated) each time I require it for another module or does it just get called once, and its output gets cached?
Obviously this is critical in some use-cases, but its not clear from the require.js documentation or other examples I’ve seen.
I just tested this out myself, and it looks like the constructor is only run once.
When I open index.html, the console shows: