gulpfile.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 reactify = require('reactify');
  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. 'react-router',
  28. 'flux',
  29. 'eventemitter2',
  30. 'immutable'
  31. ];
  32. var browserifyTask = function() {
  33. var appBundler = browserify({
  34. entries: './src/js/app.js',
  35. transform: [[reactify, {harmony: true}]],
  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');
  46. appBundler.bundle()
  47. .on('error', gutil.log)
  48. .pipe(source('app.js'))
  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 built in ' +
  53. (Date.now() - start) + 'ms'));
  54. }));
  55. };
  56. if (IS_DEVELOPMENT) {
  57. appBundler = watchify(appBundler);
  58. appBundler.on('update', rebundle);
  59. }
  60. rebundle();
  61. if (IS_DEVELOPMENT) {
  62. var vendorBundler = browserify({
  63. debug: true,
  64. require: dependencies
  65. });
  66. var start = new Date();
  67. console.log('Building VENDOR bundle');
  68. vendorBundler.bundle()
  69. .on('error', gutil.log)
  70. .pipe(source('vendor.js'))
  71. .pipe(gulp.dest('./build/js/'))
  72. .pipe(notify(function() {
  73. gutil.log(gutil.colors.green(
  74. 'VENDOR bundle built in ' + (Date.now() - start) + 'ms'));
  75. }));
  76. }
  77. };
  78. var cssTask = function() {
  79. if (IS_DEVELOPMENT) {
  80. var run = function() {
  81. var start = new Date();
  82. console.log('Building CSS bundle');
  83. gulp.src('./src/css/main.scss')
  84. .pipe(sourcemaps.init())
  85. .pipe(sass({
  86. errLogToConsole: true
  87. }))
  88. .pipe(sourcemaps.write('./maps'))
  89. .pipe(gulp.dest('./build/css/'))
  90. .pipe(notify({
  91. message: function() {
  92. gutil.log(gutil.colors.green(
  93. 'CSS bundle built in ' + (Date.now() - start) + 'ms'));
  94. },
  95. onLast: true
  96. }));
  97. };
  98. run();
  99. gulp.watch('./src/css/*.scss', run);
  100. } else {
  101. gulp.src('./src/css/main.scss')
  102. .pipe(sass({
  103. errLogToConsole: true
  104. }))
  105. .pipe(autoprefixer({
  106. browsers: ['last 2 versions'],
  107. cascade: false
  108. }))
  109. .pipe(cssmin())
  110. .pipe(gulp.dest('./dist/css/'))
  111. .pipe(notify({
  112. message: function() {
  113. gutil.log(gutil.colors.green('CSS bundle built.'));
  114. },
  115. onLast: true
  116. }));
  117. }
  118. };
  119. var imageminTask = function() {
  120. gulp.src('./src/img/*')
  121. .pipe(imagemin({
  122. progressive: true,
  123. svgoPlugins: [{removeViewBox: false}]
  124. }))
  125. .pipe(gulp.dest('./dist/img/'))
  126. .pipe(notify({
  127. message: function() {
  128. gutil.log(gutil.colors.green('IMAGES/SVG optimized.'));
  129. },
  130. onLast: true
  131. }));
  132. };
  133. gulp.task('default', function() {
  134. browserifyTask();
  135. cssTask();
  136. imageminTask();
  137. });