const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const uglify = require('gulp-uglify');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
-- 常用的方法 --
/*
gulp.task - Define tasks
gulp.src - Point to files to use
gulp.dest - Points to folder to output
gulp.watch - Watch files and folders for changes
*/
// 调用方法打印
gulp.task('message', function(){
return console.log('Gulp is running...');
});
// 拷贝所有的html文件
gulp.task('copyHtml', function(){
gulp.src('src/*.html')
.pipe(gulp.dest('dist'));
});
// 压缩图片
gulp.task('imageMin', () =>
gulp.src('src/images/*')
.pipe(imagemin())
.pipe(gulp.dest('dist/images'))
);
// 压缩js
gulp.task('minify', function(){ gulp.src('src/js/*.js')
.pipe(uglify()) .pipe(gulp.dest('dist/js')); });
// 合并scss
gulp.task('sass', function(){
gulp.src('src/sass/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('dist/css'));
});
// Scripts
gulp.task('scripts', function(){
gulp.src('src/js/*.js')
.pipe(concat('main.js'))
.pipe(uglify())
.pipe(gulp.dest('dist/js'));
});
gulp.task('default', ['message', 'copyHtml', 'imageMin', 'sass', 'scripts']);
gulp.task('watch', function(){
gulp.watch('src/js/*.js', ['scripts']);
gulp.watch('src/images/*', ['imageMin']);
gulp.watch('src/sass/*.scss', ['sass']);
gulp.watch('src/*.html', ['copyHtml']);
});