123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- 'use strict';
- process.env.NODE_ENV = 'development';
- require('dotenv').config({silent: true});
- var chalk = require('chalk');
- var webpack = require('webpack');
- var WebpackDevServer = require('webpack-dev-server');
- var historyApiFallback = require('connect-history-api-fallback');
- var httpProxyMiddleware = require('http-proxy-middleware');
- var detect = require('detect-port');
- var clearConsole = require('react-dev-utils/clearConsole');
- var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
- var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
- var getProcessForPort = require('react-dev-utils/getProcessForPort');
- var openBrowser = require('react-dev-utils/openBrowser');
- var prompt = require('react-dev-utils/prompt');
- var fs = require('fs');
- var config = require('../config/webpack.config.dev');
- var paths = require('../config/paths');
- var useYarn = fs.existsSync(paths.yarnLockFile);
- var cli = useYarn ? 'yarn' : 'npm';
- var isInteractive = process.stdout.isTTY;
- if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
- process.exit(1);
- }
- var DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
- var compiler;
- var handleCompile;
- var isSmokeTest = process.argv.some(arg => arg.indexOf('--smoke-test') > -1);
- if (isSmokeTest) {
- handleCompile = function (err, stats) {
- if (err || stats.hasErrors() || stats.hasWarnings()) {
- process.exit(1);
- } else {
- process.exit(0);
- }
- };
- }
- function setupCompiler(host, port, protocol) {
-
-
- compiler = webpack(config, handleCompile);
-
-
-
-
- compiler.plugin('invalid', function() {
- if (isInteractive) {
- clearConsole();
- }
- console.log('Compiling...');
- });
- var isFirstCompile = true;
-
-
- compiler.plugin('done', function(stats) {
- if (isInteractive) {
- clearConsole();
- }
-
-
-
- var messages = formatWebpackMessages(stats.toJson({}, true));
- var isSuccessful = !messages.errors.length && !messages.warnings.length;
- var showInstructions = isSuccessful && (isInteractive || isFirstCompile);
- if (isSuccessful) {
- console.log(chalk.green('Compiled successfully!'));
- }
- if (showInstructions) {
- console.log();
- console.log('The app is running at:');
- console.log();
- console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
- console.log();
- console.log('Note that the development build is not optimized.');
- console.log('To create a production build, use ' + chalk.cyan(cli + ' run build') + '.');
- console.log();
- isFirstCompile = false;
- }
-
- if (messages.errors.length) {
- console.log(chalk.red('Failed to compile.'));
- console.log();
- messages.errors.forEach(message => {
- console.log(message);
- console.log();
- });
- return;
- }
-
- if (messages.warnings.length) {
- console.log(chalk.yellow('Compiled with warnings.'));
- console.log();
- messages.warnings.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 onProxyError(proxy) {
- return function(err, req, res){
- var host = req.headers && req.headers.host;
- console.log(
- chalk.red('Proxy error:') + ' Could not proxy request ' + chalk.cyan(req.url) +
- ' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.'
- );
- console.log(
- 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' +
- chalk.cyan(err.code) + ').'
- );
- console.log();
-
-
- if (res.writeHead && !res.headersSent) {
- res.writeHead(500);
- }
- res.end('Proxy error: Could not proxy request ' + req.url + ' from ' +
- host + ' to ' + proxy + ' (' + err.code + ').'
- );
- }
- }
- function addMiddleware(devServer) {
-
-
- var proxy = require(paths.appPackageJson).proxy;
- devServer.use(historyApiFallback({
-
-
- disableDotRule: true,
-
-
-
-
-
-
-
- htmlAcceptHeaders: proxy ?
- ['text/html'] :
- ['text/html', '*/*']
- }));
- if (proxy) {
- if (typeof proxy !== 'string') {
- console.log(chalk.red('When specified, "proxy" in package.json must be a string.'));
- console.log(chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".'));
- console.log(chalk.red('Either remove "proxy" from package.json, or make it a string.'));
- process.exit(1);
- }
-
-
-
-
-
-
- var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
-
-
- var hpm = httpProxyMiddleware(pathname => mayProxy.test(pathname), {
- target: proxy,
- logLevel: 'silent',
- onProxyReq: function(proxyReq) {
-
-
-
- if (proxyReq.getHeader('origin')) {
- proxyReq.setHeader('origin', proxy);
- }
- },
- onError: onProxyError(proxy),
- secure: false,
- changeOrigin: true,
- ws: true,
- xfwd: true
- });
- devServer.use(mayProxy, hpm);
-
-
-
- devServer.listeningApp.on('upgrade', hpm.upgrade);
- }
-
-
- devServer.use(devServer.middleware);
- }
- function runDevServer(host, port, protocol) {
- var devServer = new WebpackDevServer(compiler, {
-
- compress: true,
-
-
- clientLogLevel: 'none',
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- contentBase: paths.appPublic,
-
-
-
-
-
- hot: true,
-
-
- publicPath: config.output.publicPath,
-
-
- quiet: true,
-
-
- watchOptions: {
- ignored: /node_modules/
- },
-
- https: protocol === "https",
- host: host
- });
-
- addMiddleware(devServer);
-
- devServer.listen(port, err => {
- if (err) {
- return console.log(err);
- }
- if (isInteractive) {
- clearConsole();
- }
- console.log(chalk.cyan('Starting the development server...'));
- console.log();
- openBrowser(protocol + '://' + host + ':' + port + '/');
- });
- }
- function run(port) {
- var protocol = process.env.HTTPS === 'true' ? "https" : "http";
- var host = process.env.HOST || 'localhost';
- setupCompiler(host, port, protocol);
- runDevServer(host, port, protocol);
- }
- detect(DEFAULT_PORT).then(port => {
- if (port === DEFAULT_PORT) {
- run(port);
- return;
- }
- if (isInteractive) {
- clearConsole();
- var existingProcess = getProcessForPort(DEFAULT_PORT);
- var question =
- chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' +
- ((existingProcess) ? ' Probably:\n ' + existingProcess : '')) +
- '\n\nWould you like to run the app on another port instead?';
- prompt(question, true).then(shouldChangePort => {
- if (shouldChangePort) {
- run(port);
- }
- });
- } else {
- console.log(chalk.red('Something is already running on port ' + DEFAULT_PORT + '.'));
- }
- });
|