In recent years the Node.js ecosystem has transformed frontend development. In this talk we'll discuss how Node based tools like Gulp, Grunt, Browserify and Bower can help improve your productivity and create better quality web frontends.
browserify: { main: { src: ['./lib/index.js'], dest: './dist/calc.js', options: { standalone: ['calc'] } } } }); grunt.loadNpmTasks('grunt-jshint'); grunt.loadNpmTasks('grunt-browserify'); grunt.registerTask('default', ['jshint', 'browserify']); • Similar to Ant, but Javascript and JSON like config • Easy to use • Can get bloated, can be slow
var styles = 'styles'; styles = pickFiles(styles, { srcDir: '/', destDir: 'styles' }); var sourceTrees = [ styles ]; var appCss = compileSass(sourceTrees, 'styles/calc.scss', 'styles/calc. css'); module.exports = appCss; • Inspired by Rails Asset Pipeline • Adopted by Ember.js • Goal to achieve better performance compiling file trees like SASS • Run it standalone or from within a task runner • File format not very intuitive
AlertStream(alertMessage) { //call super stream.Transform.call(this, { objectMode: true }); //the message to append as an alert this.alertMessage = alertMessage; } util.inherits(AlertStream, stream.Transform); //implement the _transform method AlertStream.prototype._transform = function(chunk, encoding, callback) { //implementation here }; //export factory to create instance module.exports = function(opt) { return new AlertStream(opt); }; • https://github.com/philmander/node4fe/blob/master/gulp/gulp-alert.js • Look! No Gulp… Just streams • Consider using through2 for real Gulp plugins
★11,249 • Now ubiquitous frontend package manager • Very simple ◦ manage dependencies in bower.json ◦ resolved published Bower packages ◦ or point to a Github repo
<script src=/node_modules/react/react.js> • Problems: ◦ node_modules isn’t arranged the way front-end packages need it to be ◦ Front-end dependencies have different conflict- resolution needs ◦ Maintaining multiple package manifests is annoying ◦ Finding browser-compatible packages is a pain http://blog.npmjs.org/post/101775448305/npm-and-front-end-packaging
module system + NPM • Parses require calls and outputs all dependencies to one file • Shims Node JS specific APIs for the browser • You may externalize shared libraries • Beware of performance implications when requiring external modules ★5,834
◦ Runs nicely with Phantom.JS on command line • Run tests in Node with Browserify ◦ Unit tests test logic, not browser compatibility ◦ Encourages you to Mock things (JSDom, etc) • Jasmine, Mocha, QUnit
is choosing your stack • Build tools ◦ My preference Gulp ◦ But make your own decision based on your context • Use Bower (or similar) • Checkout Browserify or Webpack • Automation is good! But approach with caution