我的 Gruntfile 重复了 "files"
,在相同任务的两个目标 dist
和 dev
之间共享。这是一个仅包含 Stylus 问题的示例:
"use strict";
module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-contrib-stylus");
grunt.initConfig({
stylus: {
dist: {
files: { "www/bundle.css": ["stylus/*.styl"] },
options: { compress: true, linenos: false }
},
dev: {
files: { "www/bundle.css": ["stylus/*.styl"] },
options: { compress: false, linenos: true }
}
}
});
grunt.registerTask("dev", ["stylus:dev"]);
grunt.registerTask("prod", ["stylus:prod"]);
};
有没有办法将文件配置提升一个级别,这样我就不必在两个目标中重复它?
最佳答案
Domenic,您可以使用 POJS 变量:
"use strict";
module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-contrib-stylus");
var stylusFiles = { "www/bundle.css": ["stylus/*.styl"] };
grunt.initConfig({
stylus: {
dist: {
files: stylusFiles,
options: { compress: true, linenos: false }
},
dev: {
files: stylusFiles,
options: { compress: false, linenos: true }
}
}
});
grunt.registerTask("dev", ["stylus:dev"]);
grunt.registerTask("prod", ["stylus:prod"]);
};
或者您可以使用 Grunt "Configuring Tasks" Guide 中的模板.
"use strict";
module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-contrib-stylus");
grunt.initConfig({
stylus: {
dist: {
files: { "www/bundle.css": ["stylus/*.styl"] },
options: { compress: true, linenos: false }
},
dev: {
files: "<%= stylus.dist.files %>",
options: { compress: false, linenos: true }
}
}
});
grunt.registerTask("dev", ["stylus:dev"]);
grunt.registerTask("prod", ["stylus:prod"]);
};
https://stackoverflow.com/questions/17520015/