Prevent rollup from renaming Promise to Promise$1

瘦欲@ 提交于 2019-12-07 07:53:04

问题


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'
    })
  ]
};


来源:https://stackoverflow.com/questions/45549689/prevent-rollup-from-renaming-promise-to-promise1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!