哪些babel设置适合导出库? [英] Which babel settings are suitable for exporting a library?

查看:66
本文介绍了哪些babel设置适合导出库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Babel + Webpack的新手,对.babelrc配置有些困惑.

I am new to Babel+Webpack and have some confusion regarding .babelrc configuration.

首次配置

{
    "presets": [
        [
            "@babel/env",
            {
                "modules": false,
                "useBuiltIns": "usage",
                "targets": "> 0.25%, not dead",
                "corejs": {
                    "version": 3,
                    "proposals": true
                }
            }
        ]
    ],
    "plugins": [
        "@babel/transform-runtime"
    ]
}

第二配置:

{
    "presets": [
        [
            "@babel/env",
            {
                "modules": false,
            }
        ]
    ],
    "plugins": [
        [
            "@babel/plugin-transform-runtime", //target environment are not supported
             {
                 "corejs": 3,
                 "helpers": true,
                 "regenerator": true
             }
        ]
    ]
}

第二种配置的真实事实是:

Facts that are true for second configuration's are:

  1. 增加包的大小
  2. Core-js-pure将包含不会污染全球环境的ponyfills.

我的问题是我们将要导出"umd"邮件,图书馆名称为"XYZ"的公共图书馆我感到困惑的是,上述哪一项设置是合适的,这让我真正的困惑是,如果将最后创建的捆绑包缩小并完全建立在esm模式(使用限制模式)下,并且对于公众使用,他们可以像"; XYZ.method()",那么第二种配置如何适合并停止污染全局名称空间.

My question is we are going to export a "umd" library for public use with a Library Name "XYZ" and i am confused which of the above settings are suitable as one thing really confuse me is that if the bundle i.e created at the end is minified and built completely on esm pattern(use-strict mode) and for public use they can access like "XYZ.method()", then how the second configuration is suitable and it stop polluting global namespace.

有人可以举例说明我,并帮助我清除这个概念吗?

Can anyone explain me with an example and help me clearing this concept?

推荐答案

对于库,汇总是一个比WebPack更好的选择.

For libraries Rollup is a much better option than WebPack.

这是一个很好的起点.

https://github.com/rollup/rollup-starter-lib

如果您必须使用webpack,请避免使用regenerater,因为它会给Babel的输出增加很多膨胀,如果您的代码需要它,那么使用该应用的应用程序应该包含它,因此您不必两次都拥有它在最终应用中

If you must use webpack, avoid using regenerater, as it adds a lot of bloat to the output of Babel, if you code needs it, then the consuming app should include it, so you don’t end up having it twice in the end app

这篇关于哪些babel设置适合导出库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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