segmentio/analytics.js-integrations

Promise.prototype.finally not defined in MS Edge due to replacement in @segment/tracktor module

andrewburgess opened this issue · 1 comments

Something in the @segment/tracktor module has a dependency on an old version of core-js where Promise.prototype.finally is not defined. In Microsoft Edge, the native Promise object gets replaced by the core-js version which can cause things to break if code depends on using finally

This is a related issue where the core-js author references the fact that v2 or less does not implement Promise#finally in its polyfill (see: https://github.com/zloirock/core-js/blob/v2/modules/es6.promise.js)

To reproduce, in the @segment/tracktor project, in the build/index.html file, replace the line <script type="text/javascript" src="tracktor.js"></script> with:

<script>
  console.log('running native Promise#finally')
  Promise.resolve(true).finally(() =>
    console.log('Promise#finally executed')
  )
</script>
<script type="text/javascript" src="tracktor.js"></script>
<script>
  document
    .getElementById('dynamic-btn')
    .addEventListener('click', function () {
      console.log('button clicked')
      console.log('Promise#finally no longer here')
      console.log(Promise.prototype.finally)
      Promise.resolve(true).finally(() => console.log('this will not run'))
    })
</script>

and run on Edge.

image

What I'm guessing is happening is:

  1. @babel/preset-env@7.4.5 depends on core-js-compat@3.1.1
  2. core-js-compat tells @babel/preset-env to include things from core-js
  3. @babel/preset-env finds an included core-js installed
    a. wait-on@3.3.0 depends on core-js@2.6.5
    b. jest-dev-server@4.3.0 depends on wait-on@3.3.0
  4. @babel/preset-env injects the core-js@2.6.5 polyfills it determines are needed based on the defined targets and built ins

@babel/preset-env emits a warning about this here: https://github.com/babel/babel/blob/c5ba345ac26d90fb5da8954f00f5c7285ee5ada8/packages/babel-preset-env/src/normalize-options.js#L220

I think this can be alleviated by explicitly defining a dependency on core-js@^3.6.5

Just as an extra note, this isn't an issue with the release of the Chromium backed version of MS Edge