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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 fs = require('fs');
  23. const chalk = require('chalk');
  24. const webpack = require('webpack');
  25. const WebpackDevServer = require('webpack-dev-server');
  26. const clearConsole = require('react-dev-utils/clearConsole');
  27. const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
  28. const errorOverlayMiddleware = require('react-error-overlay/middleware');
  29. const getMessages = require('./utils/getMessages');
  30. const getConfigs = require('../config/webpack.config');
  31. const paths = require('../config/paths');
  32. const configs = getConfigs({ production: false });
  33. const config = configs.find(config => config.name === 'modern');
  34. const port = process.env.PORT || 3000;
  35. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  36. const host = process.env.HOST || 'localhost';
  37. const proxy = process.env.PROXY || 'http://localhost:9000';
  38. // Force start script to proxy l10n request to the server (can be useful when working with plugins/extensions)
  39. const l10nCompiledFlag = process.argv.findIndex(val => val === 'l10nCompiled') >= 0;
  40. const l10nExtensions = process.argv.findIndex(val => val === 'l10nExtensions') >= 0;
  41. const compiler = setupCompiler(host, port, protocol);
  42. runDevServer(compiler, host, port, protocol);
  43. function setupCompiler(host, port, protocol) {
  44. const compiler = webpack(config);
  45. compiler.plugin('invalid', () => {
  46. clearConsole();
  47. console.log('Compiling...');
  48. });
  49. compiler.plugin('done', stats => {
  50. clearConsole();
  51. const jsonStats = stats.toJson({}, true);
  52. const messages = formatWebpackMessages(jsonStats);
  53. const seconds = jsonStats.time / 1000;
  54. if (!messages.errors.length && !messages.warnings.length) {
  55. console.log(chalk.green('Compiled successfully!'));
  56. console.log('Duration: ' + seconds.toFixed(2) + 's');
  57. console.log();
  58. console.log('The app is running at:');
  59. console.log();
  60. console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
  61. console.log();
  62. }
  63. if (messages.errors.length) {
  64. console.log(chalk.red('Failed to compile.'));
  65. console.log();
  66. messages.errors.forEach(message => {
  67. console.log(message);
  68. console.log();
  69. });
  70. }
  71. });
  72. return compiler;
  73. }
  74. function runDevServer(compiler, host, port, protocol) {
  75. const devServer = new WebpackDevServer(compiler, {
  76. before(app) {
  77. app.use(errorOverlayMiddleware());
  78. if (!l10nCompiledFlag) {
  79. app.get('/api/l10n/index', (req, res) => {
  80. getMessages(l10nExtensions)
  81. .then(messages => res.json({ effectiveLocale: 'en', messages }))
  82. .catch(() => res.status(500));
  83. });
  84. }
  85. },
  86. compress: true,
  87. clientLogLevel: 'none',
  88. contentBase: [paths.appPublic, paths.docRoot],
  89. disableHostCheck: true,
  90. hot: true,
  91. publicPath: config.output.publicPath,
  92. quiet: true,
  93. watchOptions: {
  94. ignored: /node_modules/
  95. },
  96. https: protocol === 'https',
  97. host,
  98. overlay: false,
  99. historyApiFallback: {
  100. disableDotRule: true
  101. },
  102. proxy: {
  103. '/api': { target: proxy, changeOrigin: true },
  104. '/static': { target: proxy, changeOrigin: true },
  105. '/integration': { target: proxy, changeOrigin: true },
  106. '/sessions/init': { target: proxy, changeOrigin: true },
  107. '/oauth2': { target: proxy, changeOrigin: true },
  108. '/batch': { target: proxy, changeOrigin: true }
  109. }
  110. });
  111. devServer.listen(port, err => {
  112. if (err) {
  113. console.log(err);
  114. return;
  115. }
  116. clearConsole();
  117. console.log(chalk.cyan('Starting the development server...'));
  118. console.log();
  119. });
  120. }