Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
fix(cli): fixes #70. dev/hot flags should be parsed (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape authored and joshwiens committed Mar 31, 2018
1 parent e1a956b commit 88907e1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ $ webpack-serve --help
Options
--config The webpack config to serve. Alias for <config>.
--content The path from which content will be served
--dev An object containing options for webpack-dev-middleware
--dev A JSON object containing options for webpack-dev-middleware
--help Show usage information and the options listed here.
--host The host the app should bind to
--hot A JSON object containing options for webpack-hot-client
--http2 Instruct the server to use HTTP2
--https-cert Specify a cert to enable https. Must be paired with a key
--https-key Specify a key to enable https. Must be paired with a cert
Expand All @@ -54,7 +55,7 @@ $ webpack-serve --help
--log-level Limit all process console messages to a specific level and above
{dim Levels: trace, debug, info, warn, error, silent}
--log-time Instruct the logger for webpack-serve and dependencies to display a timestamp
--no-clipboard Instructs the serve not to copy the server URI to the clipboard when starting
--no-clipboard Instructs the server not to copy the server URI to the clipboard when starting
--no-hot Instruct the client not to apply Hot Module Replacement patches
--no-reload Instruct middleware {italic not} to reload the page for build errors
--open Instruct the app to open in the default browser
Expand Down
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const cli = meow(chalk`
{underline Options}
--config The webpack config to serve. Alias for <config>.
--content The path from which content will be served
--dev An object containing options for webpack-dev-middleware
--dev A JSON object containing options for webpack-dev-middleware
--help Show usage information and the options listed here.
--host The host the app should bind to
--hot A JSON object containing options for webpack-hot-client
--http2 Instruct the server to use HTTP2
--https-cert Specify a cert to enable https. Must be paired with a key
--https-key Specify a key to enable https. Must be paired with a cert
Expand All @@ -43,7 +44,7 @@ const cli = meow(chalk`
--log-level Limit all process console messages to a specific level and above
{dim Levels: trace, debug, info, warn, error, silent}
--log-time Instruct the logger for webpack-serve and dependencies to display a timestamp
--no-hot-client Instruct the serve to completely disable automatic HMR functionality
--no-hot-client Instruct the server to completely disable automatic HMR functionality
--no-hot Instruct the client not to apply Hot Module Replacement patches
--no-reload Instruct middleware {italic not} to reload the page for build errors
--open Instruct the app to open in the default browser
Expand Down
20 changes: 18 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,23 @@ module.exports = (opts) => {

/* istanbul ignore if */
if (options.http2 && nodeVersion < 9) {
throw new Error('webpack-serve: The `http2` option can only be used with Node v9 and higher.');
throw new TypeError('webpack-serve: The `http2` option can only be used with Node v9 and higher.');
}

if (flags.dev && typeof flags.dev === 'string') {
options.dev = parse(flags.dev);

if (typeof options.dev !== 'object') {
throw new TypeError('webpack-serve: The --dev flag must be a string contianing a valid JSON object.');
}
}

if (flags.hot && typeof flags.hot === 'string') {
options.hot = parse(flags.hot);

if (typeof options.hot !== 'object') {
throw new TypeError('webpack-serve: The --hot flag must be a string contianing a valid JSON object.');
}
}

if (flags.hotClient === false) {
Expand All @@ -178,7 +194,7 @@ module.exports = (opts) => {
if (options.hot.host) {
const hotHost = options.hot.host.server || options.hot.host;
if (hotHost !== options.host) {
throw new Error('webpack-serve: The `hot.host` (or `hot.host.server`) property must match the `host` option.');
throw new TypeError('webpack-serve: The `hot.host` (or `hot.host.server`) property must match the `host` option.');
}
} else {
options.hot.host = options.host;
Expand Down
28 changes: 28 additions & 0 deletions test/tests/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ describe('webpack-serve Options', () => {
});
});

t('should accept a dev flag', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
const flags = {
dev: '{"publicPath":"/"}'
};

serve({ config, flags }).then(({ close, on, options }) => {
on('listening', () => {
assert.deepEqual(options.dev, { publicPath: '/' });
close(done);
});
});
});

t('should accept a host option', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
config.serve.host = '0.0.0.0';
Expand All @@ -128,6 +142,20 @@ describe('webpack-serve Options', () => {
});
});

t('should accept a hot flag', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
const flags = {
hot: '{"hot":false}'
};

serve({ config, flags }).then(({ close, on, options }) => {
on('listening', () => {
assert.deepEqual(options.hot.hot, false);
close(done);
});
});
});

t('should not accept a mismatched hot.host option', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
config.serve.host = '0.0.0.0';
Expand Down

0 comments on commit 88907e1

Please sign in to comment.