On Github cbetta / slides-techsylvania-2016
with Rollup, Riot and Redux
Cristiano Betta
Hacker @ betta.io
Hire me!2x @ Techsylvania!
former PayPal/Braintree
Ruby/JS Developer
Consultant @ Work Betta
Code Monkey @ punch.rocks
<script src="punch.js" charset="utf-8"></script>
<div id='registration'></div>
<script type="text/javascript">
punch.setup({
id: "19acb1ab...53fd459280d",
type: 'popover',
element: '#registration'
});
</script>
NodeJS dependencies in the bowser
Browserify lets you require('modules') in the browser by bundling up all of your dependencies.
browserify main.js -o bundle.js
Older
JS Only
No ES6 import/export
Not the smallest result
A bundler for javascript and friends.
webpack input.js output.js
Newer
Handles many assets
No ES6 import/export
Not always smallest result
Not the simplest
The next-generation JavaScript module bundler
rollup input.js --output output.js
Newer
JS Only
No ES6 import/export
Tree shaking
Simple to use
The next-generation JavaScript module bundler
Write ES6 (aka ES2015)
Bundle into single file
Smaller than Browserify and Webpack
Export to AMD, CommonJS, ES2015, Globals, UMD
Tree shaking!
import fs from 'fs';
class AwesomeSauce {
constructor() {
// some code
}
makeIt() {
// some code
}
}
export default AwesomeSauce;
import AwesomeSauce from 'awesome-sauce'; let sauce = new AwesomeSauce(); sauce.makeIt();
class AwesomeSauce {
// 100 lines of code
}
class EnterpriseSauce {
// 2000 lines of code
}
export EnterpriseSauce;
export default AwesomeSauce;
//import 2000 lines of code!
import { EnterpriseSauce } from 'awesome-sauce';
let sauce = new EnterpriseSauce();
sauce.makeItSecureButSlow();
// main.js
import { cube } from './maths.js';
console.log( cube( 5 ) );
// maths.js
// unused function
export function square ( x ) {
return x * x;
}
// included function
export function cube ( x ) {
return x * x * x;
}
function cube ( x ) {
return x * x * x;
}
console.log( cube( 5 ) );
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, function () { 'use strict';
function cube ( x ) {
return x * x * x;
}
console.log( cube( 5 ) );
}));
// package.json
{
...
"scripts": {
"dist:js": "rollup -c rollup/dist.js",
...
}
}
// rollup/dist.js
export default {
entry: 'js/index.js',
dest: 'dist/punch.js',
format: 'umd',
moduleName: 'punch',
plugins: [
riot(),
npm({ browser: true }),
commonjs(),
babel(),
uglify()
]
};
// js/index.js import Dropin from './dropin/main.js'; let dropin = new Dropin(); export var setup = dropin.setup; // punch.setup() from earlier! export var destroyAll = dropin.destroyAll; export var instances = dropin.instances;
<script src="punch.js" charset="utf-8"></script>
<div id='registration'></div>
<script type="text/javascript">
punch.setup({
id: "19acb1ab...53fd459280d",
type: 'popover',
element: '#registration'
});
</script>
Nothing can be said to be certain
except death and taxes
and JS frameworks
HTML enhanced for web apps!
~50kb (compressed)
A Javascript Library for building user interfaces
~146kb (compressed)
A React-like user interface micro-library
~9.18kb (compressed)
A React-like user interface micro-library
Custom Tags
Virtual Dom
Enjoyable Syntax
No IE8 support
Tiny Size!
<!-- index.html --> <div id='element'></div>
// dropin.js
import './tags/punch-dropin.tag';
import riot from 'riot';
riot.mount('#element', "punch-dropin");
<!-- punch-dropin.tag --> <punch-dropin> <h1>Hello World</h1> </punch-dropin>
<!-- punch-dropin.tag --> <punch-dropin> <punch-loader></punch-loader> <punch-form></punch-form> </punch-dropin>
There are two hard things in computer science: cache invalidation and naming things.
There are two hard things in computer science: cache invalidation and naming things and state management
There are three hard things in computer science: cache invalidation and naming things and state management
There are two hard things in computer science: cache invalidation, naming things and state management
Redux is a predictable state container for JavaScript apps.
2kb
Including dependencies!
Before uglifying/compression!!
Riot compatible!!!
npm install redux
// dropin.js
import './tags/punch-dropin.tag';
import riot from 'riot';
import { createStore } from 'redux';
import StoreMixin from 'riot-redux-mixin';
let state = ...
let store = createStore(state);
riot.mixin('redux', new StoreMixin(store));
riot.mount('#element', "punch-dropin");
<!-- punch-dropin.tag -->
<punch-dropin>
<h1>Counter: { state.count }</h1>
<script type="text/javascript">
this.mixin('redux');
this.subscribe(function(state){
this.state = state;
}.bind(this))
</script>
</punch-dropin>
// dropin.js
let state = function counter(count = 0, action) {
switch (action) {
case 'increment':
return count + 1
case 'decrement':
return count - 1
default:
return count
}
<!-- punch-dropin.tag -->
<punch-dropin>
<h1 onClick={ increment }>Counter: { state.count }</h1>
<script type="text/javascript">
this.mixin('redux');
this.increment = function() {
this.store.dispatch('increment');
}
this.subscribe(function(state){
this.state = state;
}.bind(this))
</script>
</punch-dropin>
Rollup: 12k
WebPack 2: 13k
All results in bytes, Gzipped
babel + browserify + uglify => 3915
webpack@2 + babel + uglify => 3632
babel + rollup + uglify => 3440
typescript + webpack => 3245
closure => 2890
More results
source - 37,304 bytes
bundled => 180,345 bytes
uglified => 57,171 bytes
gzipped => 18,561 bytes
Most of these are personal opinion
ES6 is lovely
RiotJS is easy and really small
Riot+Redux makes apps easy to comprehend
Rollup + NPM scripts beats Gulp and Grunt
Some stuff isn't there yet
Rollup+Riot breaks sourcemaps
WebPack Chunks are awesome
Riot Routes needs to be added
Hard to scientifically compare sizes