使用:

项目创建gulpfile.js文件
以模块导出的形式暴露gulp相应任务操作

常用gulp插件

src、dest用于创建gulp输入输出文件流
series、parallel用于创建gulp串形和并行任务

  1. // 实现这个项目的构建任务
  2. const { src, dest, series, parallel, watch } = require('gulp');
  3. // 自动加载插件
  4. const loadPlugins = require('gulp-load-plugins');
  5. const plugins = loadPlugins();
  6. // 在线运行插件
  7. const browsersync = require('browser-sync');
  8. const bs = browsersync.create();
  9. // 清空文件目录插件
  10. const del = require('del')
  11. const clean = () => {
  12. return del(['temp', 'dist'])
  13. }
  14. // html文件构建
  15. const pages = () => {
  16. return src('src/*.html', { base: 'src' })
  17. .pipe(plugins.swig({defaults: { cache: false }}))
  18. .pipe(dest('temp'))
  19. .pipe(bs.reload({ stream: true }))
  20. }
  21. // css文件构建
  22. const style = () => {
  23. return src('src/assets/styles/*.scss', { base: 'src' })
  24. .pipe(plugins.sass({ outputStyle: 'expanded' }))
  25. .pipe(dest('temp'))
  26. .pipe(bs.reload({ stream: true }))
  27. }
  28. // js文件构建
  29. const script = () => {
  30. return src('src/assets/scripts/*.js', { base: 'src' })
  31. .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
  32. .pipe(dest('temp'))
  33. .pipe(bs.reload({ stream: true }))
  34. }
  35. // 图片类型文件构建
  36. const images = () => {
  37. return src('src/assets/images/**', { base: 'src' })
  38. .pipe(plugins.imagemin())
  39. .pipe(dest('dist'))
  40. }
  41. // 字体文件构建
  42. const fonts = () => {
  43. return src('src/assets/fonts/**', { base: 'src' })
  44. .pipe(plugins.imagemin())
  45. .pipe(dest('dist'))
  46. }
  47. // 其他类型文件构建
  48. const pubs = () => {
  49. return src('public/**', { base: 'public' })
  50. .pipe(dest('dist'))
  51. }
  52. // html、js、css文件压缩,node_modules模块打包
  53. const useref = () => {
  54. return src('temp/*.html', { base: 'temp' })
  55. .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
  56. .pipe(plugins.if(/\.js$/, plugins.uglify()))
  57. .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
  58. .pipe(plugins.if(/\.html$/, plugins.htmlmin({ collapseWhiteSpace: true, minifyCss: true, minifyJs: true })))
  59. .pipe(dest('dist'))
  60. }
  61. // 创建项目运行服务器
  62. const serve = () => {
  63. watch('src/*.html', pages)
  64. watch('src/assets/styles/*.scss', style)
  65. watch('src/assets/scripts/*.js', script)
  66. watch([
  67. 'src/assets/images/**',
  68. 'src/assets/fonts/**',
  69. 'public/**',
  70. ], bs.reload)
  71. bs.init({
  72. notify: false,
  73. port: 8084,
  74. open: true,
  75. server: {
  76. baseDir: ['temp', 'src', 'public'],
  77. routes: {
  78. '/node_modules': 'node_modules'
  79. }
  80. }
  81. })
  82. }
  83. // 并行执行html,css,js文件构建任务(抽离字体、图片内容,这些内容仅在build打包时编译即可)
  84. const compile = parallel(pages, style, script);
  85. // 项目整体构建
  86. const build = series(clean, parallel(series(compile, useref), fonts, images, pubs))
  87. // 运行
  88. const dev = series(compile, serve)
  89. module.exports = {
  90. script,
  91. style,
  92. pages,
  93. images,
  94. fonts,
  95. pubs,
  96. clean,
  97. compile,
  98. build,
  99. dev,
  100. }