Skip to content

Commit 2cbe373

Browse files
jkzingyyx990803
authored andcommitted
fix(serve): make sockjs url fixed with host (#1476)
1 parent d786289 commit 2cbe373

File tree

1 file changed

+28
-19
lines changed
  • packages/@vue/cli-service/lib/commands

1 file changed

+28
-19
lines changed

packages/@vue/cli-service/lib/commands/serve.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = (api, options) => {
3030
const isProduction = process.env.NODE_ENV === 'production'
3131

3232
const path = require('path')
33+
const url = require('url')
3334
const chalk = require('chalk')
3435
const webpack = require('webpack')
3536
const WebpackDevServer = require('webpack-dev-server')
@@ -60,11 +61,37 @@ module.exports = (api, options) => {
6061
}
6162
}
6263

64+
// resolve server options
65+
const useHttps = args.https || projectDevServerOptions.https || defaults.https
66+
const protocol = useHttps ? 'https' : 'http'
67+
const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host
68+
portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port
69+
const port = await portfinder.getPortPromise()
70+
71+
const urls = prepareURLs(
72+
protocol,
73+
host,
74+
port,
75+
options.baseUrl
76+
)
77+
78+
const proxySettings = prepareProxy(
79+
projectDevServerOptions.proxy,
80+
api.resolve('public')
81+
)
82+
6383
// inject dev & hot-reload middleware entries
6484
if (!isProduction) {
85+
const sockjsUrl = url.format({
86+
protocol,
87+
port,
88+
hostname: urls.lanUrlForConfig || 'localhost',
89+
pathname: '/sockjs-node'
90+
})
91+
6592
const devClients = [
6693
// dev server client
67-
require.resolve(`webpack-dev-server/client`) + (options.baseUrl !== '/' ? '?/sockjs-node' : ''),
94+
require.resolve(`webpack-dev-server/client`) + `?${sockjsUrl}`,
6895
// hmr client
6996
require.resolve(projectDevServerOptions.hotOnly
7097
? 'webpack/hot/only-dev-server'
@@ -82,24 +109,6 @@ module.exports = (api, options) => {
82109
// create compiler
83110
const compiler = webpack(webpackConfig)
84111

85-
// resolve server options
86-
const useHttps = args.https || projectDevServerOptions.https || defaults.https
87-
const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host
88-
portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port
89-
const port = await portfinder.getPortPromise()
90-
91-
const urls = prepareURLs(
92-
useHttps ? 'https' : 'http',
93-
host,
94-
port,
95-
options.baseUrl
96-
)
97-
98-
const proxySettings = prepareProxy(
99-
projectDevServerOptions.proxy,
100-
api.resolve('public')
101-
)
102-
103112
// create server
104113
const server = new WebpackDevServer(compiler, Object.assign({
105114
clientLogLevel: 'none',

0 commit comments

Comments
 (0)