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

Sockjs prefix config #911

Merged
merged 7 commits into from
Aug 8, 2017
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
10 changes: 9 additions & 1 deletion client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,20 @@ if(hostname && (self.location.protocol === "https:" || urlParts.hostname === "0.
protocol = self.location.protocol;
}

var rootPathName = url.parse(__webpack_public_path__).pathname || ""; // eslint-disable-line no-undef

if(rootPathName.length > 1) {
rootPathName = rootPathName.replace(/\/+$/, "");
}

var sockjsPath = rootPathName + "/sockjs-node";

var socketUrl = url.format({
protocol: protocol,
auth: urlParts.auth,
hostname: hostname,
port: (urlParts.port === "0") ? self.location.port : urlParts.port,
pathname: urlParts.path == null || urlParts.path === "/" ? "/sockjs-node" : urlParts.path
pathname: urlParts.path == null || urlParts.path === "/" ? sockjsPath : urlParts.path
});

socket(socketUrl, onSocketMsg);
Expand Down
15 changes: 15 additions & 0 deletions examples/node-api-sockjs-prefix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Node.js API - Simple

```shell
node server.js
```

Starts a simple webpack-dev-server setup via the Node API. Open `http://localhost:8080/` to go the app.

## What should happen

In the app you should see "It's working."

In `app.js`, uncomment the code that results in an error and save. This error should be visible in the CLI and devtools.

Then, in `app.js`, uncomment the code that results in a warning. This warning should be visible in the CLI and devtools.
7 changes: 7 additions & 0 deletions examples/node-api-sockjs-prefix/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
document.write("It's working under a subapp");

// This results in a warning:
if(!window) require("./" + window + "parseable.js");

// This results in an error:
// if(!window) require("test");
9 changes: 9 additions & 0 deletions examples/node-api-sockjs-prefix/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<script src="/subapp/bundle.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Example: Node.js API - Simple</h1>
</body>
</html>
25 changes: 25 additions & 0 deletions examples/node-api-sockjs-prefix/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

const Webpack = require("webpack");
const path = require("path");
const WebpackDevServer = require("../../lib/Server");
const webpackConfig = require("./webpack.config");

const compiler = Webpack(webpackConfig);
const server = new WebpackDevServer(compiler, {
stats: {
colors: true
},
contentBase: path.resolve(__dirname),
watchContentBase: true,
sockjsPrefix: "/subapp",
publicPath: "/subapp/",
historyApiFallback: {
disableDotRule: true,
index: "/subapp/"
},
});

server.listen(8080, "127.0.0.1", function() {
console.log("Starting server on http://localhost:8080");
});
8 changes: 8 additions & 0 deletions examples/node-api-sockjs-prefix/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
context: __dirname,
entry: ["./app.js", "../../client/index.js?http://localhost:8080/"],
output: {
filename: "bundle.js",
publicPath: "/subapp/"
},
}
3 changes: 2 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Server(compiler, options) {
this.disableHostCheck = !!options.disableHostCheck;
this.publicHost = options.public;
this.allowedHosts = options.allowedHosts;
this.sockjsPrefix = options.sockjsPrefix || ""
this.sockets = [];
this.contentBaseWatchers = [];

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

sockServer.installHandlers(this.listeningApp, {
prefix: "/sockjs-node"
prefix: `${this.sockjsPrefix}/sockjs-node`
});
return returnValue;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
}
]
},
"sockjsPrefix": {
"description": "Optional prefix for the sockjs handler to be mounted to.",
"type": "string"
},
"socket": {
"description": "The Unix socket to listen to (instead of on a host).",
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Validation", function() {
config: { asdf: true },
message: [
" - configuration has an unknown property 'asdf'. These properties are valid:",
" object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
" object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, sockjsPrefix?, socket?, " +
"watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, " +
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, " +
"compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, " +
Expand Down