【问题标题】:Babel 7 fails with single plugin saying "Duplicate plugin/preset detected."Babel 7 失败,单个插件说“检测到重复的插件/预设”。
【发布时间】:2019-03-18 20:00:42
【问题描述】:

失败的插件是@babel/plugin-transform-regenerator(无边缘插件,160 万次下载/周)。

这是我的全部.babelrc:

{
  "presets": [],
  "plugins": [
    "@babel/plugin-transform-regenerator"
  ]
}

当我尝试使用parcel build source/main/index.html --no-source-maps --out-dir build 将其与包裹一起转译时,我收到以下错误:

/path/to/index.js: Duplicate plugin/preset detected.
If you'd like to use two separate instances of a plugin,
they need separate names, e.g.

plugins: [
  ['some-plugin', {}],
  ['some-plugin', {}, 'some unique name'],
]

at assertNoDuplicates (/.../node_modules/@babel/core/lib/config/config-descriptors.js:205:13)
at createDescriptors (/.../node_modules/@babel/core/lib/config/config-descriptors.js:114:3)
at createPluginDescriptors (/.../node_modules/@babel/core/lib/config/config-descriptors.js:105:10)
at alias (/.../node_modules/@babel/core/lib/config/config-descriptors.js:63:49)
at cachedFunction (/.../node_modules/@babel/core/lib/config/caching.js:33:19)
at plugins.plugins (/.../node_modules/@babel/core/lib/config/config-descriptors.js:28:77)
at mergeChainOpts (/.../node_modules/@babel/core/lib/config/config-chain.js:314:26)
at /.../node_modules/@babel/core/lib/config/config-chain.js:278:7
at buildRootChain (/.../node_modules/@babel/core/lib/config/config-chain.js:68:29)
at loadPrivatePartialConfig (/.../node_modules/@babel/core/lib/config/partial.js:85:55)

这是我的 package.json 版本:

"@babel/core": "^7.1.2",
"@babel/plugin-transform-regenerator": "^7.0.0",

有什么想法吗?

【问题讨论】:

  • 我从来没有使用过这个,但从给出的例子来看,你可以尝试用"plugins": ["@babel/plugin-transform-regenerator", {}]修复它。虽然我非常怀疑这会很重要。一种解决方法可能是使用"plugins": ["@babel/plugin-transform-regenerator", {}, 'some-random-name'],所以至少你要给它一个唯一的名称。
  • @icecub 这可能会奏效,但会发生这样的事情真的很奇怪......重复的插件来自哪里?
  • 通过一些研究我可以发现,您可能有几个默认插件也在此插件内部使用。这会导致上述错误。
  • @icecub 请将其作为答案,以便我可以接受它以供网络上的其他人参考。

标签: javascript npm babeljs parceljs


【解决方案1】:

经过一些研究,最可能的错误原因是您有一个或多个默认插件,该插件也在内部使用。

解决问题的最简单方法是按照错误告诉您的操作:为插件添加唯一名称:

"plugins": ["@babel/plugin-transform-regenerator", {}, 'unique-name']

【讨论】:

    【解决方案2】:

    这是一个 babel 错误,基本上是说你已经定义了你的插件 @babel/plugin-transform-regenerator 两次 - 或多或少是间接的。

    Parcel Bundler 使用 Babel 预设 @babel/preset-env 转换您的代码 by default。这些预设通常只是可共享的插件列表。正如你所看到的here,preset-env 已经在 Babel 7 中包含了"@babel/plugin-transform-regenerator"。

    简单的解决方案:只需从 .babelrc 的插件配置中删除 "@babel/plugin-transform-regenerator"。

    PS:从版本 6 迁移到 7 后有类似的经历。我的旧配置看起来像这样(在 Babel 6 中有效)

      "plugins": [
        "react-hot-loader/babel", 
        "transform-object-rest-spread", 
        "transform-class-properties", 
        "transform-runtime",
        "transform-async-generator-functions",
        "transform-async-to-generator"
      ],
      "presets": ["env", "react"]
    

    我不得不删除插件transform-object-rest-spread、transform-async-generator-functions 和transform-async-to-generator,正如所说的那样,它们包含在env 中(此处明确指定)。

    Babel 提供了一个很棒的升级工具,叫做 babel-upgrade(惊喜,惊喜),它确实很好地完成了重命名插件的工作,但不幸的是,它让我独自面对这些“重复”。

    希望,这会有所帮助。

    【讨论】:

      【解决方案3】:

      我今天也遇到了同样的问题。 我的解决方法:

      {
      "presets": [
         "@babel/preset-env",
         "@babel/preset-react"
      ],
      "plugins": [
          "transform-object-rest-spread",
          "transform-class-properties"
      ] 
      }
      

      【讨论】:

        【解决方案4】:

        只需将"@babel/plugin-transform-regenerator" 删除

        "plugins": [
            "@babel/plugin-transform-regenerator"
        ]
        

        【讨论】:

          猜你喜欢
          • 2019-07-29
          • 2020-10-16
          • 2021-05-06
          • 2012-03-24
          • 2014-02-24
          • 1970-01-01
          • 1970-01-01
          • 2016-04-20
          • 2010-09-09
          相关资源
          最近更新 更多