【问题标题】:webpack + babel-loader produces output containing `eval()`webpack + babel-loader 生成包含 `eval()` 的输出
【发布时间】:2020-03-22 00:00:33
【问题描述】:

从表面上看,我遇到了同样的问题:Webpack Babel-loader transpiles code with eval() 但这个解决方案对我不起作用。

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

如果我可以提供任何其他信息以使我的问题更清楚,请告诉我。

node: v10.15.3, npm: 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 .... ");

/***/ })

/******/ });

【问题讨论】:

    标签: javascript node.js webpack babeljs


    【解决方案1】:

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

    1. 为 webpack 配置将 devtool 设置为 'none'
    2. mode 设置为 webpack 配置的“生产”

    而且你不会看到 evals。

    UPD preset-env 浏览器选项可以使用 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"]
             }
          }
        ]
      ]
    

    【讨论】:

    • 我可以再问一个问题吗?将浏览器选项(目标)添加到 '@babel/preset-env' 预设的正确语法是什么?在我的webpack.config.js
    • 谢谢。我错过了嵌套数组。只是有一个级别。终于工作了,干杯!
    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多