Skip to content

Commit ccd113a

Browse files
kellyrmilliganshellscape
authored andcommitted
Sockjs prefix config (#911)
* started prefix work * updates to prefix work * updates to example for sockjsprefix * fix tests typo from fix * fix linting from merge conflict
1 parent 1cf4359 commit ccd113a

File tree

9 files changed

+80
-3
lines changed

9 files changed

+80
-3
lines changed

client/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,20 @@ if(hostname && (self.location.protocol === "https:" || urlParts.hostname === "0.
170170
protocol = self.location.protocol;
171171
}
172172

173+
var rootPathName = url.parse(__webpack_public_path__).pathname || ""; // eslint-disable-line no-undef
174+
175+
if(rootPathName.length > 1) {
176+
rootPathName = rootPathName.replace(/\/+$/, "");
177+
}
178+
179+
var sockjsPath = rootPathName + "/sockjs-node";
180+
173181
var socketUrl = url.format({
174182
protocol: protocol,
175183
auth: urlParts.auth,
176184
hostname: hostname,
177185
port: (urlParts.port === "0") ? self.location.port : urlParts.port,
178-
pathname: urlParts.path == null || urlParts.path === "/" ? "/sockjs-node" : urlParts.path
186+
pathname: urlParts.path == null || urlParts.path === "/" ? sockjsPath : urlParts.path
179187
});
180188

181189
socket(socketUrl, onSocketMsg);
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Node.js API - Simple
2+
3+
```shell
4+
node server.js
5+
```
6+
7+
Starts a simple webpack-dev-server setup via the Node API. Open `http://localhost:8080/` to go the app.
8+
9+
## What should happen
10+
11+
In the app you should see "It's working."
12+
13+
In `app.js`, uncomment the code that results in an error and save. This error should be visible in the CLI and devtools.
14+
15+
Then, in `app.js`, uncomment the code that results in a warning. This warning should be visible in the CLI and devtools.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
document.write("It's working under a subapp");
2+
3+
// This results in a warning:
4+
if(!window) require("./" + window + "parseable.js");
5+
6+
// This results in an error:
7+
// if(!window) require("test");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="/subapp/bundle.js" type="text/javascript" charset="utf-8"></script>
5+
</head>
6+
<body>
7+
<h1>Example: Node.js API - Simple</h1>
8+
</body>
9+
</html>
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
const Webpack = require("webpack");
4+
const path = require("path");
5+
const WebpackDevServer = require("../../lib/Server");
6+
const webpackConfig = require("./webpack.config");
7+
8+
const compiler = Webpack(webpackConfig);
9+
const server = new WebpackDevServer(compiler, {
10+
stats: {
11+
colors: true
12+
},
13+
contentBase: path.resolve(__dirname),
14+
watchContentBase: true,
15+
sockjsPrefix: "/subapp",
16+
publicPath: "/subapp/",
17+
historyApiFallback: {
18+
disableDotRule: true,
19+
index: "/subapp/"
20+
},
21+
});
22+
23+
server.listen(8080, "127.0.0.1", function() {
24+
console.log("Starting server on http://localhost:8080");
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
context: __dirname,
3+
entry: ["./app.js", "../../client/index.js?http://localhost:8080/"],
4+
output: {
5+
filename: "bundle.js",
6+
publicPath: "/subapp/"
7+
},
8+
}

lib/Server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function Server(compiler, options) {
4242
this.disableHostCheck = !!options.disableHostCheck;
4343
this.publicHost = options.public;
4444
this.allowedHosts = options.allowedHosts;
45+
this.sockjsPrefix = options.sockjsPrefix || ""
4546
this.sockets = [];
4647
this.contentBaseWatchers = [];
4748

@@ -520,7 +521,7 @@ Server.prototype.listen = function(port, hostname) {
520521
});
521522

522523
sockServer.installHandlers(this.listeningApp, {
523-
prefix: "/sockjs-node"
524+
prefix: `${this.sockjsPrefix}/sockjs-node`
524525
});
525526
return returnValue;
526527
}

lib/optionsSchema.json

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
}
5555
]
5656
},
57+
"sockjsPrefix": {
58+
"description": "Optional prefix for the sockjs handler to be mounted to.",
59+
"type": "string"
60+
},
5761
"socket": {
5862
"description": "The Unix socket to listen to (instead of on a host).",
5963
"type": "string"

test/Validation.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("Validation", function() {
4848
config: { asdf: true },
4949
message: [
5050
" - configuration has an unknown property 'asdf'. These properties are valid:",
51-
" object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
51+
" object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, sockjsPrefix?, socket?, " +
5252
"watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, " +
5353
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, " +
5454
"compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, " +

0 commit comments

Comments
 (0)