Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mode as development #1653

Merged
merged 3 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
175 changes: 5 additions & 170 deletions bin/webpack-dev-server.js
Expand Up @@ -17,7 +17,6 @@ const debug = require('debug')('webpack-dev-server');

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

const portfinder = require('portfinder');
const importLocal = require('import-local');
Expand All @@ -27,13 +26,14 @@ const webpack = require('webpack');

const options = require('./options');

const { colors, status, version, bonjour, defaultTo } = require('./utils');
const { colors, status, version, bonjour } = require('./utils');

const Server = require('../lib/Server');

const addEntries = require('../lib/utils/addEntries');
const createDomain = require('../lib/utils/createDomain');
const createLogger = require('../lib/utils/createLogger');
const createConfig = require('../lib/utils/createConfig');

let server;

Expand Down Expand Up @@ -88,6 +88,7 @@ const argv = yargs.argv;
const config = require('webpack-cli/bin/convert-argv')(yargs, argv, {
outputFilename: '/bundle.js',
});

// Taken out of yargs because we must know if
// it wasn't given by the user, in which case
// we should use portfinder.
Expand All @@ -105,182 +106,16 @@ function processOptions(config) {
return;
}

const firstWpOpt = Array.isArray(config) ? config[0] : config;

const options = config.devServer || firstWpOpt.devServer || {};

if (argv.bonjour) {
options.bonjour = true;
}

if (argv.host !== 'localhost' || !options.host) {
options.host = argv.host;
}

if (argv['allowed-hosts']) {
options.allowedHosts = argv['allowed-hosts'].split(',');
}

if (argv.public) {
options.public = argv.public;
}

if (argv.socket) {
options.socket = argv.socket;
}

if (argv.progress) {
options.progress = argv.progress;
}

if (!options.publicPath) {
// eslint-disable-next-line
options.publicPath =
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';

if (
!/^(https?:)?\/\//.test(options.publicPath) &&
options.publicPath[0] !== '/'
) {
options.publicPath = `/${options.publicPath}`;
}
}

if (!options.filename) {
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
}

if (!options.watchOptions) {
options.watchOptions = firstWpOpt.watchOptions;
}

if (argv.stdin) {
process.stdin.on('end', () => {
// eslint-disable-next-line no-process-exit
process.exit(0);
});

process.stdin.resume();
}

if (!options.hot) {
options.hot = argv.hot;
}
const options = createConfig(config, argv, { port: DEFAULT_PORT });

if (!options.hotOnly) {
options.hotOnly = argv['hot-only'];
}

if (!options.clientLogLevel) {
options.clientLogLevel = argv['client-log-level'];
}

// eslint-disable-next-line
if (options.contentBase === undefined) {
if (argv['content-base']) {
options.contentBase = argv['content-base'];

if (Array.isArray(options.contentBase)) {
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)) {
options.contentBase = path.resolve(options.contentBase);
}
// It is possible to disable the contentBase by using
// `--no-content-base`, which results in arg["content-base"] = false
} else if (argv['content-base'] === false) {
options.contentBase = false;
}
}

if (argv['watch-content-base']) {
options.watchContentBase = true;
}

if (!options.stats) {
options.stats = {
cached: false,
cachedAssets: false,
};
}

if (
typeof options.stats === 'object' &&
typeof options.stats.colors === 'undefined'
) {
options.stats = Object.assign({}, options.stats, { colors: argv.color });
}

if (argv.lazy) {
options.lazy = true;
}

if (!argv.info) {
options.noInfo = true;
}

if (argv.quiet) {
options.quiet = true;
}

if (argv.https) {
options.https = true;
}

if (argv['pfx-passphrase']) {
options.pfxPassphrase = argv['pfx-passphrase'];
}

if (argv.inline === false) {
options.inline = false;
}

if (argv['history-api-fallback']) {
options.historyApiFallback = true;
}

if (argv.compress) {
options.compress = true;
}

if (argv['disable-host-check']) {
options.disableHostCheck = true;
}

if (argv['open-page']) {
options.open = true;
options.openPage = argv['open-page'];
}

if (typeof argv.open !== 'undefined') {
options.open = argv.open !== '' ? argv.open : true;
}

if (options.open && !options.openPage) {
options.openPage = '';
}

if (argv.useLocalIp) {
options.useLocalIp = true;
}
// Kind of weird, but ensures prior behavior isn't broken in cases
// that wouldn't throw errors. E.g. both argv.port and options.port
// were specified, but since argv.port is 8080, options.port will be
// tried first instead.
options.port =
argv.port === DEFAULT_PORT
? defaultTo(options.port, argv.port)
: defaultTo(argv.port, options.port);
portfinder.basePort = DEFAULT_PORT;

if (options.port != null) {
startDevServer(config, options);

return;
}

portfinder.basePort = DEFAULT_PORT;

portfinder.getPort((err, port) => {
if (err) {
throw err;
Expand Down
181 changes: 181 additions & 0 deletions lib/utils/createConfig.js
@@ -0,0 +1,181 @@
'use strict';

const path = require('path');
const { defaultTo } = require('../../bin/utils');

function createConfig(config, argv, { port }) {
const firstWpOpt = Array.isArray(config) ? config[0] : config;
const options = firstWpOpt.devServer || {};

// This updates both config and firstWpOpt
firstWpOpt.mode = defaultTo(firstWpOpt.mode, 'development');

if (argv.bonjour) {
options.bonjour = true;
}

if (argv.host !== 'localhost' || !options.host) {
options.host = argv.host;
}

if (argv['allowed-hosts']) {
options.allowedHosts = argv['allowed-hosts'].split(',');
}

if (argv.public) {
options.public = argv.public;
}

if (argv.socket) {
options.socket = argv.socket;
}

if (argv.progress) {
options.progress = argv.progress;
}

if (!options.publicPath) {
// eslint-disable-next-line
options.publicPath =
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';

if (
!/^(https?:)?\/\//.test(options.publicPath) &&
options.publicPath[0] !== '/'
) {
options.publicPath = `/${options.publicPath}`;
}
}

if (!options.filename) {
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
}

if (!options.watchOptions) {
options.watchOptions = firstWpOpt.watchOptions;
}

if (argv.stdin) {
process.stdin.on('end', () => {
// eslint-disable-next-line no-process-exit
process.exit(0);
});

process.stdin.resume();
}

if (!options.hot) {
options.hot = argv.hot;
}

if (!options.hotOnly) {
options.hotOnly = argv['hot-only'];
}

if (!options.clientLogLevel) {
options.clientLogLevel = argv['client-log-level'];
}

// eslint-disable-next-line
if (options.contentBase === undefined) {
if (argv['content-base']) {
options.contentBase = argv['content-base'];

if (Array.isArray(options.contentBase)) {
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)) {
options.contentBase = path.resolve(options.contentBase);
}
// It is possible to disable the contentBase by using
// `--no-content-base`, which results in arg["content-base"] = false
} else if (argv['content-base'] === false) {
options.contentBase = false;
}
}

if (argv['watch-content-base']) {
options.watchContentBase = true;
}

if (!options.stats) {
options.stats = {
cached: false,
cachedAssets: false,
};
}

if (
typeof options.stats === 'object' &&
typeof options.stats.colors === 'undefined'
) {
options.stats = Object.assign({}, options.stats, { colors: argv.color });
}

if (argv.lazy) {
options.lazy = true;
}

if (!argv.info) {
options.noInfo = true;
}

if (argv.quiet) {
options.quiet = true;
}

if (argv.https) {
options.https = true;
}

if (argv['pfx-passphrase']) {
options.pfxPassphrase = argv['pfx-passphrase'];
}

if (argv.inline === false) {
options.inline = false;
}

if (argv['history-api-fallback']) {
options.historyApiFallback = true;
}

if (argv.compress) {
options.compress = true;
}

if (argv['disable-host-check']) {
options.disableHostCheck = true;
}

if (argv['open-page']) {
options.open = true;
options.openPage = argv['open-page'];
}

if (typeof argv.open !== 'undefined') {
options.open = argv.open !== '' ? argv.open : true;
}

if (options.open && !options.openPage) {
options.openPage = '';
}

if (argv.useLocalIp) {
options.useLocalIp = true;
}

// Kind of weird, but ensures prior behavior isn't broken in cases
// that wouldn't throw errors. E.g. both argv.port and options.port
// were specified, but since argv.port is 8080, options.port will be
// tried first instead.
options.port =
argv.port === port
? defaultTo(options.port, argv.port)
: defaultTo(argv.port, options.port);

return options;
}

module.exports = createConfig;