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 自定義插件javascript
'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(); }); };