Skip to content

Commit b763f76

Browse files
mefugregberge
authored andcommitted
feat: use webpack options sockPath, sockHost, and sockPort to get socket URL (#42)
1 parent 1471e29 commit b763f76

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/entry-devserver.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* global __resourceQuery */
2+
3+
import querystring from 'querystring';
14
import url from 'url';
25
import SockJS from 'sockjs-client';
36
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages';
@@ -6,13 +9,17 @@ import {
69
dismissBuildError,
710
} from 'react-error-overlay'
811

12+
let sockOptions = {}
13+
if (typeof __resourceQuery === 'string' && __resourceQuery) {
14+
sockOptions = querystring.parse(__resourceQuery.substr(1));
15+
}
16+
917
const connection = new SockJS(
1018
url.format({
1119
protocol: window.location.protocol,
12-
hostname: window.location.hostname,
13-
port: window.location.port,
14-
// Hardcoded in WebpackDevServer
15-
pathname: '/sockjs-node',
20+
hostname: sockOptions.sockHost || window.location.hostname,
21+
port: sockOptions.sockPort || window.location.port,
22+
pathname: sockOptions.sockPath || '/sockjs-node',
1623
})
1724
);
1825

src/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ class ErrorOverlayPlugin {
1010
if (compiler.options.mode !== 'development') return
1111

1212
const devServerEnabled = !!compiler.options.devServer
13+
const sockOptions = {}
14+
if (devServerEnabled) {
15+
sockOptions.sockHost = compiler.options.devServer.sockHost
16+
sockOptions.sockPath = compiler.options.devServer.sockPath
17+
sockOptions.sockPort = compiler.options.devServer.sockPort
18+
}
1319

1420
compiler.hooks.entryOption.tap(className, (context, entry) => {
15-
adjustEntry(entry, devServerEnabled)
21+
adjustEntry(entry, devServerEnabled, sockOptions)
1622
})
1723

1824
compiler.hooks.afterResolvers.tap(className, ({ options }) => {
@@ -29,7 +35,7 @@ class ErrorOverlayPlugin {
2935
}
3036
}
3137

32-
function adjustEntry(entry, enableDevServer) {
38+
function adjustEntry(entry, enableDevServer, sockOptions) {
3339
if (typeof entry === 'string') {
3440
throw new Error(
3541
`We currently do not inject our entry code into single-file anonymous entries.
@@ -39,8 +45,12 @@ Please use a multi-main (array) or object-form \`entry\` setting for now.`,
3945

4046
if (Array.isArray(entry)) {
4147
if (enableDevServer) {
42-
if (!entry.includes(chunkPathDevServer)) {
43-
entry.unshift(chunkPathDevServer)
48+
const sockHost = sockOptions.sockHost ? `&sockHost=${sockOptions.sockHost}` : ''
49+
const sockPath = sockOptions.sockPath ? `&sockPath=${sockOptions.sockPath}` : ''
50+
const sockPort = sockOptions.sockPort ? `&sockPort=${sockOptions.sockPort}` : ''
51+
const chunkPathDevServerWithParams = `${chunkPathDevServer}?${sockHost}${sockPath}${sockPort}`
52+
if (!entry.includes(chunkPathDevServerWithParams)) {
53+
entry.unshift(chunkPathDevServerWithParams)
4454
}
4555
}
4656

@@ -49,7 +59,7 @@ Please use a multi-main (array) or object-form \`entry\` setting for now.`,
4959
}
5060
} else {
5161
Object.keys(entry).forEach(entryName => {
52-
entry[entryName] = adjustEntry(entry[entryName], enableDevServer)
62+
entry[entryName] = adjustEntry(entry[entryName], enableDevServer, sockOptions)
5363
})
5464
}
5565

0 commit comments

Comments
 (0)