webserver.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Do this as the first thing so that any code reading it knows the right env.
  2. process.env.BABEL_ENV = 'development';
  3. process.env.NODE_ENV = 'development';
  4. process.env.ASSET_PATH = '/';
  5. const WebpackDevServer = require('webpack-dev-server');
  6. const webpack = require('webpack');
  7. const path = require('path');
  8. const config = require('../webpack.config');
  9. const env = require('./env');
  10. const options = config.chromeExtensionBoilerplate || {};
  11. const excludeEntriesToHotReload = options.notHotReload || [];
  12. for (const entryName in config.entry) {
  13. if (excludeEntriesToHotReload.indexOf(entryName) === -1) {
  14. config.entry[entryName] = [
  15. 'webpack/hot/dev-server',
  16. `webpack-dev-server/client?hot=true&hostname=localhost&port=${env.PORT}`,
  17. ].concat(config.entry[entryName]);
  18. }
  19. }
  20. config.plugins = [new webpack.HotModuleReplacementPlugin()].concat(
  21. config.plugins || []
  22. );
  23. delete config.chromeExtensionBoilerplate;
  24. const compiler = webpack(config);
  25. const server = new WebpackDevServer(
  26. {
  27. https: false,
  28. hot: false,
  29. client: false,
  30. host: 'localhost',
  31. port: env.PORT,
  32. static: {
  33. directory: path.join(__dirname, '../build'),
  34. },
  35. devMiddleware: {
  36. publicPath: `http://localhost:${env.PORT}/`,
  37. writeToDisk: true,
  38. },
  39. headers: {
  40. 'Access-Control-Allow-Origin': '*',
  41. },
  42. allowedHosts: 'all',
  43. },
  44. compiler
  45. );
  46. if (process.env.NODE_ENV === 'development' && module.hot) {
  47. module.hot.accept();
  48. }
  49. (async () => {
  50. await server.start();
  51. })();