Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up webpack.config.js with babel-preset-env?

Tags:

webpack

Does anybody know or have a webpack.config.js file that uses babel-preset-env? If so, please share.

Here is the config file:

const path = require('path');
const webpack = require('webpack');
const APP_DIR = path.resolve(__dirname, 'js');

module.exports = {
    entry: {
        app: "./app/javascript/app-one.js",
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                include : [
                APP_DIR,
                path.resolve(__dirname, 'node_modules/jquery')
            ],
                use: {
                    loader: 'babel-loader',
                    options: {
                        modules: true,
                        presets: ['env',
                            {
                                "targets": {
                                    "node": "current"
                                }
                            }
                        ]
                    }
                }
            }
        ]
    },
    node: {
        fs: "empty"
    },
    output: {
        path: path.resolve("./app/temp/scripts"),
        filename: "[name].bundle.js"
    }
};
like image 406
Leafyshark Avatar asked Oct 15 '17 12:10

Leafyshark


People also ask

How do I add presets to Babel config?

Using a Preset Within a Babel config, if the preset is on npm, you can pass in the name of the preset and Babel will check that it's installed in node_modules already. This is added to the presets config option, which takes an array. Otherwise, you can also specify a relative or absolute path to your presets.

What is preset ENV in Babel?

@babel/preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!


1 Answers

I think the misunderstanding you are having is that you set the target to be node: current.

preset-env only transpiles that which is necessary to support the target you are looking to support. See here: http://2ality.com/2017/02/babel-preset-env.html

Also, Node support for ES2015 is pretty solid (https://kangax.github.io/compat-table/es6/) so I would assume not much has to be transpiled.

If you want to transpile to ES5 then you should set target to for example IE 10. Most current environments have very high support for ES6 so transpilation will be minimal if at all (which is good).

like image 57
MikeDiaz1 Avatar answered Jan 14 '23 07:01

MikeDiaz1