Re:Managing Async Dependencies with JavaScript
by jamescostian on Friday May 19, 2017 @09:03PM
(#14380444)
AFAICT using async defer, one cannot express "you need to load X.js and Y.js before running Z.js, and you need to load X.js and A.js before running B.js". It looks like fetchInject allows you to express that type of thing.
But how is this different from RequireJS? I think RequireJS uses XHR and I don't think it supports CSS, but aside from that, what's new? I feel like this is just reinventing AMD.
Re:Managing Async Dependencies with JavaScript
by thehandofkwll on Saturday May 20, 2017 @12:38AM
(#14381064)
Yep, this is the race condition that the author was referring to.
RequireJs also let's you express these dependencies, but will block behind the cssdom due to the way it injects.
Actually, I made a little library which let's you manage dependencies with script async or defer, and can be considered an alternative to the authors (neat) solution. https://github.com/bohdyone/adm.js
Re:Managing Async Dependencies with JavaScript
by lewisjoe on Saturday May 20, 2017 @03:34AM
(#14381409)
Curious question: Can you explain a bit more on "will block behind the cssdom due to the way it injects"
As far as I know, requireJS doesn't do XHR requests and evals. That sounds ugly. I think it dynamically injects a script tag and loads the module. I maybe wrong.
Either way, how's it a blocking one?
So, the only thing that makes fetchInject, differ from requireJS is the non-blocking way. Is my understanding correct?
Re:Managing Async Dependencies with JavaScript
by thehandofkwll on Saturday May 20, 2017 @10:03PM
(#14385354)
This article by an engineer at Google explains it pretty well. My own testing has shown the same results. https://www.igvita.com/2014/05/20/script-injected-async-scri...
Re:Managing Async Dependencies with JavaScript
by dgoldstein0 on Saturday May 20, 2017 @03:51AM
(#14381448)
also wondering about the cssom comment. I can confirm that requirejs works by injecting <script> tags - <script async> to be precise. requirejs' api isn't promise based, but that's mostly an aesthetic concern.
btw you seem to imply that requirejs blocks - what do you mean by that?
Re:Managing Async Dependencies with JavaScript
by yladiz on Friday May 19, 2017 @09:12PM
(#14380479)
This seems to be a lot simpler in scope. RequireJS allows you to essentially do dependency injection, whereas this doesn't handle that. It also uses the fetch API rather than plain XHR as well as using and returning native promises when fetching libraries. I think it's primarily for loading resources from a different server, like a CDN, in arbitrary but definable order, and that's it.
Re:Managing Async Dependencies with JavaScript
by dgoldstein0 on Saturday May 20, 2017 @03:52AM
(#14381453)
requirejs doesn't natively support css, but it has a pretty easy to use loader api so it'd be easy to add css support and in fact one already exists: https://github.com/guybedford/require-css
Re:Managing Async Dependencies with JavaScript
by emergie on Saturday May 20, 2017 @02:36PM
(#14383660)
lib you linked is a bit crazy, it uses setInterval to guess when a css is properly loaded/applied and delays running dependent code
imho this is sufficient:
define('css', function () {
return {
load: function (name, parentRequire, onload, config) {
console.log('loading css ' + name);
const link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = name + '.css';
document.getElementsByTagName('head')[0].appendChild(link);
setTimeout(onload, 0);
}
};
});
usage: require(['lib/js/jquery-ui.min', 'css!lib/css/jquery-ui.min'], function () {
// jquery-ui magic goes here
});
Re:Managing Async Dependencies with JavaScript
by hdhzy on Saturday May 20, 2017 @03:00PM
(#14383775)
You don't need to guess, you can check document.styleSheets if it contains your file. See: https://curiosity-driven.org/minimal-loader#plugins
Re:Managing Async Dependencies with JavaScript
by bmh_ca on Friday May 19, 2017 @09:30PM
(#14380552)
Well, a note: async and defer are different and logically incompatible, in terms of load order. (IIRC) defer preserves order whereas async does not. These alter the dependency management strategy entirely.
<script src=... async defer>
for all scripts - could anyone explain?