You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

start.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* eslint-disable no-console */
  21. process.env.NODE_ENV = 'development';
  22. const chalk = require('chalk');
  23. const webpack = require('webpack');
  24. const WebpackDevServer = require('webpack-dev-server');
  25. const clearConsole = require('react-dev-utils/clearConsole');
  26. const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
  27. const errorOverlayMiddleware = require('react-error-overlay/middleware');
  28. const getConfig = require('../config/webpack.config');
  29. const paths = require('../config/paths');
  30. const config = getConfig({ production: false });
  31. const port = process.env.PORT || 3000;
  32. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  33. const host = process.env.HOST || 'localhost';
  34. const proxy = process.env.PROXY || 'http://localhost:9000';
  35. const compiler = setupCompiler(host, port, protocol);
  36. runDevServer(compiler, host, port, protocol);
  37. function setupCompiler(host, port, protocol) {
  38. const compiler = webpack(config);
  39. compiler.plugin('invalid', () => {
  40. clearConsole();
  41. console.log('Compiling...');
  42. });
  43. compiler.plugin('done', stats => {
  44. clearConsole();
  45. const jsonStats = stats.toJson({}, true);
  46. const messages = formatWebpackMessages(jsonStats);
  47. const seconds = jsonStats.time / 1000;
  48. if (!messages.errors.length && !messages.warnings.length) {
  49. console.log(chalk.green('Compiled successfully!'));
  50. console.log('Duration: ' + seconds.toFixed(2) + 's');
  51. console.log();
  52. console.log('The app is running at:');
  53. console.log();
  54. console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
  55. console.log();
  56. }
  57. if (messages.errors.length) {
  58. console.log(chalk.red('Failed to compile.'));
  59. console.log();
  60. messages.errors.forEach(message => {
  61. console.log(message);
  62. console.log();
  63. });
  64. }
  65. });
  66. return compiler;
  67. }
  68. function runDevServer(compiler, host, port, protocol) {
  69. const devServer = new WebpackDevServer(compiler, {
  70. before(app) {
  71. app.use(errorOverlayMiddleware());
  72. },
  73. compress: true,
  74. clientLogLevel: 'none',
  75. contentBase: paths.appPublic,
  76. disableHostCheck: true,
  77. hot: true,
  78. publicPath: config.output.publicPath,
  79. quiet: true,
  80. watchOptions: {
  81. ignored: /node_modules/
  82. },
  83. https: protocol === 'https',
  84. host,
  85. overlay: false,
  86. proxy: { '/': proxy }
  87. });
  88. devServer.listen(port, err => {
  89. if (err) {
  90. console.log(err);
  91. return;
  92. }
  93. clearConsole();
  94. console.log(chalk.cyan('Starting the development server...'));
  95. console.log();
  96. });
  97. }