Skip to content

Instantly share code, notes, and snippets.

@yogain123
Last active February 24, 2024 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogain123/b73384c117c79116399ec89996d258b3 to your computer and use it in GitHub Desktop.
Save yogain123/b73384c117c79116399ec89996d258b3 to your computer and use it in GitHub Desktop.
webpack 5
@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Why Webpack

Screen Shot 2022-10-08 at 10 56 58 AM

Screen Shot 2022-10-08 at 11 09 19 AM

Screen Shot 2022-10-08 at 11 10 26 AM

Screen Shot 2022-10-08 at 11 12 31 AM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Basics

Screen Shot 2022-10-08 at 11 14 50 AM

Adding Config

Screen Shot 2022-10-08 at 11 33 58 AM


Screen Shot 2022-10-08 at 11 34 30 AM

If no mode is defined then by default it is production mode

Screen Shot 2022-10-08 at 11 35 24 AM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Fixing button click

Screen Shot 2022-10-08 at 11 40 45 AM

IIFE is has a separate scope in the browser, and this is done so that it does not pollute the entire namespace or the entire global space in the browser

Screen Shot 2022-10-08 at 11 42 20 AM

this is good approch as it does not create any conflict between any other file loader in the browser as it will have its own scope (execution context)


Screen Shot 2022-10-08 at 11 44 06 AM

@yogain123
Copy link
Author

CommonJS module

Screen Shot 2022-10-08 at 11 45 39 AM

Nodejs natively only understand commonJS , to use ES6 in node, we kind of need to teach nodejs to handle commonJS imports
Screen Shot 2022-10-08 at 11 54 28 AM

@yogain123
Copy link
Author

Loader Section

Screen Shot 2022-10-08 at 11 55 05 AM

Webpack by default doesnot know how to bbe dealing with file other than JavaScript (JS, JSON), we need to be teaching webpack exactly how to be loading other file type, that is where loader coming to help
We need to be defining different loader for different file types

Screen Shot 2022-10-08 at 11 57 23 AM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

CSS Loaders

Screen Shot 2022-10-08 at 12 01 58 PM

Screen Shot 2022-10-08 at 12 02 27 PM

when you index.js file is hit it sees an import of index.css , and css-loader now know how to be resolve this dependency, but it doesnot know what to be doing with it, so that data besically passes to the style loader, so it's kind of creating a style tag in html doc and putting css there
loader executes from right to left in order


Screen Shot 2022-10-08 at 12 05 49 PM

@yogain123
Copy link
Author

CSS Loader Part 2 (CSS modules)

Screen Shot 2022-10-08 at 12 15 22 PM

All class and id get exported in an object format

Screen Shot 2022-10-08 at 12 15 52 PM

Screen Shot 2022-10-08 at 12 16 14 PM


Screen Shot 2022-10-08 at 12 17 27 PM


Adding global style

Screen Shot 2022-10-08 at 12 18 16 PM

@yogain123
Copy link
Author

SCSS Loader

Screen Shot 2022-10-08 at 12 20 50 PM

Screen Shot 2022-10-08 at 12 24 17 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Loading Images In JS

Screen Shot 2022-10-08 at 12 28 38 PM


to make sure all assets goes inside images folder with hashed filename and proper extension

Screen Shot 2022-10-08 at 12 29 43 PM

@yogain123
Copy link
Author

Fonts Loader

Screen Shot 2022-10-08 at 12 32 56 PM

Screen Shot 2022-10-08 at 12 33 49 PM

this 2 are inbuild modules in webpack so it does not required to be installed as a dependecny

@yogain123
Copy link
Author

Plugins Introductions

Screen Shot 2022-10-08 at 12 35 35 PM

Multiple Entry bundle

Screen Shot 2022-10-08 at 12 40 32 PM


Screen Shot 2022-10-08 at 12 41 01 PM

@yogain123
Copy link
Author

Plugins

Screen Shot 2022-10-08 at 12 42 48 PM

wht exactly HTML Webpack pluging doing?
it will going to injecting all the dependecny directly into the html file. (automatically injecting)

Env plugin?
to setup some env variable

html minimizer plugins?
minify bundle size

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

HTML Webpack plugins

Screen Shot 2022-10-08 at 12 52 14 PM

It created its own index.html file and injected dependency there

Screen Shot 2022-10-08 at 12 52 41 PM


but we want dependency to be injecting in our own index.html file which we created
Screen Shot 2022-10-08 at 12 54 22 PM

But now our html seems to be working but we lost all our css and images?
lets fix it next

@yogain123
Copy link
Author

Webpack dev server

Screen Shot 2022-10-08 at 1 00 44 PM

Screen Shot 2022-10-08 at 1 01 04 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

importing

importing other css in css

Screen Shot 2022-10-08 at 1 09 22 PM

importing vendor module css in scss
Screen Shot 2022-10-08 at 1 10 01 PM


npm run dev -> webpack serve ,, its going to be just outputting it directly in browser and it sort of maintaining the dist folder in memory, thats why we do not see the dist folder here

@yogain123
Copy link
Author

Copy assets to dist folder

Screen Shot 2022-10-08 at 1 20 00 PM

@yogain123
Copy link
Author

Optimization Section

Screen Shot 2022-10-08 at 1 21 53 PM

Code Splitting

Screen Shot 2022-10-08 at 1 23 03 PM

Code splitting means bringing out some common dependency into seperate bundle for eg: here some common dependeny are: bootstrap, lodash
this prevents duplications

Screen Shot 2022-10-08 at 1 24 54 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

webpack-bundle-analyzer

Screen Shot 2022-10-08 at 1 28 33 PM


Screen Shot 2022-10-08 at 1 29 06 PM


Screen Shot 2022-10-08 at 1 29 37 PM

webpack split chunks

Screen Shot 2022-10-08 at 1 37 06 PM

this is generate sepeate chunk for css file in js format (css in js)

@yogain123
Copy link
Author

Lady Loading | Dynamic imports

Screen Shot 2022-10-08 at 1 39 54 PM

@yogain123
Copy link
Author

Adding hash to the output bundle file

Screen Shot 2022-10-08 at 1 42 21 PM

this hash version is only going to be changing only when any dependecny inside that bundle changes

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Producation Optimization

Screen Shot 2022-10-08 at 1 44 32 PM


Screen Shot 2022-10-08 at 1 45 11 PM


we split html and css so that each can be cache seperatly in browser so that change made in html does not require to pull css again from server and vice versa


Screen Shot 2022-10-08 at 1 50 48 PM

Screen Shot 2022-10-08 at 1 50 56 PM

Now we do not want to inject style directly in html (style-loader does this, ) instead we want a sepeate css file

Screen Shot 2022-10-08 at 1 52 26 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Shimming

Screen Shot 2022-10-08 at 1 54 33 PM

Screen Shot 2022-10-08 at 2 00 47 PM


Screen Shot 2022-10-08 at 2 01 19 PM


Screen Shot 2022-10-08 at 1 58 27 PM


It is now going to resolve mnt which was not found earlier, and that is how you be resolvng and modifiing the global level exports that are required by some of those legacy libraries

@yogain123
Copy link
Author

Remove dead CSS

Screen Shot 2022-10-08 at 2 02 49 PM

glob is a helper lib for scanner the folder


Screen Shot 2022-10-08 at 2 03 27 PM


Screen Shot 2022-10-08 at 2 05 01 PM

Screen Shot 2022-10-08 at 2 05 35 PM


what if some of your css classess are getting added dynamically, then we can use safeList and pass class in an array format so that it is always included and never purged

Screen Shot 2022-10-08 at 2 07 59 PM

@yogain123
Copy link
Author

Tree Shaking (Removing dead JS)

Screen Shot 2022-10-08 at 2 09 13 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

@yogain123
Copy link
Author

Merging webpack configs

Screen Shot 2022-10-08 at 2 17 01 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

React App using Webpack

Screen Shot 2022-10-08 at 2 20 54 PM


Screen Shot 2022-10-08 at 2 22 43 PM


Screen Shot 2022-10-08 at 2 24 09 PM


Screen Shot 2022-10-08 at 2 26 17 PM

Screen Shot 2022-10-08 at 2 26 40 PM

You will see an error, as jsx is not understood by default, we teach webpack to handle jsx files

Screen Shot 2022-10-08 at 2 27 49 PM

@yogain123
Copy link
Author

yogain123 commented Oct 8, 2022

Adding Babel loader

Screen Shot 2022-10-08 at 2 32 24 PM
Screen Shot 2022-10-08 at 2 32 38 PM


Screen Shot 2022-10-08 at 2 33 22 PM

Corrected dev server config

Screen Shot 2022-10-08 at 2 34 17 PM


Telling webpack not to mess with the route as it is handled by browserRoute (Client side)

Screen Shot 2022-10-08 at 2 36 49 PM

@yogain123
Copy link
Author

Post CSS Loader Configurations

it is like adding polyfills for css to support in all browsers

Screen Shot 2022-10-08 at 2 46 15 PM


Screen Shot 2022-10-08 at 2 45 57 PM


now polyfills are auto added by plugins to support diff browser

Screen Shot 2022-10-08 at 2 46 48 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment