// gulpfile.js
var gulp = require('gulp'), runSequence = require('run-sequence'), gulpif = require('gulp-if'), less = require('gulp-less'), csslint = require('gulp-csslint'), rev = require('gulp-rev'), minifyCss = require('gulp-minify-css'), changed = require('gulp-changed'), jshint = require('gulp-jshint'), stylish = require('jshint-stylish'), revCollector = require('gulp-rev-collector'), minifyHtml = require('gulp-minify-html'), autoprefixer = require('gulp-autoprefixer'), del = require('del'), babel = require("gulp-babel"); // 用于ES6转化ES5 var cssSrc = 'src/css/*', fontSrc = 'src/fonts/*', imgSrc = 'src/images/*', cssRevSrc = 'src/css/revCss', jsSrc = 'src/js/*.js', fontDest = 'dist/font', imgDest = 'dist/images', cssDest = 'dist/css', jsDest = 'dist/js', condition = true; function changePath(basePath){ var nowCssSrc = []; for (var i = 0; i < cssSrc.length; i++) { nowCssSrc.push(cssRevSrc + '/' + cssSrc[i]); } return nowCssSrc; } //Fonts & Images 根据MD5获取版本号 gulp.task('revFont', function(){ return gulp.src(fontSrc) .pipe(rev()) .pipe(gulp.dest(fontDest)) .pipe(rev.manifest()) .pipe(gulp.dest('config/font')); }); gulp.task('revImg', function(){ return gulp.src('src/images/**') .pipe(rev()) .pipe(gulp.dest(imgDest)) .pipe(rev.manifest()) .pipe(gulp.dest('config/img')); }); gulp.task('revCss', function(){ return gulp.src('src/css/**') .pipe(rev()) .pipe(gulp.dest(cssDest)) .pipe(rev.manifest()) .pipe(gulp.dest('config/css')); }); //压缩JS/生成版本号 gulp.task('revJs', function(){ return gulp.src(jsSrc) /*.pipe(gulpif( condition, uglify() ))*/ .pipe(rev()) .pipe(babel({ presets: [ ['es2015', { modules: false }] ] })) .pipe(gulp.dest(jsDest)) .pipe(rev.manifest()) .pipe(gulp.dest('config/js')); }); //CSS里更新引入文件版本号 gulp.task('revCollectorCss', function () { return gulp.src(['config/**/*.json', 'src/css/*.css']) .pipe(revCollector()) .pipe(gulp.dest(cssDest)); }); //JS里更新引入文件版本号 gulp.task('revCollectorJs', function () { return gulp.src(['config/**/*.json', 'src/js/*.js']) .pipe(revCollector()) .pipe(gulp.dest(jsDest)); }); //检测CSS gulp.task('lintCss', function(){ return gulp.src(cssSrc) .pipe(csslint()) .pipe(csslint.reporter()) .pipe(csslint.failReporter()); }); //压缩/合并CSS/生成版本号 /*gulp.task('miniCss', function(){ return gulp.src(cssSrc) .pipe(less()) .pipe(gulpif( condition, minifyCss({ compatibility: 'ie7' }) )) .pipe(rev()) .pipe(gulpif( condition, changed(cssDest) )) /!*.pipe(autoprefixer({ browsers: ['last 2 versions'], cascade: false, remove: false }))*!/ .pipe(gulp.dest(cssDest)) .pipe(rev.manifest()) .pipe(gulp.dest('config/css')); });*/ //压缩Html/更新引入文件版本 gulp.task('miniHtml', function () { return gulp.src(['config/**/*.json', 'src/*.html']) .pipe(revCollector()) // .pipe(gulpif( // condition, minifyHtml({ // empty: true, // spare: true, // quotes: true // }) // )) .pipe(gulp.dest('dist')); }); gulp.task('delRevCss', function(){ del([cssRevSrc,cssRevSrc.replace('src/', 'dist/')]); }); gulp.task('cleanDist', function(){ del(['config/', 'dist/']); }); //清除缓存文件 gulp.task('clean', function(){ del([fontDest, cssDest, jsDest,]);// cssRevSrc, cssRevSrc.replace('src/', 'dist/') }); // 移动图标 gulp.task('moveIcon', function(){ return gulp.src('src/favicon.ico') .pipe(gulp.dest('dist/')); }); //检测JS gulp.task('lintJs', function(){ return gulp.src(jsSrc) //.pipe(jscs()) //检测JS风格 .pipe(jshint({ "undef": false, "unused": false })) //.pipe(jshint.reporter('default')) //错误默认提示 .pipe(jshint.reporter(stylish)) //高亮提示 .pipe(jshint.reporter('fail')); }); //移动JS子级目录 gulp.task('moveDirJs', function(){ return gulp.src('src/js/*/**/') .pipe(gulp.dest('dist/js')); }); //移动libs子级目录 gulp.task('moveLibs', function(){ return gulp.src('src/libs/**/') .pipe(gulp.dest('dist/libs')); }); //移动images子级目录 gulp.task('moveImages', function(){ return gulp.src('src/images/**/') .pipe(gulp.dest('dist/images')); }); //移动IM的dist子级目录 gulp.task('moveImdist', function(){ return gulp.src('src/dist/**/') .pipe(gulp.dest('dist/dist')); }); /* * rev找到 * revCollector替换 * */ //正式构建 gulp.task('build', function (done) { runSequence( // ['clean'], ['revFont', 'revImg', 'revCss', 'revJs'], // ['lintJs'], ['moveDirJs', 'moveLibs', 'moveImages', 'moveImdist'], ['revCollectorCss', 'revCollectorJs'], ['miniHtml', 'delRevCss'], ['moveIcon'], done); }); gulp.task('js', function (done) { runSequence( // ['clean'], ['moveDirJs'], ['revJs'], // ['lintJs'], done); }); //开发构建 // gulp.task('dev', function (done) { // condition = false; // runSequence( // ['revFont', 'revImg'], // // ['lintJs'], // ['revCollectorCss'], // ['miniCss', 'miniJs'], // ['miniHtml', 'delRevCss'], // done); // }); gulp.task('default', ['build']);
package.josn
{ "devDependencies": { "@babel/core": "^7.2.2", "@babel/preset-env": "^7.2.3", "apache-server-configs": "2.14.0", "archiver": "^0.14.3", "babel-cli": "^6.26.0", "babel-core": "^6.26.3", "babel-preset-env": "^1.7.0", "babel-preset-es2015": "^6.24.1", "del": "^1.1.1", "glob": "^5.0.5", "gulp": "^3.8.11", "gulp-autoprefixer": "^2.1.0", "gulp-babel": "^7.0.1", "gulp-changed": "^1.2.1", "gulp-csslint": "^0.1.5", "gulp-header": "^1.2.2", "gulp-if": "^1.2.5", "gulp-jshint": "^1.11.2", "gulp-less": "^3.0.3", "gulp-load-plugins": "^0.10.0", "gulp-minify-css": "^1.2.0", "gulp-minify-html": "^1.0.4", "gulp-remove-use-strict": "0.0.2", "gulp-rev": "^5.1.0", "gulp-rev-collector": "^1.0.0", "gulp-uglify": "^1.5.4", "gulp-util": "^3.0.6", "jquery": "1.11.3", "jshint": "^2.8.0", "jshint-stylish": "^2.0.1", "mocha": "^2.2.4", "normalize.css": "3.0.3", "run-sequence": "^1.0.2" }, "engines": { "node": ">=0.10.0" }, "h5bp-configs": { "directories": { "archive": "archive", "dist": "dist", "src": "src", "test": "test" } }, "homepage": "", "license": { "type": "MIT", "url": "" }, "name": "gulp-auto-version", "private": true, "scripts": { "build": "gulp build", "test": "" }, "version": "1.0.0", "dependencies": { "gulp-rev-append": "^0.1.8" } }