防止汇总将Promise重命名为Promise $ 1 [英] Prevent rollup from renaming Promise to Promise$1

查看:86
本文介绍了防止汇总将Promise重命名为Promise $ 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:原来这对Babel来说不是问题,但是使用之前运行的Rollup。无论如何,谢谢你的帮助,并为噪音感到抱歉。

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.

我使用汇总捆绑了许多模块,包括Promise polyfill(故意覆盖全局Promise)。但是,汇总将承诺识别为全局名称并转换

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;

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?

推荐答案

尝试使用汇总 - 插件注入并配置它以添加从'your-promise-polyfill'导入Promise到任何引用 Promise 。这样,Rollup不会认为它需要重命名polyfill中声明的函数以避免与全局冲突,因为它不会意识到 是一个与它发生冲突的全局。

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

这篇关于防止汇总将Promise重命名为Promise $ 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆