Advertisement
Guest User

example of gulpfile

a guest
Feb 17th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 5.78 KB | None | 0 0
  1. var gulp    = require('gulp'),
  2. browserSync = require('browser-sync'),
  3. concat      = require('gulp-concat'),
  4. imagemin    = require('gulp-imagemin'),
  5. pngquant    = require('imagemin-pngquant'),
  6. plumber     = require('gulp-plumber'),
  7. notifier    = require('node-notifier'),
  8. cssnano     = require('gulp-cssnano'),
  9. eslint      = require('gulp-eslint'),
  10. del         = require('del'),
  11. replace     = require('gulp-replace'),
  12. stylus      = require('gulp-stylus'),
  13. jeet        = require('jeet'),
  14. rupture     = require('rupture'),
  15. csso        = require('gulp-csso'),
  16. extReplace  = require('gulp-ext-replace'),
  17. runSequence = require('run-sequence'),
  18. cmq         = require('gulp-merge-media-queries'),
  19. nunjucks    = require('gulp-nunjucks-html'),
  20. path        = require('path'),
  21. data        = require('gulp-data'),
  22. Server      = require('karma').Server,
  23. reload      = browserSync.reload;
  24.  
  25. // watch files for changes and reload
  26. gulp.task('serve', function() {
  27.   'use strict';
  28.   browserSync({
  29.       proxy: 'esif.localhost'
  30.   });
  31.   // Perform the site init
  32.   runSequence('build:dev');
  33.  
  34.   // Compile Stylus
  35.   gulp.watch('src/styles/**/*.styl', ['styles']);
  36.  
  37.   // Compile Standard JS
  38.   gulp.watch('src/scripts/*.js', ['scripts']);
  39.  
  40.   // Compile Standard JS
  41.   gulp.watch('src/images', ['images']);
  42.  
  43.   // Compile HTML
  44.   gulp.watch('src/html/**/*.nunjucks', ['nunjucks']);
  45.  
  46.   // Compile JSON
  47.   gulp.watch('src/model/**/*.json', ['nunjucks']);
  48. });
  49.  
  50. var errors = 0,
  51.     supportedBrowsers = [
  52.     'last 2 versions',
  53.     'safari >= 8',
  54.     'ie >= 10',
  55.     'ff >= 20',
  56.     'ios 6',
  57.     'android 4'
  58. ],
  59. onError = function (err) {
  60.   'use strict';
  61.   notifier.notify({ title: 'Development Build', message: 'Failed', icon: 'failed.png' });
  62.   console.log(err);
  63.   errors = errors+1
  64.   this.emit('end');
  65. };
  66.  
  67. // Combine styles
  68. gulp.task('styles', function(callback) {
  69.   'use strict';
  70.   return gulp.src('src/styles/core.styl')
  71.     .pipe(plumber(
  72.       { errorHandler: onError }
  73.     ))
  74.     .pipe(stylus({use: [jeet(), rupture()]}))
  75.     .pipe(concat('core.min.css'))
  76.     .pipe(cmq())
  77.     .pipe(csso())
  78.     .pipe(cssnano({
  79.       autoprefixer: {browsers: supportedBrowsers, add: true}
  80.     }))
  81.     .pipe(gulp.dest('dist/static/css'))
  82.     .pipe(reload({stream:true}))
  83.      callback();
  84. });
  85.  
  86. // Process and move supplementary CSS
  87. gulp.task('extrastyles', function(callback) {
  88.   'use strict';
  89.   gulp.src('src/styles/print.styl')
  90.     .pipe(plumber(
  91.       { errorHandler: onError }
  92.     ))
  93.     .pipe(stylus())
  94.     .pipe(concat('print.min.css'))
  95.     .pipe(gulp.dest('dist/static/css'))
  96.   gulp.src('src/styles/fonts.css')
  97.     .pipe(gulp.dest('dist/static/css'))
  98.      callback();
  99. });
  100.  
  101. // Move libraries
  102. gulp.task('libs', function() {
  103.   'use strict';
  104.   return gulp.src('node_modules/jquery/dist/jquery.min.js')
  105.     .pipe(plumber(
  106.       { errorHandler: onError }
  107.     ))
  108.     .pipe(gulp.dest('dist/static/libs'))
  109. });
  110.  
  111. // Combine JS
  112. gulp.task('scripts', function() {
  113.   'use strict';
  114.   return gulp.src('src/scripts/*.js')
  115.     .pipe(plumber(
  116.       { errorHandler: onError }
  117.     ))
  118.     .pipe(eslint())
  119.     .pipe(concat('core.min.js'))
  120.     .pipe(gulp.dest('dist/static/scripts'))
  121.     .pipe(reload({stream:true}))
  122. });
  123.  
  124. // Process nunjucks html files (.nunjucks)
  125. gulp.task('nunjucks', function() {
  126.   'use strict';
  127.   return gulp.src('src/html/pages/**/*.nunjucks')
  128.     .pipe(plumber(
  129.       { errorHandler: onError }
  130.     ))
  131.     .pipe(data(function(file) {
  132.       return require('./src/model/' + path.basename(file.path) + '.json');
  133.     }))
  134.     .pipe(data(function() {
  135.       return require('./src/model/globals.json');
  136.     }))
  137.     .pipe(nunjucks({
  138.       searchPaths: ['src/html/templates']
  139.     }))
  140.     .pipe(extReplace('.html'))
  141.     .pipe(gulp.dest('dist'))
  142.     .pipe(reload({stream:true}))
  143. });
  144.  
  145. // Replaces variables in the master page (layout.nunjucks)
  146. gulp.task('processHTML', ['nunjucks'], function () {
  147.   'use strict';
  148.   return gulp.src(['dist/**/*.html'])
  149.    .pipe(replace('$$site_name$$', 'esif'))
  150.    .pipe(replace('$$site_url$$', 'localhost:3000'))
  151.    .pipe(replace('$$site_desc$$', 'To be added'))
  152.    .pipe(gulp.dest('dist'))
  153. });
  154.  
  155. // Perform Basic Build (note, don't call directly, use build:dev or build)
  156. gulp.task('build', function (callback) {
  157.   'use strict';
  158.   runSequence(
  159.     'processHTML',
  160.     'styles',
  161.     'extrastyles',
  162.     'libs',
  163.     'scripts',
  164.     callback
  165.   );
  166. });
  167.  
  168. // Standard build - not deployment ready and doesn't perform a clean build
  169. gulp.task('build:dev', function() {
  170.   'use strict';
  171.   runSequence('build');
  172.   notifier.notify({ title: 'Development Build', message: 'Completed', icon: 'passed.png' });
  173. })
  174.  
  175. // Production Build - ready for deployment and cleans build first
  176. gulp.task('build:prod', function() {
  177.   'use strict';
  178.   runSequence('clean', ['build', 'images']);
  179.   notifier.notify({ title: 'Production Build', message: 'Done', icon: 'passed.png' });
  180. })
  181.  
  182. // Compress and minify images to reduce their file size
  183. gulp.task('images', function() {
  184.   'use strict';
  185.   var imgSrc = 'src/images/**/*',
  186.       imgDst = 'dist/static/images';
  187.  
  188.   return gulp.src(imgSrc)
  189.     .pipe(plumber())
  190.     .pipe(imagemin({
  191.          progressive: true,
  192.          svgoPlugins: [{removeViewBox: false}],
  193.          use: [pngquant()]
  194.     }))
  195.     .pipe(gulp.dest(imgDst))
  196. });
  197. // Deletes contents of dist folder
  198. gulp.task('clean', function(cb) {
  199.     'use strict';
  200.     del(['dist/**/*.*'], cb)
  201. });
  202.  
  203. // Run a single batch of tests
  204. gulp.task('test', function (done) {
  205.   'use strict';
  206.   return new Server({
  207.     configFile: __dirname + '/karma.conf.js',
  208.     singleRun: true
  209.   }, done).start();
  210. });
  211.  
  212. // Run tests continiously
  213. gulp.task('tdd', function (done) {
  214.   'use strict';
  215.   return new Server({
  216.     configFile: __dirname + '/karma.conf.js',
  217.   }, done).start();
  218. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement