Skip to content

Commit a947336

Browse files
committedDec 29, 2016
Refactor to ES6
1 parent fb74672 commit a947336

17 files changed

+230
-200
lines changed
 

‎.eslintrc

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"no-process-exit": "warn",
2323
"space-in-parens": "error",
2424
"no-trailing-spaces": "error",
25-
"no-use-before-define": "off",
26-
"no-unused-vars": "off",
25+
"no-unused-vars": "error",
2726
"key-spacing": "error",
2827
"space-infix-ops": "error",
2928
"no-unsafe-negation": "error",
@@ -44,6 +43,8 @@
4443
}
4544
}],
4645
"no-console": "off",
47-
"valid-jsdoc": "error"
46+
"valid-jsdoc": "error",
47+
"no-useless-computed-key": "error",
48+
"prefer-const": "error"
4849
}
4950
}

‎bin/webpack-dev-server.js

+45-44
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,64 @@
11
#!/usr/bin/env node
2+
"use strict";
23

3-
var path = require("path");
4-
var open = require("opn");
5-
var fs = require("fs");
6-
var net = require("net");
7-
var url = require("url");
8-
var portfinder = require("portfinder");
4+
const path = require("path");
5+
const open = require("opn");
6+
const fs = require("fs");
7+
const net = require("net");
8+
const url = require("url");
9+
const portfinder = require("portfinder");
910

1011
// Local version replaces global one
1112
try {
12-
var localWebpackDevServer = require.resolve(path.join(process.cwd(), "node_modules", "webpack-dev-server", "bin", "webpack-dev-server.js"));
13+
const localWebpackDevServer = require.resolve(path.join(process.cwd(), "node_modules", "webpack-dev-server", "bin", "webpack-dev-server.js"));
1314
if(__filename !== localWebpackDevServer) {
1415
return require(localWebpackDevServer);
1516
}
1617
} catch(e) {}
1718

18-
var Server = require("../lib/Server");
19-
var webpack = require("webpack");
19+
const Server = require("../lib/Server");
20+
const webpack = require("webpack");
2021

2122
function versionInfo() {
22-
return "webpack-dev-server " + require("../package.json").version + "\n" +
23-
"webpack " + require("webpack/package.json").version;
23+
return `webpack-dev-server ${require("../package.json").version}\n` +
24+
`webpack ${require("webpack/package.json").version}`;
2425
}
2526

2627
function colorInfo(useColor, msg) {
2728
if(useColor)
2829
// Make text blue and bold, so it *pops*
29-
return "\u001b[1m\u001b[34m" + msg + "\u001b[39m\u001b[22m";
30+
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
3031
return msg;
3132
}
3233

3334
function colorError(useColor, msg) {
3435
if(useColor)
3536
// Make text red and bold, so it *pops*
36-
return "\u001b[1m\u001b[31m" + msg + "\u001b[39m\u001b[22m";
37+
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
3738
return msg;
3839
}
3940

40-
var yargs = require("yargs")
41-
.usage(versionInfo() +
42-
"\nUsage: http://webpack.github.io/docs/webpack-dev-server.html");
41+
const yargs = require("yargs")
42+
.usage(`${versionInfo()
43+
}\nUsage: http://webpack.github.io/docs/webpack-dev-server.html`);
4344

4445
require("webpack/bin/config-yargs")(yargs);
4546

4647
// It is important that this is done after the webpack yargs config,
4748
// so it overrides webpack's version info.
4849
yargs.version(versionInfo);
4950

50-
var ADVANCED_GROUP = "Advanced options:";
51-
var DISPLAY_GROUP = "Stats options:";
52-
var SSL_GROUP = "SSL options:";
53-
var CONNECTION_GROUP = "Connection options:";
54-
var RESPONSE_GROUP = "Response options:";
55-
var BASIC_GROUP = "Basic options:";
51+
const ADVANCED_GROUP = "Advanced options:";
52+
const DISPLAY_GROUP = "Stats options:";
53+
const SSL_GROUP = "SSL options:";
54+
const CONNECTION_GROUP = "Connection options:";
55+
const RESPONSE_GROUP = "Response options:";
56+
const BASIC_GROUP = "Basic options:";
5657

5758
// Taken out of yargs because we must know if
5859
// it wasn't given by the user, in which case
5960
// we should use portfinder.
60-
var DEFAULT_PORT = 8080;
61+
const DEFAULT_PORT = 8080;
6162

6263
yargs.options({
6364
"lazy": {
@@ -185,9 +186,9 @@ yargs.options({
185186
}
186187
});
187188

188-
var argv = yargs.argv;
189+
const argv = yargs.argv;
189190

190-
var wpOpt = require("webpack/bin/convert-argv")(yargs, argv, {
191+
const wpOpt = require("webpack/bin/convert-argv")(yargs, argv, {
191192
outputFilename: "/bundle.js"
192193
});
193194

@@ -201,9 +202,9 @@ function processOptions(wpOpt) {
201202
return;
202203
}
203204

204-
var firstWpOpt = Array.isArray(wpOpt) ? wpOpt[0] : wpOpt;
205+
const firstWpOpt = Array.isArray(wpOpt) ? wpOpt[0] : wpOpt;
205206

206-
var options = wpOpt.devServer || firstWpOpt.devServer || {};
207+
const options = wpOpt.devServer || firstWpOpt.devServer || {};
207208

208209
if(argv.host !== "localhost" || !options.host)
209210
options.host = argv.host;
@@ -217,7 +218,7 @@ function processOptions(wpOpt) {
217218
if(!options.publicPath) {
218219
options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || "";
219220
if(!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== "/")
220-
options.publicPath = "/" + options.publicPath;
221+
options.publicPath = `/${options.publicPath}`;
221222
}
222223

223224
if(!options.filename)
@@ -326,17 +327,17 @@ function processOptions(wpOpt) {
326327
}
327328

328329
function startDevServer(wpOpt, options) {
329-
var protocol = options.https ? "https" : "http";
330+
const protocol = options.https ? "https" : "http";
330331

331332
// the formatted domain (url without path) of the webpack server
332-
var domain = url.format({
333+
const domain = url.format({
333334
protocol: protocol,
334335
hostname: options.host,
335336
port: options.socket ? 0 : options.port.toString()
336337
});
337338

338339
if(options.inline !== false) {
339-
var devClient = [require.resolve("../client/") + "?" + (options.public ? protocol + "://" + options.public : domain)];
340+
const devClient = [`${require.resolve("../client/")}?${options.public ? `${protocol}://${options.public}` : domain}`];
340341

341342
if(options.hotOnly)
342343
devClient.push("webpack/hot/only-dev-server");
@@ -354,7 +355,7 @@ function startDevServer(wpOpt, options) {
354355
});
355356
}
356357

357-
var compiler;
358+
let compiler;
358359
try {
359360
compiler = webpack(wpOpt);
360361
} catch(e) {
@@ -371,13 +372,13 @@ function startDevServer(wpOpt, options) {
371372
}));
372373
}
373374

374-
var uri = domain + (options.inline !== false || options.lazy === true ? "/" : "/webpack-dev-server/");
375+
const uri = domain + (options.inline !== false || options.lazy === true ? "/" : "/webpack-dev-server/");
375376

376-
var server;
377+
let server;
377378
try {
378379
server = new Server(compiler, options);
379380
} catch(e) {
380-
var OptionsValidationError = require("../lib/OptionsValidationError");
381+
const OptionsValidationError = require("../lib/OptionsValidationError");
381382
if(e instanceof OptionsValidationError) {
382383
console.error(colorError(options.stats.colors, e.message));
383384
process.exit(1); // eslint-disable-line
@@ -388,7 +389,7 @@ function startDevServer(wpOpt, options) {
388389
if(options.socket) {
389390
server.listeningApp.on("error", function(e) {
390391
if(e.code === "EADDRINUSE") {
391-
var clientSocket = new net.Socket();
392+
const clientSocket = new net.Socket();
392393
clientSocket.on("error", function(e) {
393394
if(e.code === "ECONNREFUSED") {
394395
// No other server listening on this socket so it can be safely removed
@@ -405,7 +406,7 @@ function startDevServer(wpOpt, options) {
405406
});
406407
server.listen(options.socket, options.host, function(err) {
407408
if(err) throw err;
408-
var READ_WRITE = 438; // chmod 666 (rw rw rw)
409+
const READ_WRITE = 438; // chmod 666 (rw rw rw)
409410
fs.chmod(options.socket, READ_WRITE, function(err) {
410411
if(err) throw err;
411412
reportReadiness(uri, options);
@@ -420,19 +421,19 @@ function startDevServer(wpOpt, options) {
420421
}
421422

422423
function reportReadiness(uri, options) {
423-
var useColor = argv.color;
424-
var startSentence = "Project is running at " + colorInfo(useColor, uri)
424+
const useColor = argv.color;
425+
let startSentence = `Project is running at ${colorInfo(useColor, uri)}`
425426
if(options.socket) {
426-
startSentence = "Listening to socket at " + colorInfo(useColor, options.socket);
427+
startSentence = `Listening to socket at ${colorInfo(useColor, options.socket)}`;
427428
}
428429
console.log((argv["progress"] ? "\n" : "") + startSentence);
429430

430-
console.log("webpack output is served from " + colorInfo(useColor, options.publicPath));
431-
var contentBase = Array.isArray(options.contentBase) ? options.contentBase.join(", ") : options.contentBase;
431+
console.log(`webpack output is served from ${colorInfo(useColor, options.publicPath)}`);
432+
const contentBase = Array.isArray(options.contentBase) ? options.contentBase.join(", ") : options.contentBase;
432433
if(contentBase)
433-
console.log("Content not from webpack is served from " + colorInfo(useColor, contentBase));
434+
console.log(`Content not from webpack is served from ${colorInfo(useColor, contentBase)}`);
434435
if(options.historyApiFallback)
435-
console.log("404s will fallback to " + colorInfo(useColor, options.historyApiFallback.index || "/index.html"));
436+
console.log(`404s will fallback to ${colorInfo(useColor, options.historyApiFallback.index || "/index.html")}`);
436437
if(options.open)
437438
open(uri);
438439
}

‎examples/hmr/webpack.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var webpack = require("webpack");
1+
"use strict";
2+
3+
const webpack = require("webpack");
24

35
module.exports = {
46
context: __dirname,

‎examples/listen-socket/check-socket.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22

3-
var net = require("net");
3+
const net = require("net");
44

5-
var client = net.createConnection("./webpack.sock");
5+
const client = net.createConnection("./webpack.sock");
66
client.on("connect", function() {
77
console.log("Successfully connected to socket, exiting");
88
process.exit(1); // eslint-disable-line

‎examples/node-api-middleware/server.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
var Webpack = require("webpack");
2-
var WebpackDevServer = require("../../lib/Server");
3-
var webpackConfig = require("./webpack.config");
1+
"use strict";
42

5-
var compiler = Webpack(webpackConfig);
6-
var server = new WebpackDevServer(compiler, {
3+
const Webpack = require("webpack");
4+
const WebpackDevServer = require("../../lib/Server");
5+
const webpackConfig = require("./webpack.config");
6+
7+
const compiler = Webpack(webpackConfig);
8+
const server = new WebpackDevServer(compiler, {
79
stats: {
810
colors: true
911
},
1012
setup: function(app) {
1113
app.use(function(req, res, next) {
12-
console.log("Using middleware for " + req.url);
14+
console.log(`Using middleware for ${req.url}`);
1315
next();
1416
});
1517
}

‎examples/node-api-simple/server.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
var Webpack = require("webpack");
2-
var WebpackDevServer = require("../../lib/Server");
3-
var webpackConfig = require("./webpack.config");
1+
"use strict";
42

5-
var compiler = Webpack(webpackConfig);
6-
var server = new WebpackDevServer(compiler, {
3+
const Webpack = require("webpack");
4+
const WebpackDevServer = require("../../lib/Server");
5+
const webpackConfig = require("./webpack.config");
6+
7+
const compiler = Webpack(webpackConfig);
8+
const server = new WebpackDevServer(compiler, {
79
stats: {
810
colors: true
911
}

‎examples/proxy-hot-reload/webpack.config.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var fs = require("fs");
1+
"use strict";
22

3-
var proxyConfig = require("./proxy-config");
4-
var proxyOptions = {
3+
const fs = require("fs");
4+
5+
const proxyConfig = require("./proxy-config");
6+
let proxyOptions = {
57
context: "/api",
68
target: proxyConfig.target,
79
pathRewrite: proxyConfig.pathRewrite,
@@ -11,7 +13,7 @@ var proxyOptions = {
1113
fs.watch("./proxy-config.js", function() {
1214
delete require.cache[require.resolve("./proxy-config")];
1315
try {
14-
var newProxyConfig = require("./proxy-config");
16+
const newProxyConfig = require("./proxy-config");
1517
if(proxyOptions.target !== newProxyConfig.target) {
1618
console.log("Proxy target changed:", newProxyConfig.target);
1719
proxyOptions = {

‎examples/webpack-config-array/webpack.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var webpack = require("webpack");
1+
"use strict";
2+
3+
const webpack = require("webpack");
24
module.exports = [
35
{
46
context: __dirname,

‎lib/OptionsValidationError.js

+45-43
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
var optionsSchema = require("./optionsSchema.json");
1+
"use strict";
2+
3+
const optionsSchema = require("./optionsSchema.json");
24

35
function OptionsValidationError(validationErrors) {
46
Error.call(this);
57
Error.captureStackTrace(this, OptionsValidationError);
68
this.name = "WebpackDevServerOptionsValidationError";
7-
this.message = "Invalid configuration object. " +
8-
"webpack-dev-server has been initialised using a configuration object that does not match the API schema.\n" +
9+
this.message = `${"Invalid configuration object. " +
10+
"webpack-dev-server has been initialised using a configuration object that does not match the API schema.\n"}${
911
validationErrors.map(function(err) {
10-
return " - " + indent(OptionsValidationError.formatValidationError(err), " ", false);
11-
}).join("\n");
12+
return ` - ${indent(OptionsValidationError.formatValidationError(err), " ", false)}`;
13+
}).join("\n")}`;
1214
this.validationErrors = validationErrors;
1315
}
1416
module.exports = OptionsValidationError;
@@ -17,48 +19,48 @@ OptionsValidationError.prototype = Object.create(Error.prototype);
1719
OptionsValidationError.prototype.constructor = OptionsValidationError;
1820

1921
OptionsValidationError.formatValidationError = function formatValidationError(err) {
20-
var dataPath = "configuration" + err.dataPath;
22+
const dataPath = `configuration${err.dataPath}`;
2123
switch(err.keyword) {
2224
case "additionalProperties":
23-
return dataPath + " has an unknown property '" + err.params.additionalProperty + "'. These properties are valid:\n" +
24-
getSchemaPartText(err.parentSchema);
25+
return `${dataPath} has an unknown property '${err.params.additionalProperty}'. These properties are valid:\n${
26+
getSchemaPartText(err.parentSchema)}`;
2527
case "oneOf":
2628
case "anyOf":
2729
case "enum":
28-
return dataPath + " should be one of these:\n" +
29-
getSchemaPartText(err.parentSchema);
30+
return `${dataPath} should be one of these:\n${
31+
getSchemaPartText(err.parentSchema)}`;
3032
case "allOf":
31-
return dataPath + " should be:\n" +
32-
getSchemaPartText(err.parentSchema);
33+
return `${dataPath} should be:\n${
34+
getSchemaPartText(err.parentSchema)}`;
3335
case "type":
3436
switch(err.params.type) {
3537
case "object":
36-
return dataPath + " should be an object.";
38+
return `${dataPath} should be an object.`;
3739
case "array":
38-
return dataPath + " should be an array.";
40+
return `${dataPath} should be an array.`;
3941
case "string":
40-
return dataPath + " should be a string.";
42+
return `${dataPath} should be a string.`;
4143
case "boolean":
42-
return dataPath + " should be a boolean.";
44+
return `${dataPath} should be a boolean.`;
4345
case "number":
44-
return dataPath + " should be a number.";
46+
return `${dataPath} should be a number.`;
4547
}
46-
return dataPath + " should be " + err.params.type + ":\n" +
47-
getSchemaPartText(err.parentSchema);
48+
return `${dataPath} should be ${err.params.type}:\n${
49+
getSchemaPartText(err.parentSchema)}`;
4850
case "instanceof":
49-
return dataPath + " should be an instance of " + getSchemaPartText(err.parentSchema) + ".";
50-
case "required":
51-
var missingProperty = err.params.missingProperty.replace(/^\./, "");
52-
return dataPath + " misses the property '" + missingProperty + "'.\n" +
53-
getSchemaPartText(err.parentSchema, ["properties", missingProperty]);
51+
return `${dataPath} should be an instance of ${getSchemaPartText(err.parentSchema)}.`;
52+
case "required": // eslint-disable-line no-case-declarations
53+
const missingProperty = err.params.missingProperty.replace(/^\./, "");
54+
return `${dataPath} misses the property '${missingProperty}'.\n${
55+
getSchemaPartText(err.parentSchema, ["properties", missingProperty])}`;
5456
case "minLength":
5557
if(err.params.limit === 1)
56-
return dataPath + " should not be empty.";
58+
return `${dataPath} should not be empty.`;
5759
else
58-
return dataPath + " " + err.message;
60+
return `${dataPath} ${err.message}`;
5961
default:
60-
return dataPath + " " + err.message + " (" + JSON.stringify(err, 0, 2) + ").\n" +
61-
getSchemaPartText(err.parentSchema);
62+
return `${dataPath} ${err.message} (${JSON.stringify(err, 0, 2)}).\n${
63+
getSchemaPartText(err.parentSchema)}`;
6264
}
6365
}
6466

@@ -70,9 +72,9 @@ function getSchemaPart(path, parents, additionalPath) {
7072
additionalPath = additionalPath.split("/");
7173
path = path.concat(additionalPath);
7274
}
73-
var schemaPart = optionsSchema;
74-
for(var i = 1; i < path.length; i++) {
75-
var inner = schemaPart[path[i]];
75+
let schemaPart = optionsSchema;
76+
for(let i = 1; i < path.length; i++) {
77+
const inner = schemaPart[path[i]];
7678
if(inner)
7779
schemaPart = inner;
7880
}
@@ -81,16 +83,16 @@ function getSchemaPart(path, parents, additionalPath) {
8183

8284
function getSchemaPartText(schemaPart, additionalPath) {
8385
if(additionalPath) {
84-
for(var i = 0; i < additionalPath.length; i++) {
85-
var inner = schemaPart[additionalPath[i]];
86+
for(let i = 0; i < additionalPath.length; i++) {
87+
const inner = schemaPart[additionalPath[i]];
8688
if(inner)
8789
schemaPart = inner;
8890
}
8991
}
9092
while(schemaPart.$ref) schemaPart = getSchemaPart(schemaPart.$ref);
91-
var schemaText = OptionsValidationError.formatSchema(schemaPart);
93+
let schemaText = OptionsValidationError.formatSchema(schemaPart);
9294
if(schemaPart.description)
93-
schemaText += "\n" + schemaPart.description;
95+
schemaText += `\n${schemaPart.description}`;
9496
return schemaText;
9597
}
9698

@@ -111,18 +113,18 @@ function formatSchema(schema, prevSchemas) {
111113
return "number";
112114
case "object":
113115
if(schema.properties) {
114-
var required = schema.required || [];
115-
return "object { " + Object.keys(schema.properties).map(function(property) {
116-
if(required.indexOf(property) < 0) return property + "?";
116+
const required = schema.required || [];
117+
return `object { ${Object.keys(schema.properties).map(function(property) {
118+
if(required.indexOf(property) < 0) return `${property}?`;
117119
return property;
118-
}).concat(schema.additionalProperties ? ["..."] : []).join(", ") + " }";
120+
}).concat(schema.additionalProperties ? ["..."] : []).join(", ")} }`;
119121
}
120122
if(schema.additionalProperties) {
121-
return "object { <key>: " + formatInnerSchema(schema.additionalProperties) + " }";
123+
return `object { <key>: ${formatInnerSchema(schema.additionalProperties)} }`;
122124
}
123125
return "object";
124126
case "array":
125-
return "[" + formatInnerSchema(schema.items) + "]";
127+
return `[${formatInnerSchema(schema.items)}]`;
126128
}
127129
switch(schema.instanceof) {
128130
case "Function":
@@ -142,9 +144,9 @@ function formatSchema(schema, prevSchemas) {
142144

143145
function indent(str, prefix, firstLine) {
144146
if(firstLine) {
145-
return prefix + str.replace(/\n(?!$)/g, "\n" + prefix);
147+
return prefix + str.replace(/\n(?!$)/g, `\n${prefix}`);
146148
} else {
147-
return str.replace(/\n(?!$)/g, "\n" + prefix);
149+
return str.replace(/\n(?!$)/g, `\n${prefix}`);
148150
}
149151
}
150152

‎lib/Server.js

+49-47
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
var fs = require("fs");
2-
var chokidar = require("chokidar");
3-
var path = require("path");
4-
var webpackDevMiddleware = require("webpack-dev-middleware");
5-
var express = require("express");
6-
var compress = require("compression");
7-
var sockjs = require("sockjs");
8-
var http = require("http");
9-
var spdy = require("spdy");
10-
var httpProxyMiddleware = require("http-proxy-middleware");
11-
var serveIndex = require("serve-index");
12-
var historyApiFallback = require("connect-history-api-fallback");
13-
var webpack = require("webpack");
14-
var OptionsValidationError = require("./OptionsValidationError");
15-
var optionsSchema = require("./optionsSchema.json");
16-
17-
var clientStats = { errorDetails: false };
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const chokidar = require("chokidar");
5+
const path = require("path");
6+
const webpackDevMiddleware = require("webpack-dev-middleware");
7+
const express = require("express");
8+
const compress = require("compression");
9+
const sockjs = require("sockjs");
10+
const http = require("http");
11+
const spdy = require("spdy");
12+
const httpProxyMiddleware = require("http-proxy-middleware");
13+
const serveIndex = require("serve-index");
14+
const historyApiFallback = require("connect-history-api-fallback");
15+
const webpack = require("webpack");
16+
const OptionsValidationError = require("./OptionsValidationError");
17+
const optionsSchema = require("./optionsSchema.json");
18+
19+
const clientStats = { errorDetails: false };
1820

1921
function Server(compiler, options) {
2022
// Default options
2123
if(!options) options = {};
2224

23-
var validationErrors = webpack.validateSchema(optionsSchema, options);
25+
const validationErrors = webpack.validateSchema(optionsSchema, options);
2426
if(validationErrors.length) {
2527
throw new OptionsValidationError(validationErrors);
2628
}
@@ -36,7 +38,7 @@ function Server(compiler, options) {
3638
this.contentBaseWatchers = [];
3739

3840
// Listening for events
39-
var invalidPlugin = function() {
41+
const invalidPlugin = function() {
4042
this.sockWrite(this.sockets, "invalid");
4143
}.bind(this);
4244
compiler.plugin("compile", invalidPlugin);
@@ -47,7 +49,7 @@ function Server(compiler, options) {
4749
}.bind(this));
4850

4951
// Init express server
50-
var app = this.app = new express();
52+
const app = this.app = new express();
5153

5254
// middleware for serving webpack bundle
5355
this.middleware = webpackDevMiddleware(compiler, options);
@@ -76,22 +78,22 @@ function Server(compiler, options) {
7678
res.setHeader("Content-Type", "text/html");
7779
/* eslint-disable quotes */
7880
res.write('<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>');
79-
var path = this.middleware.getFilenameFromUrl(options.publicPath || "/");
80-
var fs = this.middleware.fileSystem;
81+
const path = this.middleware.getFilenameFromUrl(options.publicPath || "/");
82+
const fs = this.middleware.fileSystem;
8183

8284
function writeDirectory(baseUrl, basePath) {
83-
var content = fs.readdirSync(basePath);
85+
const content = fs.readdirSync(basePath);
8486
res.write("<ul>");
8587
content.forEach(function(item) {
86-
var p = basePath + "/" + item;
88+
const p = `${basePath}/${item}`;
8789
if(fs.statSync(p).isFile()) {
8890
res.write('<li><a href="');
8991
res.write(baseUrl + item);
9092
res.write('">');
9193
res.write(item);
9294
res.write('</a></li>');
9395
if(/\.js$/.test(item)) {
94-
var htmlItem = item.substr(0, item.length - 3);
96+
const htmlItem = item.substr(0, item.length - 3);
9597
res.write('<li><a href="');
9698
res.write(baseUrl + htmlItem);
9799
res.write('">');
@@ -106,7 +108,7 @@ function Server(compiler, options) {
106108
res.write('<li>');
107109
res.write(item);
108110
res.write('<br>');
109-
writeDirectory(baseUrl + item + "/", p);
111+
writeDirectory(`${baseUrl + item}/`, p);
110112
res.write('</li>');
111113
}
112114
});
@@ -117,14 +119,14 @@ function Server(compiler, options) {
117119
res.end("</body></html>");
118120
}.bind(this));
119121

120-
var contentBase;
122+
let contentBase;
121123
if(options.contentBase !== undefined) {
122124
contentBase = options.contentBase;
123125
} else {
124126
contentBase = process.cwd();
125127
}
126128

127-
var features = {
129+
const features = {
128130
compress: function() {
129131
if(options.compress) {
130132
// Enable gzip compression.
@@ -146,9 +148,9 @@ function Server(compiler, options) {
146148
*/
147149
if(!Array.isArray(options.proxy)) {
148150
options.proxy = Object.keys(options.proxy).map(function(context) {
149-
var proxyOptions;
151+
let proxyOptions;
150152
// For backwards compatibility reasons.
151-
var correctedContext = context.replace(/^\*$/, "**").replace(/\/\*$/, "");
153+
const correctedContext = context.replace(/^\*$/, "**").replace(/\/\*$/, "");
152154

153155
if(typeof options.proxy[context] === "string") {
154156
proxyOptions = {
@@ -165,8 +167,8 @@ function Server(compiler, options) {
165167
});
166168
}
167169

168-
var getProxyMiddleware = function(proxyConfig) {
169-
var context = proxyConfig.context || proxyConfig.path;
170+
const getProxyMiddleware = function(proxyConfig) {
171+
const context = proxyConfig.context || proxyConfig.path;
170172

171173
// It is possible to use the `bypass` method without a `target`.
172174
// However, the proxy middleware has no use in this case, and will fail to instantiate.
@@ -192,8 +194,8 @@ function Server(compiler, options) {
192194
* ]
193195
*/
194196
options.proxy.forEach(function(proxyConfigOrCallback) {
195-
var proxyConfig;
196-
var proxyMiddleware;
197+
let proxyConfig;
198+
let proxyMiddleware;
197199

198200
if(typeof proxyConfigOrCallback === "function") {
199201
proxyConfig = proxyConfigOrCallback();
@@ -205,14 +207,14 @@ function Server(compiler, options) {
205207

206208
app.use(function(req, res, next) {
207209
if(typeof proxyConfigOrCallback === "function") {
208-
var newProxyConfig = proxyConfigOrCallback();
210+
const newProxyConfig = proxyConfigOrCallback();
209211
if(newProxyConfig !== proxyConfig) {
210212
proxyConfig = newProxyConfig;
211213
proxyMiddleware = getProxyMiddleware(proxyConfig);
212214
}
213215
}
214-
var bypass = typeof proxyConfig.bypass === "function";
215-
var bypassUrl = bypass && proxyConfig.bypass(req, res, proxyConfig) || false;
216+
const bypass = typeof proxyConfig.bypass === "function";
217+
const bypassUrl = bypass && proxyConfig.bypass(req, res, proxyConfig) || false;
216218

217219
if(bypassUrl) {
218220
req.url = bypassUrl;
@@ -257,7 +259,7 @@ function Server(compiler, options) {
257259
// Redirect every request to the port contentBase
258260
app.get("*", function(req, res) {
259261
res.writeHead(302, {
260-
"Location": "//localhost:" + contentBase + req.path + (req._parsedUrl.search || "")
262+
"Location": `//localhost:${contentBase}${req.path}${req._parsedUrl.search || ""}`
261263
});
262264
res.end();
263265
});
@@ -308,7 +310,7 @@ function Server(compiler, options) {
308310
}.bind(this)
309311
};
310312

311-
var defaultFeatures = ["setup", "headers", "middleware"];
313+
const defaultFeatures = ["setup", "headers", "middleware"];
312314
if(options.proxy)
313315
defaultFeatures.push("proxy", "middleware");
314316
if(contentBase !== false)
@@ -341,7 +343,7 @@ function Server(compiler, options) {
341343
}
342344

343345
// Use built-in self-signed certificate if no certificate was configured
344-
var fakeCert = fs.readFileSync(path.join(__dirname, "../ssl/server.pem"));
346+
const fakeCert = fs.readFileSync(path.join(__dirname, "../ssl/server.pem"));
345347
options.https.key = options.https.key || fakeCert;
346348
options.https.cert = options.https.cert || fakeCert;
347349

@@ -363,7 +365,7 @@ Server.prototype.use = function() {
363365

364366
Server.prototype.setContentHeaders = function(req, res, next) {
365367
if(this.headers) {
366-
for(var name in this.headers) {
368+
for(const name in this.headers) {
367369
res.setHeader(name, this.headers[name]);
368370
}
369371
}
@@ -373,8 +375,8 @@ Server.prototype.setContentHeaders = function(req, res, next) {
373375

374376
// delegate listen call and init sockjs
375377
Server.prototype.listen = function() {
376-
var returnValue = this.listeningApp.listen.apply(this.listeningApp, arguments);
377-
var sockServer = sockjs.createServer({
378+
const returnValue = this.listeningApp.listen.apply(this.listeningApp, arguments);
379+
const sockServer = sockjs.createServer({
378380
// Use provided up-to-date sockjs-client
379381
sockjs_url: "/__webpack_dev_server__/sockjs.bundle.js",
380382
// Limit useless logs
@@ -389,7 +391,7 @@ Server.prototype.listen = function() {
389391
this.sockets.push(conn);
390392

391393
conn.on("close", function() {
392-
var connIndex = this.sockets.indexOf(conn);
394+
const connIndex = this.sockets.indexOf(conn);
393395
if(connIndex >= 0) {
394396
this.sockets.splice(connIndex, 1);
395397
}
@@ -435,9 +437,9 @@ Server.prototype.sockWrite = function(sockets, type, data) {
435437
}
436438

437439
Server.prototype.serveMagicHtml = function(req, res, next) {
438-
var _path = req.path;
440+
const _path = req.path;
439441
try {
440-
if(!this.middleware.fileSystem.statSync(this.middleware.getFilenameFromUrl(_path + ".js")).isFile())
442+
if(!this.middleware.fileSystem.statSync(this.middleware.getFilenameFromUrl(`${_path}.js`)).isFile())
441443
return next();
442444
// Serve a page that executes the javascript
443445
/* eslint-disable quotes */
@@ -473,7 +475,7 @@ Server.prototype._sendStats = function(sockets, stats, force) {
473475
}
474476

475477
Server.prototype._watch = function(path) {
476-
var watcher = chokidar.watch(path).on("change", function() {
478+
const watcher = chokidar.watch(path).on("change", function() {
477479
this.sockWrite(this.sockets, "content-changed");
478480
}.bind(this))
479481

‎test/Compress.test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
var request = require("supertest");
2-
var helper = require("./helper");
3-
var config = require("./fixtures/simple-config/webpack.config");
1+
"use strict";
2+
3+
const request = require("supertest");
4+
const helper = require("./helper");
5+
const config = require("./fixtures/simple-config/webpack.config");
46

57
describe("Compress", function() {
6-
var server;
7-
var req;
8+
let server;
9+
let req;
810

911
before(function(done) {
1012
server = helper.start(config, {

‎test/ContentBase.test.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
var request = require("supertest");
2-
var path = require("path");
3-
var helper = require("./helper");
4-
var config = require("./fixtures/contentbase-config/webpack.config");
1+
"use strict";
2+
3+
const request = require("supertest");
4+
const path = require("path");
5+
const helper = require("./helper");
6+
const config = require("./fixtures/contentbase-config/webpack.config");
57
require("mocha-sinon");
68

7-
var contentBasePublic = path.join(__dirname, "fixtures/contentbase-config/public");
8-
var contentBaseOther = path.join(__dirname, "fixtures/contentbase-config/other");
9+
const contentBasePublic = path.join(__dirname, "fixtures/contentbase-config/public");
10+
const contentBaseOther = path.join(__dirname, "fixtures/contentbase-config/other");
911

1012
describe("ContentBase", function() {
11-
var server;
12-
var req;
13+
let server;
14+
let req;
1315
afterEach(helper.close);
1416

1517
describe("to directory", function() {

‎test/HistoryApiFallback.test.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
var path = require("path");
2-
var request = require("supertest");
3-
var helper = require("./helper");
4-
var config = require("./fixtures/historyapifallback-config/webpack.config");
5-
var config2 = require("./fixtures/historyapifallback-2-config/webpack.config");
6-
var config3 = require("./fixtures/historyapifallback-3-config/webpack.config");
1+
"use strict";
2+
3+
const path = require("path");
4+
const request = require("supertest");
5+
const helper = require("./helper");
6+
const config = require("./fixtures/historyapifallback-config/webpack.config");
7+
const config2 = require("./fixtures/historyapifallback-2-config/webpack.config");
8+
const config3 = require("./fixtures/historyapifallback-3-config/webpack.config");
79

810
describe("HistoryApiFallback", function() {
9-
var server;
10-
var req;
11+
let server;
12+
let req;
1113

1214
afterEach(helper.close);
1315

‎test/Lazy.test.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var should = require("should");
2-
var helper = require("./helper");
3-
var config = require("./fixtures/simple-config/webpack.config");
1+
"use strict";
2+
3+
const should = require("should");
4+
const helper = require("./helper");
5+
const config = require("./fixtures/simple-config/webpack.config");
46

57
describe("Lazy", function() {
68
afterEach(helper.close);

‎test/Routes.test.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
var request = require("supertest");
2-
var fs = require("fs");
3-
var path = require("path");
4-
var helper = require("./helper");
5-
var config = require("./fixtures/simple-config/webpack.config");
1+
"use strict";
62

7-
var directoryIndex = fs.readFileSync(path.join(__dirname, "fixtures/directory-index.txt"), "utf-8");
8-
var magicHtml = fs.readFileSync(path.join(__dirname, "fixtures/magic-html.txt"), "utf-8");
3+
const request = require("supertest");
4+
const fs = require("fs");
5+
const path = require("path");
6+
const helper = require("./helper");
7+
const config = require("./fixtures/simple-config/webpack.config");
8+
9+
const directoryIndex = fs.readFileSync(path.join(__dirname, "fixtures/directory-index.txt"), "utf-8");
10+
const magicHtml = fs.readFileSync(path.join(__dirname, "fixtures/magic-html.txt"), "utf-8");
911

1012
describe("Routes", function() {
11-
var server;
12-
var req;
13+
let server;
14+
let req;
1315

1416
before(function(done) {
1517
server = helper.start(config, {

‎test/Validation.test.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
var config = require("./fixtures/simple-config/webpack.config");
2-
var OptionsValidationError = require("../lib/OptionsValidationError");
3-
var Server = require("../lib/Server");
4-
var webpack = require("webpack");
1+
"use strict";
2+
3+
const config = require("./fixtures/simple-config/webpack.config");
4+
const OptionsValidationError = require("../lib/OptionsValidationError");
5+
const Server = require("../lib/Server");
6+
const webpack = require("webpack");
57

68
describe("Validation", function() {
7-
var compiler;
9+
let compiler;
810
before(function() {
911
compiler = webpack(config);
1012
});
11-
var testCases = [{
13+
const testCases = [{
1214
name: "invalid `hot` configuration",
1315
config: { hot: "asdf" },
1416
message: [
@@ -42,7 +44,7 @@ describe("Validation", function() {
4244
]
4345
}];
4446
testCases.forEach(function(testCase) {
45-
it("should fail validation for " + testCase.name, function() {
47+
it(`should fail validation for ${testCase.name}`, function() {
4648
try {
4749
new Server(compiler, testCase.config);
4850
} catch(e) {

‎test/helper.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
var Server = require("../lib/Server");
2-
var webpack = require("webpack");
1+
"use strict";
32

4-
var server;
3+
const Server = require("../lib/Server");
4+
const webpack = require("webpack");
5+
6+
let server;
57

68
module.exports = {
79
start: function(config, options, done) {
810
if(options.quiet === undefined) {
911
options.quiet = true;
1012
}
11-
var compiler = webpack(config);
13+
const compiler = webpack(config);
1214
server = new Server(compiler, options);
1315

1416
server.listen(8080, "localhost", function(err) {

0 commit comments

Comments
 (0)
Please sign in to comment.