gulpfile.babel.js 2.2 KB

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