Skip to content

Commit d10fe6f

Browse files
committed
Merge branch 'better-ss'
2 parents c1bfc5c + 97dd907 commit d10fe6f

4 files changed

Lines changed: 110 additions & 24 deletions

File tree

lib/async-tasks.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ module.exports = [
4545
step: "Starting the Server",
4646
fn: async.startServer
4747
},
48-
{
49-
step: "Adding serve static middlewares",
50-
fn: async.addServeStaticMiddleware
51-
},
5248
{
5349
step: "Starting the HTTPS Tunnel",
5450
fn: async.startTunnel

lib/async.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,6 @@ module.exports = {
246246
}
247247
});
248248
},
249-
/**
250-
* @param bs
251-
* @param done
252-
*/
253-
addServeStaticMiddleware: function (bs, done) {
254-
bs.options
255-
.get("serveStatic")
256-
.forEach(function (dir) {
257-
bs.addMiddleware("*", utils.serveStatic(dir));
258-
});
259-
done();
260-
},
261249
/**
262250
* @param {BrowserSync} bs
263251
* @param {Function} done

lib/browser-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ BrowserSync.prototype.doFileReload = function (data) {
655655

656656
/**
657657
* If the current item will cause the browser
658-
* to reload, fire the correct
658+
* to reload, fire the correct
659659
*/
660660
if (willReload) {
661661
bs.io.sockets.emit("browser:reload");

lib/server/utils.js

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"use strict";
22

3-
var fs = require("fs");
4-
var filePath = require("path");
5-
var connect = require("connect");
6-
var http = require("http");
7-
var https = require("https");
8-
var Map = require("immutable").Map;
9-
var snippet = require("./../snippet").utils;
3+
var fs = require("fs");
4+
var filePath = require("path");
5+
var connect = require("connect");
6+
var Immutable = require("immutable");
7+
var http = require("http");
8+
var https = require("https");
9+
var Map = require("immutable").Map;
10+
var snippet = require("./../snippet").utils;
11+
var _ = require("./../../lodash.custom");
12+
var serveStatic = require("serve-static");
13+
var logger = require("../logger");
1014

1115
var utils = {
1216
/**
@@ -79,6 +83,25 @@ var utils = {
7983
})
8084
}
8185

86+
if (options.get("serveStatic")) {
87+
88+
var ssMiddlewares = utils.getServeStaticMiddlewares(options.get("serveStatic"), options.get("serveStaticOptions", Immutable.Map({})).toJS());
89+
var withErrors = ssMiddlewares.filter(function(x) { return x.errors.length > 0 });
90+
var withoutErrors = ssMiddlewares.filter(function(x) { return x.errors.length === 0 });
91+
92+
if (withErrors.size) {
93+
withErrors.forEach(function (item) {
94+
logger.logger.error("{red:Warning!} %s", item.errors[0].data.message);
95+
});
96+
}
97+
98+
if (withoutErrors.size) {
99+
withoutErrors.forEach(function (item) {
100+
defaultMiddlewares.push.apply(defaultMiddlewares, item.items);
101+
});
102+
}
103+
}
104+
82105
/**
83106
* Add the proxy Middleware to the end of the stack
84107
*/
@@ -146,6 +169,85 @@ var utils = {
146169
res.setHeader("Access-Control-Allow-Credentials", true);
147170
next();
148171
}
172+
},
173+
getServeStaticMiddlewares: function (ssOption, serveStaticOptions) {
174+
175+
return ssOption.map(function (dir, i) {
176+
177+
if (Immutable.Map.isMap(dir)) {
178+
179+
var ssOptions = (function () {
180+
if (dir.get("options")) {
181+
return dir.get("options").toJS();
182+
}
183+
return {}
184+
})();
185+
186+
var route = dir.get("route");
187+
var _dir = dir.get("dir");
188+
189+
if (!isValidOption(route) || !isValidOption(_dir)) {
190+
return {
191+
items: [],
192+
errors: [{
193+
type: "Invalid Object",
194+
data: {
195+
message: "Serve Static requires both 'route' and 'dir' options when using an Object"
196+
}
197+
}]
198+
}
199+
}
200+
var ssItems = (function () {
201+
if (_.isString(route)) {
202+
return [{
203+
id: "Serve static " + i,
204+
route: getRoute(route),
205+
handle: serveStatic(_dir, ssOptions)
206+
}]
207+
}
208+
return route.map(function (item, j) {
209+
return {
210+
id: "Serve static " + i + "." + j,
211+
route: getRoute(item),
212+
handle: serveStatic(_dir, ssOptions)
213+
}
214+
}).toJS()
215+
})();
216+
return {
217+
items: ssItems,
218+
errors: []
219+
};
220+
}
221+
222+
if (_.isString(dir)) {
223+
return {
224+
items: [
225+
{
226+
id: "Serve static " + i,
227+
route: "",
228+
handle: serveStatic(dir, serveStaticOptions)
229+
}
230+
],
231+
errors: []
232+
}
233+
}
234+
235+
return {
236+
items: [],
237+
errors: [{
238+
type: "Invalid Type",
239+
data: {
240+
message: "Only strings and Objects (with route+dir) are supported for the ServeStatic option"
241+
}
242+
}]
243+
}
244+
});
245+
function isValidOption (x) {
246+
return _.isString(x) || (_.isArray(x) && x.length > 0) || (Immutable.List.isList(x) && x.size > 0);
247+
}
248+
function getRoute (x) {
249+
return x[0] === "/" ? x : "/" + x;
250+
}
149251
}
150252
};
151253

0 commit comments

Comments
 (0)