gulpfile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. var gulp = require('gulp'),
  3. util = require('util'),
  4. path = require('path'),
  5. gutil = require('gulp-util'),
  6. path = require('path'),
  7. immutable = require('immutable'),
  8. merge = require('merge-stream'),
  9. gulpWebpack = require('gulp-webpack'),
  10. webpack = require('webpack'),
  11. sass = require('gulp-sass'),
  12. sourcemaps = require('gulp-sourcemaps');
  13. var plugins = {
  14. 'Promise': 'imports?this=>global!exports?global.Promise!babel-polyfill',
  15. 'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
  16. },
  17. base = immutable.fromJS(require('./webpack.conf.js')),
  18. options = {
  19. dev: base.mergeDeep({
  20. devtool: 'source-map',
  21. plugins: [
  22. new webpack.DefinePlugin({
  23. 'process.env': { NODE_ENV: '"development"' }
  24. }),
  25. new webpack.ProvidePlugin(plugins),
  26. new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.js", Infinity)
  27. ],
  28. output: {
  29. filename: 'admin.js'
  30. }
  31. }),
  32. prod: base.mergeDeep({
  33. devtool: 'source-map',
  34. plugins: [
  35. new webpack.DefinePlugin({
  36. 'process.env': { NODE_ENV: '"production"' }
  37. }),
  38. new webpack.optimize.UglifyJsPlugin({
  39. sourceMap: true,
  40. compress: {
  41. warnings: false
  42. }
  43. }),
  44. new webpack.ProvidePlugin(plugins),
  45. new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.min.js", Infinity)
  46. ],
  47. output: {
  48. filename: 'admin.min.js'
  49. }
  50. })
  51. };
  52. var compileJS = function(watch) {
  53. var devOpts = options.dev.set('watch', watch),
  54. prodOpts = options.prod.set('watch', watch);
  55. var prod = gulp.src('app/main.js')
  56. .pipe(gulpWebpack(prodOpts.toJS()))
  57. .pipe(gulp.dest('js/'));
  58. /*var dev = gulp.src('app/main.js')
  59. .pipe(gulpWebpack(devOpts.toJS()))
  60. .pipe(gulp.dest('js/'));*/
  61. // return merge(prod, dev);
  62. return prod;
  63. };
  64. var compileCSS = function(event) {
  65. return gulp.src('./scss/**/*.scss')
  66. .on('end', function() {
  67. // console.log(util.inspect(event));
  68. if (event && event.path) {
  69. gutil.log(gutil.colors.green('√'), 'Saved change for "' + event.path.replace(__dirname, '') + '"');
  70. }
  71. })
  72. .on('error', gutil.log)
  73. .pipe(sourcemaps.init())
  74. .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
  75. .pipe(sourcemaps.write('./'))
  76. .pipe(gulp.dest('./css-compiled'));
  77. };
  78. gulp.task('js', function() {
  79. compileJS(false);
  80. });
  81. gulp.task('css', function() {
  82. compileCSS();
  83. });
  84. gulp.task('watch', function() {
  85. compileJS(true);
  86. gulp.watch('./scss/**/*.scss', compileCSS);
  87. });
  88. gulp.task('watch-js', function() {
  89. compileJS(true);
  90. });
  91. gulp.task('watch-css', function() {
  92. compileCSS();
  93. gulp.watch('./scss/**/*.scss', compileCSS);
  94. });
  95. gulp.task('all', ['css', 'js']);
  96. gulp.task('default', ['all']);