gulpfile.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. var gulp = require('gulp');//gulp核心
  3. /*var sass = require('gulp-sass');//编译sass*/
  4. var less = require('gulp-less');//编译less
  5. var mincss = require('gulp-clean-css');//压缩css
  6. var minjs = require('gulp-uglify');//压缩JS
  7. var rename = require('gulp-rename');//文件重命名
  8. var watch = require('gulp-watch');//监控文件
  9. var livereload = require('gulp-livereload');//热更新
  10. var plumber = require('gulp-plumber');//提示错误并忽略
  11. const { series } = require('gulp');
  12. // 引入静态服务器 browser-sync 使用npm i broswer-sync -g 全局安装
  13. const broswer = require("browser-sync").create()
  14. /*路径*/
  15. var lessPath = "./less/*.less";
  16. var cssDestPath = "./css";
  17. var jsPath = "./jssrc/*.js";
  18. var jsDestPath = "./js";
  19. var projects = ['.', "page_news",
  20. "component",
  21. 'page_about_us',
  22. "page_bidding_information",
  23. "page_auction_notice",
  24. "page_cooperation",
  25. "page_detailed",
  26. "page_financial",
  27. "page_donwload",
  28. "page_settlein"]
  29. var lessTasks=[]
  30. var javascriptTasks=[]
  31. projects.forEach(projectName => {
  32. lessTasks.push('less:'+projectName)
  33. javascriptTasks.push('javascript:'+projectName)
  34. //编译sass
  35. gulp.task('less:'+projectName, function () {
  36. return gulp.src(projectName+"/"+lessPath)
  37. .pipe(plumber())
  38. .pipe(less())
  39. .pipe(mincss())
  40. .pipe(rename({
  41. suffix: ".min"
  42. }))
  43. .pipe(gulp.dest(projectName+"/"+cssDestPath))
  44. .pipe(livereload());
  45. });
  46. //压缩JS
  47. gulp.task('javascript:'+projectName, function () {
  48. return gulp.src(projectName+"/"+jsPath)
  49. .pipe(plumber())
  50. .pipe(minjs())
  51. .pipe(rename({
  52. suffix: ".min"
  53. }))
  54. .pipe(gulp.dest(projectName+"/"+jsDestPath))
  55. .pipe(livereload())
  56. })
  57. });
  58. gulp.task('less',gulp.parallel(lessTasks))
  59. gulp.task('javascript',gulp.parallel(javascriptTasks))
  60. //监测文件
  61. gulp.task('watch', function () {
  62. gulp.watch(lessPath, gulp.series("less"));
  63. gulp.watch(jsPath, gulp.series("javascript"));
  64. })
  65. // 开启浏览服务
  66. gulp.task('browser', function () {
  67. broswer.init({
  68. server: "./",
  69. port: 3000
  70. })
  71. })
  72. // gulp.task('default',function(){
  73. // livereload.listen();
  74. // gulp.series(gulp.parallel("less","javascript","watch"));
  75. // // gulp.run("less","javascript","watch");
  76. // });
  77. gulp.task('default', gulp.parallel(function () {
  78. console.log("----------------")
  79. livereload.listen();
  80. }, "less", "javascript", gulp.parallel("watch", "browser")));