Skip to content

Commit

Permalink
Reversing some of the changes we won't do for now, and adding the fai…
Browse files Browse the repository at this point in the history
…ling test
  • Loading branch information
weaverryan committed Jul 21, 2017
1 parent e206a12 commit b27f7c9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
8 changes: 3 additions & 5 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

'use strict';

const chalk = require('chalk');
const path = require('path');
const fs = require('fs');

Expand Down Expand Up @@ -95,8 +94,7 @@ class WebpackConfig {
*/
if (publicPath.includes('://')) {
if (this.useDevServer()) {
console.log(chalk.bgYellow.black(' WARNING ') + chalk.yellow(' You should not pass an absolute URL to setPublicPath() and use the dev-server at the same time'));
console.log();
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.');
}
} else {
if (publicPath.indexOf('/') !== 0) {
Expand Down Expand Up @@ -131,8 +129,8 @@ class WebpackConfig {
* @returns {string}
*/
getRealPublicPath() {
// if we're using webpack-dev-server and have no absolute url, use it & add the publicPath
if (this.useDevServer() && !this.publicPath.includes('://')) {
// if we're using webpack-dev-server, use it & add the publicPath
if (this.useDevServer()) {
// avoid 2 middle slashes
return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath;
}
Expand Down
46 changes: 43 additions & 3 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,52 @@ describe('WebpackConfig object', () => {
}).to.throw('The value passed to setPublicPath() must start with "/"');
});

it('You can set to a URL when using devServer', () => {
it('Setting to a URL when using devServer throws an error', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.setPublicPath('https://examplecdn.com');

expect(config.publicPath).to.equal('https://examplecdn.com/');
expect(() => {
config.setPublicPath('https://examplecdn.com');
}).to.throw('You cannot pass an absolute URL to setPublicPath() and use the dev-server');
});
});

describe('getRealPublicPath', () => {
it('Returns normal with no dev server', () => {
const config = createConfig();
config.setPublicPath('/public');

expect(config.getRealPublicPath()).to.equal('/public/');
});

it('Prefix when using devServer', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.setPublicPath('/public');

expect(config.getRealPublicPath()).to.equal('http://localhost:8080/public/');
});

it('No prefix with devServer & devServerKeepPublicPath option', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.runtimeConfig.devServerKeepPublicPath = true;
config.setPublicPath('/public');

expect(config.getRealPublicPath()).to.equal('/public/');
});

it('devServer & devServerKeepPublicPath option allows absolute publicPath', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.runtimeConfig.devServerKeepPublicPath = true;
config.setPublicPath('http://coolcdn.com/public');
config.setManifestKeyPrefix('/public/');

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

Expand Down
17 changes: 0 additions & 17 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,6 @@ describe('The config-generator function', () => {
expect(actualConfig.devServer).to.be.undefined;
});

it('devServer and an absolute URL as publicPath', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
config.setManifestKeyPrefix('public');
config.setPublicPath('https://cdn.example.com');
config.addEntry('main', './main');

const actualConfig = configGenerator(config);
expect(actualConfig.output.publicPath).to.equal('https://cdn.example.com/');
expect(actualConfig.devServer).to.not.be.undefined;

const manifestPlugin = findPlugin(ManifestPlugin, actualConfig.plugins);
expect(manifestPlugin.opts.publicPath).to.equal('https://cdn.example.com/');
});

it('devServer no hot mode', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
Expand Down

0 comments on commit b27f7c9

Please sign in to comment.