From 910b6bc8836a24feb8688aa7fb9a4e96da699fc0 Mon Sep 17 00:00:00 2001 From: robertfausk Date: Fri, 30 Jun 2017 14:09:28 +0200 Subject: [PATCH 1/7] convert error into warning when using dev-server and an absolute URL for publicPath to get webpack encore working in docker environment --- lib/WebpackConfig.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 79458ca6..faba4957 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -9,6 +9,7 @@ 'use strict'; +const chalk = require('chalk'); const path = require('path'); const fs = require('fs'); @@ -94,7 +95,8 @@ class WebpackConfig { */ 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.'); + 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(); } } else { if (publicPath.indexOf('/') !== 0) { From eb5565baeb20d9d909ee7c3fd87c9a381c567a14 Mon Sep 17 00:00:00 2001 From: robertfausk Date: Fri, 30 Jun 2017 14:35:24 +0200 Subject: [PATCH 2/7] convert error into warning test for successful config set when using devServer and setPublicPath with an absolute URL --- test/WebpackConfig.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 68d14b94..293e6678 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -105,13 +105,12 @@ describe('WebpackConfig object', () => { }).to.throw('The value passed to setPublicPath() must start with "/"'); }); - it('Setting to a URL when using devServer throws an error', () => { + it('You can set to a URL when using devServer', () => { const config = createConfig(); config.runtimeConfig.useDevServer = true; + config.setPublicPath('https://examplecdn.com'); - expect(() => { - config.setPublicPath('https://examplecdn.com'); - }).to.throw('You cannot pass an absolute URL to setPublicPath() and use the dev-server'); + expect(config.publicPath).to.equal('https://examplecdn.com/'); }); }); From e206a12520b4e994ba6003a4ec9dccb66955f96c Mon Sep 17 00:00:00 2001 From: robertfausk Date: Fri, 30 Jun 2017 15:32:22 +0200 Subject: [PATCH 3/7] fix issue in generated public path of manifest.json - when using devServer and absolute URL for publicPath - also add test case --- lib/WebpackConfig.js | 4 ++-- test/config-generator.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index faba4957..6ccaa995 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -131,8 +131,8 @@ class WebpackConfig { * @returns {string} */ getRealPublicPath() { - // if we're using webpack-dev-server, use it & add the publicPath - if (this.useDevServer()) { + // if we're using webpack-dev-server and have no absolute url, use it & add the publicPath + if (this.useDevServer() && !this.publicPath.includes('://')) { // avoid 2 middle slashes return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath; } diff --git a/test/config-generator.js b/test/config-generator.js index 9d5b739e..059a8a21 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -375,6 +375,23 @@ 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; From b27f7c9e6c004eee067949574fdf14e2419ab59f Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 12 Jul 2017 20:41:28 -0400 Subject: [PATCH 4/7] Reversing some of the changes we won't do for now, and adding the failing test --- lib/WebpackConfig.js | 8 +++---- test/WebpackConfig.js | 46 +++++++++++++++++++++++++++++++++++++--- test/config-generator.js | 17 --------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 6ccaa995..79458ca6 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -9,7 +9,6 @@ 'use strict'; -const chalk = require('chalk'); const path = require('path'); const fs = require('fs'); @@ -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) { @@ -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; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 293e6678..90a981e6 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -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'); }); }); diff --git a/test/config-generator.js b/test/config-generator.js index 059a8a21..9d5b739e 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -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; From 830fdb5af3f68992d22f0f5e641f1ef147862f1e Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 12 Jul 2017 20:54:33 -0400 Subject: [PATCH 5/7] Adding --keep-public-path to dev-server to allow you to fully control the publicPath --- bin/encore.js | 3 +++ lib/WebpackConfig.js | 6 +++--- lib/config/RuntimeConfig.js | 1 + lib/config/parse-runtime.js | 1 + test/WebpackConfig.js | 2 +- test/config/parse-runtime.js | 9 +++++++++ 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/encore.js b/bin/encore.js index 73087d4f..6ab9613a 100755 --- a/bin/encore.js +++ b/bin/encore.js @@ -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(); diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 79458ca6..119fdd45 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -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) { @@ -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; } diff --git a/lib/config/RuntimeConfig.js b/lib/config/RuntimeConfig.js index 37ed2ee9..4b035ac5 100644 --- a/lib/config/RuntimeConfig.js +++ b/lib/config/RuntimeConfig.js @@ -19,6 +19,7 @@ class RuntimeConfig { this.useDevServer = null; this.devServerUrl = null; this.devServerHttps = null; + this.devServerKeepPublicPath = false; this.useHotModuleReplacement = null; this.babelRcFileExists = null; diff --git a/lib/config/parse-runtime.js b/lib/config/parse-runtime.js index 7295a560..99cde4a9 100644 --- a/lib/config/parse-runtime.js +++ b/lib/config/parse-runtime.js @@ -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'; diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 90a981e6..27a31641 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -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/'); }); }); diff --git a/test/config/parse-runtime.js b/test/config/parse-runtime.js index b28f6410..817af584 100644 --- a/test/config/parse-runtime.js +++ b/test/config/parse-runtime.js @@ -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', () => { @@ -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; + }); }); From 92e22afccb8a8fbcbaa743a3b1eff6bbc67f4c7e Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 15 Jul 2017 10:35:00 -0400 Subject: [PATCH 6/7] Allowing an absolute publicPath with dev-server, but showing a warning --- bin/encore.js | 1 - index.js | 4 +++ lib/WebpackConfig.js | 39 +++++++++++------------- lib/config-generator.js | 2 +- lib/config/RuntimeConfig.js | 1 + lib/config/parse-runtime.js | 4 +++ lib/config/validator.js | 18 ++++++++++- lib/logger.js | 59 +++++++++++++++++++++++++++++++++++++ test/WebpackConfig.js | 12 +------- test/config/validator.js | 18 +++++++++++ 10 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 lib/logger.js diff --git a/bin/encore.js b/bin/encore.js index 6ab9613a..b8ba29e2 100755 --- a/bin/encore.js +++ b/bin/encore.js @@ -10,7 +10,6 @@ 'use strict'; -const path = require('path'); const parseRuntime = require('../lib/config/parse-runtime'); const context = require('../lib/context'); const chalk = require('chalk'); diff --git a/index.js b/index.js index 6b38a21f..c8c20f08 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ const configGenerator = require('./lib/config-generator'); const validator = require('./lib/config/validator'); const PrettyError = require('pretty-error'); const runtimeConfig = require('./lib/context').runtimeConfig; +const logger = require('./lib/logger'); // at this time, the encore executable should have set the runtimeConfig if (!runtimeConfig) { @@ -21,6 +22,9 @@ if (!runtimeConfig) { } let webpackConfig = new WebpackConfig(runtimeConfig); +if (runtimeConfig.verbose) { + logger.verbose(); +} module.exports = { /** diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 119fdd45..40173d3f 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -86,23 +86,11 @@ class WebpackConfig { } setPublicPath(publicPath) { - /* - * Do not allow absolute URLs *and* the webpackDevServer - * to be used at the same time. The webpackDevServer basically - * provides the publicPath (and so in those cases, publicPath) - * is simply used as the default manifestKeyPrefix. - */ - if (publicPath.includes('://')) { - 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) { - // technically, not starting with "/" is legal, but not - // what you want in most cases. Let's not let the user make - // a mistake (and we can always change this later). - throw new Error('The value passed to setPublicPath() must start with "/" or be a full URL (http://...)'); - } + if (publicPath.includes('://') === false && publicPath.indexOf('/') !== 0) { + // technically, not starting with "/" is legal, but not + // what you want in most cases. Let's not let the user make + // a mistake (and we can always change this later). + throw new Error('The value passed to setPublicPath() must start with "/" or be a full URL (http://...)'); } // guarantee a single trailing slash @@ -129,13 +117,20 @@ class WebpackConfig { * @returns {string} */ getRealPublicPath() { - // if we're using webpack-dev-server, use it & add the publicPath - if (this.useDevServer() && false === this.runtimeConfig.devServerKeepPublicPath) { - // avoid 2 middle slashes - return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath; + if (!this.useDevServer()) { + return this.publicPath; + } + + if (this.runtimeConfig.devServerKeepPublicPath) { + return this.publicPath; + } + + if (this.publicPath.includes('://')) { + return this.publicPath; } - return this.publicPath; + // if using dev-server, prefix the publicPath with the dev server URL + return this.runtimeConfig.devServerUrl.replace(/\/$/,'') + this.publicPath; } addEntry(name, src) { diff --git a/lib/config-generator.js b/lib/config-generator.js index 26fca25c..69ea3161 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -384,8 +384,8 @@ class ConfigGenerator { publicPath: this.webpackConfig.publicPath, // avoid CORS concerns trying to load things like fonts from the dev server headers: { 'Access-Control-Allow-Origin': '*' }, - // required by FriendlyErrorsWebpackPlugin hot: this.webpackConfig.useHotModuleReplacementPlugin(), + // required by FriendlyErrorsWebpackPlugin quiet: true, compress: true, historyApiFallback: true, diff --git a/lib/config/RuntimeConfig.js b/lib/config/RuntimeConfig.js index 4b035ac5..f2bd95b9 100644 --- a/lib/config/RuntimeConfig.js +++ b/lib/config/RuntimeConfig.js @@ -25,6 +25,7 @@ class RuntimeConfig { this.babelRcFileExists = null; this.helpRequested = false; + this.verbose = false; } } diff --git a/lib/config/parse-runtime.js b/lib/config/parse-runtime.js index 99cde4a9..36aa9073 100644 --- a/lib/config/parse-runtime.js +++ b/lib/config/parse-runtime.js @@ -29,14 +29,18 @@ module.exports = function(argv, cwd) { case 'dev': runtimeConfig.isValidCommand = true; runtimeConfig.environment = 'dev'; + runtimeConfig.verbose = true; break; case 'production': runtimeConfig.isValidCommand = true; runtimeConfig.environment = 'production'; + runtimeConfig.verbose = false; break; case 'dev-server': runtimeConfig.isValidCommand = true; runtimeConfig.environment = 'dev'; + runtimeConfig.verbose = true; + runtimeConfig.useDevServer = true; runtimeConfig.devServerHttps = argv.https; runtimeConfig.useHotModuleReplacement = argv.hot || false; diff --git a/lib/config/validator.js b/lib/config/validator.js index 14f6952c..a7eda7cc 100644 --- a/lib/config/validator.js +++ b/lib/config/validator.js @@ -10,6 +10,7 @@ 'use strict'; const pathUtil = require('./path-util'); +const logger = require('./../logger'); class Validator { /** @@ -46,9 +47,24 @@ class Validator { } _validateDevServer() { - if (this.webpackConfig.useVersioning && this.webpackConfig.useDevServer()) { + if (!this.webpackConfig.useDevServer()) { + return; + } + + if (this.webpackConfig.useVersioning) { throw new Error('Don\'t enable versioning with the dev-server. A good setting is Encore.enableVersioning(Encore.isProduction()).'); } + + /* + * An absolute publicPath is incompatible with webpackDevServer. + * This is because we want to *change* the publicPath to point + * to the webpackDevServer URL (e.g. http://localhost:8080/). + * There are some valid use-cases for not wanting this behavior + * (see #59), but we want to warn the user. + */ + if (this.webpackConfig.publicPath.includes('://')) { + logger.warning(`Passing an absolute URL to setPublicPath() *and* using the dev-server can cause issues. Your assets will load from the publicPath (${this.webpackConfig.publicPath}) instead of from the dev server URL (${this.webpackConfig.runtimeConfig.devServerUrl}).`); + } } } diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 00000000..f51f1500 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,59 @@ +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +'use strict'; + +const chalk = require('chalk'); +let isVerbose = false; +let quiet = false; +let messages = { + debug: [], + warning: [], +}; + +function log(message) { + if (quiet) { + return; + } + + console.log(message); +} + +module.exports = { + debug(message) { + messages.debug.push(message); + + if (isVerbose) { + log(`${chalk.bgBlack.white(' DEBUG ')} ${message}`); + } + }, + + warning(message) { + messages.warning.push(message); + + log(`${chalk.bgYellow.black(' WARNING ')} ${chalk.yellow(message)}`); + }, + + clearMessages() { + messages.debug = []; + messages.warning = []; + }, + + getMessages() { + return messages; + }, + + quiet() { + quiet = true; + }, + + verbose() { + isVerbose = true; + } +}; diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 27a31641..06bdc71c 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -104,15 +104,6 @@ describe('WebpackConfig object', () => { config.setPublicPath('foo/'); }).to.throw('The value passed to setPublicPath() must start with "/"'); }); - - it('Setting to a URL when using devServer throws an error', () => { - const config = createConfig(); - config.runtimeConfig.useDevServer = true; - - expect(() => { - config.setPublicPath('https://examplecdn.com'); - }).to.throw('You cannot pass an absolute URL to setPublicPath() and use the dev-server'); - }); }); describe('getRealPublicPath', () => { @@ -142,11 +133,10 @@ describe('WebpackConfig object', () => { expect(config.getRealPublicPath()).to.equal('/public/'); }); - it('devServer & devServerKeepPublicPath option allows absolute publicPath', () => { + it('devServer does not prefix if publicPath is absolute', () => { 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/'); diff --git a/test/config/validator.js b/test/config/validator.js index 9ab2810c..8f4f8555 100644 --- a/test/config/validator.js +++ b/test/config/validator.js @@ -13,6 +13,9 @@ const expect = require('chai').expect; const WebpackConfig = require('../../lib/WebpackConfig'); const RuntimeConfig = require('../../lib/config/RuntimeConfig'); const validator = require('../../lib/config/validator'); +const logger = require('../../lib/logger'); + +logger.quiet(); function createConfig() { const runtimeConfig = new RuntimeConfig(); @@ -65,4 +68,19 @@ describe('The validator function', () => { validator(config); }).to.throw('Don\'t enable versioning with the dev-server'); }); + + it('warning with dev-server and absolute publicPath', () => { + const config = createConfig(); + config.outputPath = '/tmp/public/build'; + config.setPublicPath('https://absoluteurl.com/build'); + config.setManifestKeyPrefix('build/'); + config.addEntry('main', './main'); + config.runtimeConfig.useDevServer = true; + + logger.clearMessages(); + validator(config); + + expect(logger.getMessages().warning).to.have.lengthOf(1); + expect(logger.getMessages().warning[0]).to.include('Passing an absolute URL to setPublicPath() *and* using the dev-server can cause issues'); + }); }); From 4bc1e19fd44aa892f54368ca500922a1ed32fd76 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 20 Jul 2017 20:50:22 -0400 Subject: [PATCH 7/7] Using real public path, though it doesn't look like it matters --- lib/config-generator.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 69ea3161..908b3686 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -381,7 +381,8 @@ class ConfigGenerator { return { contentBase: contentBase, - publicPath: this.webpackConfig.publicPath, + // this doesn't appear to be necessary, but here in case + publicPath: this.webpackConfig.getRealPublicPath(), // avoid CORS concerns trying to load things like fonts from the dev server headers: { 'Access-Control-Allow-Origin': '*' }, hot: this.webpackConfig.useHotModuleReplacementPlugin(),