SlideShare a Scribd company logo
Introduction to Webpack
Anjali Chawla
Software Dev, Expedia
What is webpack?
• Webpack is a module bundler. It takes modules with dependencies
and generates static assets representing those modules.
• You can use webpack for both your client side JS files as well as your
server side node JS files.
Image source: https://webpack.github.io/docs
What does webpack do
• Manage dependencies
• require
• import (e.g. CommonJS, AMD, ESM, etc.)
• Build tasks - convert and preprocess
• Minify
• Combine
• Sass / less conversion
• Babel transpile
• It combines the build system and module bundling, i.e. instead of
building sass and optimizing images in one part of project, and then
combining the script files in another, it combines everything in the
module itself
What does webpack do
• Wraps everything that is a part of the module in the module itself, and creates a bundle of it.
app.js
Webpack.config.js
Import stylesheets from ..
Import logo from ..
Import jquery from $
…
Reads entry point and
load the dependencies
./src/app.js
node_modules Project modules
1
./bin/app.bundle.js
Bundles module
into a single file
2
Pain points
Pain points
• Module management
• Managing common assets
• Replacement of resources in case of any change
• Module Management
• Bundling all in one
• Managing dependency tree
• It’s the idea that you define, “split points”: areas of your code that
could be easily split off into a separate file and then load on-demand.
Solution : Code Splitting
// This is a split point
require.ensure([], () => {
// All the code in here, and everything that is imported will be in a separate file
const library = require('some-big-library');
$('foo').click(() => library.doSomething());
}, <‘name of the chunk’>);
• A split point creates a chunk for the dependencies, that are checked for
repeatition and split accordingly.
• A chunk is loaded at runtime as jsonp and is loaded only once.
• Managing Common Assets
Solution : Plugins - CommonsChunk Plugin
• This will remove all modules in the vendor chunk from the app chunk.
The bundle.js will now contain just your app code, without any of its
dependencies. All dependencies are in vendor.bundle.js.
var webpack = require("webpack");
module.exports = { entry: {
app: "./app.js",
vendor: ["jquery", "underscore", ...], },
output: { filename: "bundle.js" },
plugins: [
new webpack.optimize.CommonsChunkPlugin(/*chunkName=*/”vendor", /* filename=*/ "vendor.bundle.js”
) ] };
• Reloading entire page on every change
Solution : Hot Module Replacement
• HMR is a way of exchanging modules in a running application (and
adding/removing modules). Updating changed modules without a full page
reload.
• app code asks HMR runtime to check for updates in the manifest files created by
webpack compiler.
• If an update exists, it is applied and only that part of the application is reloaded /
updated.
• Every module has to enable HMR, explicitly
var requestHandler = require("./handler.js"); var server = require("http").createServer();
server.on("request", requestHandler); server.listen(8080);
if(module.hot){ // check if HMR is enabled
module.hot.accept("./handler.js", function() {{ // accept update of dependency
server.removeListener("request", requestHandler); // replace request handler of server
requestHandler = require("./handler.js");
server.on("request", requestHandler); });
}
Additional Benefits
• Integrating webpack with gulp / grunt
var gulp = require('gulp');
var webpack = require('webpack-stream');
gulp.task('default', function(callback) {
return gulp.src('src/entry.js')
.pipe(webpack())
.pipe(gulp.dest('dist/'));
});
module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-webpack");
grunt.initConfig({
webpack: {
options: { // configuration for all builds },
build: { // configuration for this build }
},
};
• Profiling
• webpack --profile --json > stats.json
• http://webpack.github.io/analyse/
• Sample application: https://bitbucket.org/anjali_chawla/webpack_intro/src
• Documentation: http://webpack.github.io/docs/
• Plugins: http://webpack.github.io/docs/list-of-plugins.html
• Loaders: http://webpack.github.io/docs/list-of-loaders.htm
References

More Related Content

PPTX
Webpack Introduction
PDF
Bundle your modules with Webpack
PPTX
Improving build solutions dependency management with webpack
PPTX
An Intro into webpack
PPTX
Packing for the Web with Webpack
PDF
Webpack DevTalk
PDF
Front-end build tools - Webpack
PDF
Webpack
Webpack Introduction
Bundle your modules with Webpack
Improving build solutions dependency management with webpack
An Intro into webpack
Packing for the Web with Webpack
Webpack DevTalk
Front-end build tools - Webpack
Webpack

What's hot (20)

PDF
Webpack Tutorial, Uppsala JS
PDF
Packing it all: JavaScript module bundling from 2000 to now
PDF
Webpack: from 0 to 2
PDF
webpack 101 slides
PPTX
PPTX
Bundling your front-end with Webpack
PDF
Production optimization with React and Webpack
PPTX
Javascript Bundling and modularization
PPTX
Webpack and Web Performance Optimization
PPTX
Lecture: Webpack 4
PDF
Grunt.js and Yeoman, Continous Integration
PPTX
002. Working with Webpack
PDF
Nuxt.js - Introduction
PPTX
006. React - Redux framework
PPTX
005. a React project structure
PPTX
PPTX
Backbone.js
PDF
Vue 淺談前端建置工具
PPTX
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
Webpack Tutorial, Uppsala JS
Packing it all: JavaScript module bundling from 2000 to now
Webpack: from 0 to 2
webpack 101 slides
Bundling your front-end with Webpack
Production optimization with React and Webpack
Javascript Bundling and modularization
Webpack and Web Performance Optimization
Lecture: Webpack 4
Grunt.js and Yeoman, Continous Integration
002. Working with Webpack
Nuxt.js - Introduction
006. React - Redux framework
005. a React project structure
Backbone.js
Vue 淺談前端建置工具
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
Ad

Similar to Webpack (20)

PPTX
webpack introductionNotice Demystifyingf
PPTX
Webpack/Parcel: What’s Happening Behind the React App?
PPT
PPTX
React Basic and Advance || React Basic
PDF
Full Stack React Workshop [CSSC x GDSC]
PDF
Reactjs Basics
PDF
Warsaw Frontend Meetup #1 - Webpack
PPTX
JavaScript Module Loaders
PDF
JavaScript Modules Done Right
PPTX
Reactjs notes.pptx for web development- tutorial and theory
PPTX
Introduction to Webpack 5.0 Presentation
PDF
Create ReactJS Component & publish as npm package
PPTX
Introduction to ReactJS UI Web Dev .pptx
PDF
Building and deploying React applications
PPTX
Introduction to angular | Concepts and Environment setup
PDF
JS Module Server
PPTX
Angular kickstart slideshare
PDF
BMO - Intelligent Projects with Maven
PPTX
MongoDB Days Silicon Valley: Building Applications with the MEAN Stack
PPTX
Introduction to React JS.pptx
webpack introductionNotice Demystifyingf
Webpack/Parcel: What’s Happening Behind the React App?
React Basic and Advance || React Basic
Full Stack React Workshop [CSSC x GDSC]
Reactjs Basics
Warsaw Frontend Meetup #1 - Webpack
JavaScript Module Loaders
JavaScript Modules Done Right
Reactjs notes.pptx for web development- tutorial and theory
Introduction to Webpack 5.0 Presentation
Create ReactJS Component & publish as npm package
Introduction to ReactJS UI Web Dev .pptx
Building and deploying React applications
Introduction to angular | Concepts and Environment setup
JS Module Server
Angular kickstart slideshare
BMO - Intelligent Projects with Maven
MongoDB Days Silicon Valley: Building Applications with the MEAN Stack
Introduction to React JS.pptx
Ad

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Machine learning based COVID-19 study performance prediction
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
NewMind AI Weekly Chronicles - August'25 Week I
20250228 LYD VKU AI Blended-Learning.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.

Webpack

  • 1. Introduction to Webpack Anjali Chawla Software Dev, Expedia
  • 2. What is webpack? • Webpack is a module bundler. It takes modules with dependencies and generates static assets representing those modules. • You can use webpack for both your client side JS files as well as your server side node JS files. Image source: https://webpack.github.io/docs
  • 3. What does webpack do • Manage dependencies • require • import (e.g. CommonJS, AMD, ESM, etc.) • Build tasks - convert and preprocess • Minify • Combine • Sass / less conversion • Babel transpile • It combines the build system and module bundling, i.e. instead of building sass and optimizing images in one part of project, and then combining the script files in another, it combines everything in the module itself
  • 4. What does webpack do • Wraps everything that is a part of the module in the module itself, and creates a bundle of it. app.js
  • 5. Webpack.config.js Import stylesheets from .. Import logo from .. Import jquery from $ … Reads entry point and load the dependencies ./src/app.js node_modules Project modules 1 ./bin/app.bundle.js Bundles module into a single file 2
  • 7. Pain points • Module management • Managing common assets • Replacement of resources in case of any change
  • 8. • Module Management • Bundling all in one • Managing dependency tree
  • 9. • It’s the idea that you define, “split points”: areas of your code that could be easily split off into a separate file and then load on-demand. Solution : Code Splitting // This is a split point require.ensure([], () => { // All the code in here, and everything that is imported will be in a separate file const library = require('some-big-library'); $('foo').click(() => library.doSomething()); }, <‘name of the chunk’>); • A split point creates a chunk for the dependencies, that are checked for repeatition and split accordingly. • A chunk is loaded at runtime as jsonp and is loaded only once.
  • 11. Solution : Plugins - CommonsChunk Plugin • This will remove all modules in the vendor chunk from the app chunk. The bundle.js will now contain just your app code, without any of its dependencies. All dependencies are in vendor.bundle.js. var webpack = require("webpack"); module.exports = { entry: { app: "./app.js", vendor: ["jquery", "underscore", ...], }, output: { filename: "bundle.js" }, plugins: [ new webpack.optimize.CommonsChunkPlugin(/*chunkName=*/”vendor", /* filename=*/ "vendor.bundle.js” ) ] };
  • 12. • Reloading entire page on every change
  • 13. Solution : Hot Module Replacement • HMR is a way of exchanging modules in a running application (and adding/removing modules). Updating changed modules without a full page reload. • app code asks HMR runtime to check for updates in the manifest files created by webpack compiler. • If an update exists, it is applied and only that part of the application is reloaded / updated. • Every module has to enable HMR, explicitly var requestHandler = require("./handler.js"); var server = require("http").createServer(); server.on("request", requestHandler); server.listen(8080); if(module.hot){ // check if HMR is enabled module.hot.accept("./handler.js", function() {{ // accept update of dependency server.removeListener("request", requestHandler); // replace request handler of server requestHandler = require("./handler.js"); server.on("request", requestHandler); }); }
  • 14. Additional Benefits • Integrating webpack with gulp / grunt var gulp = require('gulp'); var webpack = require('webpack-stream'); gulp.task('default', function(callback) { return gulp.src('src/entry.js') .pipe(webpack()) .pipe(gulp.dest('dist/')); }); module.exports = function(grunt) { grunt.loadNpmTasks("grunt-webpack"); grunt.initConfig({ webpack: { options: { // configuration for all builds }, build: { // configuration for this build } }, }; • Profiling • webpack --profile --json > stats.json • http://webpack.github.io/analyse/
  • 15. • Sample application: https://bitbucket.org/anjali_chawla/webpack_intro/src
  • 16. • Documentation: http://webpack.github.io/docs/ • Plugins: http://webpack.github.io/docs/list-of-plugins.html • Loaders: http://webpack.github.io/docs/list-of-loaders.htm References

Editor's Notes

  • #5: Why combine everything in a module: For the purpose of reducing HTTP calls, two types of implementations exists: Combine everything in one single file ( increases the page load time) Create separate modules and load them when needed (maintenance of dependency tree + duplicate loading of common resources) Webpack clubs the two, it splits the code (Code splitting) in such a way that is optimal for the page as well as for the HTTP calls.
  • #6: Why combine everything in a module: For the purpose of reducing HTTP calls, two types of implementations exists: Combine everything in one single file ( increases the page load time) Create separate modules and load them when needed (maintenance of dependency tree + duplicate loading of common resources) Webpack clubs the two, it splits the code (Code splitting) in such a way that is optimal for the page as well as for the HTTP calls.