Skip to content

Commit c6bdfe4

Browse files
jleifeldhiroppy
andauthoredApr 2, 2020
feat(contentBasePublicPath): allow multiple paths (#2489)
Co-authored-by: Yuta Hiroto <git@hiroppy.me>
1 parent f317358 commit c6bdfe4

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed
 

‎lib/Server.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,17 @@ class Server {
342342
const contentBasePublicPath = this.options.contentBasePublicPath;
343343

344344
if (Array.isArray(contentBase)) {
345-
contentBase.forEach((item) => {
346-
this.app.use(contentBasePublicPath, express.static(item));
345+
contentBase.forEach((item, index) => {
346+
let publicPath = contentBasePublicPath;
347+
348+
if (
349+
Array.isArray(contentBasePublicPath) &&
350+
contentBasePublicPath[index]
351+
) {
352+
publicPath = contentBasePublicPath[index] || contentBasePublicPath[0];
353+
}
354+
355+
this.app.use(publicPath, express.static(item));
347356
});
348357
} else if (isAbsoluteUrl(String(contentBase))) {
349358
this.log.warn(

‎lib/options.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@
5252
"type": "boolean"
5353
},
5454
"contentBasePublicPath": {
55-
"type": "string"
55+
"anyOf": [
56+
{
57+
"type": "string"
58+
},
59+
{
60+
"type": "array",
61+
"items": {
62+
"type": "string"
63+
},
64+
"minItems": 1
65+
}
66+
]
5667
},
5768
"contentBase": {
5869
"anyOf": [
@@ -463,7 +474,7 @@
463474
"quiet": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverquiet-)",
464475
"reporter": "should be {Function} (https://github.com/webpack/webpack-dev-middleware#reporter)",
465476
"requestCert": "should be {Boolean}",
466-
"contentBasePublicPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devservercontentbasepublicpath)",
477+
"contentBasePublicPath": "should be {String|Array} (https://webpack.js.org/configuration/dev-server/#devservercontentbasepublicpath)",
467478
"serveIndex": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverserveindex)",
468479
"serverSideRender": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#serversiderender)",
469480
"setup": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserversetup)",

‎test/server/contentBasePublicPath-option.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const contentBaseOther = path.resolve(
1616
);
1717

1818
const contentBasePublicPath = '/serve-content-base-at-this-url';
19+
const contentBasePublicOtherPath = '/serve-other-content-at-this-url';
1920

2021
describe('contentBasePublicPath option', () => {
2122
let server;
@@ -292,4 +293,45 @@ describe('contentBasePublicPath option', () => {
292293
req.patch(`${contentBasePublicPath}/`).expect(404, done);
293294
});
294295
});
296+
297+
describe('multiple contentBasePublicPath entries', () => {
298+
beforeAll((done) => {
299+
server = testServer.start(
300+
config,
301+
{
302+
contentBase: [contentBasePublic, contentBaseOther],
303+
contentBasePublicPath: [
304+
contentBasePublicPath,
305+
contentBasePublicOtherPath,
306+
],
307+
watchContentBase: true,
308+
port,
309+
},
310+
done
311+
);
312+
req = request(server.app);
313+
});
314+
315+
afterAll((done) => {
316+
testServer.close(() => {
317+
done();
318+
});
319+
});
320+
321+
it('Request the first path to index', (done) => {
322+
req.get(`${contentBasePublicPath}/`).expect(200, /Heyo/, done);
323+
});
324+
325+
it('Request the first path to other file', (done) => {
326+
req
327+
.get(`${contentBasePublicPath}/other.html`)
328+
.expect(200, /Other html/, done);
329+
});
330+
331+
it('Request the second path to foo', (done) => {
332+
req
333+
.get(`${contentBasePublicOtherPath}/foo.html`)
334+
.expect(200, /Foo!/, done);
335+
});
336+
});
295337
});

0 commit comments

Comments
 (0)
Please sign in to comment.