gulp 自动ftp至服务器时,处理开发 测试服务器地址问题
作者:互联网
var gulp=require('gulp'), babel = require('gulp-babel'), gulpSequence = require('gulp-sequence'), htmlmin = require('gulp-htmlmin'), sftp= require('gulp-sftp'), concat=require('gulp-concat'),//文件合并 uglify=require('gulp-uglify'),//js压缩 minifyCss=require('gulp-minify-css'),//css压缩 rev=require('gulp-rev'),//对文件名加MD5后缀 clean=require('gulp-clean'),//清理 revCollector=require('gulp-rev-collector');//路径替换 var myConfig = require('./my-config.js'); gulp.task('mini-rev',function(){ return gulp.src(['./src/**/*.min.js','./src/**/*.min.css']) .pipe(rev()) .pipe(gulp.dest('./dist/gulp_view')) .pipe(rev.manifest()) .pipe(gulp.dest('./dist/gulp_rev/view-min')); }); //css处理任务 gulp.task('mini-css-rev',function(){ return gulp.src(['./src/**/*.css','!./src/**/*.min.css']) .pipe(minifyCss()) .pipe(rev()) .pipe(gulp.dest('./dist/gulp_view')) .pipe(rev.manifest()) .pipe(gulp.dest('./dist/gulp_rev/view-css')); }); //js处理任务 gulp.task('mini-js-rev',function(){ return gulp.src(['./src/**/*.js','!./src/**/*.min.js']) .pipe(myConfig())//核心配置 替换环境变量 .pipe(babel({ "presets": ["env"]} )) .pipe(uglify({ mangle: false}))//mangle: true,//类型:Boolean 默认:true 是否修改变量名 .pipe(rev()) .pipe(gulp.dest('./dist/gulp_view/')) .pipe(rev.manifest()) .pipe(gulp.dest('./dist/gulp_rev/view-js')); }); //路径替换任务 gulp.task('rev-c',function(){ var options = { removeComments: true,//清除HTML注释 collapseWhitespace: true,//压缩HTML removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input /> removeScriptTypeAttributes: true,//删除<script>的type="text/javascript" removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css" minifyJS: true,//压缩页面JS minifyCSS: true//压缩页面CSS }; return gulp.src(['./dist/gulp_rev/**/*json','./src/**/*.html']) .pipe( revCollector() ) .pipe(htmlmin(options)) .pipe(gulp.dest('./dist/gulp_view')); }); //清理文件 gulp.task('clean', function() { return gulp.src(['./dist/gulp_view','./dist/gulp_rev'], {read: false}) .pipe(clean()); }); gulp.task('cp', function() { return gulp.src(['./src/**','!**/*.html','!**/*.htm','!**/*.css','!**/*.js']) .pipe(gulp.dest('./dist/gulp_view')); }); gulp.task('test', function() { console.log('开始替换 配置'); console.log('npm_config_argv==',process.env.npm_config_argv); console.log('npm_lifecycle_event==',process.env.npm_lifecycle_event); console.log('npm_lifecycle_script==',process.env.npm_lifecycle_script); }); gulp.task('sftp_dev', function () { return gulp.src('./dist/gulp_view/**') .pipe(sftp({ host: '192.168.10.132', user: 'root', port: '22', pass:'2016@pflm', remotePath: '/home/data/rights/client' })); }); gulp.task('sftp_test', function () { return gulp.src('./dist/gulp_view/**') .pipe(sftp({ host: '192.168.10.133', user: 'root', port: '22', pass:'2016@pflm', remotePath: '/home/data/rights/client' })); }); gulp.task('default',gulpSequence('clean',['cp','mini-rev', 'mini-css-rev','mini-js-rev'],'rev-c')); gulp.task('dev',gulpSequence('clean',['cp','mini-rev', 'mini-css-rev','mini-js-rev'],'rev-c','sftp_dev')); gulp.task('test',gulpSequence('clean',['cp','mini-rev', 'mini-css-rev','mini-js-rev'],'rev-c','sftp_test'));
gulp 自定义插件
'use strict'; let through = require('through2'); module.exports = function(){ return through.obj(function(file, encoding, callback) { if(file.path.indexOf('common.js')>-1){ var scripts=process.env.npm_lifecycle_event; console.log('追加变量 文件路径=',file.path); console.log('package.json scripts ==',scripts); var bool=false,text=''; switch(scripts){ case '本机': break; case 'ftp开发环境':text=';imgPath = "http://192.168.10.132";_path="http://192.168.10.132/api"';bool=true;break; case 'ftp133测试环境':text=';imgPath="http://aa-test.aaa.cn";_path="http://aa-test.aaa.cn/api"';bool=true;break; default: break; } if(bool){//追加内容 if (file.isNull()||file.isStream()) { this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!')); return callback(); } let content = file.contents.toString(); content+=text; file.contents = new Buffer(content); } console.log('追加变量 bool='+bool+',text='+text); } this.push(file);// 确保文件进入下一个 gulp 插件 callback(); }); };
标签:ftp,dist,pipe,src,rev,js,gulp,服务器时 来源: https://www.cnblogs.com/c-abc/p/10694527.html