Introduction
What is webpack?
webpack is a module bundler. which bundles everything in our project e.g images, javascript files, CSS, assets, etc.
Why do we need a webpack?
webpack provide really cool features lets have look at it one by one.by the time I am writing these article webpack 4 has released.
Minify Code
the major issue with javascript applications is that it consumes lots of memory. among the entire code, some of the code is always either of no use, just comments, spaces, etc. if we remove that extra space our file size will be less and also load faster also code quality will improve.
Transpile
almost every year since 2016 ECMAScript release a new set of standards to make things easy and suitable but till today very few browsers fully supporting to ES6 standards but ES10 already released the majority of browsers support ES5 even today's date. it's clear that it will take a lot of time for browsers to make their javascript engines compatible with all the latest ECMA standards. due to that browsers use technique called transpiler which converts ES6 code to ES5 code. this is how most of the browsers nowadays support websites that have created in modern advance javascript. webpack helps to transpile code by the time of build.
Dependency graph
e.g .if file A importing file B and file B importing file B then, in that case, it creates conflict at run time. webpack helps to avoid that.
webpackDev servers
Follow setps
Installtion
npm install --save-dev webpack npm install --save-dev webpack-cli # or specific version npm install --save-dev webpack@npm install --save-dev webpack-cli
Setup Entry point and Destination Folder
module.exports = {
entry: './path/to/my/entry/file.js'
};
const path = require('path');
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js'
}
};
Add Extra plugins if you want
const path = require('path');
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js'
}
};
change package.json
{
"name": "webpack-demo",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"lodash": "^4.17.5"
}
}enter below commands
npm run build


0 Comments