验证中...
Languages: JavaScript
Categories: JavaScript 工具
Latest update 2019-11-08 20:02
gulp.task
Raw Copy
/*
> npm install --global gulp-cli
> npm init -y
> npm install --save-dev gulp
> npm install --save-dev **
问题: Gulp Error: Cannot find module 'jshint/src/cli'
解决: > npm install --save-dev jshint gulp-jshint
> gulp
*/
var gulp = require('gulp'),
minifyCss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
clean = require('gulp-clean'),
rev = require('gulp-rev'),
concat = require('gulp-concat'),
revReplace = require('gulp-rev-replace'),
useref = require('gulp-useref'),
revCollector = require('gulp-rev-collector'),
runSequence = require('gulp-run-sequence');
//清空文件夹,避免资源冗余
gulp.task('clean',function(){
return gulp.src('dist',{read:false, allowEmpty: true}).pipe(clean());
});
//css文件压缩,更改版本号,并通过rev.manifest将对应的版本号用json表示出来
gulp.task('css',function(){
return gulp.src('css/*.css')
//.pipe( concat('wap.min.css') )
.pipe(minifyCss())
.pipe(rev())
.pipe(gulp.dest('dist/css/'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist/rev/css'))
});
//js文件压缩,更改版本号,并通过rev.manifest将对应的版本号用json表示出
gulp.task('js',function(){
return gulp.src('js/*.js')
//.pipe( concat('wap.min.js') )
.pipe(jshint())
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest('dist/js/'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist/rev/js'))
});
//通过hash来精确定位到html模板中需要更改的部分,然后将修改成功的文件生成到指定目录
gulp.task('rev',function(){
return gulp.src(['dist/rev/**/*.json','*.html'])
.pipe( revCollector() )
.pipe(gulp.dest('dist/'));
});
//合并html页面内引用的静态资源文件
gulp.task('html', function () {
return gulp.src('dist/*.html')
.pipe(useref())
.pipe(rev())
.pipe(revReplace())
.pipe(gulp.dest('dist/'));
});
// gulp 按顺序执行任务
gulp.task('default', gulp.series('clean', 'css', 'js', 'rev', 'html'))

Comment list( 0 )

Sign in for post a comment

Help Search