SlideShare a Scribd company logo
1 of 138
Production Ready JavaScript
Sanjay Krishna Anbalagan
Production Ready
Webpack
Production Ready
Webpack
Production Ready
Build a Bundler from Scratch
Webpack
2000
Production Ready
File
Size
Request
RequestResponse
Server Memory to Browser Run time Memory
RequestResponse
Server Memory to Browser Run time Memory
RequestResponse
Issues:
1. File Size ?
Reduce Variable Name Length
Reduce Variable Name Length
Remove White Spaces and trailing Spaces
Reduce Variable Name Length
Remove White Spaces and trailing Spaces
Reduce Variable Name Length
Remove White Spaces and trailing Spaces
Issues:
1. File Size ? Minified File …..!!!!!
Server Memory to Browser Run time Memory
RequestResponse
Issues:
1. File Size ? Minified File …..!!!!!
2. Multiple File ?
Server Memory to Browser Run time Memory
RequestResponse
Issues:
1. File Size ? Minified File …..!!!!!
2. Multiple File ? Combine Files …..!!!!!
Server Memory to Browser Run time Memory
RequestResponse
Issues:
1. File Size ? Minified File …..!!!!!
2. Multiple File ? Combine Files …..!!!!!
Server Memory to Browser Run time Memory
RequestResponse
2015
Client Browser
JS Version
2015
Developers Problem
Polluting Global Scope
Polluting Global Scope
IIFE Pattern
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression => Module Pattern
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
IIFE: Immediately Invoked Function Expression
2020
Issues:
1. File Size ? Minified File …..!!!!!
2. Multiple File ? Combine Files …..!!!!!
Server Memory to Browser Run time Memory
RequestResponse
Issues:
1. File Size ? Minified File …..!!!!!
2. Multiple File ? Combine Files …..!!!!!
Server Memory to Browser Run time Memory
RequestResponse
Issues:
1. File Size ? Minified File …..!!!!!
2. Multiple File ? Bundled Files ….!!!!
Server Memory to Browser Run time Memory
RequestResponse
Demo

More Related Content

Similar to Webpack Internals - Bundle - Production Ready JS

Yahoo! Developer Networks ♥ Startups
Yahoo! Developer Networks ♥ StartupsYahoo! Developer Networks ♥ Startups
Yahoo! Developer Networks ♥ StartupsTom Croucher
 
Rapid Prototyping Chatter with a PHP/Hack Canvas App on Heroku
Rapid Prototyping Chatter with a PHP/Hack Canvas App on HerokuRapid Prototyping Chatter with a PHP/Hack Canvas App on Heroku
Rapid Prototyping Chatter with a PHP/Hack Canvas App on HerokuSalesforce Developers
 
Be a library developer!
Be a library developer!Be a library developer!
Be a library developer!Kouhei Sutou
 
Develop:BBC 2013 - Turbocharge your mobile web apps by using offline
Develop:BBC 2013 - Turbocharge your mobile web apps by using offlineDevelop:BBC 2013 - Turbocharge your mobile web apps by using offline
Develop:BBC 2013 - Turbocharge your mobile web apps by using offlineJan Jongboom
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right nowCaleb Jenkins
 
JavaScript tools
JavaScript toolsJavaScript tools
JavaScript toolsHazem Saleh
 
Browser Automation - NTC 2010
Browser Automation - NTC 2010Browser Automation - NTC 2010
Browser Automation - NTC 2010Ehren Foss
 
The Scientific Filesystem
The Scientific FilesystemThe Scientific Filesystem
The Scientific FilesystemVanessa S
 
BPM-4 Migration from jBPM to Activiti
BPM-4 Migration from jBPM to ActivitiBPM-4 Migration from jBPM to Activiti
BPM-4 Migration from jBPM to ActivitiAlfresco Software
 
Games for the Masses (Jax)
Games for the Masses (Jax)Games for the Masses (Jax)
Games for the Masses (Jax)Wooga
 
How Heroku uses Heroku to build Heroku
How Heroku uses Heroku to build HerokuHow Heroku uses Heroku to build Heroku
How Heroku uses Heroku to build HerokuCraig Kerstiens
 
Continous Delivering a PHP application
Continous Delivering a PHP applicationContinous Delivering a PHP application
Continous Delivering a PHP applicationJavier López
 
Reducing passive interruptions
Reducing passive interruptionsReducing passive interruptions
Reducing passive interruptionsAndrei Tognolo
 
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)Fabien Potencier
 

Similar to Webpack Internals - Bundle - Production Ready JS (20)

Source Control 101
Source Control 101Source Control 101
Source Control 101
 
Yahoo! Developer Networks ♥ Startups
Yahoo! Developer Networks ♥ StartupsYahoo! Developer Networks ♥ Startups
Yahoo! Developer Networks ♥ Startups
 
Innovateeurope
InnovateeuropeInnovateeurope
Innovateeurope
 
Javacro 2014 Spring Security 3 Speech
Javacro 2014 Spring Security 3 SpeechJavacro 2014 Spring Security 3 Speech
Javacro 2014 Spring Security 3 Speech
 
Rapid Prototyping Chatter with a PHP/Hack Canvas App on Heroku
Rapid Prototyping Chatter with a PHP/Hack Canvas App on HerokuRapid Prototyping Chatter with a PHP/Hack Canvas App on Heroku
Rapid Prototyping Chatter with a PHP/Hack Canvas App on Heroku
 
presentation
presentationpresentation
presentation
 
Be a library developer!
Be a library developer!Be a library developer!
Be a library developer!
 
Develop:BBC 2013 - Turbocharge your mobile web apps by using offline
Develop:BBC 2013 - Turbocharge your mobile web apps by using offlineDevelop:BBC 2013 - Turbocharge your mobile web apps by using offline
Develop:BBC 2013 - Turbocharge your mobile web apps by using offline
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right now
 
JavaScript tools
JavaScript toolsJavaScript tools
JavaScript tools
 
Browser Automation - NTC 2010
Browser Automation - NTC 2010Browser Automation - NTC 2010
Browser Automation - NTC 2010
 
The Scientific Filesystem
The Scientific FilesystemThe Scientific Filesystem
The Scientific Filesystem
 
BPM-4 Migration from jBPM to Activiti
BPM-4 Migration from jBPM to ActivitiBPM-4 Migration from jBPM to Activiti
BPM-4 Migration from jBPM to Activiti
 
Don't screw it up! How to build durable API
Don't screw it up! How to build durable API Don't screw it up! How to build durable API
Don't screw it up! How to build durable API
 
Games for the Masses (Jax)
Games for the Masses (Jax)Games for the Masses (Jax)
Games for the Masses (Jax)
 
presentation
presentationpresentation
presentation
 
How Heroku uses Heroku to build Heroku
How Heroku uses Heroku to build HerokuHow Heroku uses Heroku to build Heroku
How Heroku uses Heroku to build Heroku
 
Continous Delivering a PHP application
Continous Delivering a PHP applicationContinous Delivering a PHP application
Continous Delivering a PHP application
 
Reducing passive interruptions
Reducing passive interruptionsReducing passive interruptions
Reducing passive interruptions
 
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Webpack Internals - Bundle - Production Ready JS

Editor's Notes

  1. Hello Everyone, When we say production Ready JavaScript in 2020, the word that strike our mind is
  2. Webpack
  3. In this video we are not going to learn about Webpack, instead
  4. We are going to build a basic bundler from scratch. In order to that, let travel two decades back
  5. We are in 2000, hmmmm, what does it mean ? when we say production ready JavaScript in 2000.
  6. File Size ….. Why ? Let address that with an example
  7. Back then Web Application is Purely back end work, JavaScript was primarily used as code snippets. Here is an example , A HTML page, Which displays current data and time, when we click the button. We developers want to keep the code clean by abstract the logic from HTML How do we do that ?
  8. Let move the logic from INLINE to Script Tag
  9. We defined the logic in a function inside script tag
  10. And we subscribed that function for button click Cool. This looks clean, How about taking this logic to a separate file ?
  11. Here we moved this logic to a separate file myscripts.js
  12. Aha, Now we had to worry about our javaScript file Size. Why ? When our browser request for this file, loading time of this file depends on its size
  13. Here our browser makes a request
  14. Server responds back with a copy of this Javscript file. Which is stored in
  15. Browser Run time Memory.
  16. So file Size matters in terms of download time and Client Memory. Let see this with an example
  17. Hers is our file, who size is
  18. 100 Bytes
  19. To reduce our file size, lets reduce the length of our function name and variable name here
  20. Here we replaced function name ‘displayDateAndTime’ with ‘f’ And variable name ‘date’ with ‘e’
  21. Awesome, what else we can do here?
  22. hmm….. lets remove white space and trailing spaces, And check our file size again
  23. Wow, we did it … We could bring down our file size to 75 bytes from 100 bytes.
  24. This Minified File is our Production Ready Javscript
  25. Did it end there ? No Now We want to abstract each logic to its respective file,
  26. So our next issue is handling multiple files.
  27. What we did here is, we combined both the files as a single file
  28. And minified them
  29. And now our production ready javaScript is Combined and Minified File
  30. Awesome now we are in 2015, we successfully created production ready javaScript. Wait, what happened now.
  31. The JavaScript community too grown now , new features started coming to Javscript language. Wait does that mean all our client machine needs to update to latest browser ? Wont that be stupid, do I need to ask my grand parents to install latest browser? Are there any other solution ? …….. Yes it Let start with an example to address this
  32. Hers is an example with two new javaScript features Default value for Arguments New Syntax let These two new feature will fail in my grandparents browser.
  33. To fix that, we need to rewrite the same logic in old feature we moved onlyTime argument as variable inside the function with default value fale We replaced let with var.
  34. This we call as transpillers Babel is one of the most actively used tranpilers tools.
  35. Now We combine files
  36. Into a single file
  37. Traspile them to common version
  38. And finally we minify them
  39. Now our Production ready JavaScript is Combined, Transpilled and Minified
  40. With more and more logic moving to JavaScript. Developers started facing their biggest nightmare,
  41. Global Scope Pollution
  42. Let me explain . When our file gets executed by JavaScript engine
  43. They start creating new property to window object. Wait … Why this would be problem ? Imagine, we have 10 files, and each file has 4 function, Developers had to make sure, they have to create unique-Names for each variables And if it’s a distributed team of developers, a team might end up overriding a function with the same name This will be a nightmare to debug and track the problem. Hmmm Do we have solution ? Yes of course,
  44. IIFE … Pattern, Wow what is that ?
  45. let say we have two files file1 – has getdate Definition File2 – has displayDateTime definition I want everyone to closely follow from here, Hope everyone could see this faded abbreviation of IIFE, in the top I am going to reveal each word in this abbreviation with its respective step in code, Let start with the first step
  46. Wrap our code with an anonymous function . Hmm Why Anonymous function ? When our javaScript engine execute this function definition , they don’t create new property in our window object., why ? because this function don’t have a name, Wait wait, when there is no function name how are we going to invoke it, hmm, you will get your answer in next step
  47. Wrap our anonymous function with an expression. Why ? Remember we had to invoke this Anonymous function, What we had was function declaration , if we invoke them JS engine will throw you an syntax error, So we have to convert the function Declaration to an Function Expression There are several tricks to achieve that, But we will go with brackets From now on I am going to address each file as Function Expression Now that we understood the purpose of expression, lets utitlize it, Let invoke in next step
  48. There you go, you got the purpose and meaning of each word in IIFE Pattern; Immediately invoked function Expression Ok what does this achieve ? How does this solve our global pollution issue ? When JS Engine execute: invoking Function Expression, your code inside the expression get called So variable in our code will be defined inside the inner scope of this Anonymous function expression. Not at the global scope So what we achieved here is separate inner scope with IIFE pattern; Means Our JavaScript programs are split into separate scope on its own.
  49. This is called module. … When we invoke a function expression we end up creating module. This might make you think, When there are not defined in global Scope, how these Function Expressions are going to communicate ? That’s what we are going to solve next
  50. In our Example , Function Expression 2, calls the function getDate(), which will throw an error . Why ? 1. getDate is not defined in diaplyDateAndTime 2. If we check In its outer scope – which is an anonmynous function, - not defined there either 3. If we check in outer scope of anonmynous function which is the global scope, window object, not defined there too, so its throws an error By the way This is called scope chain . Function with scope chain is called Closure … Now we can say, getDate is called inside the closure of displayDateAndTime.
  51. Now what we see here is a dependency issue, How do we solve this? What we need here is a way to export variable or functions or constant outside a Module And Import in the Consumer when needed. In this Example We need a way to export getDate out of function Expression 1 And import getDate function inside function expression 2. In general What we propose here is , every function Expression should have the ability to export and import let resolve that next.
  52. let pass two arguments to each Function Expression, exports to export its variable Require to import variables
  53. Let make exports to be an object which hold the variables , functions or constant that needs to be exposed Since Export being an Object, inorder to consume this Object, we need a getter. Yes your guess is Right … require will be our getter function, Which will give us the exports object Here we go
  54. Hmmm, What will be the basic requirement for a getter function KEY ---- in our case, since file path are unique , we will make file Path as our key Now Where does this require gets defined ?
  55. Let call that as module Manger, Before Implementing Module Manger, let see the purpose of module manger
  56. Say we have an Object
  57. Whose key is file path
  58. And its value being an Function Expression
  59. Code inside that function expression will be you file
  60. As minified version
  61. Now for file 2
  62. In file 3 , we Have three functions that are exported createButton - which adds button to your page createPara – which add Para Element to your page addListener – which attaches an eventListener for Button Click Event
  63. Now index.js Which exprts three function from file 3 And Invokes them
  64. Our Purpose here is to convert this Object of Function Expressions into an Object of Exports
  65. Now Implementation Lets start by creating an Anonymous Function Why ? We don’t want to be part of global Scope This Function takes in Function Expression Object
  66. And now we need ModuleExports Object to stored the exports returned by invoking respective Func Exp
  67. Now we get to the meat, Definition of require
  68. First step let check in module exports Object, If it not there
  69. Create an empty Object named exports Now we need to populate the exports oBject for that fiepath How?
  70. Get the function Expression for that filePath
  71. And Invoke them This will populate the Exports Object
  72. Now return that exports Object Now where and How do we invoke this require function, to populate ModuleExports Object
  73. We need to call inside our Module Manger We need a FilePath that needs to be the Starting point of our function Expressions Object
  74. Lets name that as entry Now we need to Invoke this module Manager How ? …. Yes your guess is right
  75. Wrap them with Expression
  76. And We need to invoke them with our Function-Expression Object
  77. And next we need an entry point
  78. In our case, index.js file Let's do a code walk through to understand better
  79. In our case, index.js file Let's do a code walk through to understand better
  80. In our case, index.js file Let's do a code walk through to understand better
  81. In our case, index.js file Let's do a code walk through to understand better
  82. var moduleManger = (function (funcExpressions, entry) { var moduleExports = {}; function require(id) { var exports = moduleExports[id]; if(exports){ return exports; } exports = moduleExports[id] = {}; var funcExp = funcExpressions[id]; funcExp(exports, require); return exports; } require(entry); }) var fileAsFunExpressions = { "./file1.js": (function(exports, require){ function f(){ return Date(); }; exports['getDate'] = f; }), "./file2.js": (function(exports, require){ var g = require("./file1.js").getDate; function d(p){ p.innerHTML = g() }; exports['displayDateTime'] = d; }), "./file3.js": (function(exports, require){ var d = require("./file2.js").displayDateTime; var b = document.body; var bt, t, p; function cb(){ bt = document.createElement("button"); t = document.createTextNode("Display Time"); bt.appendChild(t); b.appendChild(bt); }; function cp(){ p = document.createElement("p"); b.appendChild(p); }; function a(){ bt.addEventListener("click", ()=>d(p)); }; exports['createButton'] = cb; exports['createPara'] = cp; exports['addListener'] = a; }), "./index.js": (function(exports, require){ var me = require("./file3.js"); me.createButton(); me.createPara(); me.addListener(); }) } moduleManger(fileAsFunExpressions, './index.js')