Slide 1

Slide 1 text

Rails with Webpack Sebastian Porto 1 / 31

Slide 2

Slide 2 text

The Assests Pipeline It is great for most Rails apps 2 / 31

Slide 3

Slide 3 text

The Assests Pipeline But not great for JS heavy apps 3 / 31

Slide 4

Slide 4 text

JS heavy applications Lots of JavaScript Multiple entry points Only include code we need on each point Complex dependecy tree 4 / 31

Slide 5

Slide 5 text

Dependency tree 5 / 31

Slide 6

Slide 6 text

Resolving a dependency complex tree Possible with the Assets Pipeline but ugly 6 / 31

Slide 7

Slide 7 text

Dependency tree: Sprockets In Sprockets manifest: / / = r e q u i r e o r d e r s / / = r e q u i r e u s e r s 7 / 31

Slide 8

Slide 8 text

Dependency tree: Sprockets The in JS: Globals / / = r e q u i r e l o a d e r A p p . O r d e r s = f u n c t i o n ( ) { . . . } Or AMD / / = r e q u i r e l o a d e r r e q u i r e ( [ ' l o a d e r ' ] , f u n c t i o n ( l o a d e r ) { . . . } ) 8 / 31

Slide 9

Slide 9 text

Dependency tree: Sprockets / / = r e q u i r e l o a d e r r e q u i r e ( [ ' l o a d e r ' ] , f u n c t i o n ( l o a d e r ) { . . . } ) This is redundant coding == Hard to mantain 9 / 31

Slide 10

Slide 10 text

Wouldn’t it be nice to avoid this repetition? 10 / 31

Slide 11

Slide 11 text

What is Webpack? JavaScript module bundler 11 / 31

Slide 12

Slide 12 text

Webpack Bundle AMD, CommonJS, ES6 modules Resolves dependecies Create multiple bundles Loads about anything (CSS, images, fonts, …) 12 / 31

Slide 13

Slide 13 text

Webpack with Rails 13 / 31

Slide 14

Slide 14 text

Install Webpack with NPM n p m i n s t a l l w e b p a c k - g 14 / 31

Slide 15

Slide 15 text

Config file Specifies: entry points and output files loaders to use (e.g SASS, JSX, etc) 15 / 31

Slide 16

Slide 16 text

Write your code Using AMD, CommonJS or ES6 v a r O r d e r s = r e q u i r e ( ' o r d e r s ' ) O r d e r s . d o S o m e t h i n g ( . . . ) 16 / 31

Slide 17

Slide 17 text

Watch w e b p a c k - - w a t c h 17 / 31

Slide 18

Slide 18 text

Add the bundles to the Rails assets 18 / 31

Slide 19

Slide 19 text

Require the bundles from Sprockets In orders.js / / = r e q u i r e c o m m o n - b u n d l e / / = r e q u i r e o r d e r s - b u n d l e 19 / 31

Slide 20

Slide 20 text

The Assets pipeline will fingerprint the JS in production But we get the benefits of using Webpack: Modular code Multiple bundles 20 / 31

Slide 21

Slide 21 text

Other benefits 21 / 31

Slide 22

Slide 22 text

Code is portable e.g. if we ever need to move our front-end to something else than Rails 22 / 31

Slide 23

Slide 23 text

Modularise all the things JS Images CSS / LESS / SASS Fonts 23 / 31

Slide 24

Slide 24 text

Better encapsulation of JS No Globals 24 / 31

Slide 25

Slide 25 text

Use NPM to manage FE dependencies With package.json If that appeals to you 25 / 31

Slide 26

Slide 26 text

Leverage the NPM echo system Easily load and use packages from there 26 / 31

Slide 27

Slide 27 text

Testing is way better With Rails you need to load the whole Env Gems like Konacha and Teaspoon help Slow 27 / 31

Slide 28

Slide 28 text

Testing is way better With Webpack you can use anything you want Jest, Buster, Karma, … Faster 28 / 31

Slide 29

Slide 29 text

Browserify? Mostly does the same Webpack = batteries included Browserify = Everything in plugins! 29 / 31

Slide 30

Slide 30 text

Thanks @sebasporto 30 / 31

Slide 31

Slide 31 text

31 / 31