webpack + babel - 转换对象箭头功能不起作用

Ron*_*onZ 5 webpack babeljs arrow-functions babel-loader

我正在尝试使用 babel 配置 webpack (5),使用 babel-loader 转译为 ES5。不幸的是,输出不一致。基本上,它分为两部分:

  1. 一些 polyfill:

  2. 我的代码:

如您所见,第一部分包含箭头函数,第二部分没有。我试图将@babel/plugin-proposal-class-properties和添加@babel/plugin-transform-arrow-functions到我的.babelrc文件中,但class-properties缺少(启用调试)。

我必须承认,我不确定这class-properties是问题所在,但在谷歌上花了几个小时后,这是我最好的尝试,所以也许我对问题的根源是错误的。

网络包文件:

export default {
  entry: './src/index.js',
  mode: 'production',
  output: {
    path: path.resolve(__dirname, '..', '..', 'dist'),
    filename: 'bundle.prod.js'
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

.babelrc 文件:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "corejs": {
          "version": 3
        },
        "useBuiltIns": "usage",
        "debug": true
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-arrow-functions"
  ]
}
Run Code Online (Sandbox Code Playgroud)

节点依赖:

"@babel/core": "7.11.6",
"@babel/plugin-proposal-class-properties": "7.10.4",
"@babel/plugin-transform-arrow-functions": "7.10.4",
"@babel/preset-env": "7.11.5",
"@babel/register": "7.11.5",
"babel-loader": "8.1.0",
"core-js": "3.6.5",
"webpack": "5.0.0",
"webpack-cli": "4.0.0",
"webpack-merge": "5.2.0"
Run Code Online (Sandbox Code Playgroud)

小智 19

我在 webpack v5 上遇到了类似的问题

Bootstrap 中的箭头函数

我需要从 webback 更改配置并添加:

目标:['es5']

        module: {
            rules: [.....]
        },
        resolve: {
            extensions: [".ts", ".tsx", ".js"]
        },
        target: ['es5']
Run Code Online (Sandbox Code Playgroud)

Webback V5 目标配置

  • 要在浏览器环境中使用,您应该添加 `target: ['web', 'es5']` (5认同)