Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate grunt.js tasks for development/production environments

I would really love to be able to have a development grunt file and using the same file an production version of the script.

I have tried the suggestion on SO but my script will just fail when trying to call a dev/prod argument. I believe that the answer is for an older version of grunt, or maybe the plugins I am using.

module.exports = function (grunt) {

    // load all grunt tasks
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        compass: {
            dev: {
                options: {
                    config: 'config.rb',
                    force: true,
                    livereload: true
                }
            }
        },
        uglify: {
            build: {
                src: ['docroot/js/*.js', 'docroot/components/**/*.js'],
                dest: 'docroot/dis/main.min.js'
            }
        },
        watch: {
            options: {
                dateFormat: function(time) {
                    grunt.log.writeln('The watch finished in ' + time + 'ms at' + (new Date()).toString());
                    grunt.log.writeln('Waiting for more changes...');
                },
                livereload: true
            },
            sass: {
                files: ['docroot/sass/*.scss'],
                tasks: ['compass:dev']
            },
            /* watch and see if our javascript files change, or new packages are installed */
            js: {
                files: '<%= uglify.build.src %>',
                tasks: ['uglify']
            },
            /* watch our files for change, reload */
            livereload: {
                files: ['*.html', 'docroot/css/*.css', 'docroot/img/*', 'docroot/js/{main.min.js, plugins.min.js}'],
                options: {
                    livereload: true
                }
            }
        }
    });


    grunt.registerTask('default', 'watch');
};

Really, so long as I can get two version running by calling them with, for example:

 grunt //local
 grunt prod //live

then I can play around with the scripts and what to load.

like image 398
Jamie Hutber Avatar asked Aug 27 '13 22:08

Jamie Hutber


People also ask

What are the examples of tasks in Grunt?

Tasks are grunt's bread and butter. The stuff you do most often, like jshint or nodeunit . Every time Grunt is run, you specify one or more tasks to run, which tells Grunt what you'd like it to do. If you don't specify a task, but a task named "default" has been defined, that task will run (unsurprisingly) by default.

Is Grunt deprecated?

grunt. util. _ is deprecated and we highly encourage you to npm install lodash and var _ = require('lodash') to use lodash .

Is Grunt still used?

So task runners like Grunt and Gulp still have their place and we still use both here at Delicious Brains as build tools for different products we develop.

What is Gruntfile JS used for?

Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile). Grunt was created by Ben Alman and is written in Node.


1 Answers

You can also just register a task that calls an array of tasks

grunt.registerTask('prod', ['tasks1','task2']); 

before your default task, that would be

$ grunt prod
like image 162
Dylan Avatar answered Sep 27 '22 02:09

Dylan