Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack generates arrow function in ES5

I want to use TypeScript modules and bundle them via Webpack. Here are my config files:

webpack.config.js:

const path = require('path');

module.exports = () => {
    return {
        entry: './index.ts',
        output: {
            filename: 'bundle.js',
            path: path.resolve(__dirname, 'dist'),
        },
        module: {
            rules: [{
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            }],
        },
    };
};

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es3"
  },
  "include": ["./**/*"],
  "exclude": ["node_modules/**/*", "webpack.config.js"]
}

Maybe I got something wrong from the documentation. The aim was to generate code in ES5 (or even earlier). But here is my bundled file:

(()=>{var n=function(n,o){console.warn(o)};n(0,0),n(0,1)})();

It has an arrow function, which was added in ES6. I am confused. How can I get rid of that?


EDIT:

Here's the code I try to compile:

const func = (foo, bar: number) => {
    console.warn(bar);
};

func(0, 0);
func(2, 1);

EDIT 2:

Also, I run the compilation process in production mode. (idk, maybe that's useful information)

like image 205
Ivan Adanenko Avatar asked Oct 31 '20 09:10

Ivan Adanenko


People also ask

Are arrow functions ES6?

Introduction. The 2015 edition of the ECMAScript specification (ES6) added arrow function expressions to the JavaScript language. Arrow functions are a new way to write anonymous function expressions, and are similar to lambda functions in some other programming languages, such as Python.

Is Webpack a transpiler?

Webpack bundles our code and outputs a transpiled version down to the target as specified in the configuration file. In our webpack configuration file, module rules allow us to specify different loaders, which is an easy way to display loaders.

Why arrow function is introduced in ES6?

Arrow functions are introduced in ES6, which provides you a more accurate way to write the functions in JavaScript. They allow us to write smaller function syntax. Arrow functions make your code more readable and structured.

Is arrow function supported in ES5?

The arrow function concept was introduced in ES6. With the help of this, we can write a shorter and concise syntax for a normal function which we used to write in ES5. Note: In arrow function, if there is only one statement then we don't even need to use '{}' curly braces.


2 Answers

The decision is simply adding target: ['web', 'es5'] to your webpack configuration.

You could also set target: 'es5', but in this case, there are some problems. At least in my case without specifying 'web' TerserWebpackPlugin refused to compress files in the production mode.

like image 83
Yoskutik Avatar answered Oct 18 '22 03:10

Yoskutik


That's the problem I faced last week after upgrading the webpack to version 5.

By default, it's bundling it as ES6. In your webpack configuration, configuring output.environment should resolve the problem.

Edit: webpack only change it's internal usages with these configurations.

webpack.js.org/configuration/output/#outputenvironment

It does not compile the modules. For the module compilation, babel should be used.

like image 4
Tugay İlik Avatar answered Oct 18 '22 05:10

Tugay İlik