SlideShare una empresa de Scribd logo
1 de 48
Packing for the Web with Webpack
THIAGO TEMPLE
A few months ago…
Initial state
• Plain JavaScript
• Global functions and variables
• Unorganized code (no structure)
• Repeated code
• No tests
• Server and client code together
• Different libraries
What we wanted
• TypeScript
• Modules
• Structure
• Tests
Modules
Loading
Module loaders
SystemJS
Bundling
Module bundlers
SystemJS
Task runners
=
+
+
One tool
To rule them all
How does it work?
App.js
MoviesService.js
AnotherModule.js
MoviesCache.js
UsersService.js Lodash.js
Configuration
module.exports = {
entry: './src/app.js',
output: {
path: './dist',
filename: 'bundle.js'
}
}
Module resolution
• CommonJS
• AMD (RequireJS)
• ES6 (version 2)
Migration
Multiple entry points
module.exports = {
entry: {
page1: './src/app.js',
page2: './src/movies.js',
page3: './src/admin.js',
},
output: {
path: './dist',
filename: '[name].js'
}
}
Loaders
A loader
module.exports = {
…
module: {
loaders:[
{
test: /.jsx?$/,
include: path.resolve(__dirname, 'src'),
loader: 'babel',
}
]
}
TypeScript loader
module.exports = {
…
module: {
loaders:[
{
test: /.tsx?$/,
include: path.resolve(__dirname, 'src'),
loader: 'ts',
}
]
}
Multiple loaders
module.exports = {
…
module: {
loaders:[
{
test: /.tsx?$/,
include: path.resolve(__dirname, 'src'),
loaders: ['babel', 'ts'],
}
]
}
Not only JavaScript, css…
module.exports = {
…
module: {
loaders:[
{
test: /.css?$/,
include: path.resolve(__dirname, 'src'),
loaders: ['style', 'css'],
}
]
}
Sass
module.exports = {
…
module: {
loaders:[
{
test: /.scss?$/,
include: path.resolve(__dirname, 'src'),
loaders: ['style', 'css', 'sass'],
}
]
}
Importing CSS
/// a file.js
require('./mymodule.css');
import './mymodule.css';
CSS with images
module.exports = {
…
module: {
loaders:[
{
test: /.(gif|jpg|png)?$/,
include: path.resolve(__dirname, 'src'),
loader: 'url?limit=10000?name=images/[name].[ext]',
}
]
}
CSS modules
module.exports = {
…
module: {
loaders:[
{
test: /.css?$/,
include: path.resolve(__dirname, 'src'),
loader: 'style!css?modules&camelCase'
}
]
}
CSS modules
var styles = require('./app.css');
var el = document.createElement('div');
el.innerText = 'Hello opencode';
el.className = styles.testClass;
document.body.appendChild(el);
CSS modules
.test-class {
width: 200px;
height: 200px;
background-color: purple;
font-size: 1.5em;
}
CSS modules
Create your own
module.exports = function (source) {
return source;
};
Developing tools
Dev server
• Easy to configure
• In memory
• Hot reloading
Hot reloading
React hot reloader loader
Source maps
module.exports = {
entry: './src/app.js',
devtool: 'eval-source-map',
output: {
path: './dist',
filename: 'bundle.js'
}
}
Linting
module.exports = {
…
module: {
preloaders:[
{
test: /.tsx?$/,
loader: 'tslint',
}
],
tslint: {
emitErrors: true,
failOnHint: true
}
}
Plugins
Extract CSS
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
…
module: {
loaders:[
{
test: /.css$/,
loader: ExtractTextPlugin.extract('style', 'css'),
}
],
plugins:[
new ExtractTextPlugin('bundle.css')
]
Long-term caching
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
…
output: {
path: '…',
filename: 'bundle.[hash].js'
},
plugins:[
new ManifestPlugin(),
]
Long-term caching
{
"bundle.js": "bundle.2f0ffb545e5607fc8b8d.js"
}
Long-term caching
• Generate html from server
• Generate html using Webpack
• HtmlWebpackPlugin
Common chunk plugin
module.exports = {
entry: {
page1: './src/app.js',
page2: './src/movies.js',
page3: './src/admin.js',
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({ name: 'shared' })
]
}
Common chunk plugin - vendors
module.exports = {
entry: {
app: './src/app.js',
vendor: ['jquery', 'react', 'lodash']
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor' })
]
}
Some other plugins
• Provide
• I18n
Some other considerations
• Webpack –p
• Webpack validator
• Attention to the object exported
Conclusion
• One tool for bundling and building
• Loaders for individual files
• Plugins for the complete bundle
Merci
@vintem12
Thiago Temple
https://templecoding.com

Más contenido relacionado

La actualidad más candente

Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS
偉格 高
 

La actualidad más candente (20)

Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
 
Lecture: Webpack 4
Lecture: Webpack 4Lecture: Webpack 4
Lecture: Webpack 4
 
Webpack: your final module bundler
Webpack: your final module bundlerWebpack: your final module bundler
Webpack: your final module bundler
 
Webpack Tutorial, Uppsala JS
Webpack Tutorial, Uppsala JSWebpack Tutorial, Uppsala JS
Webpack Tutorial, Uppsala JS
 
Production optimization with React and Webpack
Production optimization with React and WebpackProduction optimization with React and Webpack
Production optimization with React and Webpack
 
Webpack DevTalk
Webpack DevTalkWebpack DevTalk
Webpack DevTalk
 
Javascript Bundling and modularization
Javascript Bundling and modularizationJavascript Bundling and modularization
Javascript Bundling and modularization
 
Webpack: from 0 to 2
Webpack: from 0 to 2Webpack: from 0 to 2
Webpack: from 0 to 2
 
webpack 101 slides
webpack 101 slideswebpack 101 slides
webpack 101 slides
 
Webpack and Web Performance Optimization
Webpack and Web Performance OptimizationWebpack and Web Performance Optimization
Webpack and Web Performance Optimization
 
DotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + reactDotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + react
 
An Overview on Nuxt.js
An Overview on Nuxt.jsAn Overview on Nuxt.js
An Overview on Nuxt.js
 
Hey webpack
Hey webpackHey webpack
Hey webpack
 
Grunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous IntegrationGrunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous Integration
 
One step in the future: CSS variables
One step in the future: CSS variablesOne step in the future: CSS variables
One step in the future: CSS variables
 
Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS
 
Nuxt.js - Introduction
Nuxt.js - IntroductionNuxt.js - Introduction
Nuxt.js - Introduction
 
Nuxt.JS Introdruction
Nuxt.JS IntrodructionNuxt.JS Introdruction
Nuxt.JS Introdruction
 
Nuxt Talk
Nuxt TalkNuxt Talk
Nuxt Talk
 
Front-end build tools - Webpack
Front-end build tools - WebpackFront-end build tools - Webpack
Front-end build tools - Webpack
 

Destacado

Destacado (17)

webpack 入門
webpack 入門webpack 入門
webpack 入門
 
Webpack
WebpackWebpack
Webpack
 
Advanced webpack
Advanced webpackAdvanced webpack
Advanced webpack
 
GraphQL With Relay Part Deux
GraphQL With Relay Part DeuxGraphQL With Relay Part Deux
GraphQL With Relay Part Deux
 
React redux
React reduxReact redux
React redux
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for Beginners
 
From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)
 
Warsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - WebpackWarsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - Webpack
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Ramda, a functional JavaScript library
Ramda, a functional JavaScript libraryRamda, a functional JavaScript library
Ramda, a functional JavaScript library
 
React & Redux
React & ReduxReact & Redux
React & Redux
 
"Webpack: 7 бед — один ответ" — Денис Измайлов, MoscowJS 17
"Webpack: 7 бед — один ответ" — Денис Измайлов, MoscowJS 17"Webpack: 7 бед — один ответ" — Денис Измайлов, MoscowJS 17
"Webpack: 7 бед — один ответ" — Денис Измайлов, MoscowJS 17
 
Webpack integration
Webpack integrationWebpack integration
Webpack integration
 
Как Webpack сделал меня счастливее
Как Webpack сделал меня счастливееКак Webpack сделал меня счастливее
Как Webpack сделал меня счастливее
 
Webpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ StepsWebpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ Steps
 
Webpack - Czym jest webpack i dlaczego chcesz go używać? - wersja krótka
Webpack - Czym jest webpack i dlaczego chcesz go używać? - wersja krótkaWebpack - Czym jest webpack i dlaczego chcesz go używać? - wersja krótka
Webpack - Czym jest webpack i dlaczego chcesz go używać? - wersja krótka
 
Современный фронтенд -- как не утонуть в море хайпа?
Современный фронтенд -- как не утонуть в море хайпа?Современный фронтенд -- как не утонуть в море хайпа?
Современный фронтенд -- как не утонуть в море хайпа?
 

Similar a Packing for the Web with Webpack

Similar a Packing for the Web with Webpack (20)

uRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orguRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.org
 
JS & NodeJS - An Introduction
JS & NodeJS - An IntroductionJS & NodeJS - An Introduction
JS & NodeJS - An Introduction
 
RequireJS
RequireJSRequireJS
RequireJS
 
JavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and FutureJavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and Future
 
Webpack Encore - Asset Management for the rest of us
Webpack Encore - Asset Management for the rest of usWebpack Encore - Asset Management for the rest of us
Webpack Encore - Asset Management for the rest of us
 
The MEAN stack
The MEAN stack The MEAN stack
The MEAN stack
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applications
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java Developers
 
Ext JS Introduction
Ext JS IntroductionExt JS Introduction
Ext JS Introduction
 
Vue js and Dyploma
Vue js and DyplomaVue js and Dyploma
Vue js and Dyploma
 
IOC + Javascript
IOC + JavascriptIOC + Javascript
IOC + Javascript
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done Right
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Node JS Core Module PowerPoint Presentation
Node JS Core Module PowerPoint PresentationNode JS Core Module PowerPoint Presentation
Node JS Core Module PowerPoint Presentation
 
Heroku pop-behind-the-sense
Heroku pop-behind-the-senseHeroku pop-behind-the-sense
Heroku pop-behind-the-sense
 
Gradle
GradleGradle
Gradle
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Packing for the Web with Webpack

Notas del editor

  1. Good with long term caching