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

Fix prefer hot from cli #2617

Merged
merged 1 commit into from
May 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const options = {
describe: 'Print compilation progress in percentage',
group: BASIC_GROUP,
},
hot: {
type: 'boolean',
describe: 'Enables/disables HMR',
group: ADVANCED_GROUP,
},
'hot-only': {
type: 'boolean',
describe: 'Do not refresh page if HMR fails',
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ 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;
const hasHot = typeof argv.hot !== 'undefined';
const hasHotOnly = typeof argv.hotOnly !== 'undefined';

if (hasHot || hasHotOnly) {
options.hot = hasHotOnly ? 'only' : argv.hot;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argv-parser doesn't support multiple types for argument so for compatibility we have --hot and --hot-only, anyway we should deprecate our CLI in v4 in favor webpack server (webpack-cli), so we can do it safely

}

// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
Expand Down
30 changes: 30 additions & 0 deletions test/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ const execa = require('execa');
const testBin = require('../helpers/test-bin');

describe('CLI', () => {
it('--hot', (done) => {
testBin('--hot')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stderr).toContain('/hot/dev-server');
done();
})
.catch(done);
});

it('--no-hot', (done) => {
testBin('--no-hot')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stderr).not.toContain('/hot/dev-server');
done();
})
.catch(done);
});

it('--hot-only', (done) => {
testBin('--hot-only')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stderr).toContain('/hot/only-dev-server');
done();
})
.catch(done);
});

it('--progress', (done) => {
testBin('--progress')
.then((output) => {
Expand Down
56 changes: 52 additions & 4 deletions test/server/utils/__snapshots__/createConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,55 @@ Object {
}
`;

exports[`createConfig hot option (in devServer config) 1`] = `
exports[`createConfig hot only option (in devServer config) 1`] = `
Object {
"hot": "only",
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig hot only option 1`] = `
Object {
"hot": "only",
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig hot option (false) (in devServer config) 1`] = `
Object {
"hot": false,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig hot option (false) 1`] = `
Object {
"hot": false,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig hot option (true) (in devServer config) 1`] = `
Object {
"hot": true,
"port": 8080,
Expand All @@ -305,7 +353,7 @@ Object {
}
`;

exports[`createConfig hot option 1`] = `
exports[`createConfig hot option (true) 1`] = `
Object {
"hot": true,
"port": 8080,
Expand Down Expand Up @@ -875,7 +923,7 @@ Object {
"disableHostCheck": "_disableHostCheck",
"historyApiFallback": "_historyApiFallback",
"host": "_foo",
"hot": "_hot",
"hot": true,
"https": "_https",
"open": "_open",
"openPage": "_openPage",
Expand All @@ -901,7 +949,7 @@ Object {
"disableHostCheck": "_disableHostCheck",
"historyApiFallback": "_historyApiFallback",
"host": "_foo",
"hot": "_hot",
"hot": true,
"https": "_https",
"open": "_open",
"openPage": "_openPage",
Expand Down
50 changes: 48 additions & 2 deletions test/server/utils/createConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('hot option', () => {
it('hot option (true)', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, { hot: true }),
Expand All @@ -308,7 +308,17 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('hot option (in devServer config)', () => {
it('hot option (false)', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, { hot: false }),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('hot option (true) (in devServer config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { hot: true },
Expand All @@ -320,6 +330,42 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('hot option (false) (in devServer config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { hot: false },
}),
// eslint-disable-next-line no-undefined
Object.assign({}, argv, { hot: undefined }),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('hot only option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, { hotOnly: true }),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('hot only option (in devServer config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { hot: 'only' },
}),
// eslint-disable-next-line no-undefined
Object.assign({}, argv, { hot: undefined }),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('clientLogLevel option', () => {
const config = createConfig(
webpackConfig,
Expand Down