gulpfile.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const gulp = require("gulp"),
  2. htmlmin = require("gulp-htmlmin"),
  3. minifyCss = require("gulp-minify-css"),
  4. sass = require("gulp-sass"),
  5. uglify = require("gulp-uglify"),
  6. babel = require("gulp-babel"),
  7. connect = require("gulp-connect");
  8. // gulp.task('default', () => {
  9. // console.log("default");
  10. // });
  11. gulp.task("html", () => {
  12. gulp.src("src/**/*.html")
  13. .pipe(htmlmin({
  14. removeComments: true,//清除HTML注释
  15. collapseWhitespace: true,//压缩HTML
  16. collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input checked />
  17. removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
  18. removeScriptTypeAttributes: false,//删除<script>的type="text/javascript"
  19. removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
  20. minifyJS: true,//压缩页面JS
  21. minifyCSS: true//压缩页面CSS
  22. }))
  23. .pipe(gulp.dest("dist"))
  24. .pipe(connect.reload());
  25. })
  26. gulp.task("css", () => {
  27. gulp.src("src/css/*.css")
  28. .pipe(sass())
  29. .pipe(minifyCss())
  30. .pipe(gulp.dest("dist/css"))
  31. .pipe(connect.reload());
  32. })
  33. gulp.task("js", () => {
  34. gulp.src("src/js/**/*.js")
  35. .pipe(babel({
  36. presets: ['@babel/env']
  37. }))
  38. .pipe(uglify())
  39. .pipe(gulp.dest("dist/js"))
  40. .pipe(connect.reload());
  41. })
  42. gulp.task("lib" ,() => {
  43. gulp.src("src/libs/**/*")
  44. .pipe(gulp.dest("dist/libs"))
  45. .pipe(connect.reload());
  46. })
  47. gulp.task("img", () => {
  48. gulp.src("src/img/**/*")
  49. .pipe(gulp.dest("dist/img"))
  50. .pipe(connect.reload());
  51. })
  52. // 监听文件的改变,然后自动执行对应的任务
  53. gulp.task("watch", () => {
  54. // 监听所有html文件的改变,他、然后执行html任务
  55. gulp.watch("src/**/*.html", ["html"]);
  56. gulp.watch("src/js/**/*.js", ["js"]);
  57. gulp.watch("src/css/**/*.scss", ["css"]);
  58. })
  59. gulp.task("default", ["html", "js", "css", "lib", "img", "watch"]);