diff options
author | Stas Vilchik <vilchiks@gmail.com> | 2016-08-22 15:29:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-22 15:29:35 +0200 |
commit | 2d5742c8521dbc935bb77d9ecbc86f193556aff0 (patch) | |
tree | 507517b139bfa73fe4a00ec571adc919647ea8f2 /server/sonar-web/scripts/start.js | |
parent | 6cee9fdcad049ca21606983b05a0a4cfc13ffd53 (diff) | |
download | sonarqube-2d5742c8521dbc935bb77d9ecbc86f193556aff0.tar.gz sonarqube-2d5742c8521dbc935bb77d9ecbc86f193556aff0.zip |
optimize js build (#1145)
Diffstat (limited to 'server/sonar-web/scripts/start.js')
-rw-r--r-- | server/sonar-web/scripts/start.js | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/server/sonar-web/scripts/start.js b/server/sonar-web/scripts/start.js new file mode 100644 index 00000000000..ceb9d61928a --- /dev/null +++ b/server/sonar-web/scripts/start.js @@ -0,0 +1,158 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +process.env.NODE_ENV = 'development'; + +var url = require('url'); +var express = require('express'); +var proxy = require('express-http-proxy'); +var webpack = require('webpack'); +var chalk = require('chalk'); +var config = require('../config/webpack/webpack.config.dev.js'); + +var app = express(); + +var DEFAULT_PORT = process.env.PORT || 8080; +var API_HOST = process.env.API_HOST || 'http://localhost:9000'; + +var compiler; + +var friendlySyntaxErrorLabel = 'Syntax error:'; + +function isLikelyASyntaxError (message) { + return message.indexOf(friendlySyntaxErrorLabel) !== -1; +} + +// This is a little hacky. +// It would be easier if webpack provided a rich error object. + +function formatMessage (message) { + return message + // Make some common errors shorter: + .replace( + // Babel syntax error + 'Module build failed: SyntaxError:', + friendlySyntaxErrorLabel + ) + .replace( + // Webpack file not found error + /Module not found: Error: Cannot resolve 'file' or 'directory'/, + 'Module not found:' + ) + // Internal stacks are generally useless so we strip them + .replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, '') // at ... ...:x:y + // Webpack loader names obscure CSS filenames + .replace('./~/css-loader!./~/postcss-loader!', ''); +} + +function setupCompiler () { + compiler = webpack(config); + + compiler.plugin('invalid', function () { + console.log(chalk.cyan.bold('Compiling...')); + }); + + compiler.plugin('done', function (stats) { + var hasErrors = stats.hasErrors(); + var hasWarnings = stats.hasWarnings(); + if (!hasErrors && !hasWarnings) { + console.log(chalk.green.bold('Compiled successfully!')); + return; + } + + var json = stats.toJson(); + var formattedErrors = json.errors.map(message => + 'Error in ' + formatMessage(message) + ); + var formattedWarnings = json.warnings.map(message => + 'Warning in ' + formatMessage(message) + ); + + if (hasErrors) { + console.log(chalk.red.bold('Failed to compile:')); + console.log(); + if (formattedErrors.some(isLikelyASyntaxError)) { + // If there are any syntax errors, show just them. + // This prevents a confusing ESLint parsing error + // preceding a much more useful Babel syntax error. + formattedErrors = formattedErrors.filter(isLikelyASyntaxError); + } + formattedErrors.forEach(message => { + console.log(message); + console.log(); + }); + // If errors exist, ignore warnings. + return; + } + + if (hasWarnings) { + console.log(chalk.yellow('Compiled with warnings.')); + console.log(); + formattedWarnings.forEach(message => { + console.log(message); + console.log(); + }); + + console.log('You may use special comments to disable some warnings.'); + console.log('Use ' + chalk.yellow( + '// eslint-disable-next-line') + ' to ignore the next line.'); + console.log('Use ' + chalk.yellow( + '/* eslint-disable */') + ' to ignore all warnings in a file.'); + } + }); +} + +function runDevServer (port) { + app.use(require('webpack-dev-middleware')(compiler, { + noInfo: true, + quiet: true, + publicPath: config.output.publicPath + })); + + app.use(require('webpack-hot-middleware')(compiler, { + noInfo: true, + quiet: true + })); + + app.all('*', proxy(API_HOST, { + forwardPath: function (req) { + return url.parse(req.url).path; + } + })); + + app.listen(port, 'localhost', function (err) { + if (err) { + console.log(err); + return; + } + + console.log(chalk.green.bold( + 'The app is running at http://localhost:' + port + '/')); + console.log(chalk.cyan.bold('Compiling...')); + console.log(); + }); +} + +function run (port) { + setupCompiler(); + runDevServer(port); +} + +run(DEFAULT_PORT); + |