Tuesday, July 3, 2018

Learn Babel Loader Javascript ES6





What is babel or babel.js? Babel is a transpiler that changed the syntax of modern JavaScript (ES6+) into syntax that all browsers can fully support.


JavaScript is a programming language that is growing very rapidly. The community is great, and every year there is always a new version.

However, the development of rapid turns out to require a long time to be adopted by the browser or Node.js. Then if we want to try the syntax of the latest in JavaScript, do we need to wait until the entire browser successfully adapted to the update? Of course not!


With babel, you can write down the syntax of JavaScript as the latest version without worrying about support on your browser. Because babel will change the syntax of which we write into the code that is acceptable to the browser.

If You're curious how babel works, babel provides a playground that we can use to change the syntax of modern JavaScript (ES6+) into the syntax old. To try it, please go to the following links: https://babeljs.io/repl.

20200313195932ff6b88f0352e20c9c08f27fd89aff700.png
On the playground, we can also choose the preset that we want. By default, presets will lead ES2015 (ES6).
You already know at a glance about babel. Well, on webpack, we can also use babel in the form of a loader.
Although webpack by default can process JavaScript files without the need for the help of a loader, the process does not change the syntax of which we write.
This means that if we write down the syntax of modern JavaScript, we will also find her on file bundle.js.
2020031320001286d0cce40a7f1f8ac22836dcbab4867b.png
Although Google Chrome and Mozilla Firefox already support the writing of the syntax of ES6, however at least we need a bit of care to support older browsers such as Internet Explorer or the browser of the old version of the other.
To use babel in webpack as a loader, we need to install three packages using npm in devDependencies.
The first package @babel/core, the second babel-loader, and the third @babel/preset-env.

  1. npm install @babel/core babel-loader @babel/preset-env --save-dev


Package @babel/core is a package of the core that should be installed when we want to use babel, both in webpack or tools to the other.
Package babel-loader is the package needed to use babel as a loader in webpack.
The last package, @babel/preset-env, is a preset package that we will use to help babel-loader perform their duties.
@babel/preset-env is a preset intelligence that allows us to use the latest JavaScript without setting specific syntax of the JavaScript version of what we use.
File package.json will look like this after installing a third of the packages:

  1. {

  2.   "name": "webclock",

  3.   "version": "1.0.0",

  4.   "description": "",

  5.   "main": "index.js",

  6.   "scripts": {

  7.     "build": "webpack"

  8.   },

  9.   "author": "",

  10.   "license": "ISC",

  11.   "dependencies": {

  12.     "jquery": "^3.4.1",

  13.     "moment": "^2.24.0"

  14.   },

  15.   "devDependencies": {

  16.     "@babel/core": "^7.8.4",

  17.     "@babel/preset-env": "^7.8.4",

  18.     "babel-loader": "^8.0.6",

  19.     "css-loader": "^3.4.2",

  20.     "style-loader": "^1.1.3",

  21.     "webpack": "^4.41.6",

  22.     "webpack-cli": "^3.3.11"

  23.   }

  24. }


After successfully installing a third of the packages, the next step we can use the babel-loader and the presets in the webpack configuration.

  1. const path = require("path");

  2.  

  3. module.exports = {

  4.     entry: "./src/index.js",

  5.     output: {

  6.         path: path.resolve(__dirname, "dist"),

  7.         filename: "bundle.js"

  8.     },

  9.     mode: "production",

  10.     module: {

  11.         rules: [

  12.             /* style and css loader */

  13.             {

  14.                 test: /\.css$/,

  15.                 use: [

  16.                     {

  17.                         loader: "style-loader"

  18.                     },

  19.                     {

  20.                         loader: "css-loader"

  21.                     }

  22.                 ]

  23.             },

  24.             /* babel loader */

  25.             {

  26.                 test: /\.js$/,

  27.                 exclude: "/node_modules/",

  28.                 use: [

  29.                     {

  30.                         loader: "babel-loader",

  31.                         options: {

  32.                             presets: ["@babel/preset-env"]

  33.                         }

  34.                     }

  35.                 ]

  36.             }

  37.         ]

  38.     }

  39. }


When applying the rule to the file .js, do not forget to set the property to exclude with the value "/node_modules/".
What does it mean? To set the property to exclude it means we exclude webpack to process the file .js located in the folder "node_modules".
This can minimize the process is not necessary, thus speeding up the build on our new project.
Then, on the babel-loader application, we also use the options property to set the property presets in it. On the property presets, we set the preset (in the form of an array literal) we've installed using npm, namely @babel/preset-env.
After using the babel-loader in the webpack configuration, let's try to build and reopen the file bundle.js.
Then the code that we write in ES6 will be changed in the syntax that all browsers can accept.
20200313200507f7a720e39de819c62a15f1be147aa62e.png
Even on the file bundle that certainly was not there again, the syntax is written using ES6.
20200313200535f035aab9b99622b9e95be3f0d98fc827.png
However, although the syntax has been changed, the project will remain normal.

No comments:

Post a Comment