{"id":11691,"date":"2014-02-06T22:56:18","date_gmt":"2014-02-06T17:26:18","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11691"},"modified":"2014-02-07T15:58:51","modified_gmt":"2014-02-07T10:28:51","slug":"getting-started-with-grunt","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-started-with-grunt\/","title":{"rendered":"Getting Started with Grunt:Task Runner"},"content":{"rendered":"<p>I was working on one of my projects which involved multiple task execution on regular basis. Well, I think that many of us have faced similar issues on their projects which increases our work and making it more time-consuming and increased work. Working in a community where people are actively developing tools to automate tedious processes, you assumes to have some tools to automate and speed up this process too.<\/p>\n<p>For such scenarios, we have\u00a0<strong>Grunt, <\/strong>to the rescue. I loved the way it made my work flow so easy.<\/p>\n<p>Grunt already has a lot of plugins for the task like compiling less CSS, command shell, minifying\u00a0CSS etc., or you can also create our own Grunt plugin for tasks which are repetitive or used on daily basis.<\/p>\n<p>Now we will understand grunt with the help of a plugin called &#8220;cssmin&#8221;, used to minify CSS files.<\/p>\n<p>Lets start with the setting up of grunt<\/p>\n<ol>\n<li>Installing grunt-cli globally\n<pre><code>npm install -g grunt-cli<\/code><\/pre>\n<\/li>\n<li>In your project directory install grunt and grunt plugin command\n<pre><code>npm install grunt --save-dev <\/code>\r\n<code>npm install grunt-contrib-mincss --save-dev\u00a0<\/code><\/pre>\n<div>\u00a0It will add grunt and grunt plugins in devDependencies\u00a0in package.json.<\/div>\n<\/li>\n<li>Gruntfile.js or Gruntfile.coffee<br \/>\n<strong>Gruntfile\u00a0<\/strong>must be there in project root directory after .json\u00a0package.This file contains<\/p>\n<ul>\n<li>The &#8220;wrapper&#8221; function<\/li>\n<li>Project and task configuration<\/li>\n<li>Loading Grunt plugins and tasks<\/li>\n<li>Custom tasks<br \/>\nGruntfile must look like:<\/li>\n<li>[js]<br \/>\n module.exports=function(grunt){<br \/>\n \/\/here grunt code comes<br \/>\n \/\/project configuration<br \/>\n grunt.initConfig({<br \/>\n      pkg:\u00a0grunt.file.readJSON(&#8216;package.json&#8217;),<br \/>\n      cssmin:{<br \/>\n            minify: {<br \/>\n            expand: true,<br \/>\n            cwd: &#8216;public\/stylesheets\/css\/&#8217;,<br \/>\n            src: [&#8216;*.css&#8217;, &#8216;!*.min.css&#8217;],<br \/>\n            dest: &#8216;public\/stylesheets\/mincss\/&#8217;,<br \/>\n            ext: &#8216;.min.css&#8217;<br \/>\n                     }<br \/>\n            }<br \/>\n       });<br \/>\n\/\/load grunt plugins and register tasks<br \/>\ngrunt.loadNpmTasks(&quot;grunt-contrib-cssmin&quot;);<br \/>\ngrunt.registerTask(&#8216;default&#8217;, [&#8216;cssmin&#8217;]);<br \/>\n[\/js]<\/p>\n<p><span style=\"font-family: 'Courier 10 Pitch', Courier, monospace;font-size: 13px;font-style: normal;line-height: 1.5\"><br \/>\n<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><span>Now its your turn to be the magician. Open your terminal, go to your project directory and write\u00a0<\/span><\/p>\n<p><span style=\"background-color: #f4f4f4\"> <code>grunt cssmin<\/code><\/span><\/p>\n<p>All your minified css will be generated in destination directory which you have mentioned in Gruntfile.js.<\/p>\n<p>Hope it will make your work flow easier \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on one of my projects which involved multiple task execution on regular basis. Well, I think that many of us have faced similar issues on their projects which increases our work and making it more time-consuming and increased work. Working in a community where people are actively developing tools to automate tedious [&hellip;]<\/p>\n","protected":false},"author":66,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1185],"tags":[245,1307,1306,55,1124],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11691"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11691"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11691\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}