gulpfile.babel.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * React Static Boilerplate
  3. * Copyright (c) Konstantin Tarkus | MIT License
  4. */
  5. import gulp from 'gulp';
  6. import gulpLoadPlugins from 'gulp-load-plugins';
  7. import del from 'del';
  8. import run from 'run-sequence';
  9. import minimist from 'minimist';
  10. import webpack from 'webpack';
  11. import WebpackDevServer from 'webpack-dev-server';
  12. import webpackConfig from './webpack.config.js';
  13. const $ = gulpLoadPlugins();
  14. const argv = minimist(process.argv.slice(2));
  15. const verbose = !!argv.verbose;
  16. const bundler = webpack(webpackConfig);
  17. // Default Gulp task
  18. gulp.task('default', ['serve']);
  19. // Clean output directory
  20. gulp.task('clean', () => del(['.tmp', 'build/*', '!build/.git'], {dot: true}));
  21. // Bundling and optimization
  22. gulp.task('bundle', cb => {
  23. function bundle(err, stats) {
  24. if (err) {
  25. throw new $.util.PluginError('webpack', err);
  26. }
  27. console.log(stats.toString(webpackConfig[0].stats));
  28. cb();
  29. }
  30. bundler.run(bundle);
  31. });
  32. // React.js-based web pages
  33. gulp.task('pages', () =>
  34. require('./build/pages.js')()
  35. .pipe(gulp.dest('build'))
  36. );
  37. gulp.task('build', cb => run('clean', 'bundle', 'pages', cb));
  38. gulp.task('serve', cb => {
  39. global.hot = true;
  40. run('build', () => {
  41. console.log('outputPath:', bundler.outputPath);
  42. const server = new WebpackDevServer(bundler, {
  43. contentBase: './build',
  44. hot: true,
  45. quiet: false,
  46. noInfo: false,
  47. lazy: false,
  48. filename: '../app.js',
  49. watchOptions: {
  50. aggregateTimeout: 300,
  51. poll: 1000
  52. },
  53. publicPath: 'http://localhost:3000/',
  54. stats: { colors: true },
  55. historyApiFallback: true
  56. });
  57. server.listen(3000, 'localhost', () => {
  58. $.util.log('Server is running at ', $.util.colors.magenta('http://localhost:3000/'));
  59. cb();
  60. });
  61. });
  62. });