Skip to content

feat: add client.hotEntry and --client-hot-entry #3294

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

Merged
merged 8 commits into from
May 15, 2021
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Options:
--no-http2 Do not use HTTP/2.
--bonjour Broadcasts the server via ZeroConf networking on start.
--no-bonjour Do not broadcast the server via ZeroConf networking on start.
--client-hot-entry Tell devServer to inject a Hot Module Replacement entry.
--no-client-hot-entry Do not tell devServer to inject a Hot Module Replacement entry.
--client-progress Print compilation progress in percentage in the browser.
--no-client-progress Do not print compilation progress in percentage in the browser.
--client-overlay Show a full-screen overlay in the browser when there are compiler errors or warnings.
Expand Down
18 changes: 18 additions & 0 deletions bin/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ module.exports = {
'Do not broadcast the server via ZeroConf networking on start.',
negative: true,
},
{
name: 'client-hot-entry',
type: Boolean,
configs: [
{
type: 'boolean',
},
],
description: 'Tell devServer to inject a Hot Module Replacement entry.',
negatedDescription:
'Do not tell devServer to inject a Hot Module Replacement entry.',
negative: true,
processor(opts) {
opts.client = opts.client || {};
opts.client.hotEntry = opts.clientHotEntry;
delete opts.clientHotEntry;
},
},
{
name: 'client-progress',
type: Boolean,
Expand Down
2 changes: 1 addition & 1 deletion lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
],
"description": "Tells devServer to inject a client entry."
},
"needHotEntry": {
"hotEntry": {
"anyOf": [
{
"type": "boolean"
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/DevServerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class DevServerPlugin {
if (
hotEntry &&
checkInject(
options.client ? options.client.needHotEntry : null,
options.client ? options.client.hotEntry : null,
compilerOptions,
true
)
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ function normalizeOptions(compiler, options) {
options.client.overlay = true;
}

// client.hotEntry
if (typeof options.client.hotEntry === 'undefined') {
options.client.hotEntry = options.hot;
}

options.devMiddleware = options.devMiddleware || {};

if (typeof options.firewall === 'undefined') {
Expand Down
24 changes: 12 additions & 12 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ exports[`options validate should throw an error on the "client" option with '{"h
-> Tells clients connected to devServer to use the provided host."
`;

exports[`options validate should throw an error on the "client" option with '{"hotEntry":[""]}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.hotEntry should be one of these:
boolean | function
-> Tells devServer to inject a Hot Module Replacement entry.
Details:
* configuration.client.hotEntry should be a boolean.
* configuration.client.hotEntry should be an instance of function."
`;

exports[`options validate should throw an error on the "client" option with '{"logging":"silent"}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.logging should be one of these:
Expand All @@ -40,16 +50,6 @@ exports[`options validate should throw an error on the "client" option with '{"n
* configuration.client.needClientEntry should be an instance of function."
`;

exports[`options validate should throw an error on the "client" option with '{"needHotEntry":[""]}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.needHotEntry should be one of these:
boolean | function
-> Tells devServer to inject a Hot Module Replacement entry.
Details:
* configuration.client.needHotEntry should be a boolean.
* configuration.client.needHotEntry should be an instance of function."
`;

exports[`options validate should throw an error on the "client" option with '{"overlay":""}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.overlay should be one of these:
Expand Down Expand Up @@ -110,14 +110,14 @@ exports[`options validate should throw an error on the "client" option with '{"t
exports[`options validate should throw an error on the "client" option with '{"unknownOption":true}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client has an unknown property 'unknownOption'. These properties are valid:
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, needHotEntry? }
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, hotEntry? }
-> Specifies client properties. https://webpack.js.org/configuration/dev-server/#devserverclient"
`;

exports[`options validate should throw an error on the "client" option with 'whoops!' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client should be an object:
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, needHotEntry? }
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, hotEntry? }
-> Specifies client properties. https://webpack.js.org/configuration/dev-server/#devserverclient"
`;

Expand Down
24 changes: 12 additions & 12 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ exports[`options validate should throw an error on the "client" option with '{"h
-> Tells clients connected to devServer to use the provided host."
`;

exports[`options validate should throw an error on the "client" option with '{"hotEntry":[""]}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.hotEntry should be one of these:
boolean | function
-> Tells devServer to inject a Hot Module Replacement entry.
Details:
* configuration.client.hotEntry should be a boolean.
* configuration.client.hotEntry should be an instance of function."
`;

exports[`options validate should throw an error on the "client" option with '{"logging":"silent"}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.logging should be one of these:
Expand All @@ -40,16 +50,6 @@ exports[`options validate should throw an error on the "client" option with '{"n
* configuration.client.needClientEntry should be an instance of function."
`;

exports[`options validate should throw an error on the "client" option with '{"needHotEntry":[""]}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.needHotEntry should be one of these:
boolean | function
-> Tells devServer to inject a Hot Module Replacement entry.
Details:
* configuration.client.needHotEntry should be a boolean.
* configuration.client.needHotEntry should be an instance of function."
`;

exports[`options validate should throw an error on the "client" option with '{"overlay":""}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client.overlay should be one of these:
Expand Down Expand Up @@ -110,14 +110,14 @@ exports[`options validate should throw an error on the "client" option with '{"t
exports[`options validate should throw an error on the "client" option with '{"unknownOption":true}' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client has an unknown property 'unknownOption'. These properties are valid:
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, needHotEntry? }
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, hotEntry? }
-> Specifies client properties. https://webpack.js.org/configuration/dev-server/#devserverclient"
`;

exports[`options validate should throw an error on the "client" option with 'whoops!' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.client should be an object:
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, needHotEntry? }
object { transport?, host?, path?, port?, logging?, progress?, overlay?, needClientEntry?, hotEntry? }
-> Specifies client properties. https://webpack.js.org/configuration/dev-server/#devserverclient"
`;

Expand Down
4 changes: 4 additions & 0 deletions test/cli/__snapshots__/cli.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ Options:
networking on start.
--no-bonjour Do not broadcast the server via ZeroConf
networking on start.
--client-hot-entry Tell devServer to inject a Hot Module
Replacement entry.
--no-client-hot-entry Do not tell devServer to inject a Hot Module
Replacement entry.
--client-progress Print compilation progress in percentage in
the browser.
--no-client-progress Do not print compilation progress in
Expand Down
4 changes: 4 additions & 0 deletions test/cli/__snapshots__/cli.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ Options:
networking on start.
--no-bonjour Do not broadcast the server via ZeroConf
networking on start.
--client-hot-entry Tell devServer to inject a Hot Module
Replacement entry.
--no-client-hot-entry Do not tell devServer to inject a Hot Module
Replacement entry.
--client-progress Print compilation progress in percentage in
the browser.
--no-client-progress Do not print compilation progress in
Expand Down
44 changes: 44 additions & 0 deletions test/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,50 @@ describe('CLI', () => {
.catch(done);
});

it('--client-hot-entry', (done) => {
testBin('--client-hot-entry --stats=detailed')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stdout).toContain('webpack/hot/dev-server.js');

done();
})
.catch(done);
});

it('--no-client-hot-entry', (done) => {
testBin('--no-client-hot-entry --stats=detailed')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stdout).not.toContain('webpack/hot/dev-server.js');

done();
})
.catch(done);
});

it('should not inject HMR entry "--client-hot-entry" and "--no-hot"', (done) => {
testBin('--client-hot-entry --no-hot --stats=detailed')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stdout).not.toContain('webpack/hot/dev-server.js');

done();
})
.catch(done);
});

it('should not inject HMR entry with "--no-client-hot-entry" and "--hot"', (done) => {
testBin('--no-client-hot-entry --hot --stats=detailed')
.then((output) => {
expect(output.exitCode).toEqual(0);
expect(output.stdout).not.toContain('webpack/hot/dev-server.js');

done();
})
.catch(done);
});

it('--http2', (done) => {
testBin('--http2')
.then((output) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('client option', () => {
config,
{
client: {
needHotEntry: false,
hotEntry: false,
},
port,
},
Expand Down
Loading