packing for the web with webpack

48
Packing for the Web with Webpack THIAGO TEMPLE

Upload: thiago-temple

Post on 15-Apr-2017

348 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Packing for the Web with Webpack

Packing for the Web with WebpackTHIAGO TEMPLE

Page 2: Packing for the Web with Webpack

A few months ago…

Page 3: Packing for the Web with Webpack

Initial state• Plain JavaScript

• Global functions and variables

• Unorganized code (no structure)

• Repeated code

• No tests

• Server and client code together

• Different libraries

Page 4: Packing for the Web with Webpack

What we wanted• TypeScript

• Modules

• Structure

• Tests

Page 5: Packing for the Web with Webpack

Modules

Page 6: Packing for the Web with Webpack

Loading

Page 7: Packing for the Web with Webpack

Module loaders

SystemJS

Page 8: Packing for the Web with Webpack

Bundling

Page 9: Packing for the Web with Webpack

Module bundlers

SystemJS

Page 10: Packing for the Web with Webpack

Task runners

Page 11: Packing for the Web with Webpack

=+

+

Page 12: Packing for the Web with Webpack

One tool

Page 13: Packing for the Web with Webpack

To rule them all

Page 14: Packing for the Web with Webpack

How does it work?

App.js

MoviesService.js

AnotherModule.js

MoviesCache.js

UsersService.js Lodash.js

Page 15: Packing for the Web with Webpack

Configuration

module.exports = { entry: './src/app.js', output: { path: './dist', filename: 'bundle.js' }}

Page 16: Packing for the Web with Webpack

Module resolution• CommonJS

• AMD (RequireJS)

• ES6 (version 2)

Page 17: Packing for the Web with Webpack

Migration

Page 18: Packing for the Web with Webpack

Multiple entry points

module.exports = { entry: { page1: './src/app.js', page2: './src/movies.js', page3: './src/admin.js', }, output: { path: './dist', filename: '[name].js' }}

Page 19: Packing for the Web with Webpack

Loaders

Page 20: Packing for the Web with Webpack

A loader

module.exports = { … module: { loaders:[ { test: /\.jsx?$/, include: path.resolve(__dirname, 'src'), loader: 'babel', } ]}

Page 21: Packing for the Web with Webpack

TypeScript loader

module.exports = { … module: { loaders:[ { test: /\.tsx?$/, include: path.resolve(__dirname, 'src'), loader: 'ts', } ]}

Page 22: Packing for the Web with Webpack

Multiple loaders

module.exports = { … module: { loaders:[ { test: /\.tsx?$/, include: path.resolve(__dirname, 'src'), loaders: ['babel', 'ts'], } ]}

Page 23: Packing for the Web with Webpack

Not only JavaScript, css…

module.exports = { … module: { loaders:[ { test: /\.css?$/, include: path.resolve(__dirname, 'src'), loaders: ['style', 'css'], } ]}

Page 24: Packing for the Web with Webpack

Sass

module.exports = { … module: { loaders:[ { test: /\.scss?$/, include: path.resolve(__dirname, 'src'), loaders: ['style', 'css', 'sass'], } ]}

Page 25: Packing for the Web with Webpack

Importing CSS

/// a file.js

require('./mymodule.css');

import './mymodule.css';

Page 26: Packing for the Web with Webpack

CSS with images

module.exports = { … module: { loaders:[ { test: /\.(gif|jpg|png)?$/, include: path.resolve(__dirname, 'src'), loader: 'url?limit=10000?name=images/[name].[ext]', } ]}

Page 27: Packing for the Web with Webpack

CSS modules

module.exports = { … module: { loaders:[ { test: /\.css?$/, include: path.resolve(__dirname, 'src'), loader: 'style!css?modules&camelCase' } ]}

Page 28: Packing for the Web with Webpack

CSS modules

var styles = require('./app.css');

var el = document.createElement('div');el.innerText = 'Hello opencode';el.className = styles.testClass;

document.body.appendChild(el);

Page 29: Packing for the Web with Webpack

CSS modules

.test-class { width: 200px; height: 200px; background-color: purple; font-size: 1.5em;}

Page 30: Packing for the Web with Webpack

CSS modules

Page 31: Packing for the Web with Webpack

Create your own

module.exports = function (source) { return source;};

Page 32: Packing for the Web with Webpack

Developing tools

Page 33: Packing for the Web with Webpack

Dev server• Easy to configure

• In memory

• Hot reloading

Page 34: Packing for the Web with Webpack

Hot reloading

Page 35: Packing for the Web with Webpack

React hot reloader loader

Page 36: Packing for the Web with Webpack

Source maps

module.exports = { entry: './src/app.js', devtool: 'eval-source-map', output: { path: './dist', filename: 'bundle.js' }}

Page 37: Packing for the Web with Webpack

Linting

module.exports = { … module: { preloaders:[ { test: /\.tsx?$/, loader: 'tslint', } ], tslint: { emitErrors: true, failOnHint: true }}

Page 38: Packing for the Web with Webpack

Plugins

Page 39: Packing for the Web with Webpack

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') ]

Page 40: Packing for the Web with Webpack

Long-term caching

var ExtractTextPlugin = require('extract-text-webpack-plugin');module.exports = { … output: { path: '…', filename: 'bundle.[hash].js' }, plugins:[ new ManifestPlugin(), ]

Page 41: Packing for the Web with Webpack

Long-term caching

{ "bundle.js": "bundle.2f0ffb545e5607fc8b8d.js"}

Page 42: Packing for the Web with Webpack

Long-term caching• Generate html from server

• Generate html using Webpack• HtmlWebpackPlugin

Page 43: Packing for the Web with Webpack

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' }) ]}

Page 44: Packing for the Web with Webpack

Common chunk plugin - vendors

module.exports = { entry: { app: './src/app.js', vendor: ['jquery', 'react', 'lodash'] }, plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: 'vendor' }) ]}

Page 45: Packing for the Web with Webpack

Some other plugins• Provide

• I18n

Page 46: Packing for the Web with Webpack

Some other considerations• Webpack –p

• Webpack validator

• Attention to the object exported

Page 47: Packing for the Web with Webpack

Conclusion• One tool for bundling and building

• Loaders for individual files

• Plugins for the complete bundle

Page 48: Packing for the Web with Webpack

Merci

@vintem12

Thiago Temple

https://templecoding.com