Grunt: How do I run seperate processes for CSS and JS

90 Views Asked by At

As you can see I have tasks for CSS ['sass:main'] and for JS ['jshint:main', 'concat:main', 'uglify:main'], but I want to do separate tasks for separate files (JS and CSS) and listen for changes (watch). Can someone point me in the correct direction, I'm not really sure what I should be searching for. Is this something that watch can handle, or is there another plugin? I'm a little new to grunt so still trying to figure out how to use it. Thanks

GruntFile.js:

module.exports = function(grunt) {
    var config = {
        pkg: grunt.file.readJSON('package.json'),
        jshint: {
            options: {
                globals: {
                    jQuery: true,
                    console: true,
                    module: true,
                    document: true
                }
            },  
            main: {
                src: [
                    'assets/templates/main/js/crm/*.js',
                ]
            }
        },
        concat: {
            options: {
                separator: '\n\n'
            },
            main: {
                src: [
                    'assets/templates/main/js/crm/*.js',
                ],
                dest: 'assets/templates/main/js/crm.min.js'
            }
        },
        sass: {
            options: {
                style: 'compressed'
            },
            main: {
                files: {
                    'assets/templates/main/css/main.min.css': 'assets/templates/main/sass/main.scss',
                }
            }
        },
        uglify: {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            main: {
                src: 'assets/templates/main/js/crm.min.js',
                dest: 'assets/templates/main/js/crm.min.js'
            }
        },
        watch: {
            mainjs: {
                files: ['assets/templates/main/js/crm/*.js'],
                tasks: ['jshint:main', 'concat:main', 'uglify:main'],
            },
            mainsass: {
                files: ['assets/templates/main/sass/*.scss''],
                tasks: ['sass:main'],
            }
        },
        concurrent: {
            maincss: ['sass:main'],
            mainjs:  ['jshint:main', 'concat:main', 'uglify:main']
        }
    };

    grunt.initConfig(config);

    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-concurrent');

    grunt.registerTask('main', ['jshint:main', 'concat:main', 'uglify:main', 'sass:main']);
    grunt.registerTask('main-watch', ['jshint:main', 'concat:main', 'uglify:main', 'sass:main', 'concurrent:mainsass']);
};

When I try run tasks:

$ grunt main-watch
Loading "Gruntfile.js" tasks...ERROR
SyntaxError: Invalid or unexpected token
Warning: Task "main-watch" not found. Use --force to continue.

Aborted due to warnings.
1

There are 1 best solutions below

0
On

It sounds like you want to perform two concurrent watch tasks. You can do that using a configuration like this:

    ...

    concurrent: {
        options: { logConcurrentOutput: true },
        watch: ['watch:mainjs', 'watch:mainsass']
    }
};

...

grunt.registerTask('main-watch', ['concurrent:watch']);

Note that logConcurrentOutput defaults to false, so if you want to see output logged to the console, you need to set it to true.