配置gulpfile,添加task任务

需求:在本地静态环境下,写好css后,需要同步至生产坏境,每次都需要复制,比较麻烦,有时候还会出现忘了的情况。

基于css本就用gulp进行压缩和实时编译,所以直接把同步的功能写在gulpfile中。

步骤如下:

1、在bin目录下新建cpcss文件,不带任何后缀名,可用git touch 新建

#!/bin/sh

cp /d/mycode/all.css /d/prd/mycode/all.css

cp -R /d/mycode/img  /d/prd/mycode

cp -R /d/mycode/css /d/prd/mycode 

echo
"cpcss finished"

前一个路径为静态目录路径,后一个为需要同步到的生产环境的目录路径


2、配置gulpfile.js

var through = require('through2');
const execFile = require('child_process').execFile;
var cpcss = function(){
execFile('sh', ['~/bin/cpcss'], function(error, stdout, stderr){
if (error) {
throw error;
}
console.log(stdout);
});
};

gulp.task('cpcss', function(){
cpcss();
});

gulp.task('replace-less', function(){ 

.........

return gulp.src(toMockupPath)
......
.pipe(through.obj( function (chunk, enc, cb ) { //pipe()配置
cpcss();
cb();
}));
});

运行gulp run,即可检测是否配置成功,成功会打印出"cpcss finished"

原文地址:https://www.cnblogs.com/chaoli/p/6796265.html