Webpack with ES6 tutorial for beginners

webpack with es6

Webpack is actually the best tool to create web pages. Is the next generation tool after gulp and grunt. For me is really easy to configure and manage and I want to show you how I can make projects with this tool.

Prerequisites

First of all, we need nodes running on our machines. Open terminal or command line and try if you have installed node.js:

if you have installed you see the version of node, if not you need to install it, we have a little post explaining how to install:

Install Node in Mac OS X

You can clone the finished project from GitHub for reference: Github EBAVS/ Webpack ES6 tutorial

Configure Project

Now we need to initialize and configure the basics. Create a project folder on your computer and enter.

Initialise Project

First, we need the basics. We work with nodes and this tool uses a file called package.json to manage projects and dependencies. To create this file we need to execute the npm init command and answer questions.

Install Webpack

Now is time to install webpack. We search to install webpack itself and a tool called webpack-dev-server that we will use later.

The –save-dev option means that webpack is installed locally like development dependency in the project. If you want to install globally you can use -g option instead. I prefer local because is changing very fast and I don’t to leave working if I update globally to the last version.

First Steps and Configs

We create 2 folders. One for our code and another one for transpiled code (transpile is the way of transform one code into another code, for example, we will transform es6 in javascript)

And create an index.js file inside src/ folder with the next easy content.

Create webpack.config.js and add the basic part of the configuration:

This is the minimal configuration: we add a conf object with a path telling the two important paths. A second area, with module.export tells webpack what file needs to look like and what file need to generate.

Then we need to create a command to execute webpack through the command line. Open package.json and add “serve” task to scripts area. Scripts area looks like this:

“serve” task executes webpack calling webpack.config.js and transpile js to another file.

And now try to run with the next command in bash:

We created our bundle.js file. But we haven’t HTML yet and for the test, we need to use node:

 

No, this is not a web!

But’s not the point. Why? Because we are developing a web and this not a web. We don’t want a node web application. We want a web. Ok, let’s add HTML basic. Now make an index.html inside src/ folder and add basic HTML code, we don’t need more:

Install a required plugin for this operation.

Open webpack.config.js and then open a new section called plugin adding new HTML webpack plugin:

You can see HtmlWebpackPlugin Documentation here: Jantimon Github Webpack plugin html

Now we can execute again:

If we look inside dist/ folder we could see two files; index.html and bundle.js. HtmlWebpackPlugin gets js from config, and inject script tag inside HTML, if you open index.html with the browser and open inspector you should see “I’m alive!” message.

Actually, this is a little project, but don’t want to stop here. This is only the beginning. Let’s continue.

 

Begin to build a real web page with webpack

At this point, you have webpack with a little configuration but isn’t real. In a real project, you maybe use Bootstrap or Foundation with jQuery or maybe Angular.  Anyway, also we need to know what we want to do, and I believe that a better thing is a TODO list!

For a real and simple to-do list, we need some images, fonts, CSS, maybe effects and good code. Good code, actually, means ES6 or ES7. We choose ES6. Then, we have all chosen, let’s do begin.

 

ES6 or ECMAScript 2015

Modern browsers do not understand ES6, we need to transpile to old-fashioned javascript. To make these transpilations we use babel. Babel is a library that understands a lot of languages and can transpile to javascript.

Install Babel (you can go to https://babeljs.io/ to see all capabilities, I only choose few for this project):

Now we tell webpack that use babel for transpile, to do that we need to add the area of a module with rules config:

Explanation: We are telling you that all js files (test property) in src/ folder needs to be loaded with babel-loader.

Now, to be sure that we are writing good and correct code we install a lint loader. Every time we write bad code lint tell us to correct:

And their rule inside modules:

We use enforce to be sure that we are executing eslint before babel. Thes use excludes because not want lint al external module.

Now in our root folder need to create a file called .eslintrc.json with this content:

We are telling that eslint uses Airbnb rules. You can install other rules, Google has their:

And change “extends” to “google”, but not our case, Airbnb has good ones, you can inspect their rules: https://github.com/airbnb/javascript

Optional: if you want you can add a task in package.json spripts area to lint only. I think that is not needed but is good to be there:

 

Install dependent libraries

It’s time to install bootstrap and jQuery. These libraries have their package in npm repository. Install it:

See that we used –save and not –save-dev. This is because these two libraries are used in our project and need to be there in production.

Test jQuery

Now test jQuery. Open index.js from src/ folder and change a little bit, remove console.log and add next lines:

If we transpile and open the browser we can see “I’m alive” on a browser page. Cooooool! we are moving forward, we have a phrase in the browser instead of the inspector.

But for use with Bootstrap we need more config, open webpack.config.js and add a new first line:

Then we tell config that uses a new plugin to convert jQuery into a global variable, plugins will be:

if you halt this step, after add bootstrap, you will have an error saying that jQuery is not found.

Configure and test CSS/Bootstrap

Bootstrap is a collection of CSS and JS libraries. Include Bootstrap in our code needs an extra effort, first we install all needed and then I explain what is every module:

Yeah!

  • style-loader: gets our CSS files and inject them inside <style> tags.
  • CSS-loader: interprets @import and url() from CSS and resolve them
  • sass-loader (node-sass): gets sass and transpiles to CSS
  • file-loader: gets a file and return new name with md5 hash. It’s needed by previous loaders.

Open webpack.config.js and add:

Paste this code after babel-loader rule.

ES6 doesn’t know how to handle and import current CSS. If you try to import bootstrap CSS directly to index.js you will have an error. To avoid this we make a style.scss and put it in src/ folder with next content:

The tilde (~) means that need to look inside the node_modules folder.

And finally, we add code to index.js that looks like this:

I’ve added a new div to see if bootstrap is working. Run npm task …….. And yeees!

happy clapping GIF by Originals

Webpack-dev-server

Now, we have to work our base to begin the development, but I don’t want to run webpack every time I made a change in code. To avoid running tasks every time, we have two options; You can run webpack with –watch, which is good or you can run webpack-dev-server, which is better.

The difference is that –watch option leave webpack executed and run every time that rode is changed. It’s ok, but we need more power because we are developing web, and debug javascript, then is better a web server. With webpack-dev-server, you will have a real web-server integrated with webpack. We installed with webpack, the only want we need changes configuration. Open package.json and change server option:

Then if you run:

You will see all compilations, listings and then ready to browse: http://localhost:8080/

Changing webpack-dev-server configuration

Alternatively, you can change some parameters of the web server adding some parameters in webpack.config.js:

Adding devServer to config we can change the port, I prefer 3000 instead 8080.

You can see all options here: https://webpack.js.org/configuration/dev-server/

 

Developing Project

We have now the project with a good base. Let’s begin to create our real to-do application.

Making Views

ES6 have one important feature, have templating. You can store HTML in a variable and then inject it later on our web page.

Make new views.js in src/ folder. We put inside the little templates for creating the page:

We have here to constants with a little bit of HTML. The first constant is the container. The second one is the input text box where we will write tasks.

After definition, we add to an object collection and use export to say ES6 that we can use in other files.

Then we can open style.scss file and add some CSS to separate box to the top of the page:

 

Making Controller

Now we make the controller, create a file called controller.js.  Thi file will be responsible to load views and manage events and data.

It’s time to make with a few lines, only for initialize and show data to the browser:

Then we have a constructor that receives jQuery and views and store them in properties. Then you have initViews method that initializes HTML and injects it to DOM via jQuery. Then you have bindEvents to bind events to DOM and start a method that calls previous methods.

Put it all working

We have two files (views and controllers) but aren’t called yet. Then open index.js and will see this code:

Import jQuery and bootstrap and styles and then import views and Controller and call it. Simple. If you browse now you will see the first approach to our ToDo List.

First todo test

Isn’t working yet. But we will go to our final steps!

 

Making all working

We have the base. It’s time to finish this little project coding final events. We don’t want to extend more, don’t store todos in any place. Simply add and remove. Let’s do it!

Adding to Views

Into views, we have only brand and input text. We need to list items and to-do items. Add next lines to view.js to add new views:

And now change view collection constant to add new elements:

Cool, we have now all view parts ready.

Finishing Controller

The controller has the simple binding, addItem method is empty and is static. We need to code all of this. But, first of all, we need to finish initViews because we want to inject the item list on the page:

We have now a list rendered on the page. Time to code addItem method, but for code, we need to change the constructor to create an item list to store our todos:

And:

Note that we are calling removeItem, create it:

And bindEvents changed to; we need to change call to addItem because isn’t static;

You can run the project and see that all are working as expected. If not, please, feel free to comment on any errors you have and I will help you to make this work.

ebavs todo tutorial working

 

Webpack config for debugging

If you browse the project and open the web inspector you will notice that is impossible to debug the project because the code is hidden inside webpack envelope.  If you try to look at variable values or do some debug you will enter in a full head pain!

To avoid this situation and serve correct code to chrome we need to add an option to our webpack.config.js :

The devtool option allows saying webpack and webpack-dev-server that we need to use source map to our code. If you browse the project you will notice that can search files with CMD + P (Ctrl+P) and add breakpoints, etc …ebavs webpack debugging

 

Final Words

In a real-world project is probable that you don’t use jquery and bootstrap in a project. Actually are other cool tools you can use in your project like underscore, lodash or some others instead of jquery.

For me, include jquery in this project was a real pain because isn’t prepared for modern life projects. jQuery was created to make it easy life to create simple projects and projects with ES6  and Webpack are other words.

The same for bootstrap. On EBAVS/ bootstrap are changed to other CSS templates like foundation or skeleton. Bootstrap it’s a big project and sometimes we only need the grid and some helper functions to develop a single project. Bootstrap is a little monster now with a lot of components that we don’t use.

You can clone a project from GitHub: Github EBAVS/ webpack ES6 tutorial