Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack Babel-loader transpiles code with eval()

I'm having an issue with Webpack and Babel. I'm trying transpile my JavaScript code into a bundle file. Here's the file structure and the snippets:

file structure:

- src
| file.js
package.json
webpack.config.js

package.json:

{
  "name": "babel-webpack-starter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.8.3",
    "webpack-cli": "^2.1.3",
    "webpack-dev-server": "^3.1.4"
  }
}

webpack.config.js:

const path = require('path');

module.exports = {
    entry: {
        app: './src/file.js'
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'app.bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['env']
                        }
                    }
                ]
            }
        ]
    }
}

When I enter webpack --mode development, it creates the file app.bundle.js successfully inside the directory build.

enter image description here

However, it doesn't seem to be working properly, because at the end of build/app.bundle.js where I'm looking for the code from src/file.js I have the following :

/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\n\nvar fun = function fun() {\n  return console.log('Hello World');\n};\n\n//# sourceURL=webpack:///./src/file.js?");

/***/ })

Which is strange, am I not supposed to simply have this instead?

/***/ (function(module, exports, __webpack_require__) {

"use strict";
let fun = () => console.log('Hello World')

/***/ })

Is there a problem with the config?

like image 859
Ivan Avatar asked May 15 '18 20:05

Ivan


People also ask

How to use Babel with Webpack?

Moreover, in case you have Webpack in place to bundle your JavaScript application, you will have to install a Webpack Loader for Babel: Now, with all libraries (node packages) in place, you need to adjust your package.json and webpack.config.js (if necessary) to respect the Babel changes.

How do I use Babel-loader with Webpack?

This package allows transpiling JavaScript files using Babel and webpack. Note: Issues with the output should be reported on the Babel Issues tracker. Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so: See the babel options. You can pass options to the loader by using the options property:

How do I pass options to the Babel loader?

See the babel options. You can pass options to the loader by using the options property: This loader also supports the following loader-specific option: cacheDirectory: Default false. When set, the given directory will be used to cache the results of the loader.

How do I install Babel on npm?

In order to get Babel running, you need to install two of its main dependencies on the command line: npm install --save-dev @babel/core @babel/preset-env Moreover, in case you have Webpack in place to bundle your JavaScript application, you will have to install a Webpack Loader for Babel: npm install --save-dev babel-loader


2 Answers

This is actually not because of babel, but because of webpack. It requires an option called devtool that decides whether it should eval the code or use some sort of source-map.

You might be looking for the following:

// webpack.config.js (excerpt)
module.exports = {
    // ...
    devtool: 'inline-source-map'
    // ...
};

The inline-source-map omits eval in favor of a - well - inlined sourcemap inside the bundle. Do not use it for production, though ;-)

There are several options for devtool that all have their advantages and disadvantages. For more information on the topic, please refer to the official webpack documentation.

like image 112
Rico Herwig Avatar answered Oct 11 '22 04:10

Rico Herwig


After countless hours of research, I finally found the solution, the preset that needs to be used is babel-preset-env and not env.

const path = require('path');

module.exports = {
    entry: {
        app: './src/file.js'
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'app.bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['babel-preset-env'] // <-- here
                        }
                    }
                ]
            }
        ]
    }
}
like image 5
Ivan Avatar answered Oct 11 '22 05:10

Ivan