start.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * React Starter Kit (https://www.reactstarterkit.com/)
  3. *
  4. * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
  5. *
  6. * This source code is licensed under the MIT license found in the
  7. * LICENSE.txt file in the root directory of this source tree.
  8. */
  9. import Browsersync from 'browser-sync';
  10. import webpack from 'webpack';
  11. import webpackMiddleware from 'webpack-middleware';
  12. import webpackHotMiddleware from 'webpack-hot-middleware';
  13. import run from './run';
  14. import runServer from './runServer';
  15. import webpackConfig from './webpack.config';
  16. import clean from './clean';
  17. import copy from './copy';
  18. const DEBUG = !process.argv.includes('--release');
  19. /**
  20. * Launches a development web server with "live reload" functionality -
  21. * synchronizing URLs, interactions and code changes across multiple devices.
  22. */
  23. async function start() {
  24. await run(clean);
  25. await run(copy.bind(undefined, { watch: true }));
  26. await new Promise(resolve => {
  27. // Patch the client-side bundle configurations
  28. // to enable Hot Module Replacement (HMR) and React Transform
  29. webpackConfig.filter(x => x.target !== 'node').forEach(config => {
  30. if (Array.isArray(config.entry)) {
  31. config.entry.unshift('webpack-hot-middleware/client');
  32. } else {
  33. /* eslint-disable no-param-reassign */
  34. config.entry = ['webpack-hot-middleware/client', config.entry];
  35. /* eslint-enable no-param-reassign */
  36. }
  37. config.plugins.push(new webpack.HotModuleReplacementPlugin());
  38. config.plugins.push(new webpack.NoErrorsPlugin());
  39. config
  40. .module
  41. .loaders
  42. .filter(x => x.loader === 'babel-loader')
  43. .forEach(x => (x.query = { // eslint-disable-line no-param-reassign
  44. // Wraps all React components into arbitrary transforms
  45. // https://github.com/gaearon/babel-plugin-react-transform
  46. plugins: [
  47. ['react-transform', {
  48. transforms: [
  49. {
  50. transform: 'react-transform-hmr',
  51. imports: ['react'],
  52. locals: ['module'],
  53. }, {
  54. transform: 'react-transform-catch-errors',
  55. imports: ['react', 'redbox-react'],
  56. },
  57. ],
  58. },
  59. ],
  60. ],
  61. }));
  62. });
  63. const bundler = webpack(webpackConfig);
  64. const wpMiddleware = webpackMiddleware(bundler, {
  65. // IMPORTANT: webpack middleware can't access config,
  66. // so we should provide publicPath by ourselves
  67. publicPath: webpackConfig[0].output.publicPath,
  68. // Pretty colored output
  69. stats: webpackConfig[0].stats,
  70. // For other settings see
  71. // https://webpack.github.io/docs/webpack-dev-middleware
  72. });
  73. const hotMiddlewares = bundler
  74. .compilers
  75. .filter(compiler => compiler.options.target !== 'node')
  76. .map(compiler => webpackHotMiddleware(compiler));
  77. let handleServerBundleComplete = () => {
  78. runServer((err, host) => {
  79. if (!err) {
  80. const bs = Browsersync.create();
  81. bs.init({
  82. ...(DEBUG ? {} : { notify: false, ui: false }),
  83. proxy: {
  84. target: host,
  85. middleware: [wpMiddleware, ...hotMiddlewares],
  86. },
  87. // no need to watch '*.js' here, webpack will take care of it for us,
  88. // including full page reloads if HMR won't work
  89. files: ['build/content/**/*.*'],
  90. }, resolve);
  91. handleServerBundleComplete = runServer;
  92. }
  93. });
  94. };
  95. bundler.plugin('done', () => handleServerBundleComplete());
  96. });
  97. }
  98. export default start;