添加babel-preset-env和babel-preset-stage-2

时间:2018-03-27 16:04:50

标签: javascript babel babel-preset-env

我必须使用babel-preset-env和babel-preset-stage-2才能将webpack用于包含传播运算符的代码。之后我成功使用webpack构建我的bundle,但这次我在运行时遇到了生成器运行时错误。

所以首先我尝试在这个页面做一个解决方案,babel-polyfill,但我的团队不想让我使用它,而是他们让我使用transform-runtime插件,所以我安装它并把它放在babelrc文件中但是仍然得到相同的生成器 - 运行时错误,任何想法?我该怎么办?

这是babelrc文件

{
  "presets": [
    "env",
    "stage-2",
    "es2015"
  ],
  "plugins": [
    "transform-async-to-generator",
    [
      "transform-runtime",
      {
        "helpers": false,
        "polyfill": false,
        "regenerator": true
      }
    ]
  ]
}

webpack配置文件

var path = require('path')

module.exports = {
  entry: path.resolve(__dirname, 'partner/index.js'),
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'partner_bundle.js'
  },
  target: 'web',
  module: {
    rules: [
      {
        test: /\.js$/, // Check for all js files
        loader: 'babel-loader',
        query: {
          presets: ['babel-preset-env', 'babel-preset-stage-2'].map(
            require.resolve
          )
        },
        exclude: /node_modules\/(?!other-module)/
      }
    ]
  },
  stats: {
    colors: true
  },
  devtool: 'source-map',
  resolve: { symlinks: false }
}

package.json中的babel和webpack版本

"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",

"webpack": "^4.0.1",
"webpack-cli": "^2.0.9"

1 个答案:

答案 0 :(得分:0)

尝试使用:

{
  "env": {
    "production": {
      "plugins": ["transform-react-constant-elements"]
    }
  }
}

https://babeljs.io/docs/usage/babelrc/