Skip to content

Commit d280f82

Browse files
committed
Add --compress option to enable gzip compression
The --compress option uses the `compression` express middleware to gzip compress responses from the dev server.
1 parent 30d0cc9 commit d280f82

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

bin/webpack-dev-server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var optimist = require("optimist")
4242

4343
.boolean("history-api-fallback").describe("history-api-fallback", "Fallback to /index.html for Single Page Applications.")
4444

45+
.boolean("compress").describe("compress", "enable gzip compression")
46+
4547
.describe("port", "The port").default("port", 8080)
4648

4749
.describe("host", "The hostname/ip address the server will bind to").default("host", "localhost");
@@ -132,6 +134,9 @@ if(argv["inline"])
132134
if(argv["history-api-fallback"])
133135
options.historyApiFallback = true;
134136

137+
if(argv["compress"])
138+
options.compress = true;
139+
135140
var protocol = options.https ? "https" : "http";
136141

137142
if(options.inline) {

lib/Server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var fs = require("fs");
22
var path = require("path");
33
var webpackDevMiddleware = require("webpack-dev-middleware");
44
var express = require("express");
5+
var compress = require("compression");
56
var socketio = require("socket.io");
67
var StreamCache = require("stream-cache");
78
var http = require("http");
@@ -110,6 +111,13 @@ function Server(compiler, options) {
110111
}.bind(this));
111112

112113
var features = {
114+
compress: function() {
115+
if (options.compress) {
116+
// Enable gzip compression.
117+
app.use(compress());
118+
}
119+
}.bind(this),
120+
113121
proxy: function() {
114122
if (options.proxy) {
115123
if (!Array.isArray(options.proxy)) {
@@ -213,6 +221,9 @@ function Server(compiler, options) {
213221
defaultFeatures.push("magicHtml");
214222
if(options.contentBase !== false)
215223
defaultFeatures.push("contentBase");
224+
// compress is placed last and uses unshift so that it will be the first middleware used
225+
if(options.compress)
226+
defaultFeatures.unshift("compress");
216227

217228
(options.features || defaultFeatures).forEach(function(feature) {
218229
features[feature]();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"webpack": "^1.3.0"
88
},
99
"dependencies": {
10+
"compression": "^1.5.2",
1011
"connect-history-api-fallback": "1.1.0",
1112
"express": "^4.13.3",
1213
"http-proxy": "^1.11.2",

0 commit comments

Comments
 (0)