grunt.config
存取在 Gruntfile
中定義的專案特定設定資料。
請注意,任何標示為 ☃(unicode 雪人)的方法也可以直接在 grunt
物件上使用,而任何標示為 ☆(白色星星)的方法也可以在 this
物件上的任務中使用。只要您知道即可。
初始化設定資料
請注意,以下方法也可以在 grunt
物件上使用,為 grunt.initConfig
。
grunt.config.init ☃
初始化目前專案的設定物件。指定的 configObject
由任務使用,而且可以使用 grunt.config
方法存取。幾乎每個專案的 Gruntfile
都會呼叫此方法。
grunt.config.init(configObject)
請注意,任何指定的 <% %>
範本字串會在擷取設定資料時處理。
此範例包含 grunt-contrib-jshint 外掛程式 jshint
任務的範例設定資料
grunt.config.init({
jshint: {
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js']
}
});
請參閱 開始使用 指南,取得更多設定範例。
此方法也可以用作 grunt.initConfig
。
存取設定資料
以下方法允許透過點分隔字串(例如 'pkg.author.name'
)或屬性名稱部分陣列(例如 ['pkg', 'author', 'name']
)存取 Grunt 設定資料。
請注意,如果指定的屬性名稱包含 .
點,則必須以反斜線字面值跳脫,例如 'concat.dist/built\\.js'
。如果指定部分陣列,Grunt 會使用 grunt.config.escape
方法在內部處理跳脫。
grunt.config
取得或設定專案 Grunt 設定中的值。此方法用作其他方法的別名;如果傳遞兩個引數,則會呼叫 grunt.config.set
,否則會呼叫 grunt.config.get
。
grunt.config([prop [, value]])
grunt.config.get
取得專案 Grunt 組態中的值。如果指定了 prop
,則會傳回該屬性的值,如果未定義該屬性,則傳回 null
。如果未指定 prop
,則會傳回整個組態物件的副本。範本字串會使用 grunt.config.process
方法遞迴處理。
grunt.config.get([prop])
grunt.config.process
處理值,在 Grunt 組態的內容中遞迴展開 <% %>
範本(透過 grunt.template.process
方法),當遇到時會這樣做。此方法會由 grunt.config.get
自動呼叫,但 不會 由 grunt.config.getRaw
呼叫。
grunt.config.process(value)
如果任何擷取的值完全是單一 '<%= foo %>'
或 '<%= foo.bar %>'
範本字串,且指定的 foo
或 foo.bar
屬性是非字串(且不是 null
或 undefined
)值,則會展開為實際值。這與 grunt 的工作系統自動扁平化陣列結合使用,可能會非常有用。
grunt.config.getRaw
從專案的 Grunt 組態取得原始值,不會處理 <% %>
範本字串。如果指定了 prop
,則會傳回該屬性的值,如果未定義該屬性,則傳回 null
。如果未指定 prop
,則會傳回整個組態物件的副本。
grunt.config.getRaw([prop])
grunt.config.set
在專案的 Grunt 組態中設定值。
grunt.config.set(prop, value)
請注意,任何指定的 <% %>
範本字串只會在擷取組態資料時處理。
grunt.config.escape
在指定的 propString
中跳脫 .
點。這應使用於包含點的屬性名稱。
grunt.config.escape(propString)
grunt.config.merge
在 0.4.5 中新增
將指定 configObject
的屬性遞迴合併到目前的專案組態中。陣列和純物件屬性會遞迴合併,而其他值類型會覆寫。
grunt.config.merge(configObject)
你可以使用此方法來附加組態選項、目標等,例如已定義的工作,
grunt.config.merge({
watch: {
files: ["path/to/files"],
tasks: ["task"]
}
});
陣列值會根據其索引合併。請考慮以下程式碼
grunt.initConfig({
jshint: {
files: ['Gruntfile.js', 'src/**/*.js'],
}
);
var config = {
jshint: {
files: ['hello.js'],
}
};
grunt.config.merge(config);
它將產生以下所示的組態
jshint: {
files: ['hello.js', 'src/**/*.js'],
}
結論是,在 config
變數中定義的 files
陣列的第一個值 (hello.js
) 會覆寫在 initConfig
組態呼叫中指定的第一個值 (Gruntfile.js
)。
需要組態資料
請注意,以下列出的方法也可在 this
物件上的工作中使用,作為 this.requiresConfig
。
grunt.config.requires ☆
如果缺少一個或多個必要的組態屬性,或其為 null
或 undefined
,則會導致目前的工作失敗。可以指定一個或多個字串或陣列組態屬性。
grunt.config.requires(prop [, prop [, ...]])
此方法也可在工作中使用,作為 this.requiresConfig
。