簡體   English   中英

webpack + babel-loader 生成包含 `eval()` 的輸出

[英]webpack + babel-loader produces output containing `eval()`

從表面上看,我遇到了與此相同的問題: Webpack Babel-loader 使用 eval() 轉換代碼,但此解決方案對我不起作用。

我嘗試在webpack.config.js文件中同時使用@babel/preset-envbabel-preset-env預設。 我也嘗試過(但失敗了)使用.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']
                        }
                    }
                ]
            }
        ]
    }
};

包.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 .... ");

/***/ })

/******/ });

那是因為你處於開發模式。 嘗試:

  1. devtool為“無”為的WebPack配置
  2. 將 webpack 配置的mode設置為“生產”

你不會看到評估。

可以使用 browserslist 格式查詢字符串設置 UPD 預設環境瀏覽器選項:

  "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"]
         }
      }
    ]
  ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM