webp.js

export default async function isWebpSupported() {
  // https://github.com/Modernizr/Modernizr/blob/3b0eb2be8cc847c0f331a5327bf8cd2f19632ffa/feature-detects/img/webp.js
  var webpTests = [
    {
      uri:
        'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=',
      name: 'webp'
    }
  ];

  var webp = webpTests.shift();
  function test(name, uri, cb) {
    var image = new Image();

    function addResult(event) {
      // if the event is from 'onload', check the see if the image's width is
      // 1 pixel (which indicates support). otherwise, it fails
      if (cb) {
        cb(event);
      }
    }

    image.onerror = addResult;
    image.onload = addResult;

    image.src = uri;
  }

  return new Promise((resolve, reject) => {
    // test for webp support in general
    test(webp.name, webp.uri, function (e) {
      // if the webp test loaded, test everything else.
      if (e && e.type === 'load') {
        resolve(true);
      }
      resolve(false);
    });
  });
}