Advertisement
Guest User

Untitled

a guest
Sep 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. //initialize all of our variables
  2. var app, base, concat, directory, gulp, gutil, hostname, path, refresh, sass, uglify, imagemin, minifyCSS, del, browserSync, autoprefixer, gulpSequence, shell, sourceMaps, plumber;
  3.  
  4. var autoPrefixBrowserList = ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'];
  5.  
  6. //load all of our dependencies
  7. //add more here if you want to include more libraries
  8. gulp = require('gulp');
  9. gutil = require('gulp-util');
  10. concat = require('gulp-concat');
  11. uglify = require('gulp-uglify');
  12. sass = require('gulp-sass');
  13. sourceMaps = require('gulp-sourcemaps');
  14. imagemin = require('gulp-imagemin');
  15. minifyCSS = require('gulp-minify-css');
  16. browserSync = require('browser-sync');
  17. autoprefixer = require('gulp-autoprefixer');
  18. gulpSequence = require('gulp-sequence').use(gulp);
  19. shell = require('gulp-shell');
  20. plumber = require('gulp-plumber');
  21.  
  22. var reload = browserSync.reload;
  23.  
  24. gulp.task('browserSync', function() {
  25. browserSync({
  26. server: {
  27. baseDir: "app/"
  28. },
  29. options: {
  30. reloadDelay: 250
  31. },
  32. notify: true,
  33. });
  34. gulp.watch("app/.html").on("change", reload);
  35. gulp.watch('app/scripts/src/').on("change", reload);
  36. gulp.watch('app/images/').on("change", reload);
  37. gulp.watch('app/styles/**/.scss').on("change", reload);
  38.  
  39. });
  40.  
  41. //compressing images & handle SVG files
  42. gulp.task('images', function(tmp) {
  43. gulp.src(['app/images/.jpg', 'app/images/.png'])
  44. //prevent pipe breaking caused by errors from gulp plugins
  45. .pipe(plumber())
  46. .pipe(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true }))
  47. .pipe(gulp.dest('app/images'));
  48. });
  49.  
  50. //compressing images & handle SVG files
  51. gulp.task('images-deploy', function() {
  52. gulp.src(['app/images/**/*', '!app/images/README'])
  53. //prevent pipe breaking caused by errors from gulp plugins
  54. .pipe(plumber())
  55. .pipe(gulp.dest('dist/images'));
  56. });
  57.  
  58. //compiling our Javascripts
  59. gulp.task('scripts', function() {
  60. //this is where our dev JS scripts are
  61. return gulp.src(['app/scripts/src/_includes//*.js', 'app/scripts/src//*.js'])
  62. //prevent pipe breaking caused by errors from gulp plugins
  63. .pipe(plumber())
  64. //this is the filename of the compressed version of our JS
  65. .pipe(concat('app.js'))
  66. //catch errors
  67. .on('error', gutil.log)
  68. //where we will store our finalized, compressed script
  69. .pipe(gulp.dest('app/scripts'))
  70. //notify browserSync to refresh
  71. .pipe(browserSync.reload({stream: true}));
  72. });
  73.  
  74. //compiling our Javascripts for deployment
  75. gulp.task('scripts-deploy', function() {
  76. //this is where our dev JS scripts are
  77. return gulp.src(['app/scripts/src/_includes//*.js', 'app/scripts/src//*.js'])
  78. //prevent pipe breaking caused by errors from gulp plugins
  79. .pipe(plumber())
  80. //this is the filename of the compressed version of our JS
  81. .pipe(concat('app.js'))
  82. //compress :D
  83. .pipe(uglify())
  84. //where we will store our finalized, compressed script
  85. .pipe(gulp.dest('dist/scripts'));
  86. });
  87.  
  88. //compiling our SCSS files
  89. gulp.task('styles', function() {
  90. //the initializer / master SCSS file, which will just be a file that imports everything
  91. return gulp.src('app/styles/main.scss')
  92. //prevent pipe breaking caused by errors from gulp plugins
  93. .pipe(plumber({
  94. errorHandler: function (err) {
  95. console.log(err);
  96. this.emit('end');
  97. }
  98. }))
  99. //get sourceMaps ready
  100. .pipe(sourceMaps.init())
  101. //include SCSS and list every "include" folder
  102. .pipe(sass({
  103. errLogToConsole: true,
  104. includePaths: [
  105. 'app/styles'
  106. ]
  107. }))
  108. .pipe(autoprefixer({
  109. browsers: autoPrefixBrowserList,
  110. cascade: true
  111. }))
  112. //catch errors
  113. .on('error', gutil.log)
  114. //the final filename of our combined css file
  115. .pipe(concat('main.css'))
  116. //get our sources via sourceMaps
  117. .pipe(sourceMaps.write())
  118. //where to save our final, compressed css file
  119. .pipe(gulp.dest('app/styles'))
  120. //notify browserSync to refresh
  121. .pipe(browserSync.reload({stream: true}));
  122. });
  123.  
  124. //compiling our SCSS files for deployment
  125. gulp.task('styles-deploy', function() {
  126. //the initializer / master SCSS file, which will just be a file that imports everything
  127. return gulp.src('app/styles/scss/main.scss')
  128. .pipe(plumber())
  129. //include SCSS includes folder
  130. .pipe(sass({
  131. includePaths: [
  132. 'app/styles/scss',
  133. ]
  134. }))
  135. .pipe(autoprefixer({
  136. browsers: autoPrefixBrowserList,
  137. cascade: true
  138. }))
  139. //the final filename of our combined css file
  140. .pipe(concat('styles.css'))
  141. .pipe(minifyCSS())
  142. //where to save our final, compressed css file
  143. .pipe(gulp.dest('dist/styles'));
  144. });
  145.  
  146. //basically just keeping an eye on all HTML files
  147. gulp.task('html', function() {
  148. //watch any and all HTML files and refresh when something changes
  149. return gulp.src('app/*.html')
  150. .pipe(plumber())
  151. .pipe(browserSync.reload({stream: true}))
  152. //catch errors
  153. .on('error', gutil.log);
  154. });
  155.  
  156. //migrating over all HTML files for deployment
  157. gulp.task('html-deploy', function() {
  158. //grab everything, which should include htaccess, robots, etc
  159. gulp.src('app/*')
  160. //prevent pipe breaking caused by errors from gulp plugins
  161. .pipe(plumber())
  162. .pipe(gulp.dest('dist'));
  163.  
  164. //grab any hidden files too
  165. gulp.src('app/.*')
  166. //prevent pipe breaking caused by errors from gulp plugins
  167. .pipe(plumber())
  168. .pipe(gulp.dest('dist'));
  169.  
  170. gulp.src('app/fonts/**/*')
  171. //prevent pipe breaking caused by errors from gulp plugins
  172. .pipe(plumber())
  173. .pipe(gulp.dest('dist/fonts'));
  174.  
  175. //grab all of the styles
  176. gulp.src(['app/styles/*.css', '!app/styles/styles.css'])
  177. //prevent pipe breaking caused by errors from gulp plugins
  178. .pipe(plumber())
  179. .pipe(gulp.dest('dist/styles'));
  180.  
  181. });
  182.  
  183. //cleans our dist directory in case things got deleted
  184. gulp.task('clean', function() {
  185. return shell.task([
  186. 'rm -rf dist'
  187. ]);
  188. });
  189.  
  190. //create folders using shell
  191. gulp.task('scaffold', function() {
  192. return shell.task([
  193. 'mkdir dist',
  194. 'mkdir dist/fonts',
  195. 'mkdir dist/images',
  196. 'mkdir dist/scripts',
  197. 'mkdir dist/styles'
  198. ]
  199. );
  200. });
  201.  
  202. //this is our master task when you run gulp in CLI / Terminal
  203. //this is the main watcher to use when in active development
  204. // this will:
  205. // startup the web server,
  206. // start up browserSync
  207. // compress all scripts and SCSS files
  208.  
  209. gulp.task('watch', function () {
  210. //a list of watchers, so it will watch all of the following files waiting for changes
  211. gulp.watch('app/scripts/src/', gulp.series('scripts'));
  212. gulp.watch('app/styles//.scss',gulp.series('styles'));
  213. gulp.watch('app/images/**',gulp.series('images'));
  214. gulp.watch('app/.html', gulp.series('html'));
  215. });
  216.  
  217. //this is our deployment task, it will set everything for deployment-ready files
  218. gulp.task('dev', gulp.series([ 'scripts', 'styles', 'browserSync', 'watch' ]));
  219. gulp.task('deploy', gulp.series('clean', 'scaffold', ['scripts-deploy', 'styles-deploy', 'images-deploy'], 'html-deploy'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement