Webpack Image

Webpack: Getting Started

The way we write JavaScript today is different from the code that the browser can execute. We frequently rely on other types of resources, transpiled languages, and experimental features which are yet to be supported in modern browsers. Webpack is a module bundler for JavaScript that can bridge this gap and produce cross browser–compatible code at no expense when it comes to developer experience.

Before we get started, you should keep in mind that all code presented in this Webpack tutorial is also available in the form of a complete Webpack example configuration file on GitHub. Please feel free to refer to it there and come back to this article if you have any questions.

I have made several commits in the repo for understanding webpack step by step. You can see particular on this link. And checkout to those commit e.g:

Please go through repo for understanding of the below examples link.

git checkout 89637f7ed458d8280c4542a2a87a6b2397594e7d

GitHub Webpack tutorial commits

First Commit : Simple App

=> git checkout 89637f7ed458d8280c4542a2a87a6b2397594e7d

I have made a simple application which takes username and userId from end-user and prints it on screen. It also have some validation for name and userId and gives error to end user. Also there is an image of webpack logo which we will optimize using webpack in further commits.

WebPack Tutorial Example

Index.html

https://gist.github.com/vinodchauhan7/497643664382ace761034ee6ae080afe

Open index.html file.

Second Commit : Broke Js code into 6 files

=> git checkout 1ca9ee0e85566146f477c530f4d4114f5e22372f

In this commit, I have divided the js code into 6 files so that we can simulate to a large example which will have many js files and can be used to make a single bundle file with webpack.

Third Commit : Add Webpack and package.json file

=> git checkout b9e7eb70469d5f6bd11c64cd54ba8a94532fb8a2

In this commit, I have added a package.json file and then added webpack. For package.json : npm init -y and then add : npm install — save -dev webpack webpack-cli. To start our application with webpack we have add a script in package.json file :

“scripts”: { “start”: “webpack” }

When we hit a command on npm start on terminal. It will give us error that we need to include ‘src/index.js’ file for webpack to work. On successful working of the npm start, a newFolder with dist including main.js will be there. We can include that main.js in our index.html. It will be working and webpack is handling our application. Open index.html to see on browser.

Fourth Commit : Webpack now bundle all our app

=> git checkout a08072f552066e02859a21c170ae243112bbcb0d

In this commit, We have used Import/Export for injecting the dependencies from one file to another and also remove <script> tags from html.

Fifth Commit : Add webpack config file

=> git checkout 194985095f6e24628a8c69e05f2d586bea96634c

In this commit, we have added our own webpack.config.js file for doing configurations.

Path module is used to get the path of the system, so that we can dynamically get the path of the user system without any confusion. It is given to us by npm. module.exports is the configuration which webpack will look for. mode : There can be two value “development” & “production”. We are using “development” for now, to make the things working without any complexity. entry : It is the entry file to where webpack have to look for. output.path : It will tell webpack where to create the output folder. __dirname is something which will get the current path where your project is currently on your system. You can give any name to your ouput folder. I have given ‘dist’. output.filename : Here we will telling webpack, what our output file name will be.

Also we are updating package.json

“scripts”: {“start”: “webpack — config webpack.config.js”}

Sixth Commit : Add Loader to handle css

=> git checkout b296566130db6559264bda7e7423f2feab64a782

In this commit, we are introducing loaders.

webpack only understands JavaScript and JSON files. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.

We are adding a new css file ‘index.css’ in which we are changing background & foreground color of the webpage and including it in our index.js file. And to load it in our webpack we need to use some loaders which can be found on webpack website. We are using “css-loader”,”style-loader”.

npm install — save-dev css-loader style-loader

css-loader : It will convert css into commonJs.

style-loader : It will inject js into dom.

Include below code in package.json

module: {

rules: [

{

test: /\.css$/,

use: [“style-loader”, // Inject commonjs into dom

“css-loader” //convert css into commonJs

]

}

]

}

In module tag, include rules tag.

test : Here we need to add the extension of those files on which we need to add loader for webpack to build its dependency graph. use : It is dependency array where we need to add all our loaders. Note : We need to add loader in a proper order for them to work.

Seventh Commit : Added Sass-loader and override bootstrap colors

=> git checkout 33df9811e8ba4cc2e886d3ea3e4aa3d2c9b27bf8

In this commit, We are convering sass styles and also adding sass-loader in our webpack configuration to handle .scss file extension. For supporting sass in our webpack, we need to download two loader.

npm install — save-dev sass-loader node-sass

Update webpack.config.js’s rule set :

{ test: /\.scss$/, use: [“style-loader”, “css-loader”, “sass-loader”] }