Skip to content

Instantly share code, notes, and snippets.

@camille-hdl
Last active July 19, 2018 21:03
Show Gist options
  • Save camille-hdl/f6ab4acba5cc4c0ce198c3acc7c9255d to your computer and use it in GitHub Desktop.
Save camille-hdl/f6ab4acba5cc4c0ce198c3acc7c9255d to your computer and use it in GitHub Desktop.
<!-- see it in action here : https://github.com/rollup/rollup-starter-code-splitting/blob/master/public/index.html -->
<!-- Browsers with dynamic import support -->
<script type="module">
window.esDynamicImport = true;
// this will throw if dynamic import is not supported
import("/js/es/entrypoint.js").then(function(m) {
// do something
});
</script>
<!-- browsers with ES6 modules but no dynamic import support -->
<script type="module">
// if the module script tag throws, window.esDynamicImport will be undefined
if (!window.esDynamicImport) {
// get the nomodule scripts and load (or inline) them
const noms = Array.prototype.slice.call(document.getElementsByTagName('script'), 0).filter((x) => x.hasAttribute('nomodule'));
function nextLoad (nom, s) {
if (!(nom = noms.shift())) return;
s = document.createElement('script');
if (nom.src)
s.src = nom.src, s.addEventListener('load', nextLoad), s.addEventListener('error', nextLoad);
else
s.innerHTML = nom.innerHTML;
document.head.appendChild(s);
if (!nom.src) nextLoad();
}
nextLoad();
}
</script>
<!-- No ES6 modules support : fallback to systemJS -->
<script nomodule src='https://unpkg.com/[email protected]/dist/system-production.js'></script>
<script nomodule>
System.import("/js/system/entrypoint.js").then(function(m) {
// do something
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment