简体   繁体   English

如何覆盖babel的预设插件选项

[英]How to overwrite babel's preset plugin options

I'm using babel-preset-react-app via following .babelrc: 我通过以下.babelrc使用babel-preset-react-app

{
  "presets": ["react-app"],
  "plugins": [
    "transform-es2015-modules-commonjs",
    "transform-async-generator-functions"
  ]
}

I need to overwrite babel-plugin-transform-runtime options. 我需要覆盖babel-plugin-transform-runtime选项。 I tried installing plugin and adding it to .babelrc in a following way: 我尝试安装插件并以下列方式将其添加到.babelrc:

{
  "presets": ["react-app"],
  "plugins": [
    ["babel-plugin-transform-runtime", {
      "helpers": false,
      "polyfill": false,
      "regenerator": false
    }],
    "transform-es2015-modules-commonjs",
    "transform-async-generator-functions"
  ]
}

but it doesn't work for me. 但它对我不起作用。

Is there any way I can do it without copy and pasting entire preset to my .babelrc? 有没有办法我可以做到没有复制并将整个预设粘贴到我的.babelrc?

It seems that Babel doesn't currently support these sorts of overrides (see https://github.com/babel/babel/issues/8799 ). 似乎Babel目前不支持这些类型的覆盖(请参阅https://github.com/babel/babel/issues/8799 )。 Fortunately I found a workaround for babel-preset-react-app . 幸运的是,我找到了babel-preset-react-app的解决方法。 There is an undocumented option, useESModules : 有一个未记录的选项, useESModules

['react-app', { useESModules: false }]

Here's a config using babel-plugin-react-app that works for node.js: 这是使用适用于node.js的babel-plugin-react-app的配置:

    presets: [
        ['react-app', { useESModules: false }],
        [
            '@babel/preset-env',
            {
                modules: 'commonjs',
                targets: {
                    node: 'current',
                },
            },
        ],
    ],

Of course, using babel-preset-react-app makes the most sense if you're using create-react-app for your client-side bundle. 当然,如果你正在为你的客户端捆绑使用create-react-app ,那么使用babel-preset-react-app最有意义。 If you're not using create-react-app , then you could consider just using @babel/preset-react directly, in which case you wouldn't need to worry about overriding the useESModules setting. 如果你没有使用create-react-app ,那么你可以考虑直接使用@ babel / preset-react ,在这种情况下你不必担心覆盖useESModules设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 babel-plugin- *和babel-preset- *之间有什么区别? - What is the difference between babel-plugin-* and babel-preset-* Babel Plugin/Preset 文件不允许导出对象,只能导出函数 - Babel Plugin/Preset files are not allowed to export objects, only functions 如何安装 babel-cli babel-preset-react - how to install babel-cli babel-preset-react Babel错误:插件/预设文件不允许导出对象,仅可导出功能 - Babel Error: Plugin/Preset files are not allowed to export objects, only functions 带有预设环境(无选项)和预设打字稿的 Babel 说“缺少 class 属性转换”。 为什么? - Babel with preset-env (no options) and preset-typescript says “Missing class properties transform”. Why? Babel 插件 transform-remove-console 不适用于 Vue CLI 4 @vue/cli-plugin-babel/preset? - Babel plugin transform-remove-console not working with Vue CLI 4 @vue/cli-plugin-babel/preset? 错误:插件/预设文件不允许导出对象,只能导出功能/ babel-preset-stage-0 - Error:Plugin /Preset files are not allowed to export objects,only functions/babel-preset-stage-0 babel 的 airbnb 预设实际上有什么作用? - What does babel's airbnb preset actually do? 将@ babel / plugin-proposal-nullish-coalescing-operator与“ babel-preset-expo”一起使用 - Using @babel/plugin-proposal-nullish-coalescing-operator with “babel-preset-expo” 有没有办法为babel插件提供自定义选项? - Is there a way to give custom options to a babel plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM