Skip to content

Commit b27f7c9

Browse files
committed
Reversing some of the changes we won't do for now, and adding the failing test
1 parent e206a12 commit b27f7c9

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

lib/WebpackConfig.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
'use strict';
1111

12-
const chalk = require('chalk');
1312
const path = require('path');
1413
const fs = require('fs');
1514

@@ -95,8 +94,7 @@ class WebpackConfig {
9594
*/
9695
if (publicPath.includes('://')) {
9796
if (this.useDevServer()) {
98-
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'));
99-
console.log();
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.');
10098
}
10199
} else {
102100
if (publicPath.indexOf('/') !== 0) {
@@ -131,8 +129,8 @@ class WebpackConfig {
131129
* @returns {string}
132130
*/
133131
getRealPublicPath() {
134-
// if we're using webpack-dev-server and have no absolute url, use it & add the publicPath
135-
if (this.useDevServer() && !this.publicPath.includes('://')) {
132+
// if we're using webpack-dev-server, use it & add the publicPath
133+
if (this.useDevServer()) {
136134
// avoid 2 middle slashes
137135
return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath;
138136
}

test/WebpackConfig.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,52 @@ describe('WebpackConfig object', () => {
105105
}).to.throw('The value passed to setPublicPath() must start with "/"');
106106
});
107107

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

113-
expect(config.publicPath).to.equal('https://examplecdn.com/');
112+
expect(() => {
113+
config.setPublicPath('https://examplecdn.com');
114+
}).to.throw('You cannot pass an absolute URL to setPublicPath() and use the dev-server');
115+
});
116+
});
117+
118+
describe('getRealPublicPath', () => {
119+
it('Returns normal with no dev server', () => {
120+
const config = createConfig();
121+
config.setPublicPath('/public');
122+
123+
expect(config.getRealPublicPath()).to.equal('/public/');
124+
});
125+
126+
it('Prefix when using devServer', () => {
127+
const config = createConfig();
128+
config.runtimeConfig.useDevServer = true;
129+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
130+
config.setPublicPath('/public');
131+
132+
expect(config.getRealPublicPath()).to.equal('http://localhost:8080/public/');
133+
});
134+
135+
it('No prefix with devServer & devServerKeepPublicPath option', () => {
136+
const config = createConfig();
137+
config.runtimeConfig.useDevServer = true;
138+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
139+
config.runtimeConfig.devServerKeepPublicPath = true;
140+
config.setPublicPath('/public');
141+
142+
expect(config.getRealPublicPath()).to.equal('/public/');
143+
});
144+
145+
it('devServer & devServerKeepPublicPath option allows absolute publicPath', () => {
146+
const config = createConfig();
147+
config.runtimeConfig.useDevServer = true;
148+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
149+
config.runtimeConfig.devServerKeepPublicPath = true;
150+
config.setPublicPath('http://coolcdn.com/public');
151+
config.setManifestKeyPrefix('/public/');
152+
153+
expect(config.getRealPublicPath()).to.equal('http://coolcdn.com/public');
114154
});
115155
});
116156

test/config-generator.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,6 @@ describe('The config-generator function', () => {
375375
expect(actualConfig.devServer).to.be.undefined;
376376
});
377377

378-
it('devServer and an absolute URL as publicPath', () => {
379-
const config = createConfig();
380-
config.runtimeConfig.useDevServer = true;
381-
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
382-
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
383-
config.setManifestKeyPrefix('public');
384-
config.setPublicPath('https://cdn.example.com');
385-
config.addEntry('main', './main');
386-
387-
const actualConfig = configGenerator(config);
388-
expect(actualConfig.output.publicPath).to.equal('https://cdn.example.com/');
389-
expect(actualConfig.devServer).to.not.be.undefined;
390-
391-
const manifestPlugin = findPlugin(ManifestPlugin, actualConfig.plugins);
392-
expect(manifestPlugin.opts.publicPath).to.equal('https://cdn.example.com/');
393-
});
394-
395378
it('devServer no hot mode', () => {
396379
const config = createConfig();
397380
config.runtimeConfig.useDevServer = true;

0 commit comments

Comments
 (0)