PU$]-[KAR's

… Every drop has ability to survive .


Leave a comment

Webpack Basics

Hello,

While using ES6, React.js it’s always recommended to use module bundler like ‘webpack’ so today I would like to share the basics of webpack in simpler manner. There are many of blogs already published on the internet but the main reason to post this blog is to consolidate the things under one roof and also share my personal experience and opinion while using webpack.

Webpack … Does is really required ?

There was a time where it was considered that there is no such need of any tool who takes care of the client side code for moduling, merging, generating builds automatically etc. etc. Because the at that time the code written in JavaScript was not that much vast and evolved as compare to today’s projects.
Day by day javascript is getting evolved. Now the developers are using TypeScripts, ES6 going one step ahead of javascript. SO of course there will be need of transpilers like ‘Babel’ to convert the code in javascript. And the use of javascript itself has been increased tremendously. Modules are being developed by parallel programming is going on with javascript. So there is of course a need of a tool who will take care of merging these files into single one. That’s why the module bundlers like webpack came into picture.

Major activities that can be handled by webpack

As discussed above module bundlers are the tools who take care about the all client side code. So we will just go through the important roles which webpack can perform to mange the client side code.

  • Combine the developed code at one place: Just discussed above in the world of parallel programming webpack takes care of combining all the modules that are created in different files to single file
  • Resolving dependency: Wile combing the files to one file webpack takes care of resolving the inter module dependency. That means if ‘student’ module dependent on module called ‘person’ in that case webpack will take care of having all the code of module ‘person’ will be present before executing the code for module ‘student’. We don’t have to care about the dependency. Webpack will do the job for our self.
  • Reduce the file size: Reducing the size of file is very important when the code goes on the production. Removing of just white space also makes a lot of different in file size and hence it will improves the performance as well. So webpack takes care of such things like removing white space, minification etc. So this things helps us to enhance the performance of application.
  • Transpilation:webpack alos supports the transpilers like Babel or any other too.

Getting started with webpack

To install webpack we can use simply node packet manger with following command npm install webpack –g

Now, to understand the the configuration of webpack we will consider that we have been added ‘webpack.config’ file at root folder (in project structure) with following configuration.
module.exports = {
entry: "./app/component/main.js",
output:{
filename:"public/bundle.js"
},
Watch: true,
module:{
loaders:[{
test:/\.jsx?$/,
exclude:/(node_modules)/,
loader:'babel'
}
]
},
resolve: {
extensions : [‘’,’.js’,’.myExtension’]}}

To begin with module.exports, this is the object that’s actually returned as the result of a require call.

entry: Key ‘entry’ refers the file along with its path in which we have written a code. Even we can give multiple files using array notation as well.

filename: Inside output, filename denotes the single file in which all code (which we have written in different files) is going to be get combined and will be consumed by the browser to run the file.

These are the basic configuration which we need to run the application using webpack.

To run the application there is no need to install any different webserver. We can use the webserver provided by webpack itself.
The command Npm insall webpack-dev-server –g will install the webserver.

No we can run our application with command ‘$webpack-dev-server’.
But this will contain default bar in browser so to avoid this bar we can use command ‘$webpack-dev-server- -inline’

watch: To rerun the application every time whenever we make a small change in file is a tedious job. So if we set ‘watch’ to true, This will take care of rerun the application on every change.

Now we will look at the loader section. Till this point we were looking to run application with simple javascript files but as we are using react.js, ES6 we have to provide transpiler details in loader section so that files using ES6 etc. will get transpiled into javascript file.

test: This will takes care to run the test file as we are going to specify for this key. We can specify our own custom names or path to make repository of test files too.

exclude: As name suggested this will exclude the files, currently we have set this to exclude files from node_modules folder.

loader: Currently This we have set to ‘Babel’ transpiler.

resolve: This is a section which specify what are the file types that we can process without giving the specific extension. The list of extension can be given in array including any custom extension as well.
By default webpack will process files with ‘js’ extension.

We can add preorders such as jsHint for linting errors by adding key preloader in webpack.config file.

That’s all folks !!!

Advertisements


Leave a comment

DOT LESS … What & Why ?

Hello,

Today we will be discussing few simpler points about ‘LESS’ programming i.e. one of the preprocessor for writing ‘CSS’. So to begin with …

What is DOT LESS?

Less is the preprocessor of Css and every one is well familiar with Css. It means that ‘Less’ is a style sheet which is generated dynamically and it can be compiled into Css.

Why LESS?

Now one can fairly argue that why we would add an extra layer before Css because everything is going to be converted into Css only. Then why anyone would put an extra burden?
And the answer lies in the extra features that have provided by the Less to write Css in more simpler manner and in more organized way. So First we will see the features of Less on by on and the finally we will come why you would consider Less instead of writing Css directly.

  • Adds ‘Right’ things at ‘Right’ place: We know Css is a dumb language. In Css we go on adding stuff but no variables, no functions, no reusability at all. Less empowers the Css by adding such features like variables, mixins, and functions.
  • Time Saving: You’ll be amazed at how much time you save when you aren’t forced to write the same things over and over.
  • Easier to maintain: Being able to use variables, mixins, and functions means you can define a value or group of values. This makes developer’s life easy. By declaring all this stuff at the beginning of your document, instead of through it, making it easier to make changes later.
  • Makes Css in more organized structure: Less supports nested hierarchy of controls.

To be going forward we are going to see these features in details.

Features of LESS

Now, here we will walk through few very important and widely used features of Less which helped to make developer’s life happier 🙂

  • Variables:Its pretty simple and straight forward thing. We can declare variables like constants and that can be reused across the files. e.g.
     @nice-blue: #5B83AD;
    @light-blue: @nice-blue + #111;
    #header {
    color: @light-blue;
    }
  •  

  • Mixins:Mixins are kind of setting a behavior rule to the application. Bunch of properties are declared together and can be reused across the application to apply certain behavior to certain controls.
    E.g. While setting up the border to any div or panel we will definitely declare certain properties repetitively, so these properties are can be bound together to create a mixin.
     .bordered
    {
    border-top: dotted 1px black;
    border-bottom: solid 2px black;
    }
    #menu a
    {
    color: #111;
    .bordered;
    }
     
    Here, if you look at ‘menu a’ has consumed mixin ‘bordered’, so even other classes like panel, div can consume this same mixin to apply the border in same way.
    Even we can pass parameters/varibles to the mixins as well so that we can pass our own values inside the mixins.
  •  

  • Nested Rules:Less follows the hierarchy of the DOM elements to declare nested styles.
    If you go through the following structure of CSS you will understand the point.
     #header
    {
    color: black;
    }
    #header .navigation
    {
    font-size: 12px;
    }

     
    So this Css says that DOM ‘header’ also contains class ‘navigation’ inside it. It is a bit difficult and tedious to write in Css manner but Less can understand the structure and provides a simpler way to declare styles using DOM hierarchy shown as below.
     #header
    {
    color: black;
    .navigation
    {
    font-size: 12px;
    }
    }

     Now, if you could observe we have used hierarchy of inside less itself i.e. header -> navigation.
  •  

  • Use of operators: Any number, color or variable can be operated on. Here is an examples:
     
    @var: 5px + 2.
     
    Here we can observe that Less is smart enough to understand that ‘px’ is a unit so adding ‘2’ will result into ‘7px’. Finally ‘var’ will hold value as 7px.
  •  

  • Importing the files: Less provides the features of importing the variables and mixins from the different file. This helps developer to modularize the code in better manner. The import statement will follow the following syntax.
     
    @import “@{themes}/fileName.less”;
     
  •  

  • Functions: Less provides built-in Functions as well. Like percent, saturation (incase of color saturation) and many more. So developer need not to takecare of such small things and in fact these things are widely used in styling.

Apart form these Less also provides many more features like:

     

  • Lazy loading: In this case Variables are lazy loaded and do not have to be declared before being used.
  •  

  • Lazy loading: Less can perform looping. We ca write recursive mixins, iterations in less as well.

As we dig into Less more and more the list is getting bigger and bigger.

To summarize it…


My friends, at first glance using ‘Less’ will look like more tedious but form my personal experience I can say that just start using it & more you use will more you love it. 😉