Skip to content

Commit

Permalink
refactor: CLI args and more tests (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Feb 22, 2019
1 parent 66129a8 commit a2e5d12
Show file tree
Hide file tree
Showing 9 changed files with 1,853 additions and 163 deletions.
1 change: 1 addition & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"testURL": "http://localhost/",
"collectCoverage": true,
"coveragePathIgnorePatterns": ["test"],
"moduleFileExtensions": ["js", "json"],
"testMatch": ["**/test/**/*.test.js"],
"setupFilesAfterEnv": ["<rootDir>/setupTest.js"]
Expand Down
69 changes: 38 additions & 31 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function createConfig(config, argv, { port }) {
options.bonjour = true;
}

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

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

if (argv.public) {
Expand Down Expand Up @@ -47,11 +47,11 @@ function createConfig(config, argv, { port }) {
}
}

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

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

Expand All @@ -64,38 +64,42 @@ function createConfig(config, argv, { port }) {
process.stdin.resume();
}

// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
// We should prefer CLI arg under config, now we always prefer `hot` from `devServer`
if (!options.hot) {
options.hot = argv.hot;
}

// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
// We should prefer CLI arg under config, now we always prefer `hotOnly` from `devServer`
if (!options.hotOnly) {
options.hotOnly = argv['hot-only'];
options.hotOnly = argv.hotOnly;
}

if (!options.clientLogLevel) {
options.clientLogLevel = argv['client-log-level'];
// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
// We should prefer CLI arg under config, now we always prefer `clientLogLevel` from `devServer`
if (!options.clientLogLevel && argv.clientLogLevel) {
options.clientLogLevel = argv.clientLogLevel;
}

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

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 (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.contentBase === false) {
// TODO doesn't work need fix, `false` in this case is string
options.contentBase = false;
}

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

Expand All @@ -108,7 +112,8 @@ function createConfig(config, argv, { port }) {

if (
typeof options.stats === 'object' &&
typeof options.stats.colors === 'undefined'
typeof options.stats.colors === 'undefined' &&
argv.color
) {
options.stats = Object.assign({}, options.stats, { colors: argv.color });
}
Expand All @@ -117,10 +122,12 @@ function createConfig(config, argv, { port }) {
options.lazy = true;
}

// TODO remove in `v4`
if (!argv.info) {
options.noInfo = true;
}

// TODO remove in `v4`
if (argv.quiet) {
options.quiet = true;
}
Expand All @@ -129,29 +136,29 @@ function createConfig(config, argv, { port }) {
options.https = true;
}

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

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

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

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

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

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

if (typeof argv.open !== 'undefined') {
Expand Down
Loading

0 comments on commit a2e5d12

Please sign in to comment.