Skip to content

Commit

Permalink
feat: enable compress by default (#3303)
Browse files Browse the repository at this point in the history
* feat: enable `compress` by default

* test: update snapshot

* docs: update
  • Loading branch information
snitin315 committed May 17, 2021
1 parent 88468b6 commit 4d251b5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
21 changes: 20 additions & 1 deletion examples/cli/compression/README.md
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
```

### 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
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
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
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

0 comments on commit 4d251b5

Please sign in to comment.