webpack-dev-server 2.4.2 → 2.5.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.
package/client/index.js CHANGED
@@ -1,181 +1,188 @@
1
- /* global __resourceQuery */
2
- var url = require("url");
3
- var stripAnsi = require("strip-ansi");
4
- var socket = require("./socket");
5
- var overlay = require("./overlay");
6
-
7
- function getCurrentScriptSource() {
8
- // `document.currentScript` is the most accurate way to find the current script,
9
- // but is not supported in all browsers.
10
- if(document.currentScript)
11
- return document.currentScript.getAttribute("src");
12
- // Fall back to getting all scripts in the document.
13
- var scriptElements = document.scripts || [];
14
- var currentScript = scriptElements[scriptElements.length - 1];
15
- if(currentScript)
16
- return currentScript.getAttribute("src");
17
- // Fail as there was no script to use.
18
- throw new Error("[WDS] Failed to get current script source");
19
- }
20
-
21
- var urlParts;
22
- if(typeof __resourceQuery === "string" && __resourceQuery) {
23
- // If this bundle is inlined, use the resource query to get the correct url.
24
- urlParts = url.parse(__resourceQuery.substr(1));
25
- } else {
26
- // Else, get the url from the <script> this file was called with.
27
- var scriptHost = getCurrentScriptSource();
28
- scriptHost = scriptHost.replace(/\/[^\/]+$/, "");
29
- urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true);
30
- }
31
-
32
- var hot = false;
33
- var initial = true;
34
- var currentHash = "";
35
- var logLevel = "info";
36
- var useWarningOverlay = false;
37
- var useErrorOverlay = false;
38
-
39
- function log(level, msg) {
40
- if(logLevel === "info" && level === "info")
41
- return console.log(msg);
42
- if(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning")
43
- return console.warn(msg);
44
- if(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error")
45
- return console.error(msg);
46
- }
47
-
48
- // Send messages to the outside, so plugins can consume it.
49
- function sendMsg(type, data) {
50
- if(typeof self !== "undefined" && self.window) {
51
- self.postMessage({
52
- type: "webpack" + type,
53
- data: data
54
- }, "*");
55
- }
56
- }
57
-
58
- var onSocketMsg = {
59
- hot: function() {
60
- hot = true;
61
- log("info", "[WDS] Hot Module Replacement enabled.");
62
- },
63
- invalid: function() {
64
- log("info", "[WDS] App updated. Recompiling...");
65
- sendMsg("Invalid");
66
- },
67
- hash: function(hash) {
68
- currentHash = hash;
69
- },
70
- "still-ok": function() {
71
- log("info", "[WDS] Nothing changed.")
72
- if(useWarningOverlay || useErrorOverlay) overlay.clear();
73
- sendMsg("StillOk");
74
- },
75
- "log-level": function(level) {
76
- logLevel = level;
77
- },
78
- "overlay": function(overlay) {
79
- if(typeof document !== "undefined") {
80
- if(typeof(overlay) === "boolean") {
81
- useWarningOverlay = overlay;
82
- useErrorOverlay = overlay;
83
- } else if(overlay) {
84
- useWarningOverlay = overlay.warnings;
85
- useErrorOverlay = overlay.errors;
86
- }
87
- }
88
- },
89
- ok: function() {
90
- sendMsg("Ok");
91
- if(useWarningOverlay || useErrorOverlay) overlay.clear();
92
- if(initial) return initial = false;
93
- reloadApp();
94
- },
95
- "content-changed": function() {
96
- log("info", "[WDS] Content base changed. Reloading...")
97
- self.location.reload();
98
- },
99
- warnings: function(warnings) {
100
- log("info", "[WDS] Warnings while compiling.");
101
- var strippedWarnings = warnings.map(function(warning) {
102
- return stripAnsi(warning);
103
- });
104
- sendMsg("Warnings", strippedWarnings);
105
- for(var i = 0; i < strippedWarnings.length; i++)
106
- console.warn(strippedWarnings[i]);
107
- if(useWarningOverlay) overlay.showMessage(warnings);
108
-
109
- if(initial) return initial = false;
110
- reloadApp();
111
- },
112
- errors: function(errors) {
113
- log("info", "[WDS] Errors while compiling. Reload prevented.");
114
- var strippedErrors = errors.map(function(error) {
115
- return stripAnsi(error);
116
- });
117
- sendMsg("Errors", strippedErrors);
118
- for(var i = 0; i < strippedErrors.length; i++)
119
- console.error(strippedErrors[i]);
120
- if(useErrorOverlay) overlay.showMessage(errors);
121
- },
122
- close: function() {
123
- log("error", "[WDS] Disconnected!");
124
- sendMsg("Close");
125
- }
126
- };
127
-
128
- var hostname = urlParts.hostname;
129
- var protocol = urlParts.protocol;
130
-
131
-
132
- //check ipv4 and ipv6 `all hostname`
133
- if(hostname === "0.0.0.0" || hostname === "::") {
134
- // why do we need this check?
135
- // hostname n/a for file protocol (example, when using electron, ionic)
136
- // see: https://github.com/webpack/webpack-dev-server/pull/384
137
- if(self.location.hostname && !!~self.location.protocol.indexOf("http")) {
138
- hostname = self.location.hostname;
139
- }
140
- }
141
-
142
- // `hostname` can be empty when the script path is relative. In that case, specifying
143
- // a protocol would result in an invalid URL.
144
- // When https is used in the app, secure websockets are always necessary
145
- // because the browser doesn't accept non-secure websockets.
146
- if(hostname && (self.location.protocol === "https:" || urlParts.hostname === "0.0.0.0")) {
147
- protocol = self.location.protocol;
148
- }
149
-
150
- var socketUrl = url.format({
151
- protocol: protocol,
152
- auth: urlParts.auth,
153
- hostname: hostname,
154
- port: (urlParts.port === "0") ? self.location.port : urlParts.port,
155
- pathname: urlParts.path == null || urlParts.path === "/" ? "/sockjs-node" : urlParts.path
156
- });
157
-
158
- socket(socketUrl, onSocketMsg);
159
-
160
- var isUnloading = false;
161
- self.addEventListener("beforeunload", function() {
162
- isUnloading = true;
163
- });
164
-
165
- function reloadApp() {
166
- if(isUnloading) {
167
- return;
168
- }
169
- if(hot) {
170
- log("info", "[WDS] App hot update...");
171
- var hotEmitter = require("webpack/hot/emitter");
172
- hotEmitter.emit("webpackHotUpdate", currentHash);
173
- if(typeof self !== "undefined" && self.window) {
174
- // broadcast update to window
175
- self.postMessage("webpackHotUpdate" + currentHash, "*");
176
- }
177
- } else {
178
- log("info", "[WDS] App updated. Reloading...");
179
- self.location.reload();
180
- }
181
- }
1
+ /* global __resourceQuery WorkerGlobalScope */
2
+ var url = require("url");
3
+ var stripAnsi = require("strip-ansi");
4
+ var socket = require("./socket");
5
+ var overlay = require("./overlay");
6
+
7
+ function getCurrentScriptSource() {
8
+ // `document.currentScript` is the most accurate way to find the current script,
9
+ // but is not supported in all browsers.
10
+ if(document.currentScript)
11
+ return document.currentScript.getAttribute("src");
12
+ // Fall back to getting all scripts in the document.
13
+ var scriptElements = document.scripts || [];
14
+ var currentScript = scriptElements[scriptElements.length - 1];
15
+ if(currentScript)
16
+ return currentScript.getAttribute("src");
17
+ // Fail as there was no script to use.
18
+ throw new Error("[WDS] Failed to get current script source");
19
+ }
20
+
21
+ var urlParts;
22
+ if(typeof __resourceQuery === "string" && __resourceQuery) {
23
+ // If this bundle is inlined, use the resource query to get the correct url.
24
+ urlParts = url.parse(__resourceQuery.substr(1));
25
+ } else {
26
+ // Else, get the url from the <script> this file was called with.
27
+ var scriptHost = getCurrentScriptSource();
28
+ scriptHost = scriptHost.replace(/\/[^\/]+$/, "");
29
+ urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true);
30
+ }
31
+
32
+ var hot = false;
33
+ var initial = true;
34
+ var currentHash = "";
35
+ var logLevel = "info";
36
+ var useWarningOverlay = false;
37
+ var useErrorOverlay = false;
38
+
39
+ function log(level, msg) {
40
+ if(logLevel === "info" && level === "info")
41
+ return console.log(msg);
42
+ if(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning")
43
+ return console.warn(msg);
44
+ if(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error")
45
+ return console.error(msg);
46
+ }
47
+
48
+ // Send messages to the outside, so plugins can consume it.
49
+ function sendMsg(type, data) {
50
+ if(
51
+ typeof self !== "undefined" &&
52
+ (typeof WorkerGlobalScope === "undefined" ||
53
+ !(self instanceof WorkerGlobalScope))
54
+ ) {
55
+ self.postMessage({
56
+ type: "webpack" + type,
57
+ data: data
58
+ }, "*");
59
+ }
60
+ }
61
+
62
+ var onSocketMsg = {
63
+ hot: function() {
64
+ hot = true;
65
+ log("info", "[WDS] Hot Module Replacement enabled.");
66
+ },
67
+ invalid: function() {
68
+ log("info", "[WDS] App updated. Recompiling...");
69
+ sendMsg("Invalid");
70
+ },
71
+ hash: function(hash) {
72
+ currentHash = hash;
73
+ },
74
+ "still-ok": function() {
75
+ log("info", "[WDS] Nothing changed.")
76
+ if(useWarningOverlay || useErrorOverlay) overlay.clear();
77
+ sendMsg("StillOk");
78
+ },
79
+ "log-level": function(level) {
80
+ logLevel = level;
81
+ },
82
+ "overlay": function(overlay) {
83
+ if(typeof document !== "undefined") {
84
+ if(typeof(overlay) === "boolean") {
85
+ useWarningOverlay = overlay;
86
+ useErrorOverlay = overlay;
87
+ } else if(overlay) {
88
+ useWarningOverlay = overlay.warnings;
89
+ useErrorOverlay = overlay.errors;
90
+ }
91
+ }
92
+ },
93
+ ok: function() {
94
+ sendMsg("Ok");
95
+ if(useWarningOverlay || useErrorOverlay) overlay.clear();
96
+ if(initial) return initial = false;
97
+ reloadApp();
98
+ },
99
+ "content-changed": function() {
100
+ log("info", "[WDS] Content base changed. Reloading...")
101
+ self.location.reload();
102
+ },
103
+ warnings: function(warnings) {
104
+ log("info", "[WDS] Warnings while compiling.");
105
+ var strippedWarnings = warnings.map(function(warning) {
106
+ return stripAnsi(warning);
107
+ });
108
+ sendMsg("Warnings", strippedWarnings);
109
+ for(var i = 0; i < strippedWarnings.length; i++)
110
+ log("warning", strippedWarnings[i]);
111
+ if(useWarningOverlay) overlay.showMessage(warnings);
112
+
113
+ if(initial) return initial = false;
114
+ reloadApp();
115
+ },
116
+ errors: function(errors) {
117
+ log("info", "[WDS] Errors while compiling. Reload prevented.");
118
+ var strippedErrors = errors.map(function(error) {
119
+ return stripAnsi(error);
120
+ });
121
+ sendMsg("Errors", strippedErrors);
122
+ for(var i = 0; i < strippedErrors.length; i++)
123
+ log("error", strippedErrors[i]);
124
+ if(useErrorOverlay) overlay.showMessage(errors);
125
+ },
126
+ error: function(error) {
127
+ console.error(error);
128
+ },
129
+ close: function() {
130
+ log("error", "[WDS] Disconnected!");
131
+ sendMsg("Close");
132
+ }
133
+ };
134
+
135
+ var hostname = urlParts.hostname;
136
+ var protocol = urlParts.protocol;
137
+
138
+
139
+ //check ipv4 and ipv6 `all hostname`
140
+ if(hostname === "0.0.0.0" || hostname === "::") {
141
+ // why do we need this check?
142
+ // hostname n/a for file protocol (example, when using electron, ionic)
143
+ // see: https://github.com/webpack/webpack-dev-server/pull/384
144
+ if(self.location.hostname && !!~self.location.protocol.indexOf("http")) {
145
+ hostname = self.location.hostname;
146
+ }
147
+ }
148
+
149
+ // `hostname` can be empty when the script path is relative. In that case, specifying
150
+ // a protocol would result in an invalid URL.
151
+ // When https is used in the app, secure websockets are always necessary
152
+ // because the browser doesn't accept non-secure websockets.
153
+ if(hostname && (self.location.protocol === "https:" || urlParts.hostname === "0.0.0.0")) {
154
+ protocol = self.location.protocol;
155
+ }
156
+
157
+ var socketUrl = url.format({
158
+ protocol: protocol,
159
+ auth: urlParts.auth,
160
+ hostname: hostname,
161
+ port: (urlParts.port === "0") ? self.location.port : urlParts.port,
162
+ pathname: urlParts.path == null || urlParts.path === "/" ? "/sockjs-node" : urlParts.path
163
+ });
164
+
165
+ socket(socketUrl, onSocketMsg);
166
+
167
+ var isUnloading = false;
168
+ self.addEventListener("beforeunload", function() {
169
+ isUnloading = true;
170
+ });
171
+
172
+ function reloadApp() {
173
+ if(isUnloading) {
174
+ return;
175
+ }
176
+ if(hot) {
177
+ log("info", "[WDS] App hot update...");
178
+ var hotEmitter = require("webpack/hot/emitter");
179
+ hotEmitter.emit("webpackHotUpdate", currentHash);
180
+ if(typeof self !== "undefined" && self.window) {
181
+ // broadcast update to window
182
+ self.postMessage("webpackHotUpdate" + currentHash, "*");
183
+ }
184
+ } else {
185
+ log("info", "[WDS] App updated. Reloading...");
186
+ self.location.reload();
187
+ }
188
+ }