gulpfile.babel.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import webpackConfig from './webpack.config.js';
  13. const $ = gulpLoadPlugins();
  14. const bundler = webpack(webpackConfig);
  15. // Default Gulp task
  16. gulp.task('default', ['serve']);
  17. // Clean output directory
  18. gulp.task('clean', () => del(['.tmp', 'build/*', '!build/.git'], {dot: true}));
  19. // Bundling and optimization
  20. gulp.task('bundle', cb => {
  21. function bundle(err, stats) {
  22. if (err) {
  23. throw new $.util.PluginError('webpack', err);
  24. }
  25. console.log(stats.toString(webpackConfig[0].stats));
  26. cb();
  27. }
  28. bundler.run(bundle);
  29. });
  30. // React.js-based web pages
  31. gulp.task('pages', () =>
  32. require('./build/pages.js')()
  33. .pipe(gulp.dest('build'))
  34. );
  35. gulp.task('build', cb => run('clean', 'bundle', 'pages', cb));
  36. gulp.task('serve', cb => {
  37. global.hot = true;
  38. run('build', () => {
  39. console.log('outputPath:', bundler.outputPath);
  40. const server = new WebpackDevServer(bundler, {
  41. contentBase: './build',
  42. hot: true,
  43. quiet: false,
  44. noInfo: false,
  45. lazy: false,
  46. filename: '../app.js',
  47. watchOptions: {
  48. aggregateTimeout: 300,
  49. poll: 1000
  50. },
  51. publicPath: 'http://localhost:3000/',
  52. stats: { colors: true },
  53. historyApiFallback: true
  54. });
  55. server.listen(3000, 'localhost', () => {
  56. $.util.log('Server is running at ', $.util.colors.magenta('http://localhost:3000/'));
  57. cb();
  58. });
  59. });
  60. });
  61. gulp.task('dev-server', function() {
  62. const server = new WebpackDevServer(bundler, {
  63. contentBase: path.join(__dirname, 'build'),
  64. hot: true,
  65. filename: 'app.js',
  66. watchOptions: {
  67. aggregateTimeout: 300,
  68. poll: 400
  69. },
  70. stats: { colors: true },
  71. historyApiFallback: true
  72. });
  73. server.listen(3000, 'localhost', function (err) {
  74. if (err) { console.log(err); }
  75. console.log('Listening at localhost:3000');
  76. });
  77. });