analytics.js-integrations icon indicating copy to clipboard operation
analytics.js-integrations copied to clipboard

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

Open andrewburgess opened this issue 5 years ago • 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/[email protected] depends on [email protected]
  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. [email protected] depends on [email protected] b. [email protected] depends on [email protected]
  4. @babel/preset-env injects the [email protected] 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

andrewburgess avatar Jun 11 '20 18:06 andrewburgess

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

andrewburgess avatar Jun 11 '20 20:06 andrewburgess