dnodejs 每日分享 - grunt专题系列,介绍一下grunt-contrib-copy

539 阅读1分钟
grunt 插件系列第三篇我们选择来介绍一下 grunt-contrib-copy
Copy files and folders.
从定义上可以很直观地知道:
它主要是在我们编译过程中来拷贝一些文件夹或者文件的。
我们来看看常用的配置方式把:
grunt.initConfig({
paths: {
tmp: '.tmp',
dist: 'dist'
},
copy: {
dist: {
files: [{
src: ['/*'],
dest: [''],
filter: 'isFile'
}]
}
}
});

上述代码在 Gruntfile.js 很常见,为了通用性,我们定义了 paths,然后定义了一个 copy,它只是简单地把源目录src下的所有文件拷贝到目标目录dest,然后你可以执行:

grunt copy:dist
这边我们加了一个 isFile 的过滤条件,还是说明一下原理把:
fs.statSync(filepath)[options.filter]();

其实走的是 fs 模块的 fs.Stats.prototype.isFile

copy 的配置方式有很多方式,更多内容请查看官方 git:
github.com/gruntjs/gru…