/
webpackServer.js
108 lines (95 loc) · 3.38 KB
/
webpackServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {yellow} from 'chalk'
import detect from 'detect-port'
import inquirer from 'inquirer'
import {getPluginConfig, getUserConfig} from './config'
import {DEFAULT_PORT} from './constants'
import createServerWebpackConfig from './createServerWebpackConfig'
import debug from './debug'
import devServer from './devServer'
import {clearConsole, deepToString, typeOf} from './utils'
/**
* Get the port to run the server on, detecting if the intended port is
* available first and prompting the user if not.
*/
function getServerPort(args, intendedPort, cb) {
detect(intendedPort, (err, suggestedPort) => {
if (err) return cb(err)
// No need to prompt if the intended port is available
if (suggestedPort === intendedPort) return cb(null, suggestedPort)
// Support use of --force to avoid interactive prompt
if (args.force) return cb(null, suggestedPort)
if (args.clear !== false && args.clearConsole !== false) {
clearConsole()
}
console.log(yellow(`Something is already running on port ${intendedPort}.`))
console.log()
inquirer.prompt([
{
type: 'confirm',
name: 'run',
message: 'Would you like to run the app on another port instead?',
default: true,
},
]).then(
({run}) => cb(null, run ? suggestedPort : null),
(err) => cb(err)
)
})
}
/**
* Start a development server with Webpack using a given build configuration.
*/
export default function webpackServer(args, buildConfig, cb) {
// Default environment to development - we also run the dev server while
// testing to check that HMR works.
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development'
}
if (typeof buildConfig == 'function') {
buildConfig = buildConfig(args)
}
let serverConfig
try {
let pluginConfig = getPluginConfig(args)
serverConfig = getUserConfig(args, {pluginConfig}).devServer
}
catch (e) {
return cb(e)
}
getServerPort(args, args.port || Number(serverConfig.port) || DEFAULT_PORT, (err, port) => {
if (err) return cb(err)
// A null port indicates the user chose not to run the server when prompted
if (port === null) return cb()
serverConfig.port = port
// Fallback index serving can be disabled with --no-fallback
if (args.fallback === false) {
serverConfig.historyApiFallback = false
}
// Fallback index serving can be configured with dot arguments
// e.g. --fallback.disableDotRule --fallback.verbose
else if (typeOf(args.fallback) === 'object') {
serverConfig.historyApiFallback = args.fallback
}
// The host can be overridden with --host
if (args.host) serverConfig.host = args.host
// Open a browser with --open (default browser) or --open="browser name"
if (args.open) serverConfig.open = args.open
let url = `http${serverConfig.https ? 's' : ''}://${serverConfig.host || 'localhost'}:${port}/`
if (!('status' in buildConfig.plugins)) {
buildConfig.plugins.status = {
disableClearConsole: args.clear === false || args['clear-console'] === false,
successMessage:
`The app is running at ${url}`,
}
}
let webpackConfig
try {
webpackConfig = createServerWebpackConfig(args, buildConfig, serverConfig)
}
catch (e) {
return cb(e)
}
debug('webpack config: %s', deepToString(webpackConfig))
devServer(webpackConfig, serverConfig, url, cb)
})
}