我是Grunt的新手,我正尝试使用grunt-bower-concat节点模块将所有的bower组件分别连接到单个js文件和css文件中。它工作的很好,除了我想强制grunt-bower-concat使用我的bower组件的缩小版本而不是未压缩的版本。在Gruntfile中使用JavaScript库
幸运的是,它带有一个callback feature在那里我可以自定义此:
callback: function(mainFiles, component) {
return _.map(mainFiles, function(filepath) {
// Use minified files if available
var min = filepath.replace(/\.js$/, '.min.js');
return grunt.file.exists(min) ? min : filepath;
});
}
我把它添加到我的Gruntfile:
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
bower_concat: {
all: {
dest: "src/js/<%= pkg.name %>-bower.js",
cssDest: "src/css/<%= pkg.name %>-bower.css",
callback: function(mainFiles) {
return _.map(mainFiles, function(filepath) {
var min = filepath.replace(/\.js$/, '.min.js');
return grunt.file.exists(min) ? min : filepath;
});
}
}
},
...
它失败,出现以下错误:
$ /usr/local/bin/grunt --gruntfile /Applications/MAMP/htdocs/proj/Gruntfile.js bower_concat
Running "bower_concat:all" (bower_concat) task
Fatal error: _ is not defined
Process finished with exit code 3
这个例子试图使用underscore's map function它的c学习Grunt无权访问此库。
如何加载下划线或在我的Gruntfile中使用它的函数?
这是它。谢谢。 – mykisscool 2014-11-23 16:40:09