Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

On the face of it I am having the same problem as this: Webpack Babel-loader transpiles code with eval() but this solution doesn't work for me.

I have tried using both, @babel/preset-env, and babel-preset-env presets within the webpack.config.js file. I have also tried (and failed) to implement both this configurations using the .babelrc file. Is it a module version conflict problem?

Let me know if there is any other information I can provide to make my problem clearer.

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",
...

(except from) generated 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 .... ");

/***/ })

/******/ });
like image 710
Adrian Roworth Avatar asked Oct 14 '25 22:10

Adrian Roworth


1 Answers

It is because you in development mode. Try to:

  1. set devtool to 'none' for webpack config
  2. set mode to 'production' for webpack config

And you will not see evals.

UPD preset-env browser options can be set with browserslist format query string:

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

or with array but it will be removed in next versions

  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "browsers": ["chrome 58", "ie 10", "not dead"]
         }
      }
    ]
  ]
like image 69
nickbullock Avatar answered Oct 17 '25 13:10

nickbullock