Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1.less编译压缩合并
- // 2.js合并压缩混淆
- // 3.img复制
- // 4.html压缩
- //在gulpfile中先载入gulp的包。
- var gulp = require('gulp');
- var less = require('gulp-less');
- var cssnano = require('gulp-cssnano');
- //第一个任务
- gulp.task('style',function(done){
- //这里是在执行style任务时自动执行
- gulp.src('src/style/*.less')
- .pipe(less())
- .pipe(cssnano())
- .pipe(gulp.dest('dist/styles'))
- .pipe(browserSync.reload({
- stream:true
- }));
- done();
- });
- //2.js合并 压缩 混淆
- var concat = require('gulp-concat');
- var uglify = require('gulp-uglify');
- gulp.task('script',function(done){
- gulp.src('src/scripts/*.js')
- .pipe(concat('all.js'))
- .pipe(uglify())
- .pipe(gulp.dest('dist/scripts'))
- .pipe(browserSync.reload({
- stream:true
- }));
- done();
- });
- //3.图片复制
- gulp.task('image',function(done){
- gulp.src('src/images/*.*')
- .pipe(gulp.dest('dist/images'))
- .pipe(browserSync.reload({
- stream:true
- }));
- done();
- });
- //4.html
- var htmlmin = require('gulp-htmlmin');
- gulp.task('html',function(done){
- gulp.src('src/*.html')
- .pipe(htmlmin({collapseWhitespace:true,removeComments:true}))
- .pipe(gulp.dest('dist'))
- .pipe(browserSync.reload({
- stream:true
- }));
- done();
- });
- //开启服务器
- var browserSync = require('browser-sync');
- gulp.task('serve',function(done){
- browserSync({
- server:{
- baseDir:'dist/'
- },
- },function(err,bs){
- console.log(bs.options.getIn(["urls","local"]));
- });
- gulp.watch('src/style/*.less',gulp.series("style"));
- gulp.watch('src/scripts/*.js',gulp.series("script"));
- gulp.watch('src/images/*.*',gulp.series("image"));
- gulp.watch('src/*.html',gulp.series("html"));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement