Babel 7失败,单个插件显示“检测到重复的插件/预设”。

时间:2018-10-14 02:31:34

标签: javascript npm babeljs babel parceljs

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

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

这是一个babel错误,基本上是说您两次(间接或多或少地)定义了插件@babel/plugin-transform-regenerator

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

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

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-spreadtransform-async-generator-functionstransform-async-to-generator,正如所说的,它们包含在env(此处明确指定)中。

Babel提供了一个名为babel-upgrade的出色升级工具(令人惊讶,令人惊讶),确实可以很好地完成重命名插件的工作,但不幸的是,它让我独自留下了这些“重复项”。

希望,会有所帮助。

答案 1 :(得分:0)

进行了一些研究后,最可能导致该错误的原因是您有一个或多个默认插件,该插件也在内部使用。

解决此问题的最简单方法是执行错误告诉您的操作:向插件添加唯一名称:

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