gulpfile.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const gulp = require('gulp');
  2. const browserSync = require('browser-sync');
  3. const sass = require('gulp-sass');
  4. const cleanCSS = require('gulp-clean-css');
  5. const autoprefixer = require('gulp-autoprefixer');
  6. const rename = require("gulp-rename");
  7. const imagemin = require('gulp-imagemin');
  8. const htmlmin = require('gulp-htmlmin');
  9. gulp.task('server', function() {
  10. browserSync({
  11. server: {
  12. baseDir: "dist"
  13. }
  14. });
  15. gulp.watch("src/*.html").on('change', browserSync.reload);
  16. });
  17. gulp.task('styles', function() {
  18. return gulp.src("src/sass/**/*.+(scss|sass)")
  19. .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
  20. .pipe(rename({suffix: '.min', prefix: ''}))
  21. .pipe(autoprefixer())
  22. .pipe(cleanCSS({compatibility: 'ie8'}))
  23. .pipe(gulp.dest("dist/css"))
  24. .pipe(browserSync.stream());
  25. });
  26. gulp.task('watch', function() {
  27. gulp.watch("src/sass/**/*.+(scss|sass|css)", gulp.parallel('styles'));
  28. gulp.watch("src/*.html").on('change', gulp.parallel('html'));
  29. gulp.watch("src/js/**/*.js").on('change', gulp.parallel('scripts'));
  30. gulp.watch("src/fonts/**/*").on('all', gulp.parallel('fonts'));
  31. gulp.watch("src/icons/**/*").on('all', gulp.parallel('icons'));
  32. gulp.watch("src/img/**/*").on('all', gulp.parallel('images'));
  33. });
  34. gulp.task('html', function () {
  35. return gulp.src("src/*.html")
  36. .pipe(htmlmin({ collapseWhitespace: true }))
  37. .pipe(gulp.dest("dist/"));
  38. });
  39. gulp.task('scripts', function () {
  40. return gulp.src("src/js/**/*.js")
  41. .pipe(gulp.dest("dist/js"))
  42. .pipe(browserSync.stream());
  43. });
  44. gulp.task('fonts', function () {
  45. return gulp.src("src/fonts/**/*")
  46. .pipe(gulp.dest("dist/fonts"))
  47. .pipe(browserSync.stream());
  48. });
  49. gulp.task('icons', function () {
  50. return gulp.src("src/icons/**/*")
  51. .pipe(gulp.dest("dist/icons"))
  52. .pipe(browserSync.stream());
  53. });
  54. gulp.task('images', function () {
  55. return gulp.src("src/img/**/*")
  56. .pipe(imagemin())
  57. .pipe(gulp.dest("dist/img"))
  58. .pipe(browserSync.stream());
  59. });
  60. gulp.task('default', gulp.parallel('watch', 'server', 'styles', 'scripts', 'fonts', 'icons', 'html', 'images'));