Top Banner
WHAT IS WEBPACK and how does it work? A TUTORIAL
30

Webpack Tutorial, Uppsala JS

Jan 10, 2017

Download

Technology

Emil Öberg
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Webpack Tutorial, Uppsala JS

WHAT IS WEBPACK

and how does it work?

A TUTORIAL

Page 2: Webpack Tutorial, Uppsala JS

github.com/emiloberg

twitter.com/emiloberg

monator.com

HELLO I'M EMIL ÖBERG

Page 3: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com

Page 4: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com/2016/features/

Highest rated features

Page 5: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com/2016/features/

Highest rated features

Page 6: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com/2016/buildtools/

Page 7: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com/2016/buildtools/

Page 8: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com/2016/buildtools/

Page 9: Webpack Tutorial, Uppsala JS

The usual suspects

Page 10: Webpack Tutorial, Uppsala JS

React

.js

.js

.json

.scss .svg.png

.coffeeReactReactReact

ReactReactReactAngular

ReactReactReactmomentjs

.css

.jsMODULES

WITH DEPENDENCIES

Page 11: Webpack Tutorial, Uppsala JS

bundle.js

kitten.png

bundle.css

STATIC ASSETS

Page 12: Webpack Tutorial, Uppsala JS

.js .jsx .css .less .png .svg .json .csv

Everything can be bundled

Page 13: Webpack Tutorial, Uppsala JS

var myCommonJSModule = require('my-module'); import myES2015Module from 'my-module';define(['my-module'], function (myAMDModule) {});

NARROW OR BROAD

DEFINITIONS OF

BUNDLER

or

ABOVE + JPG + CSS + PNG + JSON +

SVG + WHATEVER

Page 14: Webpack Tutorial, Uppsala JS

The usual bundler suspects

Page 15: Webpack Tutorial, Uppsala JS

d/l tech +0.4m/m new, es6 small & fast,

tree shaking

2.7m/m older(-ish) Webpack 2

proven, tree shaking code splitting all modules watch mode multi bundle

Page 16: Webpack Tutorial, Uppsala JS

Browserify code commits

Page 17: Webpack Tutorial, Uppsala JS

Webpack code commits

Page 18: Webpack Tutorial, Uppsala JS

Rollup code commits

Page 19: Webpack Tutorial, Uppsala JS

BUNDLER PLUGINS LOADERS

OPTIMIZERCODE SPLITTER

DEV TOOL

WEBPACK

Page 20: Webpack Tutorial, Uppsala JS

CODE TIME!

Page 21: Webpack Tutorial, Uppsala JS

Vs.LIVE/HOT RELOAD HOT MODULE

REPLACEMENT (HMR)

1. rebuild bundle.js 2. refresh browser

or 2. refresh only

bundle.js in browser

1. rebuild chunk 2. replace chunk

in browser

Page 22: Webpack Tutorial, Uppsala JS

webpack.github.io

Page 23: Webpack Tutorial, Uppsala JS

webpack.github.io/docs/list-of-plugins.html

Page 24: Webpack Tutorial, Uppsala JS

webpack.github.io/docs/list-of-loaders.html

Page 25: Webpack Tutorial, Uppsala JS

var autoprefixer = require('autoprefixer');module.exports = { module: { loaders: [{ test: /\.css$/, loader: 'css-loader!postcss-loader' }] }, postcss: [ autoprefixer() ]};

POSTCSS + AUTOPREFIXERnpm install postcss-loader autoprefixer !--save-dev

Page 26: Webpack Tutorial, Uppsala JS

.example { display: flex; transition: all .5s; user-select: none; background: linear-gradient(to bottom, white, black);}

.example { display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-transition: all .5s; transition: all .5s; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background: -webkit-linear-gradient(top, white, black); background: linear-gradient(to bottom, white, black);}

▲ ▼becomes

POSTCSS + AUTOPREFIXER

Page 27: Webpack Tutorial, Uppsala JS

http:!//stateofjs.com/2016/buildtools/

How happy are you with your current build tools?

Unhappy Happy

Page 28: Webpack Tutorial, Uppsala JS

State of Javascript

Webpack

Webpack dev server

Plugins

Loaders

http://stateofjs.comhttp://webpack.github.iohttp://webpack.github.io/docs/list-of-plugins.htmlhttp://webpack.github.io/docs/list-of-loaders.html

https://webpack.github.io/docs/webpack-dev-server.html

Extract TextHTML WebpackUglifyJS

Define

babel-loaderurl-loaderfile-loaderstyle-loadercss-loader

https://github.com/babel/babel-loaderhttps://github.com/webpack/url-loaderhttps://github.com/webpack/file-loaderhttps://github.com/webpack/style-loaderhttps://github.com/webpack/css-loader

https://github.com/webpack/extract-text-webpack-pluginhttps://github.com/ampedandwired/html-webpack-pluginhttps://webpack.github.io/docs/list-of-plugins.htmlhttps://github.com/mishoo/UglifyJShttps://webpack.github.io/docs/list-of-plugins.html

STUFF USED TODAY

Page 29: Webpack Tutorial, Uppsala JS

Difference Webpack and Webpack 2https://gist.github.com/sokra/27b24881210b56bbaff7

Code Splitting and Reacthttps://medium.com/modus-create-front-end-development/automatic-code-splitting-for-react-router-w-es6-imports-a0abdaa491e9

Full tutorial on HMRhttp://andrewhfarmer.com/webpack-hmr-tutorial/

More about MHRhttps://medium.com/@rajaraodv/webpack-hot-module-replacement-hmr-e756a726a07#.m17ffe4km

Webpack Video Course (pay)https://egghead.io/courses/using-webpack-for-production-javascript-applications

MORE THINGS TO READ

Page 30: Webpack Tutorial, Uppsala JS

github.com/emiloberg/webpack-tutorial

twitter.com/emiloberg