Grunt@sebasporto
View Slide
WHAT?Task runner (like make, rake)
WHY?Cross platformLots of plug-insNice loggingIs JSIs Node
WHY NOT?VerboseCould end as an unmaintainablecomplex tangle mess ofconfiguration
Main usesBuild systemLive reloadDaemonsAutomated tasksWhatever
Common plug-insLintMinifyConcatTest (Mocha, Jasmine)Optimise images
Installing gruntInstall grunt cli as globalInstall grunt as localUses package.json to keep track ofdependencies (devDependecies)
Gruntfilemodule.exports = function(grunt) {!grunt.initConfig({uglify: {build: {src: 'src/<%= pkg.name %>.js',dest: 'build/<%= pkg.name %>.min.js'}}});!grunt.loadNpmTasks('grunt-contrib-uglify');!grunt.registerTask('default', ['uglify']);};tasks configloading andregistering tasks
A basic taskgrunt.registerTask('foo', 'A task', function(arg1, arg2) {…});$ grunt foo arg1 arg2
Just Nodevar foo = require(‘foo’);!grunt.registerTask('foo', 'A task', function(arg1, arg2) {// You can do whatever you can with Node!foo.doSomething();});
A multitaskgrunt.registerMultiTask('foo', 'A task', function() {…});grunt.initConfig({foo: {dev: {…},prod: {…}}});
Multitasks$ grunt foogrunt.initConfig({foo: {dev: {…},prod: {…}}});runs dev and prod
Multitasks$ grunt foo:devgrunt.initConfig({foo: {dev: {…},prod: {…}}});runs dev only
Chaining tasksgrunt.registerTask(‘all', [‘jshint’, ‘mocha’, ‘concat’]);$ grunt all
Chaining tasksgrunt.registerTask('all', 'A task', function() {grunt.task.run(‘jshint’, ‘concat’);grunt.task.run(‘ngmin’);});$ grunt all
Async tasksgrunt.registerTask('all', 'A task', function() {var done = this.async();!doSomethingAsync(done);});$ grunt all
Eventsgrunt.event.on(‘foo:started’, handler);!grunt.event.emit(‘foo:stated’, args…);
Installing a plug-in$ npm install grunt-goserver --save-devgrunt.loadNpmTasks(‘grunt-goserver');!grunt.initConfig({goserver: {default: {srcPath: '/full/path/to/src/folder',…},},})In Gruntfile.js
Creating a plug-inInstall grunt-init moduleClone templateRun generatorCodenpm publish
Plug-in best practicesCreate an NPM module firstWrap that module in a Grunt plug-in
Thanks@sebasporto