Grunt:我如何为CSS和JS运行单独的进程

时间:2016-09-27 11:36:25

标签: gruntjs grunt-contrib-watch grunt-concurrent

正如你所看到的,我有CSS的任务[' sass:main']和JS [' jshint:main',' concat:main', ' uglify:main'],但我想为单独的文件(JS和CSS)执行单独的任务并监听更改(监视)。有人能指出我正确的方向,我不确定我应该寻找什么。这是手表可以处理的东西,还是有其他插件?我对grunt有点新意,所以仍然想弄清楚如何使用它。感谢

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']);
};

当我尝试运行任务时:

$ 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 个答案:

答案 0 :(得分:0)

听起来你想要执行两个并发监视任务。你可以使用这样的配置来做到这一点:

    ...

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

...

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

请注意,logConcurrentOutput默认为false,因此,如果您希望将输出记录到控制台,则需要将其设置为true