Skip to content

Commit 931bbaf

Browse files
committed
updated deps, added features option
fixed some errors
1 parent 50a2e10 commit 931bbaf

File tree

4 files changed

+120
-84
lines changed

4 files changed

+120
-84
lines changed

bin/webpack-dev-server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ if(!options.filename)
7474
[].concat(wpOpt).forEach(function(wpOpt) {
7575
wpOpt.output.path = "/";
7676
});
77+
7778
if(!options.watchOptions)
7879
options.watchOptions = firstWpOpt.watchOptions;
7980
if(!options.watchDelay && !options.watchOptions) // TODO remove in next major version
8081
options.watchDelay = firstWpOpt.watchDelay;
82+
8183
if(!options.hot)
8284
options.hot = argv["hot"];
8385

@@ -92,14 +94,16 @@ if(argv["content-base"]) {
9294
} else if(!options.contentBase) {
9395
options.contentBase = process.cwd();
9496
}
97+
9598
if(!options.stats) {
9699
options.stats = {
97100
cached: false,
98101
cachedAssets: false
99102
};
100103
}
101104

102-
options.stats.colors = require("supports-color");
105+
if(typeof options.stats === "object" && typeof options.stats.colors === "undefined")
106+
options.stats.colors = require("supports-color");
103107

104108
if(argv["lazy"])
105109
options.lazy = true;

example/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ webpack-dev-server
1414
http://localhost:8080/webpack-dev-server/index.html
1515
```
1616

17-
The app should display "It's working" with a green background.
17+
The app should display "It's working" with a dotted background and a SVG image.
1818

1919
There is a file named `index.html` in this directory. This file is served as content
2020

@@ -59,7 +59,7 @@ You may also update the css file or any other file used by the app.
5959
## History API Fallback
6060

6161
``` text
62-
webpack-dev-server --inline --history-api-fallback
62+
webpack-dev-server --inline --history-api-fallback --output-public-path /
6363
http://localhost:8080/some/url/from/spa
6464
```
6565

lib/Server.js

Lines changed: 104 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function Server(compiler, options) {
4949
// Init express server
5050
var app = this.app = new express();
5151

52-
// serve webpack bundle
53-
app.use(this.middleware = webpackDevMiddleware(compiler, options));
52+
// middleware for serving webpack bundle
53+
this.middleware = webpackDevMiddleware(compiler, options);
5454

5555
app.get("/__webpack_dev_server__/live.bundle.js", function(req, res) {
5656
res.setHeader("Content-Type", "application/javascript");
@@ -109,82 +109,114 @@ function Server(compiler, options) {
109109
res.end('</body></html>');
110110
}.bind(this));
111111

112-
if (options.proxy) {
113-
if (!Array.isArray(options.proxy)) {
114-
options.proxy = Object.keys(options.proxy).map(function (path) {
115-
var proxyOptions;
116-
if (typeof options.proxy[path] === 'string') {
117-
proxyOptions = {path: path, target: options.proxy[path]};
118-
} else {
119-
proxyOptions = options.proxy[path];
120-
proxyOptions.path = path;
121-
}
122-
return proxyOptions;
123-
});
124-
}
125-
options.proxy.forEach(function (proxyOptions) {
126-
proxyOptions.ws = proxyOptions.hasOwnProperty('ws') ? proxyOptions.ws : true;
127-
app.all(proxyOptions.path, function (req, res) {
128-
if(typeof proxyOptions.rewrite === 'function') proxyOptions.rewrite(req, proxyOptions);
129-
if (proxyOptions.host) {
130-
req.headers.host = proxyOptions.host;
112+
var features = {
113+
proxy: function() {
114+
if (options.proxy) {
115+
if (!Array.isArray(options.proxy)) {
116+
options.proxy = Object.keys(options.proxy).map(function (path) {
117+
var proxyOptions;
118+
if (typeof options.proxy[path] === 'string') {
119+
proxyOptions = {path: path, target: options.proxy[path]};
120+
} else {
121+
proxyOptions = options.proxy[path];
122+
proxyOptions.path = path;
123+
}
124+
return proxyOptions;
125+
});
131126
}
132-
proxy.web(req, res, proxyOptions, function(err){
133-
var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
134-
this.io.sockets.emit("proxy-error", [msg]);
135-
res.statusCode = 502;
136-
res.end();
127+
options.proxy.forEach(function (proxyOptions) {
128+
proxyOptions.ws = proxyOptions.hasOwnProperty('ws') ? proxyOptions.ws : true;
129+
app.all(proxyOptions.path, function (req, res) {
130+
if(typeof proxyOptions.rewrite === 'function') proxyOptions.rewrite(req, proxyOptions);
131+
if (proxyOptions.host) {
132+
req.headers.host = proxyOptions.host;
133+
}
134+
proxy.web(req, res, proxyOptions, function(err){
135+
var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
136+
this.io.sockets.emit("proxy-error", [msg]);
137+
res.statusCode = 502;
138+
res.end();
139+
}.bind(this));
140+
if (proxyOptions.configure) {
141+
proxyOptions.configure(proxy);
142+
}
143+
}.bind(this));
137144
}.bind(this));
138-
if (proxyOptions.configure) {
139-
proxyOptions.configure(proxy);
145+
}
146+
}.bind(this),
147+
148+
historyApiFallback: function() {
149+
if (options.historyApiFallback) {
150+
// Fall back to /index.html if nothing else matches.
151+
app.use(historyApiFallback(typeof options.historyApiFallback === 'object' ? options.historyApiFallback : null));
152+
}
153+
}.bind(this),
154+
155+
contentBase: function() {
156+
if(options.contentBase !== false) {
157+
var contentBase = options.contentBase || process.cwd();
158+
159+
if(typeof contentBase === "object") {
160+
console.log('Using contentBase as a proxy is deprecated and will be removed in the next major version. Please use the proxy option instead.\n\nTo update remove the contentBase option from webpack.config.js and add this:');
161+
console.log('proxy: {\n\t"*": <your current contentBase configuration>\n}');
162+
// Proxy every request to contentBase.target
163+
app.all("*", function(req, res) {
164+
proxy.web(req, res, contentBase, function(err) {
165+
var msg = "cannot proxy to " + contentBase.target + " (" + err.message + ")";
166+
this.io.sockets.emit("proxy-error", [msg]);
167+
res.statusCode = 502;
168+
res.end();
169+
}.bind(this));
170+
}.bind(this));
171+
} else if(/^(https?:)?\/\//.test(contentBase)) {
172+
// Redirect every request to contentBase
173+
app.get("*", function(req, res) {
174+
res.writeHead(302, {
175+
'Location': contentBase + req.path + (req._parsedUrl.search || "")
176+
});
177+
res.end();
178+
}.bind(this));
179+
} else if(typeof contentBase === "number") {
180+
// Redirect every request to the port contentBase
181+
app.get("*", function(req, res) {
182+
res.writeHead(302, {
183+
'Location': "//localhost:" + contentBase + req.path + (req._parsedUrl.search || "")
184+
});
185+
res.end();
186+
}.bind(this));
187+
} else {
188+
// route content request
189+
app.get("*", express.static(contentBase), serveIndex(contentBase));
140190
}
141-
}.bind(this));
142-
}.bind(this));
143-
}
191+
}
192+
}.bind(this),
144193

145-
if (options.historyApiFallback) {
146-
// Fall back to /index.html if nothing else matches.
147-
app.use(historyApiFallback(typeof options.historyApiFallback === 'object' ? options.historyApiFallback : null));
148-
// include our middleware to ensure it is able to handle '/index.html' requrst after redirect
149-
app.use(this.middleware);
150-
}
194+
middleware: function() {
195+
// include our middleware to ensure it is able to handle '/index.html' requrst after redirect
196+
app.use(this.middleware);
197+
}.bind(this),
151198

152-
if(options.contentBase !== false) {
153-
var contentBase = options.contentBase || process.cwd();
199+
headers: function() {
200+
app.get("*", this.setContentHeaders.bind(this));
201+
}.bind(this),
154202

155-
if(typeof contentBase === "object") {
156-
console.log('Using contentBase as a proxy is deprecated and will be removed in the next major version. Please use the proxy option instead.\n\nTo update remove the contentBase option from webpack.config.js and add this:');
157-
console.log('proxy: {\n\t"*": <your current contentBase configuration>\n}');
158-
// Proxy every request to contentBase.target
159-
app.all("*", function(req, res) {
160-
proxy.web(req, res, contentBase, function(err) {
161-
var msg = "cannot proxy to " + contentBase.target + " (" + err.message + ")";
162-
this.io.sockets.emit("proxy-error", [msg]);
163-
res.statusCode = 502;
164-
res.end();
165-
}.bind(this));
166-
}.bind(this));
167-
} else if(/^(https?:)?\/\//.test(contentBase)) {
168-
// Redirect every request to contentBase
169-
app.get("*", function(req, res) {
170-
res.writeHead(302, {
171-
'Location': contentBase + req.path + (req._parsedUrl.search || "")
172-
});
173-
res.end();
174-
}.bind(this));
175-
} else if(typeof contentBase === "number") {
176-
// Redirect every request to the port contentBase
177-
app.get("*", function(req, res) {
178-
res.writeHead(302, {
179-
'Location': "//localhost:" + contentBase + req.path + (req._parsedUrl.search || "")
180-
});
181-
res.end();
182-
}.bind(this));
183-
} else {
184-
// route content request
185-
app.get("*", this.setContentHeaders.bind(this), this.serveMagicHtml.bind(this), express.static(contentBase), serveIndex(contentBase));
186-
}
187-
}
203+
magicHtml: function() {
204+
app.get("*", this.serveMagicHtml.bind(this));
205+
}.bind(this)
206+
};
207+
208+
var defaultFeatures = ["headers", "middleware"];
209+
if(options.proxy)
210+
defaultFeatures.push("proxy");
211+
if(options.historyApiFallback)
212+
defaultFeatures.push("historyApiFallback", "middleware");
213+
defaultFeatures.push("magicHtml");
214+
if(options.contentBase !== false)
215+
defaultFeatures.push("contentBase");
216+
217+
(options.features || defaultFeatures).forEach(function(feature) {
218+
features[feature]();
219+
}, this);
188220

189221
this.listeningApp = options.https
190222
? https.createServer({

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88
},
99
"dependencies": {
1010
"connect-history-api-fallback": "1.1.0",
11-
"express": "^4.3.2",
12-
"http-proxy": "^1.1.4",
11+
"express": "^4.13.3",
12+
"http-proxy": "^1.11.2",
1313
"optimist": "~0.6.0",
14-
"serve-index": "^1.2.0",
14+
"serve-index": "^1.7.2",
1515
"socket.io": "^1.3.6",
1616
"socket.io-client": "^1.3.6",
1717
"stream-cache": "~0.0.1",
18-
"strip-ansi": "^2.0.1",
19-
"supports-color": "^1.3.1",
20-
"webpack-dev-middleware": "^1.0.7"
18+
"strip-ansi": "^3.0.0",
19+
"supports-color": "^3.1.1",
20+
"webpack-dev-middleware": "^1.2.0"
2121
},
2222
"devDependencies": {
23-
"css-loader": "~0.15.1",
23+
"css-loader": "~0.18.0",
2424
"file-loader": "~0.8.4",
2525
"jade": "^1.11.0",
2626
"jade-loader": "~0.7.1",
2727
"less": "^2.5.1",
2828
"less-loader": "~2.2.0",
2929
"style-loader": "~0.12.3",
30-
"url-loader": "~0.5.5",
31-
"webpack": "^1.10.0"
30+
"url-loader": "~0.5.6",
31+
"webpack": "^1.12.1"
3232
},
3333
"license": "MIT",
3434
"repository": {

0 commit comments

Comments
 (0)