webpack: what it is, what it does, whether you need it

24

Upload: mike-wilcox

Post on 15-Apr-2017

44 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Webpack: What it is, What it does, Whether you need it
Page 2: Webpack: What it is, What it does, Whether you need it

by Mike Wilcox, March 2017

Webpack what it is what it does whether you need it

Page 3: Webpack: What it is, What it does, Whether you need it
Page 4: Webpack: What it is, What it does, Whether you need it

Webpack Main Features• Module bundler / loader

• AMD, Common JS, ES imports

• Babel & JSX Transformations

• Dev Server • Hot Module Reloading*

• Task Runner • LESS/SCSS, Tree shaking, optimization, code splitting, etc, etc…

• Server Side Rendering

• Large Community*

*Key reasons for using Webpack

Webpack being relatively new allows it to avoid non-standard loading methods such as AMD or Angular’s global injection.

The plugin system, while sometimes confusing, allows Webpack to grow and be maintainable.

Page 5: Webpack: What it is, What it does, Whether you need it

Competitors• RequireJS

• Duo

• SystemJS

• Ember

• StealJS

• Browserify + Grunt (or Gulp)

Page 6: Webpack: What it is, What it does, Whether you need it

Competitors• RequireJS

• Duo

• SystemJS

• Ember

• StealJS

• Browserify + Grunt (or Gulp)

StealJS is almost on parity with Webpack and definitely worth consideration.

Browserify + Grunt will get you 95% of the functionality of Webpack - Unfortunately, that 5% is Hot Module Loading

Page 7: Webpack: What it is, What it does, Whether you need it
Page 8: Webpack: What it is, What it does, Whether you need it

Do You Need It?Pros

It does a lot

ConsIt does too much

Spoiler alert: yes, you need it

Page 9: Webpack: What it is, What it does, Whether you need it

Webpack 2.0 Features

• Native ES6 import, export and dynamic import()

• Tree Shaking for ES6

• Uses Promise for module loading • Need polyfill in old browsers (if you’re using code splitting)

• Chunk error handling

• Otherwise, 2.0 is a refactor

Just released 2.0 late in 2016:

Page 10: Webpack: What it is, What it does, Whether you need it

Module Bundlers• dojo.provide dojo.require

• RequireJS

• CommonJS / Browserify

• SystemJS

• StealJS

• babel-loader: ES import/export

Page 11: Webpack: What it is, What it does, Whether you need it

Task Runners• Grunt

• Gulp

• Broccoli

• NPM Scripts

Page 12: Webpack: What it is, What it does, Whether you need it

More Features (we will not cover)• Tree shaking

• optimization

• inlining

• code splitting / chunks

• lazy loading / code on demand

• targeted (multi version) builds

• server side rendering

• long term caching

• offline - service workers List of Plugins

Page 13: Webpack: What it is, What it does, Whether you need it

PAIN POINTS

Page 14: Webpack: What it is, What it does, Whether you need it

Grunt CSSWhat the config looks like in Grunt:

less: { main: { options: { sourceMap: true, }, files: { 'dist/main.css': 'less/main.less' } } }

Page 15: Webpack: What it is, What it does, Whether you need it

Webpack CSS

const cssPlugin = new ExtractTextPlugin({ filename: 'main.css', });

module: { rules: [ { test: /\.less$/, use: cssPlugin.extract({ use: ['css-loader?sourceMap', 'less-loader?sourceMap'], fallback: 'style-loader' }) } ] }, devtool: ‘eval-source-map‘

What the config looks like in Webpack:

Page 16: Webpack: What it is, What it does, Whether you need it
Page 17: Webpack: What it is, What it does, Whether you need it

Moar Pain Points• Does too many things - essentially violates the single responsibility rule

• CSS HMR setup is ridiculously obtuse

• Documentation • A complete lack of examples (what is a code snippet worth?) • Even the how-to's are lacking • 10 ways to do everything.... sometimes all 10 are wrong • Does not allow custom command line arguments • Wants to own the HTML page • Creates horribly obfuscated code

These are largely observational opinions, not facts

Page 18: Webpack: What it is, What it does, Whether you need it
Page 19: Webpack: What it is, What it does, Whether you need it

Even MOAR Pain Points• Loads files with ES imports... unless those files are dependencies

• Lack of feedback or errors when things go wrong • If something doesn’t work, it is a HARD STOP • Webpack is (near) impossible to debug

• webpack-dev-server is in-memory, which, while great for performance… • Horrible for debugging server issues • bower becomes worthless • The dev server does not have a relative path to... anything. • Polyfills become painful • Simple static assets are painful

Okay, these are more fact-based.

Page 20: Webpack: What it is, What it does, Whether you need it
Page 21: Webpack: What it is, What it does, Whether you need it

Benefits• Hot Module Reloading

• Hard to do in any transpiled code

• ES Imports • Not exclusive, but not trivial

• Code splitting • Vendor code splitting is easy - all else, not as easy, but very doable

• “Everything” is supported • If it exists in the world of front end development, you can do it with Weback

Page 22: Webpack: What it is, What it does, Whether you need it

Webpack Resources• Create React App

• Automates Webpack config. Great for beginners. Uses an old version and lacks a few critical features.

• Survive JS • “The Book” on webpack. Literally the missing documentation.

• HJS Webpack • A config abstraction that is supposed to make setup easier

Page 23: Webpack: What it is, What it does, Whether you need it

DEMO

https://github.com/clubajax/react-scaffold

Page 24: Webpack: What it is, What it does, Whether you need it