Skip to content

Commit 85de417

Browse files
ConnormihaSpaceK33z
authored andcommitted
Use arrow function if it possible and get rid of .bind in server part (#835)
1 parent 234294a commit 85de417

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

lib/Server.js

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,43 @@ function Server(compiler, options) {
3939
this.contentBaseWatchers = [];
4040

4141
// Listening for events
42-
const invalidPlugin = function() {
42+
const invalidPlugin = () => {
4343
this.sockWrite(this.sockets, "invalid");
44-
}.bind(this);
44+
};
4545
compiler.plugin("compile", invalidPlugin);
4646
compiler.plugin("invalid", invalidPlugin);
47-
compiler.plugin("done", function(stats) {
47+
compiler.plugin("done", (stats) => {
4848
this._sendStats(this.sockets, stats.toJson(clientStats));
4949
this._stats = stats;
50-
}.bind(this));
50+
});
5151

5252
// Init express server
5353
const app = this.app = new express();
5454

5555
// middleware for serving webpack bundle
5656
this.middleware = webpackDevMiddleware(compiler, options);
5757

58-
app.get("/__webpack_dev_server__/live.bundle.js", function(req, res) {
58+
app.get("/__webpack_dev_server__/live.bundle.js", (req, res) => {
5959
res.setHeader("Content-Type", "application/javascript");
6060
fs.createReadStream(path.join(__dirname, "..", "client", "live.bundle.js")).pipe(res);
6161
});
6262

63-
app.get("/__webpack_dev_server__/sockjs.bundle.js", function(req, res) {
63+
app.get("/__webpack_dev_server__/sockjs.bundle.js", (req, res) => {
6464
res.setHeader("Content-Type", "application/javascript");
6565
fs.createReadStream(path.join(__dirname, "..", "client", "sockjs.bundle.js")).pipe(res);
6666
});
6767

68-
app.get("/webpack-dev-server.js", function(req, res) {
68+
app.get("/webpack-dev-server.js", (req, res) => {
6969
res.setHeader("Content-Type", "application/javascript");
7070
fs.createReadStream(path.join(__dirname, "..", "client", "index.bundle.js")).pipe(res);
7171
});
7272

73-
app.get("/webpack-dev-server/*", function(req, res) {
73+
app.get("/webpack-dev-server/*", (req, res) => {
7474
res.setHeader("Content-Type", "text/html");
7575
fs.createReadStream(path.join(__dirname, "..", "client", "live.html")).pipe(res);
7676
});
7777

78-
app.get("/webpack-dev-server", function(req, res) {
78+
app.get("/webpack-dev-server", (req, res) => {
7979
res.setHeader("Content-Type", "text/html");
8080
/* eslint-disable quotes */
8181
res.write('<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>');
@@ -118,7 +118,7 @@ function Server(compiler, options) {
118118
/* eslint-enable quotes */
119119
writeDirectory(options.publicPath || "/", path);
120120
res.end("</body></html>");
121-
}.bind(this));
121+
});
122122

123123
let contentBase;
124124
if(options.contentBase !== undefined) {
@@ -128,14 +128,14 @@ function Server(compiler, options) {
128128
}
129129

130130
const features = {
131-
compress: function() {
131+
compress() {
132132
if(options.compress) {
133133
// Enable gzip compression.
134134
app.use(compress());
135135
}
136136
},
137137

138-
proxy: function() {
138+
proxy() {
139139
if(options.proxy) {
140140
/**
141141
* Assume a proxy configuration specified as:
@@ -148,7 +148,7 @@ function Server(compiler, options) {
148148
* }
149149
*/
150150
if(!Array.isArray(options.proxy)) {
151-
options.proxy = Object.keys(options.proxy).map(function(context) {
151+
options.proxy = Object.keys(options.proxy).map((context) => {
152152
let proxyOptions;
153153
// For backwards compatibility reasons.
154154
const correctedContext = context.replace(/^\*$/, "**").replace(/\/\*$/, "");
@@ -168,7 +168,7 @@ function Server(compiler, options) {
168168
});
169169
}
170170

171-
const getProxyMiddleware = function(proxyConfig) {
171+
const getProxyMiddleware = (proxyConfig) => {
172172
const context = proxyConfig.context || proxyConfig.path;
173173

174174
// It is possible to use the `bypass` method without a `target`.
@@ -194,7 +194,7 @@ function Server(compiler, options) {
194194
* }
195195
* ]
196196
*/
197-
options.proxy.forEach(function(proxyConfigOrCallback) {
197+
options.proxy.forEach((proxyConfigOrCallback) => {
198198
let proxyConfig;
199199
let proxyMiddleware;
200200

@@ -206,7 +206,7 @@ function Server(compiler, options) {
206206

207207
proxyMiddleware = getProxyMiddleware(proxyConfig);
208208

209-
app.use(function(req, res, next) {
209+
app.use((req, res, next) => {
210210
if(typeof proxyConfigOrCallback === "function") {
211211
const newProxyConfig = proxyConfigOrCallback();
212212
if(newProxyConfig !== proxyConfig) {
@@ -230,7 +230,7 @@ function Server(compiler, options) {
230230
}
231231
},
232232

233-
historyApiFallback: function() {
233+
historyApiFallback() {
234234
if(options.historyApiFallback) {
235235
// Fall back to /index.html if nothing else matches.
236236
app.use(
@@ -239,16 +239,16 @@ function Server(compiler, options) {
239239
}
240240
},
241241

242-
contentBaseFiles: function() {
242+
contentBaseFiles() {
243243
if(Array.isArray(contentBase)) {
244-
contentBase.forEach(function(item) {
244+
contentBase.forEach((item) => {
245245
app.get("*", express.static(item));
246246
});
247247
} else if(/^(https?:)?\/\//.test(contentBase)) {
248248
console.log("Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.");
249249
console.log('proxy: {\n\t"*": "<your current contentBase configuration>"\n}'); // eslint-disable-line quotes
250250
// Redirect every request to contentBase
251-
app.get("*", function(req, res) {
251+
app.get("*", (req, res) => {
252252
res.writeHead(302, {
253253
"Location": contentBase + req.path + (req._parsedUrl.search || "")
254254
});
@@ -258,7 +258,7 @@ function Server(compiler, options) {
258258
console.log("Using a number as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.");
259259
console.log('proxy: {\n\t"*": "//localhost:<your current contentBase configuration>"\n}'); // eslint-disable-line quotes
260260
// Redirect every request to the port contentBase
261-
app.get("*", function(req, res) {
261+
app.get("*", (req, res) => {
262262
res.writeHead(302, {
263263
"Location": `//localhost:${contentBase}${req.path}${req._parsedUrl.search || ""}`
264264
});
@@ -270,45 +270,45 @@ function Server(compiler, options) {
270270
}
271271
},
272272

273-
contentBaseIndex: function() {
273+
contentBaseIndex() {
274274
if(Array.isArray(contentBase)) {
275-
contentBase.forEach(function(item) {
275+
contentBase.forEach((item) => {
276276
app.get("*", serveIndex(item));
277277
});
278278
} else if(!/^(https?:)?\/\//.test(contentBase) && typeof contentBase !== "number") {
279279
app.get("*", serveIndex(contentBase));
280280
}
281281
},
282282

283-
watchContentBase: function() {
283+
watchContentBase: () => {
284284
if(/^(https?:)?\/\//.test(contentBase) || typeof contentBase === "number") {
285285
throw new Error("Watching remote files is not supported.");
286286
} else if(Array.isArray(contentBase)) {
287-
contentBase.forEach(function(item) {
287+
contentBase.forEach((item) => {
288288
this._watch(item);
289-
}.bind(this));
289+
});
290290
} else {
291291
this._watch(contentBase);
292292
}
293-
}.bind(this),
293+
},
294294

295-
middleware: function() {
295+
middleware: () => {
296296
// include our middleware to ensure it is able to handle '/index.html' request after redirect
297297
app.use(this.middleware);
298-
}.bind(this),
298+
},
299299

300-
headers: function() {
300+
headers: () => {
301301
app.all("*", this.setContentHeaders.bind(this));
302-
}.bind(this),
302+
},
303303

304-
magicHtml: function() {
304+
magicHtml: () => {
305305
app.get("*", this.serveMagicHtml.bind(this));
306-
}.bind(this),
306+
},
307307

308-
setup: function() {
308+
setup: () => {
309309
if(typeof options.setup === "function")
310310
options.setup(app, this);
311-
}.bind(this)
311+
}
312312
};
313313

314314
const defaultFeatures = ["setup", "headers", "middleware"];
@@ -330,9 +330,9 @@ function Server(compiler, options) {
330330
if(options.compress)
331331
defaultFeatures.unshift("compress");
332332

333-
(options.features || defaultFeatures).forEach(function(feature) {
333+
(options.features || defaultFeatures).forEach((feature) => {
334334
features[feature]();
335-
}, this);
335+
});
336336

337337
if(options.https) {
338338
// for keep supporting CLI parameters
@@ -390,16 +390,16 @@ Server.prototype.listen = function() {
390390
}
391391
}
392392
});
393-
sockServer.on("connection", function(conn) {
393+
sockServer.on("connection", (conn) => {
394394
if(!conn) return;
395395
this.sockets.push(conn);
396396

397-
conn.on("close", function() {
397+
conn.on("close", () => {
398398
const connIndex = this.sockets.indexOf(conn);
399399
if(connIndex >= 0) {
400400
this.sockets.splice(connIndex, 1);
401401
}
402-
}.bind(this));
402+
});
403403

404404
if(this.clientLogLevel)
405405
this.sockWrite([conn], "log-level", this.clientLogLevel);
@@ -411,7 +411,7 @@ Server.prototype.listen = function() {
411411

412412
if(!this._stats) return;
413413
this._sendStats([conn], this._stats.toJson(clientStats), true);
414-
}.bind(this));
414+
});
415415

416416
sockServer.installHandlers(this.listeningApp, {
417417
prefix: "/sockjs-node"
@@ -420,22 +420,22 @@ Server.prototype.listen = function() {
420420
}
421421

422422
Server.prototype.close = function(callback) {
423-
this.sockets.forEach(function(sock) {
423+
this.sockets.forEach((sock) => {
424424
sock.close();
425425
});
426426
this.sockets = [];
427-
this.listeningApp.close(function() {
427+
this.listeningApp.close(() => {
428428
this.middleware.close(callback);
429-
}.bind(this));
429+
});
430430

431-
this.contentBaseWatchers.forEach(function(watcher) {
431+
this.contentBaseWatchers.forEach((watcher) => {
432432
watcher.close();
433433
});
434434
this.contentBaseWatchers = [];
435435
}
436436

437437
Server.prototype.sockWrite = function(sockets, type, data) {
438-
sockets.forEach(function(sock) {
438+
sockets.forEach((sock) => {
439439
sock.write(JSON.stringify({
440440
type: type,
441441
data: data
@@ -467,9 +467,7 @@ Server.prototype._sendStats = function(sockets, stats, force) {
467467
stats &&
468468
(!stats.errors || stats.errors.length === 0) &&
469469
stats.assets &&
470-
stats.assets.every(function(asset) {
471-
return !asset.emitted;
472-
})
470+
stats.assets.every((asset) => !asset.emitted)
473471
)
474472
return this.sockWrite(sockets, "still-ok");
475473
this.sockWrite(sockets, "hash", stats.hash);
@@ -482,9 +480,9 @@ Server.prototype._sendStats = function(sockets, stats, force) {
482480
}
483481

484482
Server.prototype._watch = function(path) {
485-
const watcher = chokidar.watch(path).on("change", function() {
483+
const watcher = chokidar.watch(path).on("change", () => {
486484
this.sockWrite(this.sockets, "content-changed");
487-
}.bind(this))
485+
});
488486

489487
this.contentBaseWatchers.push(watcher);
490488
}

lib/util/addDevServerEntrypoints.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptio
1111
else if(devServerOptions.hot)
1212
devClient.push("webpack/hot/dev-server");
1313

14-
[].concat(webpackOptions).forEach(function(wpOpt) {
14+
[].concat(webpackOptions).forEach((wpOpt) => {
1515
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
16-
Object.keys(wpOpt.entry).forEach(function(key) {
16+
Object.keys(wpOpt.entry).forEach((key) => {
1717
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
1818
});
1919
} else if(typeof wpOpt.entry === "function") {

0 commit comments

Comments
 (0)