frontend engineer toolbox

81
The Front End Developer ToolBox Code Editing And Environment Designing In The Browser Yeoman Unit Testing (Mocha) CSS Pre-Compilation (Sass) Friday, February 15, 13

Upload: ynon-perek

Post on 27-Jan-2015

126 views

Category:

Documents


1 download

DESCRIPTION

A collection of useful tools, techniques and workflow for modern frontend developers. Included: Yeoman, mocha, sass, compass, debugging,

TRANSCRIPT

Page 1: Frontend Engineer Toolbox

The Front End Developer ToolBox

Code Editing And EnvironmentDesigning In The BrowserYeomanUnit Testing (Mocha)CSS Pre-Compilation (Sass)

Friday, February 15, 13

Page 2: Frontend Engineer Toolbox

Setting Up The Environment

IDE TipsShell TipsOther Tools

Friday, February 15, 13

Page 3: Frontend Engineer Toolbox

Use IDE File Templates

• Use templates often

• Set up your own

Friday, February 15, 13

Page 4: Frontend Engineer Toolbox

Use IDE Live Templates

• Live templates or snippets help you to type less

Friday, February 15, 13

Page 5: Frontend Engineer Toolbox

Get To Know In-File Shortcuts

• Jump to start/end tag (Cmd+Alt+[ and Cmd + Alt ] in Webstorm)

• Go To Line (Cmd + L In Webstorm)

• Join Lines(Shift + Ctrl + J In Webstorm)

• Select word or sentend(alt + up / down In Webstorm)

• Toggle Case(Cmd + Shift + U in Webstorm)

Friday, February 15, 13

Page 6: Frontend Engineer Toolbox

Get To Know Cross File Shortcuts

• Jump To Last Edit Location (Cmd + Shift + Backspace)

• Jump Between Tabs

• Jump To File (Cmd+Shift+O)

• Tip: Use partial dir names

• Tip: Exclude folders from search

Friday, February 15, 13

Page 8: Frontend Engineer Toolbox

Tabs Or Spaces

• Your choice, but be consistent

• Use IDE to enforce code style

Friday, February 15, 13

Page 9: Frontend Engineer Toolbox

Code Quality Tool

• Integrate JSHint into your browser

Friday, February 15, 13

Page 10: Frontend Engineer Toolbox

Share IDE Settings

Friday, February 15, 13

Page 11: Frontend Engineer Toolbox

Other Environment Candies

• Use http-server to start a local http server

• Or, just drop the files to Dropbox/Public

• Use ssh keys to save typing passwords

• Use localtunnel to share localhosthttp://progrium.com/localtunnel/

Friday, February 15, 13

Page 12: Frontend Engineer Toolbox

Testing

• BrowserStack maintain a stack of virtual browsers

• Use them for testing instead of installing all OS yourself

Friday, February 15, 13

Page 13: Frontend Engineer Toolbox

In Browser Design

Using Chrome Developer Tools as a mini IDE

Friday, February 15, 13

Page 14: Frontend Engineer Toolbox

Edit Styles On Page (AND Save Back)

• After changing the style, go to Sources

• Right click on a file and choose local modifications

Friday, February 15, 13

Page 15: Frontend Engineer Toolbox

Use Tincr to autosave

• Auto save JS and CSS files after locally modified in browser

Friday, February 15, 13

Page 16: Frontend Engineer Toolbox

Use SpriteCow to create sprite sheets

http://www.spritecow.com/

Friday, February 15, 13

Page 17: Frontend Engineer Toolbox

Know Thy Cog

• Chrome developer tools has a “hidden” options panel

• Use the cog

Friday, February 15, 13

Page 18: Frontend Engineer Toolbox

Debugging In The Browser

• Use chrome JavaScript debugger

• DOM change breakpoints

• XHR Breakpoints

• Event Handler Breakpoints

• Demo

Friday, February 15, 13

Page 19: Frontend Engineer Toolbox

Meet Yeoman

Modern Workflows for Modern Webapps

Friday, February 15, 13

Page 21: Frontend Engineer Toolbox

Create A New Project

• Create a new directory, enter it and type

$ yeoman init

Friday, February 15, 13

Page 22: Frontend Engineer Toolbox

What’s In The Box

• Starter files from HTML5Boilerplate

• Default .htaccess file

• Package management with bower

• Build script with grunt.js

• Unit testing with Mocha

• .editorconfig and .jshintrc

Friday, February 15, 13

Page 23: Frontend Engineer Toolbox

Package Management

• Yeoman keeps track of JavaScript and CSS libraries you use

• Each is installed under components directory with its own spec file (package.json)

• Easily maintain, install and update libraries

Friday, February 15, 13

Page 24: Frontend Engineer Toolbox

Searching For A Component (Package)

• Use yeoman search to find a package

yeoman search jquery

# outputs

Search results:

- jquery git://github.com/maccman/package-jquery.git - jquery-ui git://github.com/maccman/package-jquery-ui.git - jquery-infinite-scroll git://github.com/paulirish/infinite-scroll.git

Friday, February 15, 13

Page 25: Frontend Engineer Toolbox

Installing A New Component

• Use yeoman install to install the new component

yeoman install backbone

# or if you wish to install multiple packages at once..yeoman install jquery spine

Friday, February 15, 13

Page 26: Frontend Engineer Toolbox

Listing installed components

• Use yeoman list to show what’s installed

yeoman list

# the output will be

/myapp/!"# backbone#0.9.2$ !"" jquery#1.7.2$ %"" underscore#1.3.3!"" jquery#1.7.2%"" underscore#1.3.3

Friday, February 15, 13

Page 28: Frontend Engineer Toolbox

Beyond Default Repository

• Yeoman can install libraries from:

• github repositories

• zip files

• local files or paths

yeoman install git://github.com/tkrotoff/jquery-simplecolorpicker.git

Friday, February 15, 13

Page 29: Frontend Engineer Toolbox

Lab: Install Yeoman And Dependencies

• Install yeoman

• Start a new project

• Install jQuery

• Install jQuery-UI

• Show a Dialog using jQuery UI

Friday, February 15, 13

Page 30: Frontend Engineer Toolbox

Build Script With Grunt.JS

A task based command line build tool for javascript

Friday, February 15, 13

Page 31: Frontend Engineer Toolbox

Hello Gruntfile.js

• Yeoman’s grunt config is kept in Gruntfile.js

• What you’ll find:

• Lint Options

• Minification Options

• Compass/Sass/CoffeeScript precompilation

• Mocha tests

Friday, February 15, 13

Page 32: Frontend Engineer Toolbox

Grunt Tasks

• Linting

• Minification

• Concatenation

• Image Optimization

• r.js

Friday, February 15, 13

Page 33: Frontend Engineer Toolbox

Linting

• Run with:yeoman lint

• All jshint configuration options are found in:https://github.com/gabrielhurley/js-openclient/blob/master/.jshintrc

• Note default configuration ignores jshint. Need to modify your gruntfile

• Demo

 lint: {      files: [        'Gruntfile.js',        'app/scripts/*.js',        'spec/**/*.js'      ],      options: {        options: {          node: true, ...          devel: true,          strict: false        },        globals: {          jQuery: true,          define: true,          require: true        }      }    },

Friday, February 15, 13

Page 34: Frontend Engineer Toolbox

HTML Minification

• Yeoman will minify and concat all your HTML files

• Run withyeoman html

• Part of the build process

• Demo

    // HTML minification    html: {      // files: ['**/*.html']      files: ['app/minify_demo.html']    },

Friday, February 15, 13

Page 36: Frontend Engineer Toolbox

CSS Minification and Concatenation

• Same concept as JS

• Automatically replace groups with minified result

<!-- build:css styles/all.css --><link rel="stylesheet" href="styles/one.css"><link rel="stylesheet" href="styles/two.css"><link rel="stylesheet" href="styles/three.css"><!-- endbuild -->

Friday, February 15, 13

Page 37: Frontend Engineer Toolbox

Image Optimization

• Yeoman will optimize your images

• Done automatically as part of the build process

• Can add image folders

// Optimizes JPGs and PNGs (with jpegtran & optipng)img: {  dist: '<config:rev.img>',  my: [ 'app/images/*.jpg', 'app/images/*.png' ]},

Friday, February 15, 13

Page 38: Frontend Engineer Toolbox

Require.JS Optimization

• Yeoman will automatically use r.js to concat and minify require.js modules

• Use usemin to minify

<!-- build:js scripts/amd-app.js --><script data-main="scripts/main" src="scripts/vendor/require.js"></script><!-- endbuild -->

Friday, February 15, 13

Page 39: Frontend Engineer Toolbox

Yeoman During Development

• Use yeoman server to start a test server

• Get automatic browser reload when files change

• Automatically recompiles sass

Friday, February 15, 13

Page 40: Frontend Engineer Toolbox

Yeoman Disadvantages

• Doesn’t work well with existing projects

• Still buggy

• 1.0 Is around the corner

Friday, February 15, 13

Page 41: Frontend Engineer Toolbox

Q & A

Friday, February 15, 13

Page 42: Frontend Engineer Toolbox

Unit Testing With Mocha

Friday, February 15, 13

Page 43: Frontend Engineer Toolbox

Benefits of testing

• Discover bugs before they happen

• Write cleaner code

• Don’t fear regression

Friday, February 15, 13

Page 44: Frontend Engineer Toolbox

How To Use Mocha

// replace this by your own specs(function () {  describe('Give it some context', function () {    describe('maybe a bit more context here', function () {       it('should run here few assertions', function () {        assert( 1 === 1 , 'One is the one');      });      }); });})();

Friday, February 15, 13

Page 45: Frontend Engineer Toolbox

How To Use Mocha

// replace this by your own specs(function () {  describe('Give it some context', function () {    describe('maybe a bit more context here', function () {       it('should run here few assertions', function () {        assert( 1 === 1 , 'One is the one');      });      }); });})();

Test Group (suite)

Friday, February 15, 13

Page 46: Frontend Engineer Toolbox

How To Use Mocha

// replace this by your own specs(function () {  describe('Give it some context', function () {    describe('maybe a bit more context here', function () {       it('should run here few assertions', function () {        assert( 1 === 1 , 'One is the one');      });      }); });})();

Single test

Friday, February 15, 13

Page 47: Frontend Engineer Toolbox

Assertion Libraries

• Mocha isn’t an assertion library

• We’ll use Chai for the assertions

• Alternatives: should.js, expect.js, better-assert.js

assert( 1 === 1 , 'One is the one');

Friday, February 15, 13

Page 48: Frontend Engineer Toolbox

Mocha with Chai

• Mocha provides:

• describe

• it

• Chai provides:

• assert

Friday, February 15, 13

Page 49: Frontend Engineer Toolbox

Sync Test

describe('Array', function () {   describe('#indexOf()', function() {     it('should return -1 when value not found', function() {      assert.equal( [1,2,3].indexOf(5), -1 );      assert.equal( [1,2,3].indexOf(0), -1 );    });     it('should return the index when value found', function() {      assert.equal( [1,2,3].indexOf(2), 1 );    });   }); });

Friday, February 15, 13

Page 50: Frontend Engineer Toolbox

Async Test

describe('Image Loading', function() {   it( 'should load images async', function(done) {     var img = new Image();

    img.onload = function() {      done();    };

     img.src = 'http://www.oklahomafood.coop/shop/members/getimage.php?image_id=4610';   }); });

Friday, February 15, 13

Page 51: Frontend Engineer Toolbox

Async Test

describe('Image Loading', function() {   it( 'should load images async', function(done) {     var img = new Image();

    img.onload = function() {      done();    };

     img.src = 'http://www.oklahomafood.coop/shop/members/getimage.php?image_id=4610';   }); });

Friday, February 15, 13

Page 52: Frontend Engineer Toolbox

Tests Setup / Teardown

• Mocha has hooks:

• before - before the suite

• after - after the suite

• beforeEach - before each case

• afterEach - after each case

Friday, February 15, 13

Page 53: Frontend Engineer Toolbox

Tests Setup / Teardown

describe('Image Loading', function() {     beforeEach(function() {      console.log('<<< Starting Test');    });     afterEach(function() {      console.log('>>>> End Test');    });});

Friday, February 15, 13

Page 54: Frontend Engineer Toolbox

Chai’s Assertions

• Rich assertions library

• Has both TDD and BDD syntax. We’ll use TDD.

• Entry point: assert

Friday, February 15, 13

Page 55: Frontend Engineer Toolbox

Chai’s Assertions

• Basic Assertions

assert('foo' !== 'bar', 'foo is not bar');

assert(Array.isArray([]), 'empty arrays are arrays');

Friday, February 15, 13

Page 56: Frontend Engineer Toolbox

Chai’s Assertions

• Use assert.equal for == checks

assert.equal( 3, '3', '== coerces values to strings');

Friday, February 15, 13

Page 57: Frontend Engineer Toolbox

Chai’s Assertions

• Use assert.strictEqual for === checks

assert.strictEqual( 3, '3', '=== fails to coerce');

Friday, February 15, 13

Page 58: Frontend Engineer Toolbox

Chai’s Assertions

• Use deepEqual to check object’s equality

assert.deepEqual( { tea: 'green' }, { tea: 'green' });

Friday, February 15, 13

Page 59: Frontend Engineer Toolbox

Chai’s Assertions

• Use isTrue / isFalse for truthy / falsy values

var teaServed = true;assert.isTrue( teaServed, 'the tea has been served');

var teaServed = false;assert.isFalse( teaServed, 'no tea yet? hmm...');

Friday, February 15, 13

Page 60: Frontend Engineer Toolbox

Chai’s Assertions

• Basic Assertions

• isNull, isNotNull

• isDefined, isUndefined

• isFunction, isNotFunction, isObject, isNotObject

• isArray, isNotArray,

• isString, isNotString, isNumber, isNotNumber, isBoolean, isNotBoolean

Friday, February 15, 13

Page 61: Frontend Engineer Toolbox

Chai’s Assertions

• Cool Assertions

• include( haystack, needle ): search for needle in a haystack. Supports strings AND arrays

• deepProperty( object, prop ):object has a property named prop, which can be a dotted string

• closeTo( actual, expected, delta )

assert.deepProperty({ tea: { green: 'matcha' }}, 'tea.green');

assert.closeTo(1.5, 1, 0.5, 'numbers are close');

Friday, February 15, 13

Page 62: Frontend Engineer Toolbox

Demo

Mocha + Require + Chai

Friday, February 15, 13

Page 63: Frontend Engineer Toolbox

Q & A

Friday, February 15, 13

Page 64: Frontend Engineer Toolbox

Sass & Compass

Styling with attitude

Friday, February 15, 13

Page 65: Frontend Engineer Toolbox

Welcome To Sassy Compass

• Sass adds tons of features to CSS by precompiling:

• variables

• mixins

• Flow Control

• Compass is a framework of predefined sass declarations

Friday, February 15, 13

Page 66: Frontend Engineer Toolbox

Meet Sass: variables

$main-color: #ce4dd6;$style: solid; #navbar {  border-bottom: {    color: $main-color;    style: $style;  }}

Friday, February 15, 13

Page 67: Frontend Engineer Toolbox

Meet Sass: Nested Rules

#navbar {  width: 80%;  height: 23px;   ul { list-style-type: none; }

  li {    float: left;    a { font-weight: bold; }  }}

Friday, February 15, 13

Page 68: Frontend Engineer Toolbox

Meet Sass: Math

#navbar {  $navbar-width: 800px;  $items: 5;  $navbar-color: #ce4dd6;   width: $navbar-width;   li {    float: left;    width: $navbar-width/$items - 10px;    list-style: none;  }}

Friday, February 15, 13

Page 69: Frontend Engineer Toolbox

Meet Sass: Parent Reference

a {  color: #ce4dd6;  &:hover { color: #ffb3ff; }  &:visited { color: #c458cb; }}

Friday, February 15, 13

Page 70: Frontend Engineer Toolbox

Using Sass With Yeoman

• Add sass commands to main.scss

• Even better - create a _base.scss partial

• Demo

Friday, February 15, 13

Page 71: Frontend Engineer Toolbox

Bigger Sass

• Use partials to build your scss files

• Use mixins for reusable snippets

@mixin my-small-rounded-corners {  -moz-border-radius: 5px;  -webkit-border-radius: 5px;  border-radius: 5px;} .rounded {  @include my-small-rounded-corners;}

Friday, February 15, 13

Page 72: Frontend Engineer Toolbox

Mixin Parameters

@mixin my-small-rounded-corners($r:5px) {  -moz-border-radius: $r;  -webkit-border-radius: $r;  border-radius: $r;} .rounded {  @include my-small-rounded-corners(8px);}

Friday, February 15, 13

Page 73: Frontend Engineer Toolbox

Meet Compass

• A (huge) collection of mixins and classes built with sass

• Includes new functions

• Mainly used for CSS3 related features (instead of typing browser prefix yourself)

• Docs:http://compass-style.org/

Friday, February 15, 13

Page 74: Frontend Engineer Toolbox

How To Use

• Use compass by including their partials

• Automatically used by yeoman

@import "compass/css3";@import "compass/typography";

Friday, February 15, 13

Page 75: Frontend Engineer Toolbox

What You Get

• Border Radius

.simple   { @include border-radius(4px, 4px); }

.compound { @include border-radius(2px 5px, 3px 6px); }

.crazy    { @include border-radius(1px 3px 5px 7px, 2px 4px 6px 8px)}

Friday, February 15, 13

Page 76: Frontend Engineer Toolbox

What You Get

• Background Size

Friday, February 15, 13

Page 77: Frontend Engineer Toolbox

What You Get

• Box Shadow

.box-shadow-example div {  width: 40px;  height: 40px;  background: #eeeeee;  margin: 20px;  float: left; } // Default single box shadow#box-shadow-default {  @include single-box-shadow; }} #box-shadow-custom {  @include box-shadow(red 2px 2px 10px); }}

Friday, February 15, 13

Page 78: Frontend Engineer Toolbox

What You Get

• CSS Column Count

#two-column {  @include column-count(2);  width: 300px;  margin-bottom: 20px;} #three-column {  @include column-count(3);  width: 300px;  margin-bottom: 20px;}

Friday, February 15, 13

Page 79: Frontend Engineer Toolbox

What You Get

• Inline-block can be tricky

#inline-block {  @include inline-block;  padding: 4px 10px;  background: red;  color: white;}

#inline-block {  display: -moz-inline-stack;  display: inline-block;  vertical-align: middle;  *vertical-align: auto;  zoom: 1;  *display: inline;  padding: 4px 10px;  background: red;  color: white;}

Friday, February 15, 13

Page 80: Frontend Engineer Toolbox

Sass & Compass Resources

• Sass Tutorial:http://sass-lang.com/tutorial.html

• Sass Full Reference:http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html

• Compass Tutorials:http://compass-style.org/help/tutorials/

• Compass Reference:http://compass-style.org/reference/compass/

Friday, February 15, 13

Page 81: Frontend Engineer Toolbox

Thanks For Listening

• Ynon Perek

• Contact me at:

[email protected]

• Slides at:

• http://ynonperek.com

• Photos from:

• http://www.123rf.com/freeimages.php

• http://morguefile.com/archive

Friday, February 15, 13