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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 getMessages = require('./utils/getMessages');
  31. const config = getConfig({ production: false });
  32. const port = process.env.PORT || 3000;
  33. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  34. const host = process.env.HOST || 'localhost';
  35. const proxy = process.env.PROXY || 'http://localhost:9000';
  36. const compiler = setupCompiler(host, port, protocol);
  37. runDevServer(compiler, host, port, protocol);
  38. function setupCompiler(host, port, protocol) {
  39. const compiler = webpack(config);
  40. compiler.plugin('invalid', () => {
  41. clearConsole();
  42. console.log('Compiling...');
  43. });
  44. compiler.plugin('done', stats => {
  45. clearConsole();
  46. const jsonStats = stats.toJson({}, true);
  47. const messages = formatWebpackMessages(jsonStats);
  48. const seconds = jsonStats.time / 1000;
  49. if (!messages.errors.length && !messages.warnings.length) {
  50. console.log(chalk.green('Compiled successfully!'));
  51. console.log('Duration: ' + seconds.toFixed(2) + 's');
  52. console.log();
  53. console.log('The app is running at:');
  54. console.log();
  55. console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
  56. console.log();
  57. }
  58. if (messages.errors.length) {
  59. console.log(chalk.red('Failed to compile.'));
  60. console.log();
  61. messages.errors.forEach(message => {
  62. console.log(message);
  63. console.log();
  64. });
  65. }
  66. });
  67. return compiler;
  68. }
  69. function runDevServer(compiler, host, port, protocol) {
  70. const devServer = new WebpackDevServer(compiler, {
  71. before(app) {
  72. app.use(errorOverlayMiddleware());
  73. app.get('/api/l10n/index', (req, res) => {
  74. getMessages()
  75. .then(messages => res.json({ effectiveLocale: 'en', messages }))
  76. .catch(() => res.status(500));
  77. });
  78. },
  79. compress: true,
  80. clientLogLevel: 'none',
  81. contentBase: paths.appPublic,
  82. disableHostCheck: true,
  83. hot: true,
  84. publicPath: config.output.publicPath,
  85. quiet: true,
  86. watchOptions: {
  87. ignored: /node_modules/
  88. },
  89. https: protocol === 'https',
  90. host,
  91. overlay: false,
  92. historyApiFallback: {
  93. disableDotRule: true
  94. },
  95. proxy: {
  96. '/api': proxy,
  97. '/fonts': proxy,
  98. '/images': proxy,
  99. '/static': proxy,
  100. '/integration': proxy
  101. }
  102. });
  103. devServer.listen(port, err => {
  104. if (err) {
  105. console.log(err);
  106. return;
  107. }
  108. clearConsole();
  109. console.log(chalk.cyan('Starting the development server...'));
  110. console.log();
  111. });
  112. }