Skip to content

Commit

Permalink
refactor: replacement websocket url with hmr option
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Oct 20, 2020
1 parent bec6b3c commit 6f2aa10
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,21 @@ export interface SharedConfig {
env?: DotenvParseOutput
}

export interface HmrConfig {
protocol?: string
hostname?: string
port?: number
path?: string
}

export interface ServerConfig extends SharedConfig {
/**
* Configure hmr websocket connection.
*/
hmr?: HmrConfig | boolean
/**
* Configure dev server hostname.
*/
hostname?: string
port?: number
open?: boolean
Expand Down
14 changes: 12 additions & 2 deletions src/node/server/serverPluginClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ export const clientPlugin: ServerPlugin = ({ app, config }) => {
app.use(async (ctx, next) => {
if (ctx.path === clientPublicPath) {
const socketProtocol = ctx.protocol.toString() === 'https' ? 'wss' : 'ws'
const socketUrl = `${socketProtocol}://${ctx.host.toString()}`

let socketUrl = `${socketProtocol}://${ctx.host.toString()}`
if (config.hmr && typeof config.hmr === 'object') {
// hmr option has highest priory
let { protocol, hostname, port, path } = config.hmr
protocol = protocol || socketProtocol
hostname = hostname || ctx.hostname
port = port || ctx.port
socketUrl = `${protocol}://${hostname}:${port}`
if (path) {
socketUrl = socketUrl + '/' + path
}
}
ctx.type = 'js'
ctx.status = 200
ctx.body = clientCode.replace(`__HOST__`, JSON.stringify(socketUrl))
Expand Down

0 comments on commit 6f2aa10

Please sign in to comment.