Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use gruntjs as JavaScript developer

Tags:

gruntjs

First of all i have not really understood what gruntjs really does, but i have some idea. I am hoping that by seeing how and for what it is used i will come to see its purpose. So could anyone explain to me what is gruntjs, why it's used, and for what it's used.

  • Is it beneficial for indie developer or for team or both?

  • Is it only for big projects?

  • Is it just a trend/fad? And makes things uncomplicated for no reason?

So basically in short What are benefits of it and for whom?

like image 663
Muhammad Umer Avatar asked Mar 15 '14 18:03

Muhammad Umer


2 Answers

Grunt is a task runner. That's all it does. If you're a Java guy, think Ant.

Developers use Grunt to automate the running of tasks, especially sequences of tasks. Some of those tasks are so prevalent, such as linting JavaScript, running unit tests, or minifying JavaScript, that they're packaged up as plugins and freely available for you to use. For example, grunt-contrib-clean is a plugin that contains a clean task. This task simply deletes the contents of a list of directories, a common step in a build process. To use it, you first pull the plugin into you Gruntfile.js using

grunt.loadNpmTasks('grunt-contrib-clean');

and then configure the clean task to clear your hypothetical minified directory using

grunt.initConfig({
  clean: [ 'minified' ]
});

You can now run the clean task from the command line using

grunt clean

To visualise its potential, imagine a task that cleans a directory, then runs Jasmine unit tests using Karma, then lints and compiles LESS files, minifies and concatenates JS files, and packages them up for deployment, or outright deploys them.

So to answer your questions

  • it can be beneficial to anyone working on the project
  • the benefit is proportional to how many repetitive tasks you have to deal with
  • it's a tool, not a trend/fad, and it simplifies processes, not overcomplicates them
like image 117
Emerson Farrugia Avatar answered Oct 03 '22 06:10

Emerson Farrugia


want to do pre required task before running project then grunt is best. Means task required for running project

In grunt we can use add task .Common task like

browserify : In our project we crate multiple file by name convention for better understanding. But when we want to run the project you have to include all files rather than you can just combine all file in one file at the time for deploy project it will reduces your server time to include all files

just like include in Gruntfile.js

       uglify:{
        app: {
         src: ['app/**/*.js'],//include all js file in app folder
         dest: 'public/site.js'//one file
        }
       }
      grunt.loadNpmTasks('grunt-browserify');//use npm task for browserify 

Register Task : In project we use diff environment.If you want manage what thing to be run before the deploy by each environment.

Just like task required for only Env Dev watch task

grunt.loadNpmTasks('grunt-contrib-watch');

For this you register task dev

 env: {
   development: { NODE_ENV: 'development' },
   staging: { NODE_ENV: 'staging' },
   production: { NODE_ENV: 'production' }
 }
grunt.registerTask('dev',['env:development','watch']);//list of task required only for  dev env

grunt.registerTask('production',['env:production']);
like image 28
Sunil More Avatar answered Oct 03 '22 06:10

Sunil More