.babelrc 文件未应用

.babelrc file is not applied

我只想使用 ts 作为类型检查的形式,所以我使用 babel-loader 并仅添加 typescript-preset,如下所示。它在编写 .babelrc 文件时不适用,但只有当你在 webpack.config.js 文件内的 babel-loader 中提供选项时才会应用它,就像这样。看官方文档的时候才知道babel-loader是自动识别并应用.babelrc文件的。我错了吗?

myRepo

.babelrc

{
  "env": {
    "development": {
      "presets": ["@babel/preset-env"],
      "plugins": []
    },
    "production": {
      "presets": [
        [
          "@babel/preset-env",
          {
            "targets": { "ie": 11 }
          }
        ],
        "@babel/preset-typescript"
      ],
      "plugins": [["@babel/plugin-transform-runtime", { "corejs": 3 }]]
    }
  }
}

很多事情都可能出错,如果不能直接访问您的设置,很难排除简单的事情(比如 .babelrc 在错误的目录中)。

但是,如果您运行正在使用 babel-loader 版本 7.1.1 或更早版本,那么您很可能 运行 进入 bug 其中 babel.transform默认没有正确读取.babelrc文件。

有两种解决方案:

方案一:升级babel-loader至7.1.2+

该错误已在 7.1.2 和 7.1.4 之后的版本中修复。

解决方案 2:将 babelrc: true 添加到您的 webpack 选项

如果你因为某种原因不能升级,那么你只需要明确地告诉babel.transform去寻找一个babel配置文件:

  //...
  {
    loader: 'babel-loader',
    options: {
      babelrc: true,
    }
  }
  // ...