gulpfile.js 4.4 KB

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