diff --git a/lib/Server.js b/lib/Server.js index 136f0a8d66..2a69d75c8e 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -27,6 +27,7 @@ const normalizeOptions = require('./utils/normalizeOptions'); const updateCompiler = require('./utils/updateCompiler'); const createLogger = require('./utils/createLogger'); const getCertificate = require('./utils/getCertificate'); +const checkUrl = require('./utils/checkUrl'); const status = require('./utils/status'); const createDomain = require('./utils/createDomain'); const runBonjour = require('./utils/runBonjour'); @@ -338,7 +339,7 @@ class Server { contentBase.forEach((item) => { this.app.get('*', express.static(item)); }); - } else if (/^(https?:)?\/\//.test(contentBase)) { + } else if (checkUrl(contentBase)) { this.log.warn( 'Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.' ); @@ -389,10 +390,7 @@ class Server { contentBase.forEach((item) => { this.app.get('*', serveIndex(item)); }); - } else if ( - typeof contentBase !== 'number' && - !/^(https?:)?\/\//.test(contentBase) - ) { + } else if (typeof contentBase !== 'number' && !checkUrl(contentBase)) { this.app.get('*', serveIndex(contentBase)); } } @@ -400,14 +398,11 @@ class Server { setupWatchStaticFeature() { const contentBase = this.options.contentBase; - if ( - /^(https?:)?\/\//.test(contentBase) || - typeof contentBase === 'number' - ) { + if (checkUrl(contentBase) || typeof contentBase === 'number') { throw new Error('Watching remote files is not supported.'); } else if (Array.isArray(contentBase)) { contentBase.forEach((item) => { - if (/^(https?:)?\/\//.test(item) || typeof item === 'number') { + if (checkUrl(item) || typeof item === 'number') { throw new Error('Watching remote files is not supported.'); } this._watch(item); diff --git a/lib/utils/checkUrl.js b/lib/utils/checkUrl.js new file mode 100644 index 0000000000..e600bb8f0f --- /dev/null +++ b/lib/utils/checkUrl.js @@ -0,0 +1,7 @@ +'use strict'; + +function checkUrl(url) { + return /^(https?:)?\/\//.test(url); +} + +module.exports = checkUrl; diff --git a/lib/utils/createConfig.js b/lib/utils/createConfig.js index 242acbf5bc..bab5a37914 100644 --- a/lib/utils/createConfig.js +++ b/lib/utils/createConfig.js @@ -2,6 +2,7 @@ const path = require('path'); const defaultTo = require('./defaultTo'); +const checkUrl = require('./checkUrl'); function createConfig(config, argv, { port }) { const firstWpOpt = Array.isArray(config) ? config[0] : config; @@ -63,10 +64,7 @@ function createConfig(config, argv, { port }) { options.publicPath = (firstWpOpt.output && firstWpOpt.output.publicPath) || ''; - if ( - !/^(https?:)?\/\//.test(options.publicPath) && - options.publicPath[0] !== '/' - ) { + if (!checkUrl(options.publicPath) && options.publicPath[0] !== '/') { options.publicPath = `/${options.publicPath}`; } } @@ -113,7 +111,7 @@ function createConfig(config, argv, { port }) { options.contentBase = options.contentBase.map((p) => path.resolve(p)); } else if (/^[0-9]$/.test(options.contentBase)) { options.contentBase = +options.contentBase; - } else if (!/^(https?:)?\/\//.test(options.contentBase)) { + } else if (!checkUrl(options.contentBase)) { options.contentBase = path.resolve(options.contentBase); } } diff --git a/lib/utils/runOpen.js b/lib/utils/runOpen.js index 97b73b24b4..d625e4ee51 100644 --- a/lib/utils/runOpen.js +++ b/lib/utils/runOpen.js @@ -1,6 +1,7 @@ 'use strict'; const open = require('opn'); +const checkUrl = require('./checkUrl'); function runOpen(uri, options, log) { // https://github.com/webpack/webpack-dev-server/issues/1990 @@ -13,7 +14,7 @@ function runOpen(uri, options, log) { } const pageUrl = - options.openPage && /^(https?:)?\/\//.test(options.openPage) + options.openPage && checkUrl(options.openPage) ? options.openPage : `${uri}${options.openPage || ''}`; diff --git a/package.json b/package.json index ec620d5f74..102d09b997 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ "import-local": "^2.0.0", "internal-ip": "^4.3.0", "ip": "^1.1.5", - "is-absolute-url": "^3.0.0", "killable": "^1.0.1", "loglevel": "^1.6.3", "opn": "^5.5.0",