gulpfile.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ];
  33. var browserifyTask = function() {
  34. ['index.js', 'play.js'].forEach(function(bundle) {
  35. var appBundler = browserify({
  36. entries: './src/js/' + bundle,
  37. transform: [babelify],
  38. debug: IS_DEVELOPMENT,
  39. // required by watchify
  40. cache: {}, packageCache: {}, fullPaths: IS_DEVELOPMENT
  41. });
  42. (IS_DEVELOPMENT ? dependencies : []).forEach(function(dep) {
  43. appBundler.external(dep);
  44. });
  45. var rebundle = function() {
  46. var start = Date.now();
  47. console.log('Building BROWSERIFY(' + bundle + ') bundle');
  48. appBundler.bundle()
  49. .on('error', gutil.log)
  50. .pipe(source(bundle))
  51. .pipe(gulpif(!IS_DEVELOPMENT, streamify(uglify())))
  52. .pipe(gulp.dest(IS_DEVELOPMENT ? './build/js/' : './dist/js/'))
  53. .pipe(notify(function() {
  54. gutil.log(gutil.colors.green('BROWSERIFY(' + bundle +
  55. ') bundle built in ' + (Date.now() - start) + 'ms'));
  56. }));
  57. };
  58. if (IS_DEVELOPMENT) {
  59. appBundler = watchify(appBundler);
  60. appBundler.on('update', rebundle);
  61. }
  62. rebundle();
  63. });
  64. if (IS_DEVELOPMENT) {
  65. var vendorBundler = browserify({
  66. debug: true,
  67. require: dependencies
  68. });
  69. var start = new Date();
  70. console.log('Building VENDOR bundle');
  71. vendorBundler.bundle()
  72. .on('error', gutil.log)
  73. .pipe(source('vendor.js'))
  74. .pipe(gulp.dest('./build/js/'))
  75. .pipe(notify(function() {
  76. gutil.log(gutil.colors.green(
  77. 'VENDOR bundle built in ' + (Date.now() - start) + 'ms'));
  78. }));
  79. }
  80. };
  81. var cssTask = function() {
  82. if (IS_DEVELOPMENT) {
  83. var run = function() {
  84. var start = new Date();
  85. console.log('Building CSS bundle');
  86. gulp.src('./src/css/main.scss')
  87. .pipe(sourcemaps.init())
  88. .pipe(sass({
  89. errLogToConsole: true
  90. }))
  91. .pipe(sourcemaps.write('./maps'))
  92. .pipe(gulp.dest('./build/css/'))
  93. .pipe(notify({
  94. message: function() {
  95. gutil.log(gutil.colors.green(
  96. 'CSS bundle built in ' + (Date.now() - start) + 'ms'));
  97. },
  98. onLast: true
  99. }));
  100. };
  101. run();
  102. gulp.watch('./src/css/*.scss', run);
  103. } else {
  104. gulp.src('./src/css/main.scss')
  105. .pipe(sass({
  106. errLogToConsole: true
  107. }))
  108. .pipe(autoprefixer({
  109. browsers: ['last 2 versions'],
  110. cascade: false
  111. }))
  112. .pipe(cssmin())
  113. .pipe(gulp.dest('./dist/css/'))
  114. .pipe(notify({
  115. message: function() {
  116. gutil.log(gutil.colors.green('CSS bundle built.'));
  117. },
  118. onLast: true
  119. }));
  120. }
  121. };
  122. var imageminTask = function() {
  123. console.log('Optimizing images');
  124. gulp.src('./src/img/*')
  125. .pipe(imagemin({
  126. progressive: true,
  127. svgoPlugins: [{removeViewBox: false}]
  128. }))
  129. .pipe(gulp.dest('./' + (IS_DEVELOPMENT ? 'build' : 'dist') + '/img/'))
  130. .pipe(notify({
  131. message: function() {
  132. gutil.log(gutil.colors.green('IMAGES/SVG optimized.'));
  133. },
  134. onLast: true
  135. }));
  136. };
  137. var copyTask = function() {
  138. console.log('Copying sound files');
  139. gulp.src('./src/snd/*')
  140. .pipe(gulp.dest(IS_DEVELOPMENT ? './build/snd/' : './dist/snd/'))
  141. .pipe(notify({
  142. message: function() {
  143. gutil.log(gutil.colors.green('SOUND FILES copied.'));
  144. },
  145. onLast: true
  146. }));
  147. };
  148. gulp.task('default', function() {
  149. browserifyTask();
  150. cssTask();
  151. imageminTask();
  152. copyTask();
  153. });