Skip to content

Commit 6a241d9

Browse files
authored
Merge pull request #105 from weaverryan/tighter-manifest-key-prefix
Warning when manifest key prefix starts with "/"
2 parents d23aa92 + baa70b1 commit 6a241d9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/WebpackConfig.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const path = require('path');
1313
const fs = require('fs');
14+
const logger = require('./logger');
1415

1516
/**
1617
* @param {RuntimeConfig} runtimeConfig
@@ -109,6 +110,18 @@ class WebpackConfig {
109110
}
110111

111112
setManifestKeyPrefix(manifestKeyPrefix) {
113+
/*
114+
* Normally, we make sure that the manifest keys don't start
115+
* with an opening "/" ever... for consistency. If you need
116+
* to manually specify the manifest key (e.g. because you're
117+
* publicPath is absolute), it's easy to accidentally add
118+
* an opening slash (thereby changing your key prefix) without
119+
* intending to. Hence, the warning.
120+
*/
121+
if (manifestKeyPrefix.indexOf('/') === 0) {
122+
logger.warning(`The value passed to setManifestKeyPrefix "${manifestKeyPrefix}" starts with "/". This is allowed, but since the key prefix does not normally start with a "/", you may have just changed the prefix accidentally.`);
123+
}
124+
112125
// guarantee a single trailing slash, except for blank strings
113126
if (manifestKeyPrefix !== '') {
114127
manifestKeyPrefix = manifestKeyPrefix.replace(/\/$/, '');

test/WebpackConfig.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const RuntimeConfig = require('../lib/config/RuntimeConfig');
1515
const path = require('path');
1616
const fs = require('fs');
1717
const webpack = require('webpack');
18+
const logger = require('../lib/logger');
1819

1920
function createConfig() {
2021
const runtimeConfig = new RuntimeConfig();
@@ -148,10 +149,10 @@ describe('WebpackConfig object', () => {
148149

149150
it('You can set it!', () => {
150151
const config = createConfig();
151-
config.setManifestKeyPrefix('/foo');
152+
config.setManifestKeyPrefix('foo');
152153

153154
// trailing slash added
154-
expect(config.manifestKeyPrefix).to.equal('/foo/');
155+
expect(config.manifestKeyPrefix).to.equal('foo/');
155156
});
156157

157158
it('You can set it blank', () => {
@@ -161,6 +162,15 @@ describe('WebpackConfig object', () => {
161162
// trailing slash not added
162163
expect(config.manifestKeyPrefix).to.equal('');
163164
});
165+
166+
it('You can use an opening slash, but get a warning', () => {
167+
const config = createConfig();
168+
169+
logger.reset();
170+
logger.quiet();
171+
config.setManifestKeyPrefix('/foo/');
172+
expect(logger.getMessages().warning).to.have.lengthOf(1);
173+
});
164174
});
165175

166176
describe('configureManifestPlugin', () => {

0 commit comments

Comments
 (0)