gulpfile.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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('imagesloaded.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', [ 'hint-js', 'hint-test', 'hint-task', 'jsonlint' ]);
  30. // -------------------------- RequireJS makes pkgd -------------------------- //
  31. // refactored from gulp-requirejs-optimize
  32. // https://www.npmjs.com/package/gulp-requirejs-optimize/
  33. var gutil = require('gulp-util');
  34. var through = require('through2');
  35. var requirejs = require('requirejs');
  36. var chalk = require('chalk');
  37. function rjsOptimize( options ) {
  38. options = options || {};
  39. requirejs.define('node/print', [], function() {
  40. return function(msg) {
  41. if( msg.substring(0, 5) === 'Error' ) {
  42. gutil.log( chalk.red( msg ) );
  43. } else {
  44. gutil.log( msg );
  45. }
  46. };
  47. });
  48. var stream = through.obj(function (file, enc, cb) {
  49. if ( file.isNull() ) {
  50. return cb( null, file );
  51. }
  52. options.logLevel = 2;
  53. options.out = function( text ) {
  54. var outFile = new gutil.File({
  55. path: file.relative,
  56. contents: new Buffer( text )
  57. });
  58. cb( null, outFile );
  59. };
  60. gutil.log('RequireJS optimizing');
  61. requirejs.optimize( options, null, function( err ) {
  62. var gulpError = new gutil.PluginError( 'requirejsOptimize', err.message );
  63. stream.emit( 'error', gulpError );
  64. });
  65. });
  66. return stream;
  67. }
  68. // regex for banner comment
  69. var reBannerComment = new RegExp('^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/)\\s*');
  70. function getBanner() {
  71. var src = fs.readFileSync( 'imagesloaded.js', 'utf8' );
  72. var matches = src.match( reBannerComment );
  73. var banner = matches[0].replace( 'imagesLoaded', 'imagesLoaded PACKAGED' );
  74. return banner;
  75. }
  76. function addBanner( str ) {
  77. return replace( /^/, str );
  78. }
  79. gulp.task( 'requirejs', function() {
  80. var banner = getBanner();
  81. // HACK src is not needed
  82. // should refactor rjsOptimize to produce src
  83. return gulp.src('imagesloaded.js')
  84. .pipe( rjsOptimize({
  85. baseUrl: 'bower_components',
  86. optimize: 'none',
  87. include: [
  88. '../imagesloaded'
  89. ]
  90. }) )
  91. // remove named module
  92. .pipe( replace( "'../imagesloaded',", '' ) )
  93. // add banner
  94. .pipe( addBanner( banner ) )
  95. .pipe( rename('imagesloaded.pkgd.js') )
  96. .pipe( gulp.dest('.') );
  97. });
  98. // ----- uglify ----- //
  99. var uglify = require('gulp-uglify');
  100. gulp.task( 'uglify', [ 'requirejs' ], function() {
  101. var banner = getBanner();
  102. gulp.src('imagesloaded.pkgd.js')
  103. .pipe( uglify() )
  104. // add banner
  105. .pipe( addBanner( banner ) )
  106. .pipe( rename('imagesloaded.pkgd.min.js') )
  107. .pipe( gulp.dest('.') );
  108. });
  109. // ----- version ----- //
  110. // set version in source files
  111. var minimist = require('minimist');
  112. // use gulp version -t 1.2.3
  113. gulp.task( 'version', function() {
  114. var args = minimist( process.argv.slice(3) );
  115. var version = args.t;
  116. if ( !version || !/\d+\.\d+\.\d+/.test( version ) ) {
  117. gutil.log( 'invalid version: ' + chalk.red( version ) );
  118. return;
  119. }
  120. gutil.log( 'ticking version to ' + chalk.green( version ) );
  121. gulp.src('imagesloaded.js')
  122. .pipe( replace( /imagesLoaded v\d+\.\d+\.\d+/, 'imagesLoaded v' + version ) )
  123. .pipe( gulp.dest('.') );
  124. gulp.src( [ 'bower.json', 'package.json' ] )
  125. .pipe( replace( /"version": "\d+\.\d+\.\d+"/, '"version": "' + version + '"' ) )
  126. .pipe( gulp.dest('.') );
  127. // replace CDN links in README
  128. var minorVersion = version.match( /^\d+\.\d+/ )[0];
  129. gulp.src('README.md')
  130. .pipe( replace( /imagesloaded@\d+\.\d+/g, 'imagesloaded@' + minorVersion ))
  131. .pipe( gulp.dest('.') );
  132. });
  133. // ----- default ----- //
  134. gulp.task( 'default', [
  135. 'hint',
  136. 'uglify'
  137. ]);