You've made some technical claims in your Meta question here. These need to be addressed.
The currently accepted answer
I see no accepted answer on that question. I checked the timeline and did not see any answer accepted at any time. (I don't read timelines very often so if I did miss something I'd appreciate the correction.)
is actually an answer to a subtly different question, which also has unpleasant side effects (such as needless HTTP requests and errors in the console)
If by "currently accepted answer" you mean the one with the most votes currently, the errors in the console are perhaps annoying to some but are not generally a deal breaker. I've got large applications running with RequireJS that do generate loading errors because my code tries to load some optional modules from a few places. They are harmless. It is possible that the code in that answer will result in an HTTP request but whether it will and whether it is "needless" really depends on the context in which it is run. There are situations where it is not possible to know a priori whether jQuery is available and in those situations performing an HTTP request at runtime to try to load it may be exactly what is needed to figure out whether the application can rely on jQuery.
I worked hard to provide what I think is a good answer
It is a problematic answer.
The first part of your answer works but has a significant... hmm... quirk... The call require.defined('jquery')
will return true
only if jQuery has already been loaded. Normally, when you write a module that depends on jQuery, you put jquery
in the dependency list of your module. Like this:
define(['jquery'], function ($) { ...
(For those not familiar with RequireJS: yes, it is jquery
all lower caps. It is a source of endless confusion.)
Then, you don't have to worry about whether jQuery is already loaded: RequireJS will load it for you when it loads your module because your module makes it a dependency. (And I should point out that each encounter of jquery
in a dependency list will not cause RequireJS to fetch it anew. At most, the first encounter will cause RequireJS to fetch it, and then RequireJS will remember whether it can resolve jquery
or not.) Someone using your solution will now have to worry about writing code elsewhere that will set things correctly so that when the module that wants jQuery loads, the jQuery module has been loaded already. This is brittle. Moreover, a consequence of using require.defined
is that if it returns false
, there can be two possibilities:
jQuery really is unavailable. ("Unavailable" here means the application cannot load it, period, not "unavailable now but perhaps available in the future".)
Your application followed a code path that made it so that jQuery has just not been loaded yet, even though it is available (i.e. it can be loaded).
The UMD part suffers from the same problem and is addressing an issue that the OP did not need addressed.
I cannot read the mind of the downvoters but it seems to me there is material for downvotes here.