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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2016 SonarSource SA
  4. * mailto:contact 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. process.env.NODE_ENV = 'development';
  21. var url = require('url');
  22. var express = require('express');
  23. var proxy = require('express-http-proxy');
  24. var webpack = require('webpack');
  25. var chalk = require('chalk');
  26. var config = require('../config/webpack/webpack.config.dev.js');
  27. var app = express();
  28. var DEFAULT_PORT = process.env.PORT || 8080;
  29. var API_HOST = process.env.API_HOST || 'http://localhost:9000';
  30. var compiler;
  31. var friendlySyntaxErrorLabel = 'Syntax error:';
  32. function isLikelyASyntaxError (message) {
  33. return message.indexOf(friendlySyntaxErrorLabel) !== -1;
  34. }
  35. // This is a little hacky.
  36. // It would be easier if webpack provided a rich error object.
  37. function formatMessage (message) {
  38. return message
  39. // Make some common errors shorter:
  40. .replace(
  41. // Babel syntax error
  42. 'Module build failed: SyntaxError:',
  43. friendlySyntaxErrorLabel
  44. )
  45. .replace(
  46. // Webpack file not found error
  47. /Module not found: Error: Cannot resolve 'file' or 'directory'/,
  48. 'Module not found:'
  49. )
  50. // Internal stacks are generally useless so we strip them
  51. .replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, '') // at ... ...:x:y
  52. // Webpack loader names obscure CSS filenames
  53. .replace('./~/css-loader!./~/postcss-loader!', '');
  54. }
  55. function setupCompiler () {
  56. compiler = webpack(config);
  57. compiler.plugin('invalid', function () {
  58. console.log(chalk.cyan.bold('Compiling...'));
  59. });
  60. compiler.plugin('done', function (stats) {
  61. var hasErrors = stats.hasErrors();
  62. var hasWarnings = stats.hasWarnings();
  63. if (!hasErrors && !hasWarnings) {
  64. console.log(chalk.green.bold('Compiled successfully!'));
  65. return;
  66. }
  67. var json = stats.toJson();
  68. var formattedErrors = json.errors.map(message =>
  69. 'Error in ' + formatMessage(message)
  70. );
  71. var formattedWarnings = json.warnings.map(message =>
  72. 'Warning in ' + formatMessage(message)
  73. );
  74. if (hasErrors) {
  75. console.log(chalk.red.bold('Failed to compile:'));
  76. console.log();
  77. if (formattedErrors.some(isLikelyASyntaxError)) {
  78. // If there are any syntax errors, show just them.
  79. // This prevents a confusing ESLint parsing error
  80. // preceding a much more useful Babel syntax error.
  81. formattedErrors = formattedErrors.filter(isLikelyASyntaxError);
  82. }
  83. formattedErrors.forEach(message => {
  84. console.log(message);
  85. console.log();
  86. });
  87. // If errors exist, ignore warnings.
  88. return;
  89. }
  90. if (hasWarnings) {
  91. console.log(chalk.yellow('Compiled with warnings.'));
  92. console.log();
  93. formattedWarnings.forEach(message => {
  94. console.log(message);
  95. console.log();
  96. });
  97. console.log('You may use special comments to disable some warnings.');
  98. console.log('Use ' + chalk.yellow(
  99. '// eslint-disable-next-line') + ' to ignore the next line.');
  100. console.log('Use ' + chalk.yellow(
  101. '/* eslint-disable */') + ' to ignore all warnings in a file.');
  102. }
  103. });
  104. }
  105. function runDevServer (port) {
  106. app.use(require('webpack-dev-middleware')(compiler, {
  107. noInfo: true,
  108. publicPath: config.output.publicPath
  109. }));
  110. app.use(require('webpack-hot-middleware')(compiler));
  111. app.all('*', proxy(API_HOST, {
  112. forwardPath: function (req) {
  113. return url.parse(req.url).path;
  114. }
  115. }));
  116. app.listen(port, 'localhost', function (err) {
  117. if (err) {
  118. console.log(err);
  119. return;
  120. }
  121. console.log(chalk.green.bold(
  122. 'The app is running at http://localhost:' + port + '/'));
  123. console.log(chalk.cyan.bold('Compiling...'));
  124. console.log();
  125. });
  126. }
  127. function run (port) {
  128. setupCompiler();
  129. runDevServer(port);
  130. }
  131. run(DEFAULT_PORT);