Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. var gulp = require('gulp');
  2. var connect = require('gulp-connect');
  3.  
  4. ///////////////////////////////////////////////////////////////////
  5.  
  6. /**
  7. * Pug Filter :code
  8. * Code filter to be able to add code documentation in a pug file
  9. *
  10. * @param block String
  11. **/
  12. function codeFilter(block)
  13. {
  14. return block
  15. .replace( /&/g, '&' )
  16. .replace( /</g, '<' )
  17. .replace( />/g, '>' )
  18. .replace( /"/g, '"' )
  19. .replace( /#/g, '&#35;' )
  20. .replace( /\\/g, '\\\\' )
  21. .replace( /\n/g, '<br>' );
  22. }
  23.  
  24. ///////////////////////////////////////////////////////////////////
  25.  
  26. /**
  27. * Task: Styles [dev]
  28. * Build the sass files for development
  29. *
  30. * @version 1.1
  31. * @author Alexandre Masy <hello@alexandremasy.com>
  32. **/
  33. gulp.task('styles.dev', function(){
  34. var sass = require('gulp-sass');
  35. var autoprefixer = require('gulp-autoprefixer');
  36.  
  37. gulp.src(['src/styles/app.scss', 'src/styles/styleguide.scss'])
  38. .pipe(sass.sync().on('error', sass.logError))
  39. .pipe(autoprefixer('last 2 versions'))
  40. .pipe(gulp.dest('build/statics/styles/'))
  41. .pipe(connect.reload())
  42. ;
  43. });
  44.  
  45. /**
  46. * Task: Styles [prod]
  47. * Build the sass files for production
  48. *
  49. * @version 1.1
  50. * @author Alexandre Masy <hello@alexandremasy.com>
  51. **/
  52. gulp.task('styles.prod', function(){
  53. var sass = require('gulp-sass');
  54. var autoprefixer = require('gulp-autoprefixer');
  55.  
  56. gulp.src(['src/styles/app.scss', 'src/styles/styleguide.scss'])
  57. .pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
  58. .pipe(autoprefixer('last 2 versions'))
  59. .pipe(gulp.dest('build/statics/styles/'))
  60. ;
  61. });
  62.  
  63. /**
  64. * Task: Styles [watch]
  65. * Watch & build the sass files for development
  66. *
  67. * @version 1.2
  68. * @author Alexandre Masy <hello@alexandremasy.com>
  69. **/
  70. gulp.task('styles.watch', ['styles.dev'], function()
  71. {
  72. gulp.watch(['src/styles/**/*.scss'], ['styles.dev']);
  73. })
  74.  
  75. ///////////////////////////////////////////////////////////////////
  76.  
  77. /**
  78. * Task: Libs [vendor]
  79. * Concat the vendors libraries
  80. *
  81. * @version 1.0
  82. * @author Alexandre Masy <hello@alexandremasy.com>
  83. **/
  84. gulp.task('libs.vendor', function(){
  85. var rename = require('gulp-rename');
  86. var concat = require('gulp-concat');
  87. var plumber = require('gulp-plumber');
  88.  
  89. var src = [
  90. 'var/vendor/jquery/dist/jquery.min.js'
  91. ];
  92.  
  93. return gulp.src(src)
  94. .pipe(plumber({
  95. errorHandler: function (error) {
  96. console.log(error.message);
  97. this.emit('end');
  98. }}))
  99. .pipe(concat('vendor.js'))
  100. .pipe(gulp.dest('build/statics/libs/'))
  101. .pipe(rename({suffix: '.min'}))
  102. .pipe(gulp.dest('build/libs/'))
  103. ;
  104. });
  105.  
  106. /**
  107. * Task: Libs [dev]
  108. * Build the libraries for development
  109. *
  110. * @version 1.1
  111. * @author Alexandre Masy <hello@alexandremasy.com>
  112. **/
  113. gulp.task('libs.dev', function(){
  114. var rename = require('gulp-rename');
  115. var concat = require('gulp-concat');
  116. var plumber = require('gulp-plumber');
  117.  
  118. return gulp.src('src/libs/**/*.js')
  119. .pipe(plumber({
  120. errorHandler: function (error) {
  121. console.log(error.message);
  122. this.emit('end');
  123. }}))
  124. .pipe(concat('app.js'))
  125. .pipe(gulp.dest('build/statics/libs/'))
  126. .pipe(rename({suffix: '.min'}))
  127. .pipe(gulp.dest('build/libs/'))
  128. .pipe(connect.reload())
  129. ;
  130. });
  131.  
  132. /**
  133. * Task: Libs [prod]
  134. * Build the libraries for production
  135. *
  136. * @version 1.1
  137. * @author Alexandre Masy <hello@alexandremasy.com>
  138. **/
  139. gulp.task('libs.prod', function(){
  140. var rename = require('gulp-rename');
  141. var uglify = require('gulp-uglify');
  142. var concat = require('gulp-concat');
  143. var plumber = require('gulp-plumber');
  144.  
  145. return gulp.src('src/libs/**/*.js')
  146. .pipe(plumber({
  147. errorHandler: function (error) {
  148. console.log(error.message);
  149. this.emit('end');
  150. }}))
  151. .pipe(concat('app.js'))
  152. .pipe(gulp.dest('build/statics/libs/'))
  153. .pipe(rename({suffix: '.min'}))
  154. .pipe(uglify())
  155. .pipe(gulp.dest('build/statics/libs/'))
  156. ;
  157. });
  158.  
  159. /**
  160. * Task: Libs [watch]
  161. * Watch & build the libraries for development
  162. *
  163. * @version 1.2
  164. * @author Alexandre Masy <hello@alexandremasy.com>
  165. **/
  166. gulp.task('libs.watch', ['libs.dev'], function()
  167. {
  168. gulp.watch(['src/libs/**/*.js'], ['libs.dev']);
  169. })
  170.  
  171. ///////////////////////////////////////////////////////////////////
  172.  
  173. /**
  174. * Task: Views [production & development]
  175. * Build the pugjs files for production & development
  176. *
  177. * @version 1.1
  178. * @author Alexandre Masy <hello@alexandremasy.com>
  179. **/
  180. gulp.task('views', function()
  181. {
  182. var gulpPug = require('gulp-pug');
  183.  
  184. return gulp.src(['src/views/pages/**/*.pug', 'src/views/styleguide/**/*.pug'])
  185. .pipe(gulpPug({pretty: true, filters:{'code':codeFilter}}))
  186. .pipe(gulp.dest('build/'))
  187. .pipe(connect.reload())
  188. ;
  189. });
  190.  
  191. /**
  192. * Task: Views [watch]
  193. * Watch & build the pugjs files for production & development
  194. *
  195. * @version 1.2
  196. * @author Alexandre Masy <hello@alexandremasy.com>
  197. **/
  198. gulp.task('views.watch', ['views'], function()
  199. {
  200. gulp.watch(['src/views/**/*.pug'], ['views']);
  201. })
  202.  
  203. ///////////////////////////////////////////////////////////////////
  204.  
  205. /**
  206. * Task: Statics
  207. * Copy the statics files from the sources to the build folder
  208. *
  209. * @version 1.0
  210. * @author Alexandre Masy <hello@alexandremasy.com>
  211. **/
  212. gulp.task('statics', function()
  213. {
  214. return gulp.src(['src/statics/**'])
  215. .pipe(gulp.dest('build/statics/'));
  216. });
  217.  
  218. /**
  219. * Task: Clean
  220. * Start fresh - Delete all the build files
  221. *
  222. * @version 1.1
  223. * @author Alexandre Masy <hello@alexandremasy.com>
  224. **/
  225. gulp.task('clean', function()
  226. {
  227. var del = require('del');
  228. return del(['build/*']);
  229. })
  230.  
  231. ///////////////////////////////////////////////////////////////////
  232.  
  233. /**
  234. * Task: Connect
  235. * Create a local server to work with. Isn't it nice ?
  236. *
  237. * @version 1.1
  238. * @author Alexandre Masy <hello@alexandremasy.com>
  239. **/
  240. gulp.task('connect', function()
  241. {
  242. connect.server({
  243. root:'build/',
  244. livereload: true
  245. });
  246. });
  247.  
  248. ///////////////////////////////////////////////////////////////////
  249.  
  250. /**
  251. * Task: Watch
  252. * Watch for file change and trigger the proper workflow.
  253. *
  254. * @version 1.1
  255. * @author Alexandre Masy <hello@alexandremasy.com>
  256. **/
  257. gulp.task('watch', ['clean'], function()
  258. {
  259. gulp.start(['styles.watch', 'libs.vendor', 'libs.watch', 'views.watch', 'statics', 'connect']);
  260. });
  261.  
  262. /**
  263. * Task: Dev
  264. * Create a development build of the sources
  265. *
  266. * @version 1.0
  267. * @author Alexandre Masy <hello@alexandremasy.com>
  268. **/
  269. gulp.task('dev', ['clean'], function()
  270. {
  271. gulp.start(['styles.dev', 'libs.vendor', 'libs.dev', 'views', 'statics']);
  272. });
  273.  
  274. /**
  275. * Task: Prod
  276. * Create a production build of the sources
  277. *
  278. * @version 1.0
  279. * @author Alexandre Masy <hello@alexandremasy.com>
  280. **/
  281. gulp.task('prod', ['clean'], function()
  282. {
  283. gulp.start(['styles.prod', 'libs.vendor', 'libs.prod', 'views', 'statics']);
  284. });
  285.  
  286. ///////////////////////////////////////////////////////////////////
  287.  
  288. gulp.task('default', function()
  289. {
  290. var gutil = require('gulp-util');
  291.  
  292. gutil.log();
  293. gutil.log('-----------------------------------');
  294. gutil.log(gutil.colors.green('Alexandre Masy - Build'));
  295. gutil.log(gutil.colors.green('Version: 1.0'));
  296. gutil.log();
  297. gutil.log(gutil.colors.green('Usage: gulp <command>'));
  298. gutil.log(gutil.colors.green('Where <command> is:'));
  299. gutil.log(gutil.colors.red('$ gulp dev') + gutil.colors.blue(' - Create a development build of the sources'));
  300. gutil.log(gutil.colors.red('$ gulp prod') + gutil.colors.blue(' - Create a production build of the sources'));
  301. gutil.log(gutil.colors.red('$ gulp watch') + gutil.colors.blue(' - Watch for file change and trigger the proper workflow.'));
  302. gutil.log();
  303. gutil.log('-----------------------------------');
  304. gutil.log();
  305. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement