簡體   English   中英

在一個grunt插件中,如何獲得數組的全局和目標specifc選項的組合?

[英]In a grunt plugin, how do I get the combination of a global and target specifc option that's an array?

我正在寫一個grunt插件,它的選項可以是值數組。 具體來說,這些值是文件(與任務本身的files屬性中指定的文件不同)。 我的任務設置如下所示:

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']
        }
      },
    }

});

在我的插件中,我希望能夠獲得data選項以及在global選項和target選項中指定的所有文件的列表。

對於dev目標grunt assemble:dev我會在this.options.data看到

['test/common/data/common1.json',
 'test/common/data/common2.json',
 'test/dev/data/dev.json']

對於prod目標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以查看所有代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM