如何在grunt initConfig函数中多次运行任务

时间:2014-11-18 10:25:18

标签: javascript gruntjs

我在我的grunt构建开始时运行grunt-contrib-clean任务来清除目标目录。我还想在最后运行另一个干净的任务来做一些工作。

module.exports = function (grunt) {

grunt.initConfig({
    clean: {
        options: { force: true },
        all: {
            //..
        }
    },

    //Other tasks

    clean2: {
        options: { force: true },
        all: {
            //..
        }
    }
});

grunt.registerTask('default', ['']);
grunt.registerTask('build', ['clean', 'clean2']);

grunt.loadNpmTasks('grunt-contrib-clean');
};

如何使用不同的参数两次调用clean任务?

1 个答案:

答案 0 :(得分:3)

你已经非常接近了。查看grunt-contrib-clean的文档,看来你应该看看他们的"long" usage example

我相信您只需将clean配置为:

即可
clean: {
    target: {
        src: "path/to/target",
        options: { force: true },
        all: {
            //..
        }
    },
    otherFolder: {
        src: "path/to/other/folder",
        options: { force: true },
        all: {
            //..
        }
    }
}

然后,您可以通过引用您想要的clean来注册任务:

grunt.registerTask('build', ['clean:target', 'clean:otherFolder']);

作为旁注:我会仔细检查你是否真的需要使用force选项。 Per the documentation谨慎使用