grunt已經扯了七篇了,殊爲不易。最後一篇扯點早應該說起的東西,就是module.exports = function(grunt) {}傳入的這個grunt。以前的代碼grunt通常只出如今Gruntfile.js這幾個地方。css
require('load-grunt-tasks')(grunt); require('time-grunt')(grunt); grunt.initConfig({ ... }); grunt.registerTask('default', [...]);
但grunt的內容遠遠不止這些,它包含9個對象屬性,而上面的initConfig、registerTask不過是某些屬性對象的方法,是爲了方便用戶調用所取的別名而已。html
9個對象將分別進行介紹。node
'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', []); }
輸出以下:json
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'})返回
api
[ { 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']); }
輸出以下:app
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%>')));
打印出的內容以下:grunt
[ { 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的內容,如今就告一段落了。