Prevent rollup from renaming Promise to Promise$1

2019-05-07 03:35发布

问题:

Update: Turned out that this is not a problem with Babel, but with Rollup, which is run before. Thanks for your help anyway and sorry for the noise.

I use rollup to bundle a number of modules including a Promise polyfill (deliberately overwriting the global Promise). However, rollup recognizes Promise as a global name and transforms

export default function Promise(fn) { ... }
...
global.Promise = Promise;

to

function Promise$1(fn) { ... }
...
global.Promise = Promise$1;

The resulting code works, but I would like the following assertion to hold true:

expect(Promise.name).to.equal('Promise');

Is there any way to tell rollup to leave the constructor name intact?

回答1:

Try using rollup-plugin-inject and configuring it to add import Promise from 'your-promise-polyfill' to any files that reference Promise. That way, Rollup won't think that it needs to rename the function declared in the polyfill to avoid clashing with the global, because it won't be aware that there is a global that it's clashing with.

// rollup.config.js
import inject from 'rollup-plugin-inject';

export default {
  // ...
  plugins: [
    inject({
      Promise: 'your-promise-polyfill'
    })
  ]
};