创建多个grunt任务

时间:2017-06-13 10:53:24

标签: javascript sass gruntjs grunt-contrib-watch

watch: {
    css: {
        files: 'source/styles/**/*.scss',
        tasks: ['sass', 'autoprefixer', 'cssmin', 'clean:css'],
        options: {
            spawn: false,
        },
    },
    images: {
        files: 'source/assets/images/**/*.{png,jpg,gif}',
        tasks: ['clean:image', 'imagemin'],
        options: {
            spawn: false,
        },
    }
}



grunt.registerTask('css', [
    'sass',
    'cssmin',
    'clean:css'
]);



sass: {
    dist: {
        files: [{
            expand: true,
            src: ['source/styles/**/*.scss'],
            dest: 'build/styles/',
            ext: '.css',
            flatten: true
        }]
    },
    options: {
        compass: true,
        sourcemap: false
    }
}


grunt.registerTask('default', [
    'sass',
    'cssmin',
    'clean:css',
    'requirejs',
    'clean:js',
]);

我想创建一个新的gurnt任务。 Currenly,在我的项目中,grunt css可以很好地构建complate packae SCSS。 我需要创建一个像grunt home这样的新任务,它将构建一个特定的文件夹SCSS。

如何创建grunt home

2 个答案:

答案 0 :(得分:0)

我认为这应该可行,但我没有测试它(它会将源/样式/ SCSS中的SCSS文件编译为build / styles / css中的css):

homeSass: {
    dist: {
        files: [{
            expand: true,
            src: ['source/styles/SCSS/**/*.scss'],
            dest: 'build/styles/css/',
            ext: '.css',
            flatten: true
        }]
    },
    options: {
        compass: true,
        sourcemap: false
    }
}

grunt.registerTask('homebuild', [
    'homeSass'
]);

你在寻找吗?或者我误解了你?

答案 1 :(得分:0)

所以像这样你可以创建多个任务

sass2: {
    dist: {
        files: [{
            expand: true,
            src: ['source/styles/**/*.scss'],//path to different source file
            dest: 'build/styles/',//path to different destination for home
            ext: '.css',
            flatten: true
        }]
    },
    options: {
        compass: true,
        sourcemap: false
    }
}

grunt.registerTask('home', [
    'sass2'
]);

现在运行这个你有2种方法

1)简单地说grunt home 这只会运行特定于sass2的sass编译

2)现在,如果你想在1中同时运行任务grunt cssgrunt home,那么就这样做

sass: {
    dist: {
        files: [{
            expand: true,
            src: ['source/styles/**/src1.scss', 'source/styles/**/src2.scss'],
            dest: 'build/styles/',
            ext: '.css',
            flatten: true
        }]
    },
    options: {
        compass: true,
        sourcemap: false
    }
}

grunt.registerTask('home', [
    'sass'
]);