forked from vaadin/vaadin-development-mode-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-development-mode-detector.html
110 lines (94 loc) · 2.77 KB
/
vaadin-development-mode-detector.html
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
<script>
(function() {
const DEV_MODE_CODE_REGEXP =
/\/\*\*\s+vaadin-dev-mode:start([\s\S]*)vaadin-dev-mode:end\s+\*\*\//i;
const FlowClients = window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.clients;
function isMinified() {
function test() {
/** vaadin-dev-mode:start
return false;
vaadin-dev-mode:end **/
return true;
}
return uncommentAndRun(test);
}
function isDevelopmentMode() {
try {
if (isForceDevelopmentMode()) {
return true;
}
if (!isLocalhost()) {
return false;
}
if (FlowClients) {
return !isFlowProductionMode();
}
return !isMinified();
} catch (e) {
// Some error in this code, assume production so no further actions will be taken
return false;
}
}
function isForcedDevelopmentMode() {
return localStorage.getItem("vaadin.developmentmode.force");
}
function isLocalhost() {
return (["localhost","127.0.0.1"].indexOf(window.location.hostname) >= 0);
}
function isFlowProductionMode() {
if (FlowClients) {
const productionModeApps = Object.keys(FlowClients)
.map(key => FlowClients[key])
.filter(client => client.productionMode);
if (productionModeApps.length > 0) {
return true;
}
}
return false;
}
function uncommentAndRun(callback, args) {
if (typeof callback !== 'function') {
return;
}
const match = DEV_MODE_CODE_REGEXP.exec(callback.toString());
if (match) {
try {
// requires CSP: script-src 'unsafe-eval'
callback = new Function(match[1]);
} catch (e) {
// eat the exception
console.log('vaadin-development-mode-detector: uncommentAndRun() failed', e)
}
}
return callback(args);
}
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
// A guard against polymer-modulizer removing the window.Vaadin
// initialization above.
window['Vaadin'] = window['Vaadin'] || {};
/**
* Inspects the source code of the given `callback` function for
* specially-marked _commented_ code. If such commented code is found in the
* callback source, uncomments and runs that code instead of the callback
* itself. Otherwise runs the callback as is.
*
* The optional arguments are passed into the callback / uncommented code,
* the result is returned.
*
* See the `isMinified()` function source code in this file for an example.
*
* @memberof Vaadin
*/
window.Vaadin.runIfDevelopmentMode = function(callback, args) {
if (window.Vaadin.developmentMode) {
return uncommentAndRun(callback, args);
}
};
if (window.Vaadin.developmentMode === undefined) {
window.Vaadin.developmentMode = isDevelopmentMode();
}
})();
</script>