webpack + babel-loader产生包含`eval()`的输出 webpack.config.js package.json (来自的除外)生成的bundle.js

表面上,我遇到了与此相同的问题:Webpack Babel-loader transpiles code with eval(),但该解决方案对我不起作用。

我尝试在@babel/preset-env文件中同时使用babel-preset-envwebpack.config.js预设。我也尝试过(但失败了)使用.babelrc文件来实现这两种配置。是模块版本冲突问题吗?

让我知道是否还有其他信息可以使我的问题更加清楚。

node: v10.15.3npm: 6.4.1

webpack.config.js

'use strict';

const path = require('path');

module.exports = {
    entry: {
        app: './src/js/scripts.js'
    },output: {
        filename: 'bundle.js',path: path.resolve(__dirname,'public/dist/js')
    },module: {
        rules: [
            {
                test: /\.js$/,// include .js files
                exclude: /node_modules/,// exclude any and all files in the node_modules folder
                use: [
                    {
                        loader: 'babel-loader',options: {
                            presets: ['@babel/preset-env']
                        }
                    }
                ]
            }
        ]
    }
};

package.json

...
"devDependencies": {
        "@babel/core": "^7.7.4","@babel/preset-env": "^7.7.4","babel-loader": "^8.0.6",...

(来自的除外)生成的bundle.js

/***/ }),/***/ "./src/js/scripts.js":
/*!***************************!*\
  !*** ./src/js/scripts.js ***!
  \***************************/
/*! no exports provided */
/***/ (function(module,__webpack_exports__,__webpack_require__) {

"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _my_test__WEBPACK_IMPORTED_MODULE_0__ = __webpa .... ");

/***/ })

/******/ });
thexiang 回答:webpack + babel-loader产生包含`eval()`的输出 webpack.config.js package.json (来自的除外)生成的bundle.js

这是因为您处于开发模式。 尝试:

  1. devtool设置为“无”以进行Webpack配置
  2. 为Webpack配置将mode设置为“生产”

您将不会看到评估。

UPD预设环境浏览器选项可以使用browserslist格式查询字符串设置:

  "presets": [
    [
      "@babel/preset-env",{
        "targets": "ie 11,chrome 58,> 0.25%,not dead"
      }
    ]
  ]

或带有数组,但它将在下一版本中删除

  "presets": [
    [
      "@babel/preset-env",{
        "targets": {
          "browsers": ["chrome 58","ie 10","not dead"]
         }
      }
    ]
  ]
本文链接:https://www.f2er.com/3028383.html

大家都在问