Babel 不转译箭头函数 (Webpack)

新手上路,请多包涵

当运行 webpack 和 babel 时,生成的 bundle.js 仍然包含箭头函数。这让我在 Internet Explorer 10 中运行时出现语法错误。我希望 babel 将箭头函数替换为 IE 可以运行的正常函数。

我的 package.json 具有以下 devDependencies:

 "devDependencies": {
  "babel-cli": "^6.26.0",
  "babel-core": "^6.26.0",
  "babel-loader": "^7.1.4",
  "babel-preset-env": "^1.6.1",
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-react": "^6.24.1",
  "babel-preset-stage-1": "^6.24.1",
  "css-loader": "^0.28.9",
  "imports-loader": "^0.7.1",
  "style-loader": "^0.19.1",
  "webpack": "^3.11.0",
  "webpack-dev-server": "^2.11.2"
}

我的 webpack.config.js 看起来像这样:

 module.exports = {
  entry: [
    'babel-polyfill',
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'babel-loader'
    },
    {
      test: /\.css$/,
      use: ['style-loader', 'css-loader']
    }
    ],
  },
  resolve: {
    enforceExtension: false,
    extensions: ['.js', '.jsx']
  },
  devServer: {
    host: '0.0.0.0',
    port: 5000,
    historyApiFallback: true,
    contentBase: './'
  }
};

我的 .babelrc 看起来像这样:

 {
  "presets":
  [
    ["env", { "targets": {"browsers": ["last 2 versions"]}, "debug": true }],
    "react",
    "stage-3"
  ]
}

要转译,我运行命令:

npm 运行构建 –production

我在控制台中得到以下输出:

 Using targets:
{
  "chrome": "62",
  "android": "4.4.3",
  "edge": "15",
  "firefox": "56",
  "ie": "10",
  "ios": "10.3",
  "safari": "10.1"
}

Modules transform: commonjs

Using plugins:
  check-es2015-constants {"android":"4.4.3","ie":"10"}
  transform-es2015-arrow-functions {"android":"4.4.3","ie":"10"}
  transform-es2015-block-scoped-functions {"android":"4.4.3","ie":"10"}
  transform-es2015-block-scoping {"android":"4.4.3","ie":"10"}
  transform-es2015-classes {"android":"4.4.3","ie":"10"}
  transform-es2015-computed-properties {"android":"4.4.3","ie":"10"}
  transform-es2015-destructuring {"android":"4.4.3","edge":"15","ie":"10"}
  transform-es2015-duplicate-keys {"android":"4.4.3","ie":"10"}
  transform-es2015-for-of {"android":"4.4.3","ie":"10"}
  transform-es2015-function-name {"android":"4.4.3","edge":"15","ie":"10"}
  transform-es2015-literals {"android":"4.4.3","ie":"10"}
  transform-es2015-object-super {"android":"4.4.3","ie":"10"}
  transform-es2015-parameters {"android":"4.4.3","ie":"10"}
  transform-es2015-shorthand-properties {"android":"4.4.3","ie":"10"}
  transform-es2015-spread {"android":"4.4.3","ie":"10"}
  transform-es2015-sticky-regex {"android":"4.4.3","ie":"10"}
  transform-es2015-template-literals {"android":"4.4.3","ie":"10"}
  transform-es2015-typeof-symbol {"android":"4.4.3","ie":"10"}
  transform-es2015-unicode-regex {"android":"4.4.3","ie":"10"}
  transform-regenerator {"android":"4.4.3","ie":"10"}
  transform-exponentiation-operator {"android":"4.4.3","ie":"10"}
  transform-async-to-generator {"android":"4.4.3","ie":"10"}
  syntax-trailing-function-commas {"android":"4.4.3","ie":"10"}

transform-es2015-arrow-functions 被列为包含在内,但是当我打开生成的 bundle.js 时,我可以看到以下内容:

 ...

function encoderForArrayFormat(options) {
    switch (options.arrayFormat) {
        case 'index':
            return (key, value, index) => {
                return value === null ? [
                    encode(key, options),
...

上面使用了箭头函数,并在 Internet Explorer 中产生语法错误。其他 ES6 内容,如 ‘…’ 被转译。

我究竟做错了什么?

原文由 Henrik 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

我认为问题与 查询字符串 有关,因为它是用 ES6 编写的,没有转换为 ES5。尝试将版本从 6 降级到 5。

 yarn add query-string@5.1.1

原文由 Serhii Levchenko 发布,翻译遵循 CC BY-SA 3.0 许可协议

  1. 删除 webpack 包文件
  2. 编辑 webpack 配置文件
  3. 禁用 arrowFunction
  4. 运行网页包
module.exports = {
  output: {
    environment: {
      arrowFunction: false
    }
  }
}

更多信息在这里: 顶级功能仍然是 Webpack 5 上的箭头

原文由 Rolazar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题