Babel 7在使用单个插件时出错,显示“检测到重复的插件/预设”信息。

18

失败的插件是 @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",

有什么想法吗?


1
我从未使用过这些内容,但根据给出的示例,您可以尝试使用 "plugins": ["@babel/plugin-transform-regenerator", {}] 来修复它。虽然我非常怀疑这会有所作用。一个解决方法可能是使用 "plugins": ["@babel/plugin-transform-regenerator", {}, 'some-random-name'],这样至少您可以给它一个唯一的名称。 - icecub
@icecub 这可能有效,但这种情况发生真的很奇怪...重复的插件是从哪里来的? - Rasto
1
从我所做的一些研究中发现,可能存在一些默认插件也被此插件内部使用。这会导致所提到的错误。 - icecub
@icecub 请将其制作为答案,这样我就可以接受它,让其他人在网络上参考。 - Rasto
5个回答

17

这是一个Babel错误,基本上意味着你已经间接或直接地定义了你的插件@babel/plugin-transform-regenerator两次。

Parcel Bundler默认使用Babel预设@babel/preset-env对您的代码进行转译。在此处,您可以看到这些预设通常只是可共享的插件列表。在Babel 7中,如此处所示,preset-env已经包含了"@babel/plugin-transform-regenerator"

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

附注:我曾经有过类似的经历,在从版本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-spreadtransform-async-generator-functionstransform-async-to-generator,这些插件被包含在env中(在此明确说明)。

Babel提供了一个名为babel-upgrade的出色升级工具,它可以很好地完成重命名插件的工作,但不幸的是,它让我徒手面对这些“重复”的插件。

希望这能帮到你。


8

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

解决问题的最简单方法是按照错误提示所说的做法:向插件添加一个唯一的名称:

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


0

我遇到了相同的问题,只是使用了另一个插件,我认为它们两个是一样的。 我的插件:

@babel/plugin-proposal-object-rest-spread

最后我注意到在package.jsondevDependencieswebpack.config.jsplugins中都有一个插件。我删除了webpack文件中的那个,然后它就正常工作了。

但是你的问题可能与.babelrcpackage.json有关。如果你像我一样做,应该可以解决问题。你不必同时在两个文件中引入相同的插件。


0

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

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

0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接