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
[ Parent
| on HN ]
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