Created
December 20, 2020 13:43
-
-
Save jakub-g/6ca2ff1e83e896db2f5e36a17229606b to your computer and use it in GitHub Desktop.
dynamic-polyfilling.js
This file contains 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
polyfillIfNeeded().then(() => { | |
// we (ab)use synchronous `require`, which is not standard JS (but is provided by webpack) | |
require('./app.js').start() | |
}) | |
function polyfillIfNeeded(callback) { | |
if (browserSupportsAllFeatures()) { | |
return Promise.resolve() | |
} else { | |
return new Promise((resolve) => { | |
const polyfill = document.createElement('script') | |
polyfill.src = '/path/to/polyfill.js' | |
polyfill.onload = resolve | |
document.head.appendChild(polyfill) | |
}) | |
} | |
} | |
function browserSupportsAllFeatures() { | |
return ( | |
Promise.prototype.finally && | |
NodeList.prototype.forEach && | |
// ... | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment