load_polyfills.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Convenience function to load polyfills and return a promise when it's done.
  2. // If there are no polyfills, then this is just Promise.resolve() which means
  3. // it will execute in the same tick of the event loop (i.e. near-instant).
  4. function importBasePolyfills() {
  5. return import(/* webpackChunkName: "base_polyfills" */ './base_polyfills');
  6. }
  7. function importExtraPolyfills() {
  8. return import(/* webpackChunkName: "extra_polyfills" */ './extra_polyfills');
  9. }
  10. function loadPolyfills() {
  11. const needsBasePolyfills = !(
  12. Array.prototype.includes &&
  13. HTMLCanvasElement.prototype.toBlob &&
  14. window.Intl &&
  15. Number.isNaN &&
  16. Object.assign &&
  17. Object.values &&
  18. window.Symbol &&
  19. Promise.prototype.finally
  20. );
  21. // Latest version of Firefox and Safari do not have IntersectionObserver.
  22. // Edge does not have requestIdleCallback and object-fit CSS property.
  23. // This avoids shipping them all the polyfills.
  24. const needsExtraPolyfills = !(
  25. window.AbortController &&
  26. window.IntersectionObserver &&
  27. window.IntersectionObserverEntry &&
  28. 'isIntersecting' in IntersectionObserverEntry.prototype &&
  29. window.requestIdleCallback &&
  30. 'object-fit' in (new Image()).style
  31. );
  32. return Promise.all([
  33. needsBasePolyfills && importBasePolyfills(),
  34. needsExtraPolyfills && importExtraPolyfills(),
  35. ]);
  36. }
  37. export default loadPolyfills;