如何运行不同的sass子任务取决于观看的文件已更改?

时间:2014-06-27 12:31:37

标签: sass gruntjs grunt-contrib-watch

有没有办法观察不同的文件集并根据哪个集合更改运行不同的任务?

我问的原因是因为我正在与不同国家(英国,美国,法国,德国)的几个不同的网站合作。我的核心sass是在一个英国文件夹中,但是有单独的法语,德语和美国文件夹,其中包括对核心css的sass覆盖。

我的grunt文件看起来像这样:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            files: ['../style/v4/sass/**/*.scss', '../../US/style/v4/sass/**/*.scss', '../../France/style/v4/sass/**/*.scss', '../../Germany/style/v4/sass/**/*.scss'],
            tasks: ['sass:uk'],
            options: {
                spawn: false
            },
        },
        sass: {
            uk: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../style/v4/css/screen.css': '../style/v4/sass/screen.scss'}
            },
            us: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../../US/style/v4/css/screen.css': '../../US/style/v4/sass/screen.scss'}
            },
            fr: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../../France/style/v4/css/screen.css': '../../France/style/v4/sass/screen.scss'}
            },
            de: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../../Germany/style/v4/css/screen.css': '../../Germany/style/v4/sass/screen.scss'}
            }
        }
    });

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

    grunt.registerTask('default', ['watch']);

};

正如您所看到的,在任何英国核心文件或法国,德国,美国文件发生更改时,他们只需运行 sass:uk 编译任务。

我想要发生的是如果uk文件发生变化,那么运行所有sass子任务来编译scss。如果法国scss文件发生变化,则只运行 sass:fr 任务,如果美国scss文件更改只运行 sass:us 任务,依此类推。

我如何构建我的gruntfile来实现这个目标?

1 个答案:

答案 0 :(得分:1)

万一有人遇到同样的问题,我通过为不同国家设置个人观看任务来解决这个问题。非常明显:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            uk: {
                files: ['../style/v4/sass/**/*.scss'],
                tasks: ['sass'],
                options: {
                    spawn: false
                }
            },
            us: {
                files: ['../../US/style/v4/sass/**/*.scss'],
                tasks: ['sass:us'],
                options: {
                    spawn: false
                }
            },
            fr: {
                files: ['../../France/style/v4/sass/**/*.scss'],
                tasks: ['sass:fr'],
                options: {
                    spawn: false
                }
            },
            de: {
                files: ['../../Germany/style/v4/sass/**/*.scss'],
                tasks: ['sass:de'],
                options: {
                    spawn: false
                }
            }
        },
        sass: {
            uk: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../style/v4/css/screen.css': '../style/v4/sass/screen.scss'}
            },
            us: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../../US/style/v4/css/screen.css': '../../US/style/v4/sass/screen.scss'}
            },
            fr: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../../France/style/v4/css/screen.css': '../../France/style/v4/sass/screen.scss'}
            },
            de: {
                options: {
                    style: 'compressed',
                    sourcemap: true,
                    compass: true
                },
                files: {'../../Germany/style/v4/css/screen.css': '../../Germany/style/v4/sass/screen.scss'}
            }
        }
    });

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

    grunt.registerTask('default', ['watch']);

};