gulpfile.js 4.4 KB

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