webpack-dev-server 4.0.0-beta.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +109 -58
  2. package/bin/cli-flags.js +827 -269
  3. package/bin/process-arguments.js +332 -0
  4. package/bin/webpack-dev-server.js +46 -30
  5. package/client/clients/SockJSClient.js +11 -44
  6. package/client/clients/WebSocketClient.js +43 -0
  7. package/client/index.js +90 -98
  8. package/client/modules/logger/index.js +90 -2736
  9. package/client/modules/sockjs-client/index.js +127 -41
  10. package/client/modules/strip-ansi/index.js +69 -37
  11. package/client/overlay.js +111 -95
  12. package/client/socket.js +11 -12
  13. package/client/utils/createSocketURL.js +70 -0
  14. package/client/utils/getCurrentScriptSource.js +10 -9
  15. package/client/utils/log.js +5 -10
  16. package/client/utils/parseURL.js +43 -0
  17. package/client/utils/reloadApp.js +49 -36
  18. package/client/utils/sendMessage.js +3 -5
  19. package/lib/Server.js +1456 -539
  20. package/lib/options.json +562 -314
  21. package/lib/servers/BaseServer.js +2 -1
  22. package/lib/servers/SockJSServer.js +32 -31
  23. package/lib/servers/WebsocketServer.js +42 -41
  24. package/lib/utils/DevServerPlugin.js +275 -128
  25. package/package.json +51 -52
  26. package/CHANGELOG.md +0 -569
  27. package/client/clients/BaseClient.js +0 -23
  28. package/client/clients/WebsocketClient.js +0 -76
  29. package/client/modules/logger/SyncBailHookFake.js +0 -10
  30. package/client/utils/createSocketUrl.js +0 -94
  31. package/client/webpack.config.js +0 -57
  32. package/lib/utils/colors.js +0 -22
  33. package/lib/utils/createCertificate.js +0 -69
  34. package/lib/utils/createDomain.js +0 -31
  35. package/lib/utils/defaultPort.js +0 -3
  36. package/lib/utils/defaultTo.js +0 -7
  37. package/lib/utils/findPort.js +0 -39
  38. package/lib/utils/getCertificate.js +0 -50
  39. package/lib/utils/getColorsOption.js +0 -15
  40. package/lib/utils/getCompilerConfigArray.js +0 -8
  41. package/lib/utils/getSocketClientPath.d.ts +0 -3
  42. package/lib/utils/getSocketClientPath.js +0 -38
  43. package/lib/utils/getSocketServerImplementation.js +0 -42
  44. package/lib/utils/getStatsOption.js +0 -16
  45. package/lib/utils/getVersions.js +0 -10
  46. package/lib/utils/normalizeOptions.js +0 -122
  47. package/lib/utils/routes.js +0 -70
  48. package/lib/utils/runBonjour.js +0 -21
  49. package/lib/utils/runOpen.js +0 -83
  50. package/lib/utils/setupExitSignals.js +0 -25
  51. package/lib/utils/tryParseInt.js +0 -13
  52. package/lib/utils/updateCompiler.js +0 -14
package/client/socket.js CHANGED
@@ -1,14 +1,13 @@
1
- 'use strict';
2
1
  /* global __webpack_dev_server_client__ */
2
+ import WebSocketClient from "./clients/WebSocketClient.js"; // this WebsocketClient is here as a default fallback, in case the client is not injected
3
3
 
4
- /* eslint-disable
5
- camelcase
6
- */
7
- // this WebsocketClient is here as a default fallback,
8
- // in case the client is not injected
4
+ /* eslint-disable camelcase */
5
+
6
+ var Client = // eslint-disable-next-line camelcase, no-nested-ternary
7
+ typeof __webpack_dev_server_client__ !== "undefined" ? // eslint-disable-next-line camelcase
8
+ typeof __webpack_dev_server_client__.default !== "undefined" ? __webpack_dev_server_client__.default : __webpack_dev_server_client__ : WebSocketClient;
9
+ /* eslint-enable camelcase */
9
10
 
10
- var Client = typeof __webpack_dev_server_client__ !== 'undefined' ? __webpack_dev_server_client__ : // eslint-disable-next-line import/no-unresolved
11
- require('./clients/WebsocketClient');
12
11
  var retries = 0;
13
12
  var client = null;
14
13
 
@@ -37,12 +36,12 @@ var socket = function initSocket(url, handlers) {
37
36
  }
38
37
  });
39
38
  client.onMessage(function (data) {
40
- var msg = JSON.parse(data);
39
+ var message = JSON.parse(data);
41
40
 
42
- if (handlers[msg.type]) {
43
- handlers[msg.type](msg.data);
41
+ if (handlers[message.type]) {
42
+ handlers[message.type](message.data);
44
43
  }
45
44
  });
46
45
  };
47
46
 
48
- module.exports = socket;
47
+ export default socket;
@@ -0,0 +1,70 @@
1
+ import url from "url"; // We handle legacy API that is Node.js specific, and a newer API that implements the same WHATWG URL Standard used by web browsers
2
+ // Please look at https://nodejs.org/api/url.html#url_url_strings_and_url_objects
3
+
4
+ function createSocketURL(parsedURL) {
5
+ var hostname = parsedURL.hostname; // Node.js module parses it as `::`
6
+ // `new URL(urlString, [baseURLstring])` parses it as '[::]'
7
+
8
+ var isInAddrAny = hostname === "0.0.0.0" || hostname === "::" || hostname === "[::]"; // why do we need this check?
9
+ // hostname n/a for file protocol (example, when using electron, ionic)
10
+ // see: https://github.com/webpack/webpack-dev-server/pull/384
11
+
12
+ if (isInAddrAny && self.location.hostname && self.location.protocol.indexOf("http") === 0) {
13
+ hostname = self.location.hostname;
14
+ }
15
+
16
+ var socketURLProtocol = parsedURL.protocol || self.location.protocol; // When https is used in the app, secure web sockets are always necessary because the browser doesn't accept non-secure web sockets.
17
+
18
+ if (socketURLProtocol === "auto:" || hostname && isInAddrAny && self.location.protocol === "https:") {
19
+ socketURLProtocol = self.location.protocol;
20
+ }
21
+
22
+ socketURLProtocol = socketURLProtocol.replace(/^(?:http|.+-extension|file)/i, "ws");
23
+ var socketURLAuth = ""; // `new URL(urlString, [baseURLstring])` doesn't have `auth` property
24
+ // Parse authentication credentials in case we need them
25
+
26
+ if (parsedURL.username) {
27
+ socketURLAuth = parsedURL.username; // Since HTTP basic authentication does not allow empty username,
28
+ // we only include password if the username is not empty.
29
+
30
+ if (parsedURL.password) {
31
+ // Result: <username>:<password>
32
+ socketURLAuth = socketURLAuth.concat(":", parsedURL.password);
33
+ }
34
+ } // In case the host is a raw IPv6 address, it can be enclosed in
35
+ // the brackets as the brackets are needed in the final URL string.
36
+ // Need to remove those as url.format blindly adds its own set of brackets
37
+ // if the host string contains colons. That would lead to non-working
38
+ // double brackets (e.g. [[::]]) host
39
+ //
40
+ // All of these web socket url params are optionally passed in through resourceQuery,
41
+ // so we need to fall back to the default if they are not provided
42
+
43
+
44
+ var socketURLHostname = (hostname || self.location.hostname || "localhost").replace(/^\[(.*)\]$/, "$1");
45
+ var socketURLPort = parsedURL.port;
46
+
47
+ if (!socketURLPort || socketURLPort === "0") {
48
+ socketURLPort = self.location.port;
49
+ } // If path is provided it'll be passed in via the resourceQuery as a
50
+ // query param so it has to be parsed out of the querystring in order for the
51
+ // client to open the socket to the correct location.
52
+
53
+
54
+ var socketURLPathname = "/ws";
55
+
56
+ if (parsedURL.pathname && !parsedURL.fromCurrentScript) {
57
+ socketURLPathname = parsedURL.pathname;
58
+ }
59
+
60
+ return url.format({
61
+ protocol: socketURLProtocol,
62
+ auth: socketURLAuth,
63
+ hostname: socketURLHostname,
64
+ port: socketURLPort,
65
+ pathname: socketURLPathname,
66
+ slashes: true
67
+ });
68
+ }
69
+
70
+ export default createSocketURL;
@@ -1,22 +1,23 @@
1
- 'use strict';
2
-
3
1
  function getCurrentScriptSource() {
4
2
  // `document.currentScript` is the most accurate way to find the current script,
5
3
  // but is not supported in all browsers.
6
4
  if (document.currentScript) {
7
- return document.currentScript.getAttribute('src');
8
- } // Fall back to getting all scripts in the document.
5
+ return document.currentScript.getAttribute("src");
6
+ } // Fallback to getting all scripts running in the document.
9
7
 
10
8
 
11
9
  var scriptElements = document.scripts || [];
12
- var currentScript = scriptElements[scriptElements.length - 1];
10
+ var scriptElementsWithSrc = Array.prototype.filter.call(scriptElements, function (element) {
11
+ return element.getAttribute("src");
12
+ });
13
13
 
14
- if (currentScript) {
15
- return currentScript.getAttribute('src');
14
+ if (scriptElementsWithSrc.length > 0) {
15
+ var currentScript = scriptElementsWithSrc[scriptElementsWithSrc.length - 1];
16
+ return currentScript.getAttribute("src");
16
17
  } // Fail as there was no script to use.
17
18
 
18
19
 
19
- throw new Error('[webpack-dev-server] Failed to get current script source.');
20
+ throw new Error("[webpack-dev-server] Failed to get current script source.");
20
21
  }
21
22
 
22
- module.exports = getCurrentScriptSource;
23
+ export default getCurrentScriptSource;
@@ -1,11 +1,8 @@
1
- 'use strict';
2
-
3
- var logger = require('../modules/logger');
4
-
5
- var name = 'webpack-dev-server'; // default level is set on the client side, so it does not need
1
+ import logger from "../modules/logger/index.js";
2
+ var name = "webpack-dev-server"; // default level is set on the client side, so it does not need
6
3
  // to be set by the CLI or API
7
4
 
8
- var defaultLevel = 'info';
5
+ var defaultLevel = "info";
9
6
 
10
7
  function setLogLevel(level) {
11
8
  logger.configureDefaultLogger({
@@ -14,7 +11,5 @@ function setLogLevel(level) {
14
11
  }
15
12
 
16
13
  setLogLevel(defaultLevel);
17
- module.exports = {
18
- log: logger.getLogger(name),
19
- setLogLevel: setLogLevel
20
- };
14
+ var log = logger.getLogger(name);
15
+ export { log, setLogLevel };
@@ -0,0 +1,43 @@
1
+ import url from "url";
2
+ import getCurrentScriptSource from "./getCurrentScriptSource.js";
3
+
4
+ function parseURL(resourceQuery) {
5
+ var options = {};
6
+
7
+ if (typeof resourceQuery === "string" && resourceQuery !== "") {
8
+ var searchParams = resourceQuery.substr(1).split("&");
9
+
10
+ for (var i = 0; i < searchParams.length; i++) {
11
+ var pair = searchParams[i].split("=");
12
+ options[pair[0]] = decodeURIComponent(pair[1]);
13
+ }
14
+ } else {
15
+ // Else, get the url from the <script> this file was called with.
16
+ var scriptSource = getCurrentScriptSource();
17
+
18
+ if (scriptSource) {
19
+ var scriptSourceURL;
20
+
21
+ try {
22
+ // The placeholder `baseURL` with `window.location.href`,
23
+ // is to allow parsing of path-relative or protocol-relative URLs,
24
+ // and will have no effect if `scriptSource` is a fully valid URL.
25
+ scriptSourceURL = new URL(scriptSource, self.location.href);
26
+ } catch (error) {// URL parsing failed, do nothing.
27
+ // We will still proceed to see if we can recover using `resourceQuery`
28
+ }
29
+
30
+ if (scriptSourceURL) {
31
+ options = scriptSourceURL;
32
+ options.fromCurrentScript = true;
33
+ }
34
+ } else {
35
+ options = url.parse(self.location.href, true, true);
36
+ options.fromCurrentScript = true;
37
+ }
38
+ }
39
+
40
+ return options;
41
+ }
42
+
43
+ export default parseURL;
@@ -1,54 +1,67 @@
1
- 'use strict';
1
+ /* global __webpack_hash__ */
2
+ import hotEmitter from "webpack/hot/emitter.js";
3
+ import { log } from "./log.js";
2
4
 
3
- var _require = require('./log'),
4
- log = _require.log;
5
-
6
- function reloadApp(_ref, _ref2) {
7
- var hotReload = _ref.hotReload,
8
- hot = _ref.hot,
5
+ function reloadApp(_ref, status) {
6
+ var hot = _ref.hot,
9
7
  liveReload = _ref.liveReload;
10
- var isUnloading = _ref2.isUnloading,
11
- currentHash = _ref2.currentHash;
12
8
 
13
- if (isUnloading || !hotReload) {
9
+ if (status.isUnloading) {
10
+ return;
11
+ } // TODO Workaround for webpack v4, `__webpack_hash__` is not replaced without HotModuleReplacement plugin
12
+
13
+
14
+ var webpackHash = // eslint-disable-next-line camelcase
15
+ typeof __webpack_hash__ !== "undefined" ? // eslint-disable-next-line camelcase
16
+ __webpack_hash__ : status.previousHash || "";
17
+ var isInitial = status.currentHash.indexOf(webpackHash) === 0;
18
+
19
+ if (isInitial) {
20
+ var isLegacyInitial = webpackHash === "" && hot === false && liveReload === true;
21
+
22
+ if (isLegacyInitial) {
23
+ status.previousHash = status.currentHash;
24
+ }
25
+
14
26
  return;
15
27
  }
16
28
 
17
- if (hot) {
18
- log.info('App hot update...');
29
+ function applyReload(rootWindow, intervalId) {
30
+ clearInterval(intervalId);
31
+ log.info("App updated. Reloading...");
32
+ rootWindow.location.reload();
33
+ }
19
34
 
20
- var hotEmitter = require('webpack/hot/emitter');
35
+ var search = self.location.search.toLowerCase();
36
+ var allowToHot = search.indexOf("webpack-dev-server-hot=false") === -1;
37
+ var allowToLiveReload = search.indexOf("webpack-dev-server-live-reload=false") === -1;
21
38
 
22
- hotEmitter.emit('webpackHotUpdate', currentHash);
39
+ if (hot && allowToHot) {
40
+ log.info("App hot update...");
41
+ hotEmitter.emit("webpackHotUpdate", status.currentHash);
23
42
 
24
- if (typeof self !== 'undefined' && self.window) {
43
+ if (typeof self !== "undefined" && self.window) {
25
44
  // broadcast update to window
26
- self.postMessage("webpackHotUpdate".concat(currentHash), '*');
45
+ self.postMessage("webpackHotUpdate".concat(status.currentHash), "*");
27
46
  }
28
47
  } // allow refreshing the page only if liveReload isn't disabled
29
- else if (liveReload) {
30
- var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
48
+ else if (liveReload && allowToLiveReload) {
49
+ var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
31
50
 
32
- var intervalId = self.setInterval(function () {
33
- if (rootWindow.location.protocol !== 'about:') {
34
- // reload immediately if protocol is valid
35
- applyReload(rootWindow, intervalId);
36
- } else {
37
- rootWindow = rootWindow.parent;
51
+ var intervalId = self.setInterval(function () {
52
+ if (rootWindow.location.protocol !== "about:") {
53
+ // reload immediately if protocol is valid
54
+ applyReload(rootWindow, intervalId);
55
+ } else {
56
+ rootWindow = rootWindow.parent;
38
57
 
39
- if (rootWindow.parent === rootWindow) {
40
- // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
41
- applyReload(rootWindow, intervalId);
42
- }
58
+ if (rootWindow.parent === rootWindow) {
59
+ // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
60
+ applyReload(rootWindow, intervalId);
43
61
  }
44
- });
45
- }
46
-
47
- function applyReload(rootWindow, intervalId) {
48
- clearInterval(intervalId);
49
- log.info('App updated. Reloading...');
50
- rootWindow.location.reload();
62
+ }
63
+ });
51
64
  }
52
65
  }
53
66
 
54
- module.exports = reloadApp;
67
+ export default reloadApp;
@@ -1,14 +1,12 @@
1
- 'use strict';
2
1
  /* global __resourceQuery WorkerGlobalScope */
3
2
  // Send messages to the outside, so plugins can consume it.
4
-
5
3
  function sendMsg(type, data) {
6
- if (typeof self !== 'undefined' && (typeof WorkerGlobalScope === 'undefined' || !(self instanceof WorkerGlobalScope))) {
4
+ if (typeof self !== "undefined" && (typeof WorkerGlobalScope === "undefined" || !(self instanceof WorkerGlobalScope))) {
7
5
  self.postMessage({
8
6
  type: "webpack".concat(type),
9
7
  data: data
10
- }, '*');
8
+ }, "*");
11
9
  }
12
10
  }
13
11
 
14
- module.exports = sendMsg;
12
+ export default sendMsg;