上一回没有说完,我就是这样,作以前心中波澜壮阔,锦绣山河,等画完小草开始烦躁,完成鲜花出现动摇,而后心神涣散,最后有始无终。html
如今弥补下以前遗漏的问题。node
'use strict'; module.exports = function(grunt) { require('load-grunt-tasks')(grunt); require('time-grunt')(grunt); var path = { src: 'src', dest: 'dest' } grunt.initConfig({ path: path, copy: { test: { files: [ { expand: true, cwd: '<%=path.src%>/', src: '{,*/}*', dest: '<%=path.dest%>/' } ] } }, watch: { test: { tasks: [ 'copy' ], files: '<%=path.src%>/{,*/}*' } } }); grunt.registerTask('default', ['watch']); }
执行grunt后,源文件夹下每有文件的变更,就会将全部文件复制到目标文件夹。git
console.log('task name: ' + (process.argv[2] || 'default'));
process的nodejs中的api,这句话是打印输入的命令的第二个字符串即执行的task。好比你执行grunt default,第二个字符串就是default,执行grunt,第二个字符串就是undefined。
再次执行grunt,打印出来的内容以下github
task name: default Running "watch" task Waiting...
而后修改源文件夹下的内容,接下来显示的内容以下api
>> File "src\index.html" changed. task name: copy Running "copy:test" (copy) task Created 3 directories, copied 6 files Done, without errors. Execution Time (2015-01-27 09:52:52 UTC) loading tasks 3ms ██████ 12% copy:test 22ms █████████████████████████████ ███████████ 85% Total 26ms Completed in 2.309s at Tue Jan 27 2015 17:52:52 GMT+0800 (台北標準時間) - Waitin g...
又打印出了task name这一行,这意味着当设置此option为true时,每次执行watch中设置的task,都至关于在命令行再次输入grunt taskname。咱们把此options设置为false,作一样的操做。数组
task name: default Running "watch" task Waiting... >> File "src\index.html" changed. Running "copy:test" (copy) task Created 3 directories, copied 6 files Running "watch" task Completed in 0.043s at Tue Jan 27 2015 17:56:37 GMT+0800 (台北標準時間) - Waitin g...
没有再次执行输出task name的代码。函数
因此,若是你的Gruntfile.js中读取了输入命令的部分值并保存为变量,请将此option设置为false,否则watch启动的task中将读取不到这些变量。config: { options: { reload: true, spawn: false //按以前spawn的设置将只会打印一次task name }, files: 'Gruntfile.js' }
执行grunt watch,而后修改Gruntfile.js,打印出来的内容grunt
task name: watch Running "watch" task Waiting... Reloading watch config... task name: watch Running "watch" task Waiting... >> File "Gruntfile.js" changed. Running "watch" task Completed in 0.017s at Tue Jan 27 2015 18:25:27 GMT+0800 (台北標準時間) - Waitin g...
可见无视了spawn的设定,在本进程从新执行了原来的命令。ui
至此经常使用的plugin基本介绍结束,固然还有jshint、htmlmin、imagemin等,github上的连接已经给出,相信看过以前介绍的plugin会养成一种上github看文档的习惯。spa
下一篇介绍下module.exports = function(grunt){}中传入参数grunt的api,应该也是grunt系列的最后一篇了。