Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. /* Gulp module and Plugins */
  2. const gulp = require('gulp'),
  3. imagemin = require('gulp-imagemin'),
  4. clean = require('gulp-clean'),
  5. uglify = require('gulp-uglify'),
  6. cssmin = require('gulp-cssmin'),
  7. usemin = require('gulp-usemin'),
  8. connect = require('gulp-connect-php'),
  9. browserSync = require('browser-sync'),
  10. runSequence = require('run-sequence'),
  11. uncss = require('gulp-uncss'),
  12.  
  13. /* Source and Distribution path */
  14. workPath = 'src',
  15. savePath = 'dist';
  16.  
  17.  
  18. /* Default task */
  19. //clean dist -> copy src to dist ->
  20. //build image & minify js/css ->
  21. //remove unused css in html files ->
  22. //start a php server and browserSync and watch
  23. gulp.task('default', function(){
  24. runSequence(
  25. 'copy',
  26. ['build-img', 'usemin'],
  27. 'uncss',
  28. 'start-server'
  29. );
  30. });
  31.  
  32.  
  33. /* Clean de dist folder */
  34. gulp.task('clean', function(){
  35. return gulp.src(savePath)
  36. .pipe(clean());
  37. });
  38.  
  39.  
  40. /* Copy from src to dist */
  41. //need clean before
  42. gulp.task('copy', ['clean'], function(){
  43. return gulp.src(workPath + '/**/*')
  44. .pipe(gulp.dest(savePath));
  45. });
  46.  
  47.  
  48. /* Build image */
  49. //compact them
  50. //doesn't need to wait (synchronous)
  51. //need copy before
  52. gulp.task('build-img', function(){
  53. gulp.src(savePath + '/public/img/**/*')
  54. .pipe(imagemin())
  55. .pipe(gulp.dest(savePath + '/public/img/'));
  56. });
  57.  
  58.  
  59. /* Build JS & CSS */
  60. //js: uses uglify plugin
  61. //css: uses cssmin plugin
  62. //need copy before
  63. gulp.task('usemin', function(){
  64. return gulp.src(savePath + '/**/*.{html,phtml}')
  65. .pipe(usemin({
  66. 'path' : savePath + '/public',
  67. 'outputRelativePath' : 'public',
  68. 'js' : [uglify],
  69. 'css' : [cssmin]
  70. }))
  71. .pipe(gulp.dest(savePath));
  72. });
  73.  
  74.  
  75. /* Remove unused css in html files */
  76. //need usemin before
  77. gulp.task('uncss', function(){
  78. return gulp.src(savePath + '/public/css/**/*.css')
  79. .pipe(uncss({
  80. html : [savePath + '/app/views/**/*.{html,phtml}']
  81. }))
  82. .pipe(gulp.dest(savePath + '/public/css'));
  83. });
  84.  
  85.  
  86. /* Start a php server with dist & browserSync */
  87. gulp.task('start-server', function(){
  88. //create php server
  89. connect.server({
  90. port : 8000,
  91. hostname : '127.0.0.1',
  92. base : savePath + '/public'
  93. }, function (){
  94. //start browserSync
  95. browserSync.init({
  96. proxy: '127.0.0.1:8000'
  97. });
  98. });
  99.  
  100. //look for changes in src
  101. //then start browser-sync
  102. gulp.watch(workPath + '/**/*').on('change', function () {
  103. gulp.start('browser-sync');
  104. });
  105. });
  106.  
  107.  
  108. /* Rebuild dist & reload browser */
  109. //called after any change on src
  110. gulp.task('browser-sync', function(){
  111. return runSequence(
  112. 'copy',
  113. ['build-img', 'usemin'],
  114. 'uncss',
  115. 'browser-reload'
  116. );
  117. });
  118.  
  119.  
  120. /* Reload browser */
  121. gulp.task('browser-reload', function(){
  122. browserSync.reload();
  123. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement