Skip to content

Commit 3d55918

Browse files
committed
Fix "no content base" option, this time with tests
1 parent 9938676 commit 3d55918

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

bin/webpack-dev-server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ function processOptions(wpOpt) {
199199
if(!options.clientLogLevel)
200200
options.clientLogLevel = argv["client-log-level"];
201201

202-
if(!options.contentBase) {
202+
if(options.contentBase === undefined) {
203203
if(argv["content-base"]) {
204204
options.contentBase = argv["content-base"];
205205
if(/^[0-9]$/.test(options.contentBase))
206206
options.contentBase = +options.contentBase;
207207
else if(!/^(https?:)?\/\//.test(options.contentBase))
208208
options.contentBase = path.resolve(options.contentBase);
209209
// It is possible to disable the contentBase by using `--no-content-base`, which results in arg["content-base"] = false
210-
} else if(argv["content-base"] !== false) {
211-
options.contentBase = process.cwd();
210+
} else if(argv["content-base"] === false) {
211+
options.contentBase = false;
212212
}
213213
}
214214

lib/Server.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ function Server(compiler, options) {
105105
res.end("</body></html>");
106106
}.bind(this));
107107

108-
var contentBase = options.contentBase || process.cwd();
108+
var contentBase;
109+
if(options.contentBase !== undefined) {
110+
contentBase = options.contentBase;
111+
} else {
112+
contentBase = process.cwd();
113+
}
109114

110115
var features = {
111116
compress: function() {
@@ -222,7 +227,7 @@ function Server(compiler, options) {
222227
// Fall back to /index.html if nothing else matches.
223228
app.use(
224229
historyApiFallback(typeof options.historyApiFallback === "object" ? options.historyApiFallback : null),
225-
express.static(contentBase)
230+
contentBase ? express.static(contentBase) : undefined
226231
);
227232
}
228233
},
@@ -286,12 +291,12 @@ function Server(compiler, options) {
286291
var defaultFeatures = ["setup", "headers", "middleware"];
287292
if(options.proxy)
288293
defaultFeatures.push("proxy", "middleware");
289-
if(options.contentBase !== false)
294+
if(contentBase !== false)
290295
defaultFeatures.push("contentBaseFiles");
291296
if(options.historyApiFallback)
292297
defaultFeatures.push("historyApiFallback", "middleware");
293298
defaultFeatures.push("magicHtml");
294-
if(options.contentBase !== false)
299+
if(contentBase !== false)
295300
defaultFeatures.push("contentBaseIndex");
296301
// compress is placed last and uses unshift so that it will be the first middleware used
297302
if(options.compress)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
"less": "^2.5.1",
3131
"less-loader": "~2.2.0",
3232
"mocha": "^3.0.2",
33+
"mocha-sinon": "^1.1.6",
3334
"pug": "^2.0.0-beta5",
3435
"pug-loader": "^2.3.0",
3536
"should": "^11.1.0",
37+
"sinon": "^1.17.6",
3638
"style-loader": "~0.13.0",
3739
"supertest": "^2.0.0",
3840
"url-loader": "~0.5.6",

test/ContentBase.js renamed to test/ContentBase.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var request = require("supertest");
22
var path = require("path");
33
var helper = require("./helper");
44
var config = require("./fixtures/contentbase-config/webpack.config");
5+
require("mocha-sinon");
56

67
var contentBasePublic = path.join(__dirname, "fixtures/contentbase-config/public");
78
var contentBaseOther = path.join(__dirname, "fixtures/contentbase-config/other");
@@ -86,4 +87,36 @@ describe("ContentBase", function() {
8687
.expect(302, done);
8788
});
8889
});
90+
91+
describe("default to PWD", function() {
92+
before(function(done) {
93+
this.sinon.stub(process, "cwd");
94+
process.cwd.returns(contentBasePublic);
95+
server = helper.start(config, {}, done);
96+
req = request(server.app);
97+
});
98+
99+
it("Request to page", function(done) {
100+
req.get("/other.html")
101+
.expect(200, done);
102+
});
103+
});
104+
105+
describe("disable", function() {
106+
before(function(done) {
107+
// This is a somewhat weird test, but it is important that we mock
108+
// the PWD here, and test if /other.html in our "fake" PWD really is not requested.
109+
this.sinon.stub(process, "cwd");
110+
process.cwd.returns(contentBasePublic);
111+
server = helper.start(config, {
112+
contentBase: false
113+
}, done);
114+
req = request(server.app);
115+
});
116+
117+
it("Request to page", function(done) {
118+
req.get("/other.html")
119+
.expect(404, done);
120+
});
121+
});
89122
});

0 commit comments

Comments
 (0)