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

Add custom sockHost/sockPort/sockPath #52

Merged
merged 4 commits into from
Apr 1, 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ Modifies how the error overlay integration works in the plugin.
};
```


### `options.sockHost`

Type: `string`
Default: effectively `window.location.hostname`

Set this if you are running webpack on a host other than `window.location.hostname`. This is used by the error overlay module.

### `options.sockPort`

Type: `number`
Default: effectively `window.location.port`

Set this if you are running webpack on a port other than `window.location.port`

### `options.sockPath`

Type: `string`
Default: `/sockjs-node`

### `options.useLegacyWDSSockets`

Type: `boolean`
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/injectRefreshEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
* @returns {WebpackEntry} An injected entry object.
*/
const injectRefreshEntry = (originalEntry, options) => {
const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
const queryParams = `?options${sockHost}${sockPort}${sockPath}`;
const entryInjects = [
// Legacy WDS SockJS integration
options.useLegacyWDSSockets && require.resolve('../runtime/LegacyWebpackDevServerSocket'),
// React-refresh runtime
require.resolve('../runtime/ReactRefreshEntry'),
// Error overlay runtime
options.overlay && options.overlay.entry,
options.overlay && (options.overlay.entry + queryParams),
// React-refresh Babel transform detection
require.resolve('../runtime/BabelDetectComponent'),
].filter(Boolean);
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/ErrorOverlayEntry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO: Implement handling of this
// eslint-disable-next-line no-unused-vars
/* global __resourceQuery, __react_refresh_error_overlay__ */

const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
Expand Down Expand Up @@ -73,8 +71,13 @@ function compileMessageHandler(message) {
}
}

let overrides = {};
if (__resourceQuery) {
overrides = require('querystring').parse(__resourceQuery.slice(1));
}

// Registers handlers for compile errors
createSocket(compileMessageHandler);
createSocket(compileMessageHandler, overrides);
// Registers handlers for runtime errors
registerErrorHandler(function handleError(error) {
hasRuntimeErrors = true;
Expand Down
10 changes: 4 additions & 6 deletions src/runtime/createSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ const loadWHMEventSource = require('./WHMEventSource');
* Creates a socket server for HMR according to the user's Webpack configuration.
* @param {function(*): void} messageHandler A handler to consume Webpack compilation messages.
*/
function createSocket(messageHandler) {
function createSocket(messageHandler, options) {
// This adds support for custom WDS socket transportModes
// In the future, we should add support for custom clients to better support WDM
if (typeof __webpack_dev_server_client__ !== 'undefined') {
const SocketClient = __webpack_dev_server_client__;
const connection = new SocketClient(
// TODO: Dynamically generate this to handle resourceQuery
// TODO: Use resourceQuery to fix servers under proxies
url.format({
protocol: window.location.protocol,
hostname: window.location.hostname,
port: window.location.port,
hostname: options.sockHost || window.location.hostname,
port: options.sockPort || window.location.port,
// TODO: Support usage of custom sockets after WDS 4.0 is released
// Ref: https://github.com/webpack/webpack-dev-server/pull/2055
pathname: '/sockjs-node',
pathname: options.sockPath || '/sockjs-node',
})
);
connection.onClose(function onSocketClose() {
Expand Down