Share
Embed
Browserify All The Things
This talk is about how to use browserify to develop front-end modular code using Common.JS, and how those modules should be documented, designed, and released using an automated build system. In order to explain these concepts I'll walk you through a few of my own open-source creations, highlighting interesting points as we go along.
Nicolás Bevacqua
August 28, 2014
Transcript
146 slides
-
~function (window) { function sum (a, b) { return a
+ b; } window.math = {sum: sum}; }(this); -
<script src='foo.js'></script> <script src='bar.js'></script> <script src='baz.js'></script> <script src='sin.js'></script> <script src='cos.js'></script>
<script src='tan.js'></script> <script src='circle.js'></script> <script src='event.js'></script> -
Closures - Abuse global variables - Fine for tiny projects
- Modularity is up to you -
Closures - Abuse global variables - Fine for tiny projects
- Modularity is up to you - Manual dependency management -
define([], function () { function sum (a, b) { return
a + b; } return { sum: sum }; }); math.js -
AMD Modules - Resolves dependency graph - Lacks a straightforward
API - Drastic changes after you build -
AMD Modules - Resolves dependency graph - Lacks a straightforward
API - Drastic changes after you build - So. Much. Clutter. -
function sum (a, b) { return a + b; }
! export default {sum: sum}; math.js -
ES6 Modules - The Way Forward ™ - To use
today, add a transpilation step -
ES6 Modules - The Way Forward ™ - To use
today, add a transpilation step - Hard to interact with CJS modules -
ES6 Modules - The Way Forward ™ - To use
today, add a transpilation step - Hard to interact with CJS modules - Not generally available / popular yet -
function sum (a, b) { return a + b; }
module.exports = {sum:sum}; math.js -
CommonJS Modules API similar to ES6 Takes advantage of npm
Browserify for the client -
var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; function parseStyles
(styles) { if (typeof styles === 'string') { return styles; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } -
var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; function parseStyles
(styles) { if (typeof styles === 'string') { return styles; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } -
var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; function parseStyles
(styles) { if (typeof styles === 'string') { return styles; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } -
var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; function parseStyles
(styles) { if (typeof styles === 'string') { return styles; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } -
var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; function parseStyles
(styles) { if (typeof styles === 'string') { return styles; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } -
var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; function parseStyles
(styles) { if (typeof styles === 'string') { return styles; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } -
module.exports = function (selector, styles) { var css = parseStyles(styles);
var sheets = document.styleSheets; var s = sheets[sheets.length - 1]; var key = s.cssRules ? s.cssRules: s.rules; if (s.insertRule) { sheet.insertRule(selector + '{' + css + '}', key.length); } else if (sheet.addRule) { sheet.addRule(selector, css, key.length); } }; -
module.exports = function (selector, styles) { var css = parseStyles(styles);
var sheets = document.styleSheets; var s = sheets[sheets.length - 1]; var key = s.cssRules ? s.cssRules: s.rules; if (s.insertRule) { sheet.insertRule(selector + '{' + css + '}', key.length); } else if (sheet.addRule) { sheet.addRule(selector, css, key.length); } }; -
module.exports = function (selector, styles) { var css = parseStyles(styles);
var sheets = document.styleSheets; var s = sheets[sheets.length - 1]; var key = s.cssRules ? s.cssRules: s.rules; if (s.insertRule) { sheet.insertRule(selector + '{' + css + '}', key.length); } else if (sheet.addRule) { sheet.addRule(selector, css, key.length); } }; -
module.exports = function (selector, styles) { var css = parseStyles(styles);
var sheets = document.styleSheets; var s = sheets[sheets.length - 1]; var key = s.cssRules ? s.cssRules: s.rules; if (s.insertRule) { sheet.insertRule(selector + '{' + css + '}', key.length); } else if (sheet.addRule) { sheet.addRule(selector, css, key.length); } }; -
module.exports = function (selector, styles) { var css = parseStyles(styles);
var sheets = document.styleSheets; var s = sheets[sheets.length - 1]; var key = s.cssRules ? s.cssRules: s.rules; if (s.insertRule) { sheet.insertRule(selector + '{' + css + '}', key.length); } else if (sheet.addRule) { sheet.addRule(selector, css, key.length); } }; -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
function build () { var pkg = require('./package.json'); return browserify('./src/lib.js')
.bundle({ debug: true, standalone: 'lib' }) .pipe(source('lib.js')) .pipe(streamify(header(ext, { pkg: pkg }))) .pipe(gulp.dest('./dist')) .pipe(streamify(rename('lib.min.js'))) .pipe(streamify(uglify())) .pipe(streamify(header(min, { pkg: pkg }))) .pipe(streamify(size())) .pipe(gulp.dest('./dist')); } -
gulp.task('watch', function() { var pattern = { glob: 'src/**/*.js' };
watch(pattern, function () { gulp.start('build'); }); }); -
gulp.task('watch', function() { var pattern = { glob: 'src/**/*.js' };
watch(pattern, function () { gulp.start('build'); }); }); -
gulp.task('watch', function() { var pattern = { glob: 'src/**/*.js' };
watch(pattern, function () { gulp.start('build'); }); }); -
gulp.task('watch', function() { var pattern = { glob: 'src/**/*.js' };
watch(pattern, function () { gulp.start('build'); }); }); -
gulp.task('bump-only', function () { // major.minor.patch var bumpType = process.env.BUMP
|| 'patch'; ! return gulp.src(['./package.json', './bower.json']) .pipe(bump({ type: bumpType })) .pipe(gulp.dest('./')); }); ! gulp.task('bump', ['bump-only'], build); -
gulp.task('bump-only', function () { // major.minor.patch var bumpType = process.env.BUMP
|| 'patch'; ! return gulp.src(['./package.json', './bower.json']) .pipe(bump({ type: bumpType })) .pipe(gulp.dest('./')); }); ! gulp.task('bump', ['bump-only'], build); -
gulp.task('bump-only', function () { // major.minor.patch var bumpType = process.env.BUMP
|| 'patch'; ! return gulp.src(['./package.json', './bower.json']) .pipe(bump({ type: bumpType })) .pipe(gulp.dest('./')); }); ! gulp.task('bump', ['bump-only'], build); -
gulp.task('bump-only', function () { // major.minor.patch var bumpType = process.env.BUMP
|| 'patch'; ! return gulp.src(['./package.json', './bower.json']) .pipe(bump({ type: bumpType })) .pipe(gulp.dest('./')); }); ! gulp.task('bump', ['bump-only'], build); -
gulp.task('bump-only', function () { // major.minor.patch var bumpType = process.env.BUMP
|| 'patch'; ! return gulp.src(['./package.json', './bower.json']) .pipe(bump({ type: bumpType })) .pipe(gulp.dest('./')); }); ! gulp.task('bump', ['bump-only'], build); -
gulp.task('bump-only', function () { // major.minor.patch var bumpType = process.env.BUMP
|| 'patch'; ! return gulp.src(['./package.json', './bower.json']) .pipe(bump({ type: bumpType })) .pipe(gulp.dest('./')); }); ! gulp.task('bump', ['bump-only'], build); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
gulp.task('tag', ['bump'], function () { var pkg = require('./package.json'); var
v = 'v' + pkg.version; var message = 'Release ' + v; ! return gulp.src('./') .pipe(git.commit(message)) .pipe(git.tag(v, message)) .pipe(git.push('origin', 'master', '--tags')) .pipe(gulp.dest('./')); }); -
var cp = require('child_process'); var spawn = cp.spawn; ! gulp.task('npm',['tag'],function(done){
spawn('npm', ['publish'], { stdio: 'inherit' }).on('close', done); }); -
var cp = require('child_process'); var spawn = cp.spawn; ! gulp.task('npm',['tag'],function(done){
spawn('npm', ['publish'], { stdio: 'inherit' }).on('close', done); }); -
var cp = require('child_process'); var spawn = cp.spawn; ! gulp.task('npm',['tag'],function(done){
spawn('npm', ['publish'], { stdio: 'inherit' }).on('close', done); }); -
var cp = require('child_process'); var spawn = cp.spawn; ! gulp.task('npm',['tag'],function(done){
spawn('npm', ['publish'], { stdio: 'inherit' }).on('close', done); }); -
var doc = global.document; var tag = 'iframe'; function poser
(type) { var frame = doc.createElement(tag); frame.style.display = 'none'; doc.body.appendChild(frame); return frame.contentWindow[type]; } module.exports = poser; -
var doc = global.document; var tag = 'iframe'; function poser
(type) { var frame = doc.createElement(tag); frame.style.display = 'none'; doc.body.appendChild(frame); return frame.contentWindow[type]; } module.exports = poser; -
var doc = global.document; var tag = 'iframe'; function poser
(type) { var frame = doc.createElement(tag); frame.style.display = 'none'; doc.body.appendChild(frame); return frame.contentWindow[type]; } module.exports = poser; -
var doc = global.document; var tag = 'iframe'; function poser
(type) { var frame = doc.createElement(tag); frame.style.display = 'none'; doc.body.appendChild(frame); return frame.contentWindow[type]; } module.exports = poser; -
var doc = global.document; var tag = 'iframe'; function poser
(type) { var frame = doc.createElement(tag); frame.style.display = 'none'; doc.body.appendChild(frame); return frame.contentWindow[type]; } module.exports = poser; -
var vm = require('vm'); function poser (type) { var box
= {}; var code = 'stolen=' + type; vm.runInNewContext(code, box, 'vm'); return box.stolen; } module.exports = poser; -
var vm = require('vm'); function poser (type) { var box
= {}; var code = 'stolen=' + type; vm.runInNewContext(code, box, 'vm'); return box.stolen; } module.exports = poser; -
var vm = require('vm'); function poser (type) { var box
= {}; var code = 'stolen=' + type; vm.runInNewContext(code, box, 'vm'); return box.stolen; } module.exports = poser; -
var vm = require('vm'); function poser (type) { var box
= {}; var code = 'stolen=' + type; vm.runInNewContext(code, box, 'vm'); return box.stolen; } module.exports = poser; -
var vm = require('vm'); function poser (type) { var box
= {}; var code = 'stolen=' + type; vm.runInNewContext(code, box, 'vm'); return box.stolen; } module.exports = poser; -
Server Side GET /author/compose Accept: HTML? Full Layout + Partial
Accept: JSON? Partial View Model -
Client Side On first load GET request established by the
browser Server-side renders layout and view Client-side controller gets executed -
Client Side On navigation - after first load AJAX request
for partial model Client-side renders view Client-side controller gets executed