Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. // 1.less编译压缩合并
  2. // 2.js合并压缩混淆
  3. // 3.img复制
  4. // 4.html压缩
  5.  
  6. //在gulpfile中先载入gulp的包。
  7. var gulp = require('gulp');
  8. var less = require('gulp-less');
  9. var cssnano = require('gulp-cssnano');
  10.  
  11.  
  12. //第一个任务
  13. gulp.task('style',function(done){
  14. //这里是在执行style任务时自动执行
  15. gulp.src('src/style/*.less')
  16. .pipe(less())
  17. .pipe(cssnano())
  18. .pipe(gulp.dest('dist/styles'))
  19. .pipe(browserSync.reload({
  20. stream:true
  21. }));
  22. done();
  23. });
  24.  
  25. //2.js合并 压缩 混淆
  26. var concat = require('gulp-concat');
  27. var uglify = require('gulp-uglify');
  28. gulp.task('script',function(done){
  29. gulp.src('src/scripts/*.js')
  30. .pipe(concat('all.js'))
  31. .pipe(uglify())
  32. .pipe(gulp.dest('dist/scripts'))
  33. .pipe(browserSync.reload({
  34. stream:true
  35. }));
  36. done();
  37. });
  38.  
  39. //3.图片复制
  40. gulp.task('image',function(done){
  41. gulp.src('src/images/*.*')
  42. .pipe(gulp.dest('dist/images'))
  43. .pipe(browserSync.reload({
  44. stream:true
  45. }));
  46. done();
  47. });
  48.  
  49. //4.html
  50. var htmlmin = require('gulp-htmlmin');
  51. gulp.task('html',function(done){
  52. gulp.src('src/*.html')
  53. .pipe(htmlmin({collapseWhitespace:true,removeComments:true}))
  54. .pipe(gulp.dest('dist'))
  55. .pipe(browserSync.reload({
  56. stream:true
  57. }));
  58. done();
  59. });
  60.  
  61. //开启服务器
  62. var browserSync = require('browser-sync');
  63. gulp.task('serve',function(done){
  64. browserSync({
  65. server:{
  66. baseDir:'dist/'
  67. },
  68.  
  69.  
  70. },function(err,bs){
  71. console.log(bs.options.getIn(["urls","local"]));
  72.  
  73. });
  74.  
  75. gulp.watch('src/style/*.less',gulp.series("style"));
  76. gulp.watch('src/scripts/*.js',gulp.series("script"));
  77. gulp.watch('src/images/*.*',gulp.series("image"));
  78. gulp.watch('src/*.html',gulp.series("html"));
  79. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement