Skip to content

Commit 4d251b5

Browse files
authored
feat: enable compress by default (#3303)
* feat: enable `compress` by default * test: update snapshot * docs: update
1 parent 88468b6 commit 4d251b5

File tree

5 files changed

+100
-10
lines changed

5 files changed

+100
-10
lines changed

examples/cli/compression/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,38 @@
33
Website gzip compression makes it possible to reduce the file size of a file
44
to roughly 30% of its original size before the files are sent to the browser.
55

6+
It is enabled by default.
7+
8+
## `--compress`
9+
610
To run this example, run this command in your console or terminal:
711

812
```console
913
npm run webpack-dev-server -- --open --compress
1014
```
1115

12-
## What should happen
16+
### What should happen
1317

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

23+
## `--no-compress`
24+
25+
To run this example, run this command in your console or terminal:
26+
27+
```console
28+
npm run webpack-dev-server -- --open --no-compress
29+
```
30+
31+
### What should happen
32+
33+
1. The script should open `http://localhost:8080/`.
34+
2. Files being sent to the browser from the `webpack` bundle should be gzipped.
35+
3. Open the console in your browser's devtools and select the _Network_ tab.
36+
4. Find `bundle.js`. The response headers should not contain `Content-Encoding: gzip`.
37+
1938
## Notes
2039

2140
Some browsers, such as Chrome, won't show the `Content-Encoding: gzip` within

lib/utils/normalizeOptions.js

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ function normalizeOptions(compiler, options) {
123123
if (typeof options.setupExitSignals === 'undefined') {
124124
options.setupExitSignals = true;
125125
}
126+
127+
if (typeof options.compress === 'undefined') {
128+
options.compress = true;
129+
}
126130
}
127131

128132
module.exports = normalizeOptions;

test/server/compress-option.test.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('compress option', () => {
1414
let server;
1515
let req;
1616

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

2525
it('request to bundle file', (done) => {
26-
req
27-
.get('/main.js')
28-
.expect((res) => {
29-
if (res.header['content-encoding']) {
30-
throw new Error('Expected `content-encoding` header is undefined.');
31-
}
32-
})
33-
.expect(200, done);
26+
req.get('/main.js').expect('Content-Encoding', 'gzip').expect(200, done);
3427
});
3528
});
3629

test/server/utils/__snapshots__/normalizeOptions.test.js.snap.webpack4

+37
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Object {
77
"overlay": true,
88
"transport": "/path/to/custom/client/",
99
},
10+
"compress": true,
1011
"devMiddleware": Object {},
1112
"firewall": true,
1213
"hot": true,
@@ -42,6 +43,7 @@ Object {
4243
"overlay": true,
4344
"port": 9000,
4445
},
46+
"compress": true,
4547
"devMiddleware": Object {},
4648
"firewall": true,
4749
"hot": true,
@@ -76,6 +78,7 @@ Object {
7678
"overlay": true,
7779
"path": "/custom/path/",
7880
},
81+
"compress": true,
7982
"devMiddleware": Object {},
8083
"firewall": true,
8184
"hot": true,
@@ -110,6 +113,7 @@ Object {
110113
"overlay": true,
111114
"path": "custom/path",
112115
},
116+
"compress": true,
113117
"devMiddleware": Object {},
114118
"firewall": true,
115119
"hot": true,
@@ -144,6 +148,7 @@ Object {
144148
"overlay": true,
145149
"transport": "sockjs",
146150
},
151+
"compress": true,
147152
"devMiddleware": Object {},
148153
"firewall": true,
149154
"hot": true,
@@ -178,6 +183,7 @@ Object {
178183
"overlay": true,
179184
"transport": "ws",
180185
},
186+
"compress": true,
181187
"devMiddleware": Object {},
182188
"firewall": true,
183189
"hot": true,
@@ -212,6 +218,7 @@ Object {
212218
"overlay": true,
213219
"transport": "ws",
214220
},
221+
"compress": true,
215222
"devMiddleware": Object {},
216223
"firewall": true,
217224
"hot": true,
@@ -245,6 +252,7 @@ Object {
245252
"hotEntry": true,
246253
"overlay": true,
247254
},
255+
"compress": true,
248256
"devMiddleware": Object {
249257
"serverSideRender": true,
250258
},
@@ -280,6 +288,7 @@ Object {
280288
"hotEntry": true,
281289
"overlay": true,
282290
},
291+
"compress": true,
283292
"devMiddleware": Object {},
284293
"firewall": false,
285294
"hot": true,
@@ -313,6 +322,7 @@ Object {
313322
"hotEntry": false,
314323
"overlay": true,
315324
},
325+
"compress": true,
316326
"devMiddleware": Object {},
317327
"firewall": true,
318328
"hot": false,
@@ -346,6 +356,7 @@ Object {
346356
"hotEntry": "only",
347357
"overlay": true,
348358
},
359+
"compress": true,
349360
"devMiddleware": Object {},
350361
"firewall": true,
351362
"hot": "only",
@@ -379,6 +390,7 @@ Object {
379390
"hotEntry": true,
380391
"overlay": true,
381392
},
393+
"compress": true,
382394
"devMiddleware": Object {},
383395
"firewall": true,
384396
"hot": true,
@@ -412,6 +424,7 @@ Object {
412424
"hotEntry": true,
413425
"overlay": true,
414426
},
427+
"compress": true,
415428
"devMiddleware": Object {},
416429
"firewall": true,
417430
"hot": true,
@@ -445,6 +458,7 @@ Object {
445458
"hotEntry": true,
446459
"overlay": true,
447460
},
461+
"compress": true,
448462
"devMiddleware": Object {},
449463
"firewall": true,
450464
"hot": true,
@@ -478,6 +492,7 @@ Object {
478492
"hotEntry": true,
479493
"overlay": true,
480494
},
495+
"compress": true,
481496
"devMiddleware": Object {},
482497
"firewall": true,
483498
"hot": true,
@@ -513,6 +528,7 @@ Object {
513528
"hotEntry": true,
514529
"overlay": true,
515530
},
531+
"compress": true,
516532
"devMiddleware": Object {},
517533
"firewall": true,
518534
"hot": true,
@@ -546,6 +562,7 @@ Object {
546562
"hotEntry": true,
547563
"overlay": true,
548564
},
565+
"compress": true,
549566
"devMiddleware": Object {},
550567
"firewall": true,
551568
"hot": true,
@@ -581,6 +598,7 @@ Object {
581598
"hotEntry": true,
582599
"overlay": true,
583600
},
601+
"compress": true,
584602
"devMiddleware": Object {},
585603
"firewall": true,
586604
"hot": true,
@@ -616,6 +634,7 @@ Object {
616634
"hotEntry": true,
617635
"overlay": true,
618636
},
637+
"compress": true,
619638
"devMiddleware": Object {},
620639
"firewall": true,
621640
"hot": true,
@@ -651,6 +670,7 @@ Object {
651670
"hotEntry": true,
652671
"overlay": true,
653672
},
673+
"compress": true,
654674
"devMiddleware": Object {},
655675
"firewall": true,
656676
"hot": true,
@@ -684,6 +704,7 @@ Object {
684704
"hotEntry": true,
685705
"overlay": true,
686706
},
707+
"compress": true,
687708
"devMiddleware": Object {},
688709
"firewall": true,
689710
"hot": true,
@@ -728,6 +749,7 @@ Object {
728749
"hotEntry": true,
729750
"overlay": true,
730751
},
752+
"compress": true,
731753
"devMiddleware": Object {},
732754
"firewall": true,
733755
"hot": true,
@@ -772,6 +794,7 @@ Object {
772794
"hotEntry": true,
773795
"overlay": true,
774796
},
797+
"compress": true,
775798
"devMiddleware": Object {},
776799
"firewall": true,
777800
"hot": true,
@@ -816,6 +839,7 @@ Object {
816839
"hotEntry": true,
817840
"overlay": true,
818841
},
842+
"compress": true,
819843
"devMiddleware": Object {},
820844
"firewall": true,
821845
"hot": true,
@@ -849,6 +873,7 @@ Object {
849873
"hotEntry": true,
850874
"overlay": true,
851875
},
876+
"compress": true,
852877
"devMiddleware": Object {},
853878
"firewall": true,
854879
"hot": true,
@@ -870,6 +895,7 @@ Object {
870895
"hotEntry": true,
871896
"overlay": true,
872897
},
898+
"compress": true,
873899
"devMiddleware": Object {},
874900
"firewall": true,
875901
"hot": true,
@@ -903,6 +929,7 @@ Object {
903929
"hotEntry": true,
904930
"overlay": true,
905931
},
932+
"compress": true,
906933
"devMiddleware": Object {},
907934
"firewall": true,
908935
"hot": true,
@@ -936,6 +963,7 @@ Object {
936963
"hotEntry": true,
937964
"overlay": true,
938965
},
966+
"compress": true,
939967
"devMiddleware": Object {},
940968
"firewall": true,
941969
"hot": true,
@@ -969,6 +997,7 @@ Object {
969997
"hotEntry": true,
970998
"overlay": true,
971999
},
1000+
"compress": true,
9721001
"devMiddleware": Object {},
9731002
"firewall": true,
9741003
"hot": true,
@@ -1003,6 +1032,7 @@ Object {
10031032
"hotEntry": true,
10041033
"overlay": true,
10051034
},
1035+
"compress": true,
10061036
"devMiddleware": Object {},
10071037
"firewall": true,
10081038
"hot": true,
@@ -1036,6 +1066,7 @@ Object {
10361066
"hotEntry": true,
10371067
"overlay": true,
10381068
},
1069+
"compress": true,
10391070
"devMiddleware": Object {},
10401071
"firewall": true,
10411072
"hot": true,
@@ -1067,6 +1098,7 @@ Object {
10671098
"hotEntry": true,
10681099
"overlay": true,
10691100
},
1101+
"compress": true,
10701102
"devMiddleware": Object {},
10711103
"firewall": true,
10721104
"hot": true,
@@ -1100,6 +1132,7 @@ Object {
11001132
"hotEntry": true,
11011133
"overlay": true,
11021134
},
1135+
"compress": true,
11031136
"devMiddleware": Object {},
11041137
"firewall": true,
11051138
"hot": true,
@@ -1135,6 +1168,7 @@ Object {
11351168
"hotEntry": true,
11361169
"overlay": true,
11371170
},
1171+
"compress": true,
11381172
"devMiddleware": Object {},
11391173
"firewall": true,
11401174
"hot": true,
@@ -1168,6 +1202,7 @@ Object {
11681202
"hotEntry": true,
11691203
"overlay": true,
11701204
},
1205+
"compress": true,
11711206
"devMiddleware": Object {},
11721207
"firewall": true,
11731208
"hot": true,
@@ -1201,6 +1236,7 @@ Object {
12011236
"hotEntry": true,
12021237
"overlay": true,
12031238
},
1239+
"compress": true,
12041240
"devMiddleware": Object {},
12051241
"firewall": true,
12061242
"hot": true,
@@ -1234,6 +1270,7 @@ Object {
12341270
"hotEntry": true,
12351271
"overlay": true,
12361272
},
1273+
"compress": true,
12371274
"devMiddleware": Object {},
12381275
"firewall": true,
12391276
"hot": true,

0 commit comments

Comments
 (0)