Skip to content

Instantly share code, notes, and snippets.

@jakub-g
Created December 20, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakub-g/6ca2ff1e83e896db2f5e36a17229606b to your computer and use it in GitHub Desktop.
Save jakub-g/6ca2ff1e83e896db2f5e36a17229606b to your computer and use it in GitHub Desktop.
dynamic-polyfilling.js
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