Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev): add config for websocket connection #677

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 additions & 2 deletions src/node/server/serverPluginClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ 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
const protocol = config.hmr.protocol || socketProtocol
const hostname = config.hmr.hostname || ctx.hostname
const port = config.hmr.port || ctx.port
socketUrl = `${protocol}://${hostname}:${port}`
if (config.hmr.path) {
socketUrl = socketUrl + '/' + config.hmr.path
}
}
ctx.type = 'js'
ctx.status = 200
ctx.body = clientCode.replace(`__HOST__`, JSON.stringify(socketUrl))
Expand Down