Skip to content

Commit 162d5f5

Browse files
committed
Moving some validation logic to be more clear about the flow
1 parent 5f8ff97 commit 162d5f5

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

lib/config-generator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ class ConfigGenerator {
276276
*/
277277
let manifestPrefix = this.webpackConfig.manifestKeyPrefix;
278278
if (null === manifestPrefix) {
279-
manifestPrefix = pathUtil.generateManifestKeyPrefix(this.webpackConfig);
279+
// by convention, we remove the opening slash on the manifest keys
280+
manifestPrefix = this.webpackConfig.publicPath.replace(/^\//,'');
280281
}
281282
plugins.push(new ManifestPlugin({
282283
basePath: manifestPrefix,

lib/config/path-util.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const path = require('path');
1313

1414
module.exports = {
1515
/**
16-
* Determines the "contentBase" used for the devServer.
16+
* Determines the "contentBase" to use for the devServer.
1717
*
1818
* @param {WebpackConfig} webpackConfig
1919
* @return {String}
@@ -30,6 +30,10 @@ module.exports = {
3030
* * outputPath = /var/www/public/build
3131
* * publicPath = /build/
3232
* => contentBase should be /var/www/public
33+
*
34+
* At this point, if the publicPath is non-standard (e.g. it contains
35+
* a sub-directory or is absolute), then the user will already see
36+
* an error that they must set the manifestKeyPrefix.
3337
*/
3438

3539
// start with outputPath, then join publicPath with it, see if it equals outputPath
@@ -44,7 +48,7 @@ module.exports = {
4448
}
4549
}
4650

47-
throw new Error(`Unable to determine contentBase option for webpack's devServer configuration. The publicPath (${webpackConfig.publicPath}) string does not exist in the outputPath (${webpackConfig.outputPath}), and so the "document root" cannot be determined.`);
51+
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.`);
4852
},
4953

5054
/**
@@ -64,9 +68,13 @@ module.exports = {
6468
* ok to use the publicPath as the manifestKeyPrefix.
6569
*
6670
* @param {WebpackConfig} webpackConfig
67-
* @return {String}
6871
*/
69-
generateManifestKeyPrefix(webpackConfig) {
72+
validatePublicPathAndManifestKeyPrefix(webpackConfig) {
73+
if (webpackConfig.manifestKeyPrefix !== null) {
74+
// nothing to check - they have manually set the key prefix
75+
return;
76+
}
77+
7078
if (webpackConfig.publicPath.includes('://')) {
7179
/*
7280
* If publicPath is absolute, you probably don't want your manifests.json
@@ -103,8 +111,5 @@ module.exports = {
103111

104112
throw new Error(`Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. ${suggestion}) to use when building your manifest keys. This is caused by setOutputPath() (${outputPath}) and setPublicPath() (${publicPath}) containing paths that don't seem compatible.`);
105113
}
106-
107-
// by convention, we remove the opening slash on the manifest keys
108-
return webpackConfig.publicPath.replace(/^\//,'');
109114
}
110115
};

lib/config/validator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
'use strict';
1111

12+
const pathUtil = require('./path-util');
13+
1214
class Validator {
1315
/**
1416
* @param {WebpackConfig} webpackConfig
@@ -20,6 +22,8 @@ class Validator {
2022
validate() {
2123
this._validateBasic();
2224

25+
this._validatePublicPathAndManifestKeyPrefix();
26+
2327
this._validateDevServer();
2428
}
2529

@@ -37,6 +41,10 @@ class Validator {
3741
}
3842
}
3943

44+
_validatePublicPathAndManifestKeyPrefix() {
45+
pathUtil.validatePublicPathAndManifestKeyPrefix(this.webpackConfig);
46+
}
47+
4048
_validateDevServer() {
4149
if (this.webpackConfig.useVersioning && this.webpackConfig.useDevServer()) {
4250
throw new Error('Don\'t enable versioning with the dev-server. A good setting is Encore.enableVersioning(Encore.isProduction()).');

test/config/path-util.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ describe('path-util getContentBase()', () => {
8989
});
9090
});
9191

92-
describe('generateManifestKeyPrefix', () => {
92+
describe('validatePublicPathAndManifestKeyPrefix', () => {
9393
it('manifestKeyPrefix is correctly not required on windows', () => {
9494
const config = createConfig();
9595
config.outputPath = 'C:\\projects\\webpack-encore\\web\\build';
9696
config.setPublicPath('/build/');
9797
config.addEntry('main', './main');
9898

99-
const actualPrefix = pathUtil.generateManifestKeyPrefix(config);
100-
expect(actualPrefix).to.equal('build/');
99+
// NOT throwing an error is the assertion
100+
pathUtil.validatePublicPathAndManifestKeyPrefix(config);
101101
});
102102

103103
it('with absolute publicPath, manifestKeyPrefix must be set', () => {
@@ -108,7 +108,7 @@ describe('path-util getContentBase()', () => {
108108
config.setPublicPath('https://cdn.example.com');
109109

110110
expect(() => {
111-
pathUtil.generateManifestKeyPrefix(config);
111+
pathUtil.validatePublicPathAndManifestKeyPrefix(config);
112112
}).to.throw('Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. build/) to use');
113113
});
114114

@@ -120,7 +120,7 @@ describe('path-util getContentBase()', () => {
120120
config.setPublicPath('/subdirectory/build');
121121

122122
expect(() => {
123-
pathUtil.generateManifestKeyPrefix(config);
123+
pathUtil.validatePublicPathAndManifestKeyPrefix(config);
124124
}).to.throw('Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. build/) to use');
125125
});
126126
});

0 commit comments

Comments
 (0)