41
Why to Learn WEBPACK before diving into REACT?
To solve this problem 👋WEBPACK can come in handy.
- Webpack also takes your assets and converts them to dependencies.
You don't need to understand these below concepts. We will see these in practice in the next section. They are here to just give you an overview.
PRINCIPALS | |
---|---|
1. Entry | Entry is the entry point for the application. It is the first module (JavaScript file)that Webpack will process to build out the full dependency graph |
2. Output | Output point is where the files are to be written on disk with the name of the files |
3. Loaders | Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs |
4. Plugins | Plugins handle the additional tasks that can’t be completed by a loader. |
5. Mode | Mode tells Webpack which configuration and optimizations to use for your application. |
npm init -y
: (Initialize node project)npm i wepack webpack-cli --save-dev
: (They are installed as dev dependency cause they just become Single Page Application at the end during production.)npm i svg-inline-loader --save-dev
: (Just a random SVG dependency for our practice.) JavaScript is not able to load SVG files directly, so we are going to use the svg-inline-loader module.npm i --save-dev css-loader
: (css loader used to load css in html file)npm i --save-dev babel-loader
:(so that we can use modern JavaScript)npm i --save-dev html-webpack-plugin
:(it injects your output code into html)npm i --save-dev webpack-dev-server
:(dev server which fast reloads when you save your file.)webpack.config.js
in the root folder.Add following code in it
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './app/index.js',
module: {
rules: [
{
test: /\.svg$/,
use: 'svg-inline-loader',
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(js)$/,
use: 'babel-loader',
},
],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [new HtmlWebpackPlugin()],
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
};
entry
:
-- In the above code, we defined entry point to our codebase ie. entry: "./src/index.js",
JavaScript engine would start to read code from this file.modules
: svg-inline-loader
& css-loader
--css-loader uses style-loader
under the hood.
rules
defines some rules
test
search files with given extention
use
when files with given extention is found use particular loaderoutput
:plugins
:
- html-webpack-plugin
: It generatesindex.html
file & takes output file from above and injects it into index.html
mode
:
- It says whether code should be in production mode or development mode.Add these two scripts in package.json
✔️ For macOS users
✔️ For macOS users
"scripts": {
"start": "webpack serve",
"build": "NODE_ENV='production' webpack"
},
✔️For Window users
"scripts": {
"start": "webpack serve",
"build": "SET NODE_ENV='production' & webpack"
},
"start":"webpack"
: It seraches in node_modules for webpack"build":"SET NODE_ENV='production' & webpack"
: It creates dist folder which can be used for production.create
app/index.js
Add following code in it
const data = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
console.log(data);
start
script you can see our app running in the browser.build
script you can see webpack creates dist folder which is our production build.Finally, we can see the JS file running in the browser is nothing but the bundle.js.

41