-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.js
203 lines (162 loc) · 4.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
/* global __resourceQuery WorkerGlobalScope */
const webpackHotLog = require('webpack/hot/log');
const stripAnsi = require('./modules/strip-ansi');
const parseURL = require('./utils/parseURL');
const socket = require('./socket');
const overlay = require('./overlay');
const { log, setLogLevel } = require('./utils/log');
const sendMessage = require('./utils/sendMessage');
const reloadApp = require('./utils/reloadApp');
const createSocketURL = require('./utils/createSocketURL');
const status = {
isUnloading: false,
currentHash: '',
};
const defaultOptions = {
hot: false,
hotReload: true,
liveReload: false,
initial: true,
progress: false,
overlay: false,
};
const parsedResourceQuery = parseURL(__resourceQuery);
const options = defaultOptions;
// Handle Node.js legacy format and `new URL()`
if (parsedResourceQuery.query) {
Object.assign(options, parsedResourceQuery.query);
} else if (parsedResourceQuery.searchParams) {
const paramsToObject = (entries) => {
const result = {};
for (const [key, value] of entries) {
result[key] = value;
}
return result;
};
Object.assign(
options,
paramsToObject(parsedResourceQuery.searchParams.entries())
);
}
const socketURL = createSocketURL(parsedResourceQuery);
function setAllLogLevel(level) {
// This is needed because the HMR logger operate separately from dev server logger
webpackHotLog.setLogLevel(level);
setLogLevel(level);
}
if (options.logging) {
setAllLogLevel(options.logging);
}
self.addEventListener('beforeunload', () => {
status.isUnloading = true;
});
if (typeof window !== 'undefined') {
const qs = window.location.search.toLowerCase();
options.hotReload = qs.indexOf('hotreload=false') === -1;
}
const onSocketMessage = {
hot() {
options.hot = true;
log.info('Hot Module Replacement enabled.');
},
liveReload() {
options.liveReload = true;
log.info('Live Reloading enabled.');
},
invalid() {
log.info('App updated. Recompiling...');
// Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
if (options.overlay) {
overlay.clear();
}
sendMessage('Invalid');
},
hash(hash) {
status.currentHash = hash;
},
logging: setAllLogLevel,
overlay(value) {
if (typeof document === 'undefined') {
return;
}
options.overlay = value;
},
progress(progress) {
options.progress = progress;
},
'progress-update': function progressUpdate(data) {
if (options.progress) {
log.info(`${data.percent}% - ${data.msg}.`);
}
sendMessage('Progress', data);
},
'still-ok': function stillOk() {
log.info('Nothing changed.');
if (options.overlay) {
overlay.clear();
}
sendMessage('StillOk');
},
ok() {
sendMessage('Ok');
if (options.overlay) {
overlay.clear();
}
if (options.initial) {
return (options.initial = false);
}
reloadApp(options, status);
},
'content-changed': function contentChanged() {
log.info('Content base changed. Reloading...');
self.location.reload();
},
warnings(warnings) {
log.warn('Warnings while compiling.');
const strippedWarnings = warnings.map((warning) =>
stripAnsi(warning.message ? warning.message : warning)
);
sendMessage('Warnings', strippedWarnings);
for (let i = 0; i < strippedWarnings.length; i++) {
log.warn(strippedWarnings[i]);
}
const needShowOverlay =
typeof options.overlay === 'boolean'
? options.overlay
: options.overlay && options.overlay.warnings;
if (needShowOverlay) {
overlay.showMessage(warnings);
}
if (options.initial) {
return (options.initial = false);
}
reloadApp(options, status);
},
errors(errors) {
log.error('Errors while compiling. Reload prevented.');
const strippedErrors = errors.map((error) =>
stripAnsi(error.message ? error.message : error)
);
sendMessage('Errors', strippedErrors);
for (let i = 0; i < strippedErrors.length; i++) {
log.error(strippedErrors[i]);
}
const needShowOverlay =
typeof options.overlay === 'boolean'
? options.overlay
: options.overlay && options.overlay.errors;
if (needShowOverlay) {
overlay.showMessage(errors);
}
options.initial = false;
},
error(error) {
log.error(error);
},
close() {
log.error('Disconnected!');
sendMessage('Close');
},
};
socket(socketURL, onSocketMessage);