11import http from 'http' ;
22import openBrowser from 'react-dev-utils/openBrowser' ;
3+ import { choosePort } from 'react-dev-utils/WebpackDevServerUtils' ;
4+ import detect from 'detect-port-alt' ;
35import chokidar from 'chokidar' ;
46import { resolve as pathResolve } from 'path' ;
57import appRootDir from 'app-root-dir' ;
6- import config from '../../config/values ' ;
8+ import config from '../../config' ;
79import { log } from '../utils' ;
810
9- let HotDevelopment = require ( './hotDevelopment' ) . default ;
10- let devServer = new HotDevelopment ( ) ;
11-
12- // Any changes to our webpack bundleConfigs should restart the development devServer.
13- const watcher = chokidar . watch ( [
14- pathResolve ( appRootDir . get ( ) , 'internal' ) ,
15- pathResolve ( appRootDir . get ( ) , 'config' ) ,
16- ] ) ;
17-
18- // Wait until specific endpoint becomes responsive, then fire callback.
11+ const host = config ( 'host' ) ;
1912const onResponsive = ( hostname , port , cb ) =>
2013 setTimeout (
2114 ( ) =>
@@ -25,42 +18,63 @@ const onResponsive = (hostname, port, cb) =>
2518 1000 ,
2619 ) ;
2720
28- // Get host and port
29- const host = config ( 'host' ) ;
30- const port = config ( 'port' ) ;
21+ // Prompt the user to select port if it's already taken.
22+ // Resolves with newly selected port or null if cancelled.
23+ choosePort ( host , config ( 'port' ) )
24+ . then ( port =>
25+ detect ( config ( 'clientDevServerPort' ) ) . then ( clientPort => [
26+ port ,
27+ clientPort ,
28+ ] ) ,
29+ )
30+ . then ( ( [ port , clientPort ] ) => {
31+ if ( ! port || ! clientPort ) {
32+ console . error ( 'User cancelled' ) ;
33+ return ;
34+ }
3135
32- watcher . on ( 'ready' , ( ) => {
33- watcher . on ( 'change' , ( ) => {
34- log ( {
35- title : 'webpack' ,
36- level : 'warn' ,
37- message :
38- 'Project build configuration has changed. Restarting the development devServer...' ,
39- } ) ;
40- devServer . dispose ( ) . then ( ( ) => {
41- // Make sure our new webpack bundleConfigs aren't in the module cache.
42- Object . keys ( require . cache ) . forEach ( modulePath => {
43- if ( modulePath . indexOf ( 'config' ) !== - 1 ) {
44- delete require . cache [ modulePath ] ;
45- } else if ( modulePath . indexOf ( 'internal' ) !== - 1 ) {
46- delete require . cache [ modulePath ] ;
47- }
48- } ) ;
36+ let HotDevelopment = require ( './hotDevelopment' ) . default ;
37+ let devServer = new HotDevelopment ( port , clientPort ) ;
4938
50- // Re-require the development devServer so that all new configs are used.
51- HotDevelopment = require ( './hotDevelopment' ) . default ;
39+ // Any changes to our webpack bundleConfigs should restart the development devServer.
40+ const watcher = chokidar . watch ( [
41+ pathResolve ( appRootDir . get ( ) , 'internal' ) ,
42+ pathResolve ( appRootDir . get ( ) , 'config' ) ,
43+ ] ) ;
5244
53- // Create a new development devServer.
54- devServer = new HotDevelopment ( ) ;
55- } ) ;
56- } ) ;
45+ watcher . on ( 'ready' , ( ) => {
46+ watcher . on ( 'change' , ( ) => {
47+ log ( {
48+ title : 'webpack' ,
49+ level : 'warn' ,
50+ message :
51+ 'Project build configuration has changed. Restarting the development devServer...' ,
52+ } ) ;
53+ devServer . dispose ( ) . then ( ( ) => {
54+ // Make sure our new webpack bundleConfigs aren't in the module cache.
55+ Object . keys ( require . cache ) . forEach ( modulePath => {
56+ if ( modulePath . indexOf ( 'config' ) !== - 1 ) {
57+ delete require . cache [ modulePath ] ;
58+ } else if ( modulePath . indexOf ( 'internal' ) !== - 1 ) {
59+ delete require . cache [ modulePath ] ;
60+ }
61+ } ) ;
62+
63+ // Re-require the development devServer so that all new configs are used.
64+ HotDevelopment = require ( './hotDevelopment' ) . default ;
65+
66+ // Create a new development devServer.
67+ devServer = new HotDevelopment ( port , clientPort ) ;
68+ } ) ;
69+ } ) ;
5770
58- // Wait until devServer is started.
59- onResponsive ( host , port , ( ) => openBrowser ( `http://${ host } :${ port } ` ) ) ;
60- } ) ;
71+ // Wait until devServer is started to open browser
72+ onResponsive ( host , port , ( ) => openBrowser ( `http://${ host } :${ port } ` ) ) ;
73+ } ) ;
6174
62- // If we receive a kill cmd then we will first try to dispose our listeners.
63- process . on (
64- 'SIGTERM' ,
65- ( ) => devServer && devServer . dispose ( ) . then ( ( ) => process . exit ( 0 ) ) ,
66- ) ;
75+ // If we receive a kill cmd then we will first try to dispose our listeners.
76+ process . on (
77+ 'SIGTERM' ,
78+ ( ) => devServer && devServer . dispose ( ) . then ( ( ) => process . exit ( 0 ) ) ,
79+ ) ;
80+ } ) ;
0 commit comments