start.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * React Static Boilerplate
  3. * https://github.com/koistya/react-static-boilerplate
  4. *
  5. * Copyright © 2015-2016 Konstantin Tarkus (@koistya)
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE.txt file in the root directory of this source tree.
  9. */
  10. const browserSync = require('browser-sync');
  11. const webpack = require('webpack');
  12. const webpackDevMiddleware = require('webpack-dev-middleware');
  13. const webpackHotMiddleware = require('webpack-hot-middleware');
  14. const task = require('./task');
  15. const config = require('./webpack.config');
  16. task('start', () => new Promise(resolve => {
  17. // Hot Module Replacement (HMR) + React Hot Reload
  18. if (config.debug) {
  19. config.entry.vendor.unshift('react-hot-loader/patch', 'webpack-hot-middleware/client');
  20. config.module.loaders.find(x => x.loader === 'babel-loader')
  21. .query.plugins.unshift('react-hot-loader/babel');
  22. config.plugins.push(new webpack.HotModuleReplacementPlugin());
  23. config.plugins.push(new webpack.NoErrorsPlugin());
  24. }
  25. const bundler = webpack(config);
  26. browserSync({
  27. server: {
  28. baseDir: 'src/static',
  29. middleware: [
  30. webpackDevMiddleware(bundler, {
  31. // IMPORTANT: dev middleware can't access config, so we should
  32. // provide publicPath by ourselves
  33. publicPath: config.output.publicPath,
  34. // pretty colored output
  35. stats: config.stats,
  36. // for other settings see
  37. // http://webpack.github.io/docs/webpack-dev-middleware.html
  38. }),
  39. // bundler should be the same as above
  40. webpackHotMiddleware(bundler),
  41. // Serve index.html for all unknown requests
  42. (req, res, next) => {
  43. if (req.headers.accept.startsWith('text/html')) {
  44. req.url = '/index.html'; // eslint-disable-line no-param-reassign
  45. }
  46. next();
  47. },
  48. ],
  49. },
  50. // no need to watch '*.js' here, webpack will take care of it for us,
  51. // including full page reloads if HMR won't work
  52. files: [
  53. 'build/**/*.css',
  54. 'build/**/*.html',
  55. ],
  56. });
  57. resolve();
  58. }));