gulpfile.babel.js 1.7 KB

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