Skip to content

Commit

Permalink
Serve sockjs.js from sockjs-client dependency, don't use default from…
Browse files Browse the repository at this point in the history
… CDN (#493)
  • Loading branch information
Volune authored and SpaceK33z committed Sep 16, 2016
1 parent 155d036 commit bd976f2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
/client/live.bundle.js
/client/index.bundle.js
/client/sockjs.bundle.js
1 change: 1 addition & 0 deletions client/sockjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('sockjs-client');
6 changes: 6 additions & 0 deletions client/webpack.sockjs.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
output: {
library: "SockJS",
libraryTarget: "umd"
}
}
15 changes: 11 additions & 4 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var https = require("https");
var httpProxyMiddleware = require("http-proxy-middleware");
var serveIndex = require("serve-index");
var historyApiFallback = require("connect-history-api-fallback");
var pkg = require("../package.json");

function Server(compiler, options) {
// Default options
Expand Down Expand Up @@ -48,6 +47,10 @@ function Server(compiler, options) {
var inlinedJs = new StreamCache();
fs.createReadStream(path.join(__dirname, "..", "client", "index.bundle.js")).pipe(inlinedJs);

// Prepare the sockjs js file
var sockjsJs = new StreamCache();
fs.createReadStream(path.join(__dirname, "..", "client", "sockjs.bundle.js")).pipe(sockjsJs);

// Init express server
var app = this.app = new express();

Expand All @@ -59,6 +62,11 @@ function Server(compiler, options) {
liveJs.pipe(res);
});

app.get("/__webpack_dev_server__/sockjs.bundle.js", function(req, res) {
res.setHeader("Content-Type", "application/javascript");
sockjsJs.pipe(res);
});

app.get("/webpack-dev-server.js", function(req, res) {
res.setHeader("Content-Type", "application/javascript");
inlinedJs.pipe(res);
Expand Down Expand Up @@ -335,9 +343,8 @@ Server.prototype.setContentHeaders = function(req, res, next) {
Server.prototype.listen = function() {
var returnValue = this.listeningApp.listen.apply(this.listeningApp, arguments);
var sockServer = sockjs.createServer({
// The SockJS server package uses a version of the client script
// that doesn't match our version of the client script.
sockjs_url: 'https://cdn.jsdelivr.net/sockjs/' + pkg.dependencies['sockjs-client'] + '/sockjs.min.js',
// Use provided up-to-date sockjs-client
sockjs_url: "/__webpack_dev_server__/sockjs.bundle.js",
// Limit useless logs
log: function(severity, line) {
if(severity === "error") {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@
"ssl/"
],
"scripts": {
"prepublish": "webpack ./client/live.js client/live.bundle.js --color --config client/webpack.config.js -p && webpack ./client/index.js client/index.bundle.js --color --config client/webpack.config.js -p",
"lint": "eslint bin lib test examples client/{index,live,socket,webpack.config}.js",
"prepublish": "npm run -s client-live && npm run -s client-index && npm run -s client-sockjs",
"client-live": "webpack ./client/live.js client/live.bundle.js --color --config client/webpack.config.js -p",
"client-index": "webpack ./client/index.js client/index.bundle.js --color --config client/webpack.config.js -p",
"client-sockjs": "webpack ./client/sockjs.js client/sockjs.bundle.js --color --config client/webpack.sockjs.config.js -p",
"lint": "eslint bin lib test examples client/{index,live,socket,sockjs,webpack.config}.js",
"beautify": "npm run lint -- --fix",
"travis": "npm run lint && node lib/Server.js"
}
Expand Down

0 comments on commit bd976f2

Please sign in to comment.