Skip to content

Commit 830fdb5

Browse files
committed
Adding --keep-public-path to dev-server to allow you to fully control the publicPath
1 parent b27f7c9 commit 830fdb5

File tree

6 files changed

+18
-4
lines changed

6 files changed

+18
-4
lines changed

bin/encore.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ function showUsageInstructions() {
6262
console.log('Commands:');
6363
console.log(` ${chalk.green('dev')} : runs webpack for development`);
6464
console.log(' - Supports any webpack options (e.g. --watch)');
65+
console.log();
6566
console.log(` ${chalk.green('dev-server')} : runs webpack-dev-server`);
6667
console.log(` - ${chalk.yellow('--host')} The hostname/ip address the webpack-dev-server will bind to`);
6768
console.log(` - ${chalk.yellow('--port')} The port the webpack-dev-server will bind to`);
6869
console.log(` - ${chalk.yellow('--hot')} Enable HMR on webpack-dev-server`);
70+
console.log(` - ${chalk.yellow('--keep-public-path')} Do not change the public path (it is usually prefixed by the dev server URL)`);
6971
console.log(' - Supports any webpack-dev-server options');
72+
console.log();
7073
console.log(` ${chalk.green('production')} : runs webpack for production`);
7174
console.log(' - Supports any webpack options (e.g. --watch)');
7275
console.log();

lib/WebpackConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class WebpackConfig {
9393
* is simply used as the default manifestKeyPrefix.
9494
*/
9595
if (publicPath.includes('://')) {
96-
if (this.useDevServer()) {
97-
throw new Error('You cannot pass an absolute URL to setPublicPath() and use the dev-server at the same time. Try using Encore.isProduction() to only configure your absolute publicPath for production.');
96+
if (this.useDevServer() && false === this.runtimeConfig.devServerKeepPublicPath) {
97+
throw new Error('You cannot pass an absolute URL to setPublicPath() and use the dev-server at the same time. This is because the public path is automatically set to point to the dev server. Try using Encore.isProduction() to only configure your absolute publicPath for production. Or, if you want to override this behavior, pass the --keep-public-path option to allow this.');
9898
}
9999
} else {
100100
if (publicPath.indexOf('/') !== 0) {
@@ -130,7 +130,7 @@ class WebpackConfig {
130130
*/
131131
getRealPublicPath() {
132132
// if we're using webpack-dev-server, use it & add the publicPath
133-
if (this.useDevServer()) {
133+
if (this.useDevServer() && false === this.runtimeConfig.devServerKeepPublicPath) {
134134
// avoid 2 middle slashes
135135
return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath;
136136
}

lib/config/RuntimeConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RuntimeConfig {
1919
this.useDevServer = null;
2020
this.devServerUrl = null;
2121
this.devServerHttps = null;
22+
this.devServerKeepPublicPath = false;
2223
this.useHotModuleReplacement = null;
2324

2425
this.babelRcFileExists = null;

lib/config/parse-runtime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = function(argv, cwd) {
4040
runtimeConfig.useDevServer = true;
4141
runtimeConfig.devServerHttps = argv.https;
4242
runtimeConfig.useHotModuleReplacement = argv.hot || false;
43+
runtimeConfig.devServerKeepPublicPath = argv.keepPublicPath || false;
4344

4445
var host = argv.host ? argv.host : 'localhost';
4546
var port = argv.port ? argv.port : '8080';

test/WebpackConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('WebpackConfig object', () => {
150150
config.setPublicPath('http://coolcdn.com/public');
151151
config.setManifestKeyPrefix('/public/');
152152

153-
expect(config.getRealPublicPath()).to.equal('http://coolcdn.com/public');
153+
expect(config.getRealPublicPath()).to.equal('http://coolcdn.com/public/');
154154
});
155155
});
156156

test/config/parse-runtime.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe('parse-runtime', () => {
6969
expect(config.useDevServer).to.be.true;
7070
expect(config.devServerUrl).to.equal('http://localhost:8080/');
7171
expect(config.useHotModuleReplacement).to.be.false;
72+
expect(config.devServerKeepPublicPath).to.be.false;
7273
});
7374

7475
it('dev-server command with options', () => {
@@ -114,4 +115,12 @@ describe('parse-runtime', () => {
114115
expect(config.useDevServer).to.be.true;
115116
expect(config.useHotModuleReplacement).to.be.true;
116117
});
118+
119+
it('dev-server command --keep-public-path', () => {
120+
const testDir = createTestDirectory();
121+
const config = parseArgv(createArgv(['dev-server', '--keep-public-path']), testDir);
122+
123+
expect(config.useDevServer).to.be.true;
124+
expect(config.devServerKeepPublicPath).to.be.true;
125+
});
117126
});

0 commit comments

Comments
 (0)