bigbuffet-rw/app/soapbox/load-polyfills.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
'use strict';
// Convenience function to load polyfills and return a promise when it's done.
// If there are no polyfills, then this is just Promise.resolve() which means
// it will execute in the same tick of the event loop (i.e. near-instant).
function importBasePolyfills() {
2022-11-15 12:48:54 -08:00
return import(/* webpackChunkName: "base_polyfills" */ './base-polyfills');
2020-03-27 13:59:38 -07:00
}
function importExtraPolyfills() {
2022-11-15 12:48:54 -08:00
return import(/* webpackChunkName: "extra_polyfills" */ './extra-polyfills');
2020-03-27 13:59:38 -07:00
}
function loadPolyfills() {
const needsBasePolyfills = !(
// @ts-ignore
2020-03-27 13:59:38 -07:00
Array.prototype.includes &&
// @ts-ignore
2020-03-27 13:59:38 -07:00
HTMLCanvasElement.prototype.toBlob &&
window.Intl &&
// @ts-ignore
2020-03-27 13:59:38 -07:00
Number.isNaN &&
// @ts-ignore
2020-03-27 13:59:38 -07:00
Object.assign &&
// @ts-ignore
2020-03-27 13:59:38 -07:00
Object.values &&
window.Symbol
);
2022-12-30 10:27:13 -08:00
// Older versions of Firefox and Safari do not have IntersectionObserver.
2020-03-27 13:59:38 -07:00
// This avoids shipping them all the polyfills.
const needsExtraPolyfills = !(
window.IntersectionObserver &&
window.IntersectionObserverEntry &&
'isIntersecting' in IntersectionObserverEntry.prototype &&
2022-12-30 10:27:13 -08:00
window.requestIdleCallback
2020-03-27 13:59:38 -07:00
);
return Promise.all([
needsBasePolyfills && importBasePolyfills(),
needsExtraPolyfills && importExtraPolyfills(),
]);
}
export default loadPolyfills;