Skip to content

Commit b85a0ce

Browse files
danezSpaceK33z
authored andcommitted
Extract logic of adding entry points (#782)
1 parent 4991704 commit b85a0ce

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

bin/webpack-dev-server.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ const path = require("path");
55
const open = require("opn");
66
const fs = require("fs");
77
const net = require("net");
8-
const url = require("url");
98
const portfinder = require("portfinder");
9+
const addDevServerEntrypoints = require("../lib/util/addDevServerEntrypoints");
10+
const createDomain = require("../lib/util/createDomain");
1011

1112
// Local version replaces global one
1213
try {
@@ -327,33 +328,7 @@ function processOptions(wpOpt) {
327328
}
328329

329330
function startDevServer(wpOpt, options) {
330-
const protocol = options.https ? "https" : "http";
331-
332-
// the formatted domain (url without path) of the webpack server
333-
const domain = options.public ? `${protocol}://${options.public}` : url.format({
334-
protocol: protocol,
335-
hostname: options.host,
336-
port: options.socket ? 0 : options.port.toString()
337-
});
338-
339-
if(options.inline !== false) {
340-
const devClient = [`${require.resolve("../client/")}?${domain}`];
341-
342-
if(options.hotOnly)
343-
devClient.push("webpack/hot/only-dev-server");
344-
else if(options.hot)
345-
devClient.push("webpack/hot/dev-server");
346-
347-
[].concat(wpOpt).forEach(function(wpOpt) {
348-
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
349-
Object.keys(wpOpt.entry).forEach(function(key) {
350-
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
351-
});
352-
} else {
353-
wpOpt.entry = devClient.concat(wpOpt.entry);
354-
}
355-
});
356-
}
331+
addDevServerEntrypoints(wpOpt, options);
357332

358333
let compiler;
359334
try {
@@ -372,7 +347,7 @@ function startDevServer(wpOpt, options) {
372347
}));
373348
}
374349

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

377352
let server;
378353
try {

lib/Server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,7 @@ Server.prototype.invalidate = function() {
490490
if(this.middleware) this.middleware.invalidate();
491491
}
492492

493+
// Export this logic, so that other implementations, like task-runners can use it
494+
Server.addDevServerEntrypoints = require("./util/addDevServerEntrypoints");
495+
493496
module.exports = Server;

lib/util/addDevServerEntrypoints.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
const createDomain = require("./createDomain");
3+
4+
module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptions) {
5+
if(devServerOptions.inline !== false) {
6+
const domain = createDomain(devServerOptions);
7+
const devClient = [`${require.resolve("../../client/")}?${domain}`];
8+
9+
if(devServerOptions.hotOnly)
10+
devClient.push("webpack/hot/only-dev-server");
11+
else if(devServerOptions.hot)
12+
devClient.push("webpack/hot/dev-server");
13+
14+
[].concat(webpackOptions).forEach(function(wpOpt) {
15+
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
16+
Object.keys(wpOpt.entry).forEach(function(key) {
17+
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
18+
});
19+
} else {
20+
wpOpt.entry = devClient.concat(wpOpt.entry);
21+
}
22+
});
23+
}
24+
};

lib/util/createDomain.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
const url = require("url");
3+
4+
module.exports = function createDomain(options) {
5+
const protocol = options.https ? "https" : "http";
6+
7+
// the formatted domain (url without path) of the webpack server
8+
return options.public ? `${protocol}://${options.public}` : url.format({
9+
protocol: protocol,
10+
hostname: options.host,
11+
port: options.socket ? 0 : options.port.toString()
12+
});
13+
};

0 commit comments

Comments
 (0)