Skip to content

Commit

Permalink
Adding --keep-public-path to dev-server to allow you to fully control…
Browse files Browse the repository at this point in the history
… the publicPath
  • Loading branch information
weaverryan committed Jul 21, 2017
1 parent b27f7c9 commit 830fdb5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions bin/encore.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ function showUsageInstructions() {
console.log('Commands:');
console.log(` ${chalk.green('dev')} : runs webpack for development`);
console.log(' - Supports any webpack options (e.g. --watch)');
console.log();
console.log(` ${chalk.green('dev-server')} : runs webpack-dev-server`);
console.log(` - ${chalk.yellow('--host')} The hostname/ip address the webpack-dev-server will bind to`);
console.log(` - ${chalk.yellow('--port')} The port the webpack-dev-server will bind to`);
console.log(` - ${chalk.yellow('--hot')} Enable HMR on webpack-dev-server`);
console.log(` - ${chalk.yellow('--keep-public-path')} Do not change the public path (it is usually prefixed by the dev server URL)`);
console.log(' - Supports any webpack-dev-server options');
console.log();
console.log(` ${chalk.green('production')} : runs webpack for production`);
console.log(' - Supports any webpack options (e.g. --watch)');
console.log();
Expand Down
6 changes: 3 additions & 3 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class WebpackConfig {
* is simply used as the default manifestKeyPrefix.
*/
if (publicPath.includes('://')) {
if (this.useDevServer()) {
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.');
if (this.useDevServer() && false === this.runtimeConfig.devServerKeepPublicPath) {
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.');
}
} else {
if (publicPath.indexOf('/') !== 0) {
Expand Down Expand Up @@ -130,7 +130,7 @@ class WebpackConfig {
*/
getRealPublicPath() {
// if we're using webpack-dev-server, use it & add the publicPath
if (this.useDevServer()) {
if (this.useDevServer() && false === this.runtimeConfig.devServerKeepPublicPath) {
// avoid 2 middle slashes
return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath;
}
Expand Down
1 change: 1 addition & 0 deletions lib/config/RuntimeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RuntimeConfig {
this.useDevServer = null;
this.devServerUrl = null;
this.devServerHttps = null;
this.devServerKeepPublicPath = false;
this.useHotModuleReplacement = null;

this.babelRcFileExists = null;
Expand Down
1 change: 1 addition & 0 deletions lib/config/parse-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function(argv, cwd) {
runtimeConfig.useDevServer = true;
runtimeConfig.devServerHttps = argv.https;
runtimeConfig.useHotModuleReplacement = argv.hot || false;
runtimeConfig.devServerKeepPublicPath = argv.keepPublicPath || false;

var host = argv.host ? argv.host : 'localhost';
var port = argv.port ? argv.port : '8080';
Expand Down
2 changes: 1 addition & 1 deletion test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('WebpackConfig object', () => {
config.setPublicPath('http://coolcdn.com/public');
config.setManifestKeyPrefix('/public/');

expect(config.getRealPublicPath()).to.equal('http://coolcdn.com/public');
expect(config.getRealPublicPath()).to.equal('http://coolcdn.com/public/');
});
});

Expand Down
9 changes: 9 additions & 0 deletions test/config/parse-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('parse-runtime', () => {
expect(config.useDevServer).to.be.true;
expect(config.devServerUrl).to.equal('http://localhost:8080/');
expect(config.useHotModuleReplacement).to.be.false;
expect(config.devServerKeepPublicPath).to.be.false;
});

it('dev-server command with options', () => {
Expand Down Expand Up @@ -114,4 +115,12 @@ describe('parse-runtime', () => {
expect(config.useDevServer).to.be.true;
expect(config.useHotModuleReplacement).to.be.true;
});

it('dev-server command --keep-public-path', () => {
const testDir = createTestDirectory();
const config = parseArgv(createArgv(['dev-server', '--keep-public-path']), testDir);

expect(config.useDevServer).to.be.true;
expect(config.devServerKeepPublicPath).to.be.true;
});
});

0 comments on commit 830fdb5

Please sign in to comment.