gulp实例

时间:2023-12-29 18:52:38
前端生产环境的简单部署
http://ionichina.com/topic/558a1c1346cb5ff7268cee9d var gulp = require('gulp'); // 引入gulp组件(插件)
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var jshint = require('gulp-jshint');
var minifyCss = require('gulp-minify-css');
var notify = require('gulp-notify');
var concat = require('gulp-concat');
var useref = require('gulp-useref');
var filter = require('gulp-filter');
var gulpif = require('gulp-if');
var clean = require('gulp-clean'); //index.html css、js合并压缩
gulp.task('index', function () {
var assets = useref.assets();
return gulp.src('app/index.html')
.pipe(assets)
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest('www'));
}); // 语法检查
gulp.task('jshint', function () {
return gulp.src('app/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
}); // 复制文件
gulp.task('copy', function () {
gulp.src('app/fonts/*')
// 目标地址
.pipe(gulp.dest('www/fonts/'))
gulp.src('app/templates/**')
// 目标地址
.pipe(gulp.dest('www/templates/'))
gulp.src('app/img/*')
// 目标地址
.pipe(gulp.dest('www/img/')) }); // 清空图片、样式、js
gulp.task('clean', function () {
return gulp.src(['www/css/*', 'www/js/*', 'www/img/*', 'www/lib/*', 'www/templates/*'], {read: false})
.pipe(clean({force: true}));
}); // 注册缺省任务
gulp.task('default', ['jshint', 'clean', 'index', 'copy']);