Babel.js 巴别塔不发散箭头功能(Webpack)

cnh2zyt3  于 2022-12-08  发布在  Babel
关注(0)|答案(4)|浏览(140)

当运行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运行构建--生产
我在控制台中得到以下输出:

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-es 2015-arrow-functions被列为包含,但当我打开生成的bundle.js时,我可以看到,例如,以下内容:

...

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

上面的代码使用了一个箭头函数,在InternetExplorer中产生了一个语法错误。其他ES6的东西,比如“...”被泄露了。
我做错了什么?

q3aa0525

q3aa05251#

我认为问题与query-string有关,因为它是在ES6中编写的,而不是传递到ES5。尝试将版本从6降级到5。

yarn add query-string@5.1.1
gr8qqesn

gr8qqesn2#

1.删除Webpack包文件
1.编辑Webpack配置文件
1.禁用箭头功能
1.运行Webpack

module.exports = {
  output: {
    environment: {
      arrowFunction: false
    }
  }
}

更多信息,请访问:Webpack 5上的顶级功能仍为箭头

k4aesqcs

k4aesqcs3#

它似乎与“transform-es 2015-function-name”插件有关。请查看https://github.com/babel/babel/issues/4782#issuecomment-271367012了解详细信息。因此排除该插件应该可以完成工作。

fnvucqvd

fnvucqvd4#

请尝试使用preset-env依赖项。它对我来说工作得很好

{
   test: /\.js$/,
   exclude: /node_modules/,
   loader: 'babel-loader',
   options: {
      presets: ["@babel/preset-env"]
   }
},

相关问题