【发布时间】:2021-04-12 00:16:52
【问题描述】:
我有一个问题,我想转换我正在使用的包的 ...spread 运算符。
请注意,我在脚本版本 1.1.5 中弹出 create-react-app 的上下文中使用的是 babel 6。我对此有一个最小的复制。
我现在找不到它,但我正在使用一些 babel 文档建议的技术,我将它添加到我的 webpack 配置中:
{
test: /\.m?js$/,
exclude: {
test: /(node_modules)/, // Exclude libraries in node_modules ...
not: [
/@monaco-editor\/react/
]
},
use: {
loader: require.resolve('babel-loader'),
options: {
"plugins": [
[require.resolve("babel-plugin-transform-object-rest-spread"), { "useBuiltIns": true }]]
}
}
},
基本上是说'不要在 node_modules 中转换任何东西,除了那个包,并使用这个插件进行转换'。
当你运行这个时我遇到的问题,你得到这个错误:
./node_modules/@monaco-editor/react/lib/es/index.js
Module build failed: Error: Couldn't find preset "@babel/preset-env" relative to directory "/Users/djohnston/git/cra1/node_modules/@monaco-editor/react"
at Array.map (<anonymous>)
请注意,它抱怨的 babel 预设是 babel 7 @babel/preset-env 预设,而我使用的是 babel 6。
但是 - 如果您导航到 node_modules/@monaco-editor/react/package.json 并从中删除以下 babel 属性:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
},
然后代码开始工作。
这表明 babel 正在检查 package.json 并决定开始使用 @babel/preset-env 开始转换它,而不是只使用我在 webpack 配置中指定的配置。
为什么 babel 会这样做,有没有我可以使用的配置选项来解决这个问题?
此问题的最少复制:https://github.com/dwjohnston/cra1-monaco/tree/eject-solve-attempt
@monaco-editor/react 上的问题我已经提出了这个问题(要求他们转译扩展运算符:https://github.com/suren-atoyan/monaco-react/issues/221)
【问题讨论】:
-
看来您可能想要在
babel-loader选项中使用babelrc: false? -
@loganfsmyth 是的!这样就解决了。随意张贴作为答案。