Skip to content

Commit

Permalink
feat: use webpack options sockPath, sockHost, and sockPort to get soc…
Browse files Browse the repository at this point in the history
…ket URL (#42)
  • Loading branch information
mefu authored and gregberge committed May 23, 2019
1 parent 1471e29 commit b763f76
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/entry-devserver.js
@@ -1,3 +1,6 @@
/* global __resourceQuery */

import querystring from 'querystring';
import url from 'url';
import SockJS from 'sockjs-client';
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages';
Expand All @@ -6,13 +9,17 @@ import {
dismissBuildError,
} from 'react-error-overlay'

let sockOptions = {}
if (typeof __resourceQuery === 'string' && __resourceQuery) {
sockOptions = querystring.parse(__resourceQuery.substr(1));
}

const connection = new SockJS(
url.format({
protocol: window.location.protocol,
hostname: window.location.hostname,
port: window.location.port,
// Hardcoded in WebpackDevServer
pathname: '/sockjs-node',
hostname: sockOptions.sockHost || window.location.hostname,
port: sockOptions.sockPort || window.location.port,
pathname: sockOptions.sockPath || '/sockjs-node',
})
);

Expand Down
20 changes: 15 additions & 5 deletions src/index.js
Expand Up @@ -10,9 +10,15 @@ class ErrorOverlayPlugin {
if (compiler.options.mode !== 'development') return

const devServerEnabled = !!compiler.options.devServer
const sockOptions = {}
if (devServerEnabled) {
sockOptions.sockHost = compiler.options.devServer.sockHost
sockOptions.sockPath = compiler.options.devServer.sockPath
sockOptions.sockPort = compiler.options.devServer.sockPort
}

compiler.hooks.entryOption.tap(className, (context, entry) => {
adjustEntry(entry, devServerEnabled)
adjustEntry(entry, devServerEnabled, sockOptions)
})

compiler.hooks.afterResolvers.tap(className, ({ options }) => {
Expand All @@ -29,7 +35,7 @@ class ErrorOverlayPlugin {
}
}

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

if (Array.isArray(entry)) {
if (enableDevServer) {
if (!entry.includes(chunkPathDevServer)) {
entry.unshift(chunkPathDevServer)
const sockHost = sockOptions.sockHost ? `&sockHost=${sockOptions.sockHost}` : ''
const sockPath = sockOptions.sockPath ? `&sockPath=${sockOptions.sockPath}` : ''
const sockPort = sockOptions.sockPort ? `&sockPort=${sockOptions.sockPort}` : ''
const chunkPathDevServerWithParams = `${chunkPathDevServer}?${sockHost}${sockPath}${sockPort}`
if (!entry.includes(chunkPathDevServerWithParams)) {
entry.unshift(chunkPathDevServerWithParams)
}
}

Expand All @@ -49,7 +59,7 @@ Please use a multi-main (array) or object-form \`entry\` setting for now.`,
}
} else {
Object.keys(entry).forEach(entryName => {
entry[entryName] = adjustEntry(entry[entryName], enableDevServer)
entry[entryName] = adjustEntry(entry[entryName], enableDevServer, sockOptions)
})
}

Expand Down

0 comments on commit b763f76

Please sign in to comment.