Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What makes me "Grunt"?

Fabien Doiron
February 27, 2014

What makes me "Grunt"?

Slides from my talk at Confoo 2014. Notes to come...

As a front end developer, I want to write code. Dealing with the mundane tasks that come with static assets such as concatenation, minification and versioning, I don't care much for. In this session, I'll explain how to setup Grunt tasks to handle CSS and JavaScript assets in both development and production environments. This automated workflow allows you to easily reproduce both environments locally for testing and debugging.

## Resources
* http://www.getchef.com/
* http://www.vagrantup.com/
* http://www.phing.info/
* https://getcomposer.org/
* http://www.gruntjs.com/
* http://www.bower.io/
* http://www.npmjs.com/
* http://github.com/canvaspop/grunt-static-versioning

## Links
* http://fabien-d.github.io/
* http://twitter.com/fabien_doiron
* http://canvaspop.com
* http://dna11.com
* http://crated.com
* http://developers.canvaspop.com
* http://remade.canvaspop.com/

Fabien Doiron

February 27, 2014
Tweet

More Decks by Fabien Doiron

Other Decks in Technology

Transcript

  1. current typical tools linting / preprocessors / concatenation / minification

    / versioning / testing / coverage / dependency management / continuous deployment / version control / frameworks / libraries…
  2. i. ~ ▸ git clone … # get code from

    repository ~ ▸ git pull # get latest code from repository
  3. ii. ~ ▸ vagrant up # puts together a complete

    environment # provision environment with chef
  4. iii. ~ ▸ phing proj:build # get deps with composer,

    npm & bower # database migration with phinx # front-end build with grunt ~ ▸ grunt build # lint, preprocess, concat, min, version # my personal favourite: ascii
  5. reduce the risk of “works on my machine” by abstracting

    the output settings from the user to the build tool
  6. compiling CSS: user settings ~ ▸ sass in.scss:out.css ~ ▸

    lessc in.scss > out.css results: can vary
  7. example task (Sass) module.exports = function ( grunt ) {

    var src = '<%= grunt.option( "src" ) %>'; var tmp = '<%= grunt.option( "tmp" ) %>'; grunt.config( 'sass', { dist: { files: [ { expand: true, cwd: src + '/sass', src: [ '*.scss' ], dest: tmp + '/sass', ext: '.css' } ] } } ); grunt.loadNpmTasks( 'grunt-contrib-sass' ); };
  8. compiling CSS: tool settings ~ ▸ grunt sass ~ ▸

    grunt less results: are the same
  9. i. separate targets ~ ▸ grunt build:dev # all source

    files, commented, unminified ~ ▸ grunt build:prod # concat, minified, versioned files
  10. grunt versioning task versioning: { options: { cwd: 'public', outputConfigDir:

    'public/config' }, dist: { files: [{ assets: '<%= uglify.main.files %>', key: 'global', dest: 'js', type: 'js', ext: '.min.js' }, { … } ] } }
  11. iii. generated configuration file ~ ▸ grunt build:dev <?php return

    array( 'static.assets' => array( 'global' => array( 'css' => array( '/static/css/main.css' ), 'js' => array( '/static/js/file1.js', '/static/js/file2.js', … ) ), 'home' => array( 'css' => array(), 'js' => array( 'static/js/home1.js', '/static/js/home2.js' ) ), 'anotherKey' => array( … ) ) );
  12. iii. generated configuration file ~ ▸ grunt build:prod <?php return

    array( 'static.assets' => array( 'global' => array( 'css' => array( '/static/css/main.7f41197e.min.css' ), 'js' => array( '/static/js/plugins.7409b19a.min.js', '/static/js/common.4923a32c.min.js', … ) ), 'home' => array( 'css' => array(), 'js' => array( 'static/js/home.47b0990d.min.js' ) ), 'anotherKey' => array( … ) ) );
  13. ii. generated output ~ ▸ grunt build:dev ~ ▸ grunt

    build:prod ▾ public ▾ static ▾ css main.css ▾ js plugin1.js plugin2.js common1.js common2.js home1.js home2.js … ▾ public ▾ static ▾ css main.7f41197e.min.css ▾ js plugins.7409b19a.min.js common.4923a32c.min.js home.47b0990d.min.js …
  14. iv. use generated configuration file // inside our layouts <?php

    $this->versionedAsset()->render( 'global' ); ?> ~ ▸ grunt build:dev ~ ▸ grunt build:prod <link rel="stylesheet" href="/static/css/main.css"> … <script src="/static/js/file1.js"></script> <script src="/static/js/file2.js"></script> … <link rel="stylesheet" href="/static/css/main.7f41197e.min.css"> … <script src="/static/js/plugins.7409b19a.min.js"></script> <script src="/static/js/common.4923a32c.min.js"></script>
  15. cleaner includes in our layouts messy if( APPLICATION_ENV == "production")

    { // include concat/min files } else { // include all dev files } clean <?php $this->versionedAsset()->render( 'global' ); ?>
  16. i. build dev environment reproduce the error rule out minification

    error use devtools on source files update/add unit, integration, regression tests generally all we need
  17. ii. build prod environment reproduce the error use devtools with

    source maps still have access to source files easier to debug than on the live site update/add unit, integration, regression tests
  18. “trying to fix a bug in minified code is like

    using a new API with no documentation”
  19. being a front-end developer is hard build tools make you

    awesome automate the repetitive tasks frictionless project setup reproduce output every time access to dev and production environments locally take the stress out of debugging