Javascript Grunt模板变量和if-else
我的GrunFile中有一些用于dist文件夹的模板变量。我还想在if-else语句中使用它们来调整某些任务的配置 下面是我的GrunFile的简短版本:Javascript Grunt模板变量和if-else,javascript,variables,if-statement,gruntjs,Javascript,Variables,If Statement,Gruntjs,我的GrunFile中有一些用于dist文件夹的模板变量。我还想在if-else语句中使用它们来调整某些任务的配置 下面是我的GrunFile的简短版本: module.exports = function(grunt) { grunt.initConfig({ // Variables path: { develop: 'dev/', output: 'output/' },
module.exports = function(grunt) {
grunt.initConfig({
// Variables
path: {
develop: 'dev/',
output: 'output/'
},
// Clean empty task
cleanempty: {
output: {
src: '<%= path.output %>**/*'
}
},
// Sync
sync: {
output: (function(){
console.log(grunt.config('path.output')); // Returns undefined
if(grunt.config('path.output') === 'output/') {
return {
// Config A
}
} else {
return {
// Config B
}
}
}())
}
module.exports=函数(grunt){
grunt.initConfig({
//变数
路径:{
开发:“dev/”,
输出:'output/'
},
//清理空任务
清空:{
输出:{
src:“***”
}
},
//同步
同步:{
输出:(函数(){
console.log(grunt.config('path.output');//返回未定义的
if(grunt.config('path.output')='output/'){
返回{
//配置A
}
}否则{
返回{
//配置B
}
}
}())
}
不幸的是,我无法让它工作。grunt.config('path.output')返回未定义的。
如何阅读Grunt模板变量?如何找到更好的解决方案,我也想听听。试试看
grunt.config.get('path.output')
变量需要在grunt.initConfig外部声明。然后您需要在grunt.initConfig中引用它 在以下位置找到我的解决方案: 工作样本:
module.exports = function(grunt) {
// Variables
var path = {
develop: 'dev/',
output: 'output/'
};
grunt.initConfig({
path: path, // <-- Important part, do not forget
// Clean empty task
cleanempty: {
output: {
src: '<%= path.output %>**/*'
}
},
// Sync
sync: {
output: (function(){
console.log(path.output); // Returns output/
if(path.output) === 'output/') {
return {
// Config A
}
} else {
return {
// Config B
}
}
}())
}
//...the rest of init config
});
}
module.exports=函数(grunt){
//变数
变量路径={
开发:“dev/”,
输出:'output/'
};
grunt.initConfig({
path:path,//谢谢,但仍然返回未索引。