我正在编写一个 grunt 插件,它的选项可以是值数组。这些值特别是文件(不同于任务本身的文件属性中指定的文件)。我的任务设置如下所示:
grunt.initConfig({
assemble: {
options: {
data: ['test/common/data/common1.json', 'test/common/data/common2.json']
},
dev: {
options: {
data: ['test/dev/data/dev.json']
},
files: {
'test/actual': ['test/files/dev.hbs']
}
},
prod: {
options: {
data: ['test/prod/data/prod.json']
},
files: {
'test/actual': ['test/files/prod.hbs']
}
},
}
});
在我的插件中,我希望能够获得数据选项,其中包含在全局选项和目标选项中指定的所有文件的列表。
对于开发目标 grunt assemble:dev
我会在 this.options.data
中看到这个
['test/common/data/common1.json',
'test/common/data/common2.json',
'test/dev/data/dev.json']
对于产品目标 grunt assemble:prod
我会在 this.options.data
中看到这个
['test/common/data/common1.json',
'test/common/data/common2.json',
'test/prod/data/prod.json']
最佳答案
我找到了解决这个问题的方法,但我不确定这是否是最佳选择。
在我的插件中,我可以通过 grunt.config
方法访问全局选项和目标特定选项。
var globalDataFiles = grunt.config(['assemble', 'options', 'data']) || [];
var targetDataFiles = grunt.config(['assemble', this.target, 'options', 'data']) || [];
使用 lodash...var _ = require('lodash');
我可以联合数组:
var data = _.union(globalDataFiles, targetDataFiles);
我在我的插件中做了更多的事情,但这就是我最初解决这个问题的方式。
请看https://github.com/assemble/assemble/blob/master/tasks/assemble.js查看所有代码。
关于javascript - 在 grunt 插件中,如何获得作为数组的全局选项和目标特定选项的组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15583038/