gulpfile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var ENV = process.env.NODE_ENV;
  2. var IS_DEVELOPMENT;
  3. if (ENV !== 'development' && ENV !== 'production')
  4. throw new Error('NODE_ENV must be set to either development or production.');
  5. else
  6. IS_DEVELOPMENT = ENV === 'development';
  7. var gulp = require('gulp');
  8. var source = require('vinyl-source-stream');
  9. var browserify = require('browserify');
  10. var watchify = require('watchify');
  11. var babelify = require('babelify');
  12. var gulpif = require('gulp-if');
  13. var uglify = require('gulp-uglify');
  14. var streamify = require('gulp-streamify');
  15. var notify = require('gulp-notify');
  16. var cssmin = require('gulp-cssmin');
  17. var gutil = require('gulp-util');
  18. var sass = require('gulp-sass');
  19. var autoprefixer = require('gulp-autoprefixer');
  20. var sourcemaps = require('gulp-sourcemaps');
  21. var imagemin = require('gulp-imagemin');
  22. // external dependencies that are not rebundled while developing,
  23. // but included in production
  24. var dependencies = [
  25. 'react',
  26. 'react/addons',
  27. 'immutable',
  28. 'flux',
  29. 'eventemitter2',
  30. 'chess.js',
  31. 'lodash.omit',
  32. 'socket.io-client',
  33. 'es6-shim'
  34. ];
  35. var browserifyTask = function() {
  36. ['index.js', 'play.js'].forEach(function(bundle) {
  37. var appBundler = browserify({
  38. entries: './src/js/' + bundle,
  39. transform: [babelify],
  40. debug: IS_DEVELOPMENT,
  41. // required by watchify
  42. cache: {}, packageCache: {}, fullPaths: IS_DEVELOPMENT
  43. });
  44. (IS_DEVELOPMENT ? dependencies : []).forEach(function(dep) {
  45. appBundler.external(dep);
  46. });
  47. var rebundle = function() {
  48. var start = Date.now();
  49. console.log('Building BROWSERIFY(' + bundle + ') bundle');
  50. appBundler.bundle()
  51. .on('error', gutil.log)
  52. .pipe(source(bundle))
  53. .pipe(gulpif(!IS_DEVELOPMENT, streamify(uglify())))
  54. .pipe(gulp.dest(IS_DEVELOPMENT ? './build/js/' : './dist/js/'))
  55. .pipe(notify(function() {
  56. gutil.log(gutil.colors.green('BROWSERIFY(' + bundle +
  57. ') bundle built in ' + (Date.now() - start) + 'ms'));
  58. }));
  59. };
  60. if (IS_DEVELOPMENT) {
  61. appBundler = watchify(appBundler);
  62. appBundler.on('update', rebundle);
  63. }
  64. rebundle();
  65. });
  66. if (IS_DEVELOPMENT) {
  67. var vendorBundler = browserify({
  68. debug: true,
  69. require: dependencies
  70. });
  71. var start = new Date();
  72. console.log('Building VENDOR bundle');
  73. vendorBundler.bundle()
  74. .on('error', gutil.log)
  75. .pipe(source('vendor.js'))
  76. .pipe(gulp.dest('./build/js/'))
  77. .pipe(notify(function() {
  78. gutil.log(gutil.colors.green(
  79. 'VENDOR bundle built in ' + (Date.now() - start) + 'ms'));
  80. }));
  81. }
  82. };
  83. var cssTask = function() {
  84. if (IS_DEVELOPMENT) {
  85. var run = function() {
  86. var start = new Date();
  87. console.log('Building CSS bundle');
  88. gulp.src('./src/css/main.scss')
  89. .pipe(sourcemaps.init())
  90. .pipe(sass({
  91. errLogToConsole: true
  92. }))
  93. .pipe(sourcemaps.write('./maps'))
  94. .pipe(gulp.dest('./build/css/'))
  95. .pipe(notify({
  96. message: function() {
  97. gutil.log(gutil.colors.green(
  98. 'CSS bundle built in ' + (Date.now() - start) + 'ms'));
  99. },
  100. onLast: true
  101. }));
  102. };
  103. run();
  104. gulp.watch('./src/css/*.scss', run);
  105. } else {
  106. gulp.src('./src/css/main.scss')
  107. .pipe(sass({
  108. errLogToConsole: true
  109. }))
  110. .pipe(autoprefixer({
  111. browsers: ['last 2 versions'],
  112. cascade: false
  113. }))
  114. .pipe(cssmin())
  115. .pipe(gulp.dest('./dist/css/'))
  116. .pipe(notify({
  117. message: function() {
  118. gutil.log(gutil.colors.green('CSS bundle built.'));
  119. },
  120. onLast: true
  121. }));
  122. }
  123. };
  124. var imageminTask = function() {
  125. console.log('Optimizing images');
  126. gulp.src('./src/img/*')
  127. .pipe(imagemin({
  128. progressive: true,
  129. svgoPlugins: [{removeViewBox: false}]
  130. }))
  131. .pipe(gulp.dest('./' + (IS_DEVELOPMENT ? 'build' : 'dist') + '/img/'))
  132. .pipe(notify({
  133. message: function() {
  134. gutil.log(gutil.colors.green('IMAGES/SVG optimized.'));
  135. },
  136. onLast: true
  137. }));
  138. };
  139. var copyTask = function() {
  140. console.log('Copying sound files and fonts');
  141. gulp.src('./src/snd/*')
  142. .pipe(gulp.dest(IS_DEVELOPMENT ? './build/snd/' : './dist/snd/'))
  143. .pipe(notify({
  144. message: function() {
  145. gutil.log(gutil.colors.green('SOUND FILES copied.'));
  146. },
  147. onLast: true
  148. }));
  149. gulp.src('./src/fonts/*')
  150. .pipe(gulp.dest(IS_DEVELOPMENT ? './build/fonts/' : './dist/fonts/'))
  151. .pipe(notify({
  152. message: function() {
  153. gutil.log(gutil.colors.green('FONTS copied.'));
  154. },
  155. onLast: true
  156. }));
  157. };
  158. gulp.task('default', function() {
  159. browserifyTask();
  160. cssTask();
  161. imageminTask();
  162. copyTask();
  163. });