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

feat: enable compress by default #3303

Merged
merged 3 commits into from
May 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion examples/cli/compression/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@
Website gzip compression makes it possible to reduce the file size of a file
to roughly 30% of its original size before the files are sent to the browser.

It is enabled by default.

## `--compress`

To run this example, run this command in your console or terminal:

```console
npm run webpack-dev-server -- --open --compress
```

## What should happen
### What should happen

1. The script should open `http://localhost:8080/`.
2. Files being sent to the browser from the `webpack` bundle should be gzipped.
3. Open the console in your browser's devtools and select the _Network_ tab.
4. Find `bundle.js`. The response headers should contain `Content-Encoding: gzip`.

## `--no-compress`

To run this example, run this command in your console or terminal:

```console
npm run webpack-dev-server -- --open --no-compress
Copy link
Member

Choose a reason for hiding this comment

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

we run webpack serve right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually it is script

"webpack-dev-server": "node examples/run-example.js",

Copy link
Member

Choose a reason for hiding this comment

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

looks good then

Copy link
Member

Choose a reason for hiding this comment

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

Let's change it to npx webpack serve in future PRs

Copy link
Member

Choose a reason for hiding this comment

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

will take care

```

### What should happen

1. The script should open `http://localhost:8080/`.
2. Files being sent to the browser from the `webpack` bundle should be gzipped.
3. Open the console in your browser's devtools and select the _Network_ tab.
4. Find `bundle.js`. The response headers should not contain `Content-Encoding: gzip`.

## Notes

Some browsers, such as Chrome, won't show the `Content-Encoding: gzip` within
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ function normalizeOptions(compiler, options) {
if (typeof options.setupExitSignals === 'undefined') {
options.setupExitSignals = true;
}

if (typeof options.compress === 'undefined') {
options.compress = true;
}
}

module.exports = normalizeOptions;
11 changes: 2 additions & 9 deletions test/server/compress-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('compress option', () => {
let server;
let req;

describe('not specify', () => {
describe('enabled by default when not specified', () => {
beforeAll((done) => {
server = testServer.start(config, { port }, done);
req = request(server.app);
Expand All @@ -23,14 +23,7 @@ describe('compress option', () => {
afterAll(testServer.close);

it('request to bundle file', (done) => {
req
.get('/main.js')
.expect((res) => {
if (res.header['content-encoding']) {
throw new Error('Expected `content-encoding` header is undefined.');
}
})
.expect(200, done);
req.get('/main.js').expect('Content-Encoding', 'gzip').expect(200, done);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"overlay": true,
"transport": "/path/to/custom/client/",
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -42,6 +43,7 @@ Object {
"overlay": true,
"port": 9000,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -76,6 +78,7 @@ Object {
"overlay": true,
"path": "/custom/path/",
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -110,6 +113,7 @@ Object {
"overlay": true,
"path": "custom/path",
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -144,6 +148,7 @@ Object {
"overlay": true,
"transport": "sockjs",
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -178,6 +183,7 @@ Object {
"overlay": true,
"transport": "ws",
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -212,6 +218,7 @@ Object {
"overlay": true,
"transport": "ws",
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -245,6 +252,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {
"serverSideRender": true,
},
Expand Down Expand Up @@ -280,6 +288,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": false,
"hot": true,
Expand Down Expand Up @@ -313,6 +322,7 @@ Object {
"hotEntry": false,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": false,
Expand Down Expand Up @@ -346,6 +356,7 @@ Object {
"hotEntry": "only",
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": "only",
Expand Down Expand Up @@ -379,6 +390,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -412,6 +424,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -445,6 +458,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -478,6 +492,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -513,6 +528,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -546,6 +562,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -581,6 +598,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -616,6 +634,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -651,6 +670,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -684,6 +704,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -728,6 +749,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -772,6 +794,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -816,6 +839,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -849,6 +873,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand All @@ -870,6 +895,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -903,6 +929,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -936,6 +963,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -969,6 +997,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1003,6 +1032,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1036,6 +1066,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1067,6 +1098,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1100,6 +1132,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1135,6 +1168,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1168,6 +1202,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1201,6 +1236,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down Expand Up @@ -1234,6 +1270,7 @@ Object {
"hotEntry": true,
"overlay": true,
},
"compress": true,
"devMiddleware": Object {},
"firewall": true,
"hot": true,
Expand Down