Getting started with Grunt

Published July 2, 2013

Grunt is a way to automate many of the tasks you’re doing when you develop. Are you still minifying, compiling, concatenating, compressing, prefixing all your code manually? Nonsense poopy pants! In this tutorial I will show you how Grunt will do all this for you, and then some!

Why use a task runner? In one word: automation. The less work you have to do when performing repetitive tasks like minification, compilation, unit testing, linting, etc, the easier your job becomes.

- Grunt.js

The basic idea

The most basic Grunt setup consists of two required files: Gruntfile.js and package.json. After you’ve set up the basic structure, you customize Grunt to fit your needs with the help of plugins. A plugin can help you concatenate all your JS-files, another plugin might run your JS-code through JSHint. Here’s a full list of Grunt plugins.

Package.json is the file where you store npm metadata about the project (name, version, homepage, file dependencies, directories). It’s in this file you list all of your Grunt plugins that you are going to use.

The Gruntfile.js is where the magic happens. In here, you’ll setup all your Grunt plugins and what tasks that should be available. Grunt tasks are a way to separate the usage of plugins. You might want to run some plugins during the development phase of a project, and some when you’re going push the code to production.

Installing Grunt

First of all, you need Node.js, which you can install here. After that, you install the Grunt CLI with this command:

npm install -g grunt-cli

That’s it, you’re all set! The next step is to configure your package.json file.

Configuring your package.json

Your package.json and Gruntfile.js should always be on your projects root folder. This is a basic structure for the package.json file:

The neat thing about devDependencies is that you can easily install all the plugins with just one command: npm install, that really comes in handy if someone else starts on your project.

Setting up Gruntfile.js

Aww yiss, here’s where the fun start. The structure of a Gruntfile.js consists of 3 parts.
Project configuration, loading plugins and task registering. Below is a Gruntfile for the package.json file we created earlier. The SASS plugin below, called grunt-contrib-sass will compile our SASS into CSS, with a few options like if it should be compressed or expanded etc.

Alrighty. Now we have set up a really, really simple Grunt plugin that compiles our SASS file. We’ve also created two different tasks; development will compile our SASS into a .css file that’s expanded. Our production task will do the same thing, but since we’re good developers, we will compress the outputted CSS.

But wait. Why should I use this when Codekit, Livereload, Crunch, Compass.app, Tamagotchi and your microwave can compile SASS just as good? Because Grunt can do so much more than just compiling good ‘ol boring SASS.

Making Grunt awesome

Time to do what Grunt does best, a silly amount of things super automatically. There’s oodles and oodles of plugins that can help you in your project, and to install them you just follow the above Gruntfile.js example and customize your own Grunt setup.

Below I’ll list some of my favorite plugins and end with putting them all together.

Compress images

Image compression is all the rage right now, with responsive design and page weight reduction. So why not use a plugin that does that for you? There’s a few plugin that does this for you, but one of the better ones is grunt-contrib-imagemin which compresses both .png and .jpg for you.

The basic setup is simple, for mode advanced configuration, see the documentation

Concatenate

If you’re writing Javascript with a modular pattern, or something else fancy McPantsy. You’ve probably ended up with a bunch of .js files that needs not be concatenated, and sweet baby Jesus that’s a boring task to do. But no fear, grunt-contrib-concat is here!

CSS auto prefixer

Thanks to Netscape not monetizing the web in the 90’s, we have vendor prefixes. And it’s a real headache to keep up with that property needs which prefix. But grunt-autoprefixer takes that off your mind! It’s connected to the database of Can I Use so it’s always updated with the latest data.

The almighty watch plugin

This plugin is what make Grunt go from good to holy guacamole awesome. Grunt-contrib-watch listens to changes in certain files or directories, and when a change is detected it runs a certain task. This makes a great wrapper for the previous plugins mentioned. Let’s do a simple example and combine it with the autoprefixer task we just went through:

So, when change something in our style.css, the watch plugin will detect that automatically and run the autoprefixer’s dist task. You can basically have a watch-task for anything. Added a new image to your image folder? Have watch run the imagemin task we added.

Putting it all together

So, we’ve went through a few good plugins. Let’s combine them all together with a neat watch task.

Our development task is what we’re gonna run during development. When you run the task, it will begin with prefix your CSS, compress your SASS, concatenate the JS-files and finally start the watch task. So you only run this command once, and the watch task takes care of the rest for you.

When we’re done writing our fabulous code and it’s release time, we simply run our production task, which does almost the same thing. In addition to our development task it will also compress the code and the images. And since production means no further development, we don’t need the watch task for that.

Final thoughts

As you can see, Grunt is highly powerful and highly customizable. You’ll probably end up using the same base of plugins through your projects, but don’t forget to go through the full list of plugins. You might find some that fits your current project perfectly. For more information about Grunt, check out the official documentation.

When I initially commented I clicked the “Notify me when new comments are added” checkbox and
now each time a comment is added I get three e-mails with the same comment.
Is there any way you can remove people from that
service? Cheers!

The web has improved a whole lot over the years and it’s now
popular to play with games online. Among the games that
are most popular is to obtain access to quite a few
games and also como tener xbox live gold gratis 2015 live gold gratis, you want to get account that is live.
Xbox live offers an opportunity interact with their buddies and
to take part in several games to gamers. This is
only possible for those who have the correct requirements.
They are normally supplied on the web and also you could get them by your email.
The rules are instantaneous thus you can use the service
when you enter them in your games console.

Sick and tired of utilizing the outdated patterned that was
traditional laptop cases? You’d have noticed hundreds or thousands of people-carrying exactly the same fashion of totes.
At times it becomes not also easy as there is
almost no distinction, to search for your own bag from the others.
You emerge only using care of your bag and would have invested more time if you’re someone who moves quiet frequently.
To alleviate you from that boredom, there are plenty of that is trendy laptop bags that can be found in the industry.
In manufacturing these bags like linen, leather etc. various
substances are used You may select the right that is Trendy
leather messenger bag for yourself as well as get found.
Your cool laptop bags nz may be unique just as you. The totes
can be in truth called by you as laptop guards as they acutely protect your notebook.

Although ordinarily stuffed with more expensive, premium items, company
gift baskets are sometimes a great money-maker presents for men a side line, your gift basket company
or a brand new business. In case you presently have a present-related, create- related
or home based company of all kinds, or if you simply want to set up
a gift basket company, making gift baskets is wise.

packaging additionally determines the appearances of the product and the look which obviously affects individuals as
well as the way they’ll view the goods when producing goods.

If they like the wine packaging then it’s most likely that they are going to purchase the merchandise.
With packaging, products will be substantially in form and that they may be arranged structurally along with produce things easier as it
pertains to delivery and combine.

You are ready to select a house plan for your dream home, once you’ve bought the land that
you want to build on. If you are thinking about purchasing
a pre-made dwelling plan, you may have as your
thoughts may not fit into what’s available to create some little compromises.
Another alternative would be to buy a pre-made house plan and later have it
modified to suit your particular needs and
preferences with an architect. Do know that this is not going to be without a rather considerable expense it may nevertheless be more economical than if you were to
have a plan assembled from scratch.

You could have observed how hard it can be to locate a cheap webbitcoin hosting support should
you be beginning your personal website. As with a lot of
things, it really is not only easy unless you understand that which you do.