Skip to content

Commit

Permalink
feat(contentBasePublicPath): allow multiple paths (#2489)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuta Hiroto <git@hiroppy.me>
  • Loading branch information
jleifeld and hiroppy authored Apr 2, 2020
1 parent f317358 commit c6bdfe4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,17 @@ class Server {
const contentBasePublicPath = this.options.contentBasePublicPath;

if (Array.isArray(contentBase)) {
contentBase.forEach((item) => {
this.app.use(contentBasePublicPath, express.static(item));
contentBase.forEach((item, index) => {
let publicPath = contentBasePublicPath;

if (
Array.isArray(contentBasePublicPath) &&
contentBasePublicPath[index]
) {
publicPath = contentBasePublicPath[index] || contentBasePublicPath[0];
}

this.app.use(publicPath, express.static(item));
});
} else if (isAbsoluteUrl(String(contentBase))) {
this.log.warn(
Expand Down
15 changes: 13 additions & 2 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@
"type": "boolean"
},
"contentBasePublicPath": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
]
},
"contentBase": {
"anyOf": [
Expand Down Expand Up @@ -463,7 +474,7 @@
"quiet": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverquiet-)",
"reporter": "should be {Function} (https://github.com/webpack/webpack-dev-middleware#reporter)",
"requestCert": "should be {Boolean}",
"contentBasePublicPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devservercontentbasepublicpath)",
"contentBasePublicPath": "should be {String|Array} (https://webpack.js.org/configuration/dev-server/#devservercontentbasepublicpath)",
"serveIndex": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverserveindex)",
"serverSideRender": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#serversiderender)",
"setup": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserversetup)",
Expand Down
42 changes: 42 additions & 0 deletions test/server/contentBasePublicPath-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const contentBaseOther = path.resolve(
);

const contentBasePublicPath = '/serve-content-base-at-this-url';
const contentBasePublicOtherPath = '/serve-other-content-at-this-url';

describe('contentBasePublicPath option', () => {
let server;
Expand Down Expand Up @@ -292,4 +293,45 @@ describe('contentBasePublicPath option', () => {
req.patch(`${contentBasePublicPath}/`).expect(404, done);
});
});

describe('multiple contentBasePublicPath entries', () => {
beforeAll((done) => {
server = testServer.start(
config,
{
contentBase: [contentBasePublic, contentBaseOther],
contentBasePublicPath: [
contentBasePublicPath,
contentBasePublicOtherPath,
],
watchContentBase: true,
port,
},
done
);
req = request(server.app);
});

afterAll((done) => {
testServer.close(() => {
done();
});
});

it('Request the first path to index', (done) => {
req.get(`${contentBasePublicPath}/`).expect(200, /Heyo/, done);
});

it('Request the first path to other file', (done) => {
req
.get(`${contentBasePublicPath}/other.html`)
.expect(200, /Other html/, done);
});

it('Request the second path to foo', (done) => {
req
.get(`${contentBasePublicOtherPath}/foo.html`)
.expect(200, /Foo!/, done);
});
});
});

0 comments on commit c6bdfe4

Please sign in to comment.