Created
November 6, 2017 08:18
-
-
Save snovakovic/afc2d7d7a1e00543e514b454a22030ed to your computer and use it in GitHub Desktop.
Include polyfills only if required by browser. (works with webpack)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ___ 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