Skip to content

Instantly share code, notes, and snippets.

@snovakovic
Created November 6, 2017 08:18
Show Gist options
  • Save snovakovic/afc2d7d7a1e00543e514b454a22030ed to your computer and use it in GitHub Desktop.
Save snovakovic/afc2d7d7a1e00543e514b454a22030ed to your computer and use it in GitHub Desktop.
Include polyfills only if required by browser. (works with webpack)
// ___ Internal logic ___
/**
* Indication does browser have all required methods for application to work without polyfills
* @const {boolean}
*/
const ALL_FEATURES_SUPPORTED_BY_BROWSE = Boolean(window.Promise &&
window.Array.from &&
window.Array.prototype.includes &&
window.Map &&
window.Set &&
window.Object.assign &&
window.MutationObserver);
/**
* Inject polyfill script to html document
*
* @param {Function} done - callback that will be called after polyfills have been loaded
*/
function loadPolyfills(done) {
const js = document.createElement('script');
js.src = 'polyfills.js';
js.onload = done;
document.head.appendChild(js);
}
/**
* Load main application
*
*/
function start() {
require('./main'); // eslint-disable-line global-require
}
// ___ Execute Detection ___
if (ALL_FEATURES_SUPPORTED_BY_BROWSE) {
start();
} else {
loadPolyfills(start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment