webpack-dev-server 2.5.0 → 2.7.1

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