Webpack from A to Asia: Webpack Babel Loader

Tram Ho

Webpack from A to Asia with kentrung

When working with Javascript, sometimes we feel annoyed when applying the latest syntax, the code can run in one browser, die in another browser, or the same latest browser version is ok, but Again, on lower version because not yet supported. Fortunately, instead of having to worry about changing the code for each browser, a tool was born to solve that problem is babel. Babel is used for the purpose of converting JavaScript code written based on the new ECMAScript standard to an older version for compatibility with most browsers.

1. Installation

Inside

  • babel-loader is the loader module for webpack
  • @babel/core is babel’s core
  • @babel/preset-env is the babel preset for each environment. Example environments: chrome, opera, edge, firefox, safari, ie, ios, android, node, electron

2. Basic usage

The file and folder structure is as follows

Code file dist/index.html

Code file src/index.js

Although there are only two lines above, I have used the new ES6 syntax, which is to declare const variable instead of var and use Template Literals to execute expressions in strings. Code in the old days was written like this

Just like that, now to use babel loader we edit the webpack.config.js file

Now run webpack again to see what: npm run dev

We open the index.html page and see the log output with the desired statement.

I clicked on line 2 to see the code

It is wonderful! In general, with this whistle project, this configuration is ok, for super large, huge projects, you will have to dig deeper into the library to configure, but here is only for introduction. just waking up.


This article is over, hopefully with this article you have added a lot of useful knowledge. See you in the next article!

See more configurations for Babel loader at:

Share the news now

Source : Viblo