标签:
grunt已经扯了七篇了,殊为不易。最后一篇扯点早应该提及的东西,就是module.exports = function(grunt) {}传入的这个grunt。之前的代码grunt一般只出现在Gruntfile.js这几个地方。
require(‘load-grunt-tasks‘)(grunt); require(‘time-grunt‘)(grunt); grunt.initConfig({ ... }); grunt.registerTask(‘default‘, [...]);
但grunt的内容远远不止这些,它包含9个对象属性,而上面的initConfig、registerTask不过是某些属性对象的方法,是为了方便用户调用所取的别名而已。
9个对象将分别进行介绍。
‘use strict‘; module.exports = function(grunt) { require(‘load-grunt-tasks‘)(grunt); require(‘time-grunt‘)(grunt); var path = { src: ‘src‘, dest: ‘dest‘ } grunt.config.init({ path: path, copy: { test: { files: [ { expand: true, cwd: ‘<%=path.src%>/‘, src: ‘{,*/}*‘, dest: ‘<%=path.dest%>‘ } ] } } }); grunt.config(‘clean‘, { test: ‘<%=path.dest%>‘ }); console.log(grunt.config(‘path‘).dest); //output: dest console.log(grunt.config.process(‘<%=path.dest%>‘)); //output: dest console.log(grunt.config.get(‘clean‘).test); //output: dest console.log(grunt.config.getRaw(‘clean‘).test); //output: <%=path.dest%> grunt.registerTask(‘default‘, [‘clean‘, ‘copy‘]); }
‘use strict‘; module.exports = function(grunt) { function foron(count) { console.log(‘test for off ‘ + count); } grunt.event.on(‘test‘, foron); grunt.event.on(‘test‘, function(count) { console.log(‘test for on ‘ + count); }); grunt.event.once(‘test‘, function(count) { console.log(‘test for once ‘ + count); }); grunt.event.many(‘test‘, 2, function(count) { console.log(‘test for many ‘ + count); }); grunt.event.emit(‘test‘, 1); grunt.event.emit(‘test‘, 2); grunt.event.emit(‘test‘, 3); grunt.event.off(‘test‘, foron); grunt.event.emit(‘test‘, 4); grunt.event.removeAllListeners(); grunt.event.emit(‘test‘, 5); grunt.registerTask(‘default‘, []); }
输出如下:
test for off 1 test for on 1 test for once 1 test for many 1 test for off 2 test for on 2 test for many 2 test for off 3 test for on 3 test for on 4
[ ‘src‘, ‘src/css‘, ‘src/css/base.css‘, ‘src/css/main.css‘, ‘src/image‘, ‘src/image/haiys02.jpg‘, ‘src/index.html‘, ‘src/js‘, ‘src/js/hellogrunt.js‘, ‘src/js/helloworld.js‘ ]
grunt.file.expandMapping(patterns, dest[, options]): 返回patterns匹配路径和原路径的键值对,比如grunt.file.expandMapping(‘**‘, ‘dest‘, {cwd: ‘src‘})返回
[ { src: [ ‘src‘ ], dest: ‘dest‘ }, { src: [ ‘src/css‘ ], dest: ‘dest/css‘ }, { src: [ ‘src/css/base.css‘ ], dest: ‘dest/css/base.css‘ }, { src: [ ‘src/css/main.css‘ ], dest: ‘dest/css/main.css‘ }, { src: [ ‘src/image‘ ], dest: ‘dest/image‘ }, { src: [ ‘src/image/haiys02.jpg‘ ], dest: ‘dest/image/haiys02.jpg‘ }, { src: [ ‘src/index.html‘ ], dest: ‘dest/index.html‘ }, { src: [ ‘src/js‘ ], dest: ‘dest/js‘ }, { src: [ ‘src/js/hellogrunt.js‘ ], dest: ‘dest/js/hellogrunt.js‘ }, { src: [ ‘src/js/helloworld.js‘ ], dest: ‘dest/js/helloworld.js‘ } ]
grunt.file.match([options, ]patterns, filepaths): 返回filepaths中匹配patterns的路径
grunt.file.isMatch([options, ]patterns, filepaths): filepaths中一旦有path命中即返回true
grunt.file.exists(path1[, path2...]): 返回paths是否存在
grunt.file.isLink/Dir/File(path1[, path2...]): 返回paths是否为链接、文件夹、文件
grunt.file.isPathAbsolute(path1[, path2...]): 返回paths是否为绝对路径
grunt.file.arePathsEquivalent(path1[, path2...]): 返回paths是否相同
grunt.file.doesPathContain(dir, path1[, path2...]): 返回paths是否在dir中
grunt.file.isPathCwd(path1[, path2...]): 返回paths是否为cwd
grunt.file.isPathInCwd(path1[, path2...]): 返回paths是否在cwd中,cwd不在cwd中
grunt.file.setBase(path1[, path2...]): 设置当前的cwd
‘use strict‘; module.exports = function(grunt) { require(‘time-grunt‘)(grunt); grunt.initConfig({ log: { foo: [1, 2, 3], bar: ‘hello world‘, baz: { options: { bool: false } } } }); grunt.task.registerMultiTask(‘log‘, ‘Log stuff.‘, function() { grunt.log.writeln(this.target + ‘: ‘ + this.data); }); grunt.registerTask(‘default‘, [‘log‘]); }
输出如下:
Running "log:foo" (log) task foo: 1,2,3 Running "log:bar" (log) task bar: hello world Running "log:baz" (log) task baz: [object Object]
grunt.task.renameTask: 重命名task type,原task type将不可使用
grunt.task.loadTasks: grunt.loadTasks的真身,指定需要加载的文件的目录
grunt.task.loadNpmTasks: grunt.loadNpmTasks的真身,指定需要加载的plugin
以下语句效果相同
grunt.loadTasks(‘node_modules/grunt-contrib-clean/tasks/‘);
grunt.loadNpmTasks(‘grunt-contrib-clean‘);
grunt.task.run(tasksList): 执行任务队列
grunt.task.clearQueue(): 清空任务队列
grunt.task.normalizeMultiTaskFiles(): 将target将要处理的文件以src-dest-orig对象数组的形式序列化,以config中Gruntfile.js为例,添加代码
console.log(grunt.task.normalizeMultiTaskFiles(grunt.config.process(‘<%=copy.test%>‘)));
打印出的内容如下:
[ { src: [ ‘src/css‘ ], dest: ‘dest/css‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/css/base.css‘ ], dest: ‘dest/css/base.css‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/css/main.css‘ ], dest: ‘dest/css/main.css‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/image‘ ], dest: ‘dest/image‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/image/haiys02.jpg‘ ], dest: ‘dest/image/haiys02.jpg‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/index.html‘ ], dest: ‘dest/index.html‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/js‘ ], dest: ‘dest/js‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/js/hellogrunt.js‘ ], dest: ‘dest/js/hellogrunt.js‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } }, { src: [ ‘src/js/helloworld.js‘ ], dest: ‘dest/js/helloworld.js‘, orig: { expand: true, cwd: ‘src/‘, src: [Object], dest: ‘dest‘ } } ]
var arr = [1, 2, 3], result = grunt.util.recurse(arr, function(value) { console.log(‘value ‘ + value); return value * value; }, function(value) { if(value % 2 === 0) return false; }); console.log(‘result ‘ + result);
输出:
value 1 value 3 result 1,2,9
function add1(a, b) { return a + b; } function add2(a, b, callback) { callback(a + b); } var fn1 = grunt.util.callbackify(add1); var fn2 = grunt.util.callbackify(add2); var result1 = fn1(1, 2, function(result) { console.log(‘1 plus 2 equals ‘ + result); }); var result2 = fn2(1, 2, function(result) { console.log(‘1 plus 2 equals ‘ + result); }); console.log(result1); console.log(result2);
输出:
1 plus 2 equals 3
1 plus 2 equals 3
undefined
undefined
好了,这大体就是整个grunt的api介绍了,现在写一个比较完善和优美的Gruntfile.js应该不是什么问题了吧。
以后有机会可能会补充些编写grunt plugin的内容,现在就告一段落了。
标签:
原文地址:http://www.cnblogs.com/tarol/p/4256475.html