Skip to content

Commit 00c7cdd

Browse files
committed
Fixing a bug where the contentBase could not be calculated with an empty publicPath
1 parent 722569b commit 00c7cdd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/config/path-util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ module.exports = {
4141
// eventually, you (may) get to the right path
4242
let contentBase = outputPath;
4343
while (path.dirname(contentBase) !== contentBase) {
44-
contentBase = path.dirname(contentBase);
45-
4644
if (path.join(contentBase, publicPath) === outputPath) {
4745
return contentBase;
4846
}
47+
48+
// go up one directory
49+
contentBase = path.dirname(contentBase);
4950
}
5051

5152
throw new Error(`Unable to determine contentBase option for webpack's devServer configuration. The ${webpackConfig.manifestKeyPrefix ? 'manifestKeyPrefix' : 'publicPath'} (${webpackConfig.manifestKeyPrefix ? webpackConfig.manifestKeyPrefix : webpackConfig.publicPath}) string does not exist in the outputPath (${webpackConfig.outputPath}), and so the "document root" cannot be determined.`);

test/config/path-util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ describe('path-util getContentBase()', () => {
5555
const actualContentBase = pathUtil.getContentBase(config);
5656
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');
5757
});
58+
59+
it('contentBase is calculated correctly with no public path', function() {
60+
const config = createConfig();
61+
config.runtimeConfig.useDevServer = true;
62+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
63+
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
64+
config.setPublicPath('/');
65+
config.addEntry('main', './main');
66+
67+
const actualContentBase = pathUtil.getContentBase(config);
68+
// contentBase should point to the "document root", which
69+
// is calculated as outputPath, but without the publicPath portion
70+
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');
71+
});
5872
});
5973

6074
describe('validatePublicPathAndManifestKeyPrefix', () => {

0 commit comments

Comments
 (0)