gulpfile.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*jshint node: true, strict: false */
  2. var fs = require('fs');
  3. var gulp = require('gulp');
  4. var rename = require('gulp-rename');
  5. var replace = require('gulp-replace');
  6. // ----- hint ----- //
  7. var jshint = require('gulp-jshint');
  8. gulp.task( 'hint-js', function() {
  9. return gulp.src('js/**/*.js')
  10. .pipe( jshint() )
  11. .pipe( jshint.reporter('default') );
  12. });
  13. gulp.task( 'hint-test', function() {
  14. return gulp.src('test/unit/*.js')
  15. .pipe( jshint() )
  16. .pipe( jshint.reporter('default') );
  17. });
  18. gulp.task( 'hint-task', function() {
  19. return gulp.src('gulpfile.js')
  20. .pipe( jshint() )
  21. .pipe( jshint.reporter('default') );
  22. });
  23. var jsonlint = require('gulp-json-lint');
  24. gulp.task( 'jsonlint', function() {
  25. return gulp.src( '*.json' )
  26. .pipe( jsonlint() )
  27. .pipe( jsonlint.report('verbose') );
  28. });
  29. gulp.task( 'hint',
  30. gulp.parallel('hint-js', 'hint-test', 'hint-task', 'jsonlint' ) );
  31. // -------------------------- RequireJS makes pkgd -------------------------- //
  32. var gutil = require('gulp-util');
  33. var chalk = require('chalk');
  34. var rjsOptimize = require('gulp-requirejs-optimize');
  35. // regex for banner comment
  36. var reBannerComment = new RegExp('^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/)\\s*');
  37. function getBanner() {
  38. var src = fs.readFileSync( 'js/index.js', 'utf8' );
  39. var matches = src.match( reBannerComment );
  40. var banner = matches[0].replace( 'Flickity', 'Flickity PACKAGED' );
  41. return banner;
  42. }
  43. function addBanner( str ) {
  44. return replace( /^/, str );
  45. }
  46. gulp.task( 'requirejs', function() {
  47. var banner = getBanner();
  48. // HACK src is not needed
  49. // should refactor rjsOptimize to produce src
  50. return gulp.src('js/index.js')
  51. .pipe( rjsOptimize({
  52. baseUrl: 'bower_components',
  53. optimize: 'none',
  54. include: [
  55. 'jquery-bridget/jquery-bridget',
  56. 'flickity/js/index',
  57. 'flickity-as-nav-for/as-nav-for',
  58. 'flickity-imagesloaded/flickity-imagesloaded'
  59. ],
  60. paths: {
  61. flickity: '../',
  62. jquery: 'empty:'
  63. }
  64. }) )
  65. // remove named module
  66. .pipe( replace( "'flickity-imagesloaded/flickity-imagesloaded',", '' ) )
  67. // add banner
  68. .pipe( addBanner( banner ) )
  69. .pipe( rename('flickity.pkgd.js') )
  70. .pipe( gulp.dest('dist') );
  71. });
  72. // ----- uglify ----- //
  73. var uglify = require('gulp-uglify');
  74. gulp.task( 'uglify', function() {
  75. var banner = getBanner();
  76. return gulp.src('dist/flickity.pkgd.js')
  77. .pipe( uglify() )
  78. // add banner
  79. .pipe( addBanner( banner ) )
  80. .pipe( rename('flickity.pkgd.min.js') )
  81. .pipe( gulp.dest('dist') );
  82. });
  83. // ----- css ----- //
  84. var cleanCSS = require('gulp-clean-css');
  85. gulp.task( 'css', function() {
  86. return gulp.src('css/flickity.css')
  87. // copy to dist
  88. .pipe( gulp.dest('dist') )
  89. // minify
  90. .pipe( cleanCSS({ advanced: false }) )
  91. .pipe( rename('flickity.min.css') )
  92. .pipe( replace( '*/', '*/\n' ) )
  93. .pipe( gulp.dest('dist') );
  94. });
  95. // ----- version ----- //
  96. // set version in source files
  97. var minimist = require('minimist');
  98. var merge2 = require('merge2');
  99. // use gulp version -t 1.2.3
  100. gulp.task( 'version', function() {
  101. var args = minimist( process.argv.slice(3) );
  102. var version = args.t;
  103. if ( !version || !/\d\.\d\.\d/.test( version ) ) {
  104. gutil.log( 'invalid version: ' + chalk.red( version ) );
  105. return;
  106. }
  107. gutil.log( 'ticking version to ' + chalk.green( version ) );
  108. function sourceReplace() {
  109. return replace( /Flickity v\d\.\d+\.\d+/, 'Flickity v' + version );
  110. }
  111. var indexJs = gulp.src('js/index.js')
  112. .pipe( sourceReplace() )
  113. .pipe( gulp.dest('js') );
  114. var css = gulp.src('css/flickity.css')
  115. .pipe( sourceReplace() )
  116. .pipe( gulp.dest('css') );
  117. var json = gulp.src('package.json')
  118. .pipe( replace( /"version": "\d\.\d+\.\d+"/, '"version": "' + version + '"' ) )
  119. .pipe( gulp.dest('.') );
  120. // create a merged stream for array of streams
  121. return merge2([ indexJs, css, json ]);
  122. });
  123. // ----- default ----- //
  124. gulp.task( 'default', gulp.parallel(
  125. 'hint',
  126. 'css',
  127. gulp.series( 'requirejs', 'uglify' )
  128. ));