Webpack 4 + Babel leaving const in transpiled code

2019-07-07 17:41发布

问题:

I'm trying to get my code working on Android 4.1 Webview, which doesn't support ES6.

But I am getting this error:

Uncaught SyntaxError: Use of const in strict mode.

.babelrc config

{
  "plugins": [
    "lodash"
  ],
  "presets": [
    "@babel/preset-react",
    [
      "@babel/preset-env",
      {
        "targets": {
          "android": "4.1"
        },
        "useBuiltIns": "usage",
        "forceAllTransforms": true
      }
    ],
    "@babel/preset-stage-0"
  ]
}

webpack.config.js

rules: [
      {
        enforce: 'pre',
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "eslint-loader"
      },
      {
        test: /\.jsx?$/,
        loaders: 'babel-loader',
        options: {
          plugins: ['lodash']
        },
        exclude: /(node_modules|bower_components)/
      },
   ]

回答1:

I figured out, that the issue was caused by the "query-string" module, which is a dependency of another package. As described on github, i installed version 5 explicitly. Then everything worked well.

Github: query-string

This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, use version 5: npm install query-string@5.