-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
125 lines (106 loc) · 3.36 KB
/
main.js
File metadata and controls
125 lines (106 loc) · 3.36 KB
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
import './style.css';
import { Elm } from './src/Main.elm'
// client side
if (import.meta.hot) {
class ElmErrorOverlay extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
}
onContentChanged(html) {
this.shadowRoot.querySelector('.elm-error').innerHTML = html
}
connectedCallback() {
this.shadowRoot.innerHTML = `
<style>
.elm-error__background {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 0.5;
background: black;
}
.elm-error {
position: fixed;
top: 50%;
left: 50%;
transform: translate(calc(-50% + 0.5px), calc(-50% + 0.5px));
background: linear-gradient(#333, #303030);
color: white;
font-weight: 400;
font-family: Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace;
padding: 2rem;
border-radius: 0.25rem;
box-shadow: 0 1rem 1rem rgba(0, 0, 0, 0.125);
border-top: solid 0.5rem indianred;
max-height: calc(100vh - 8rem);
overflow: auto;
max-width: 100vw;
box-sizing: border-box;
}
</style>
<div class="elm-error__background"></div>
<div class="elm-error"></div>
`
}
}
import.meta.hot.on('elm:error', (data) => {
if (!customElements.get('elm-error-overlay')) {
customElements.define('elm-error-overlay', ElmErrorOverlay)
}
let existingOverlay = document.querySelector('elm-error-overlay')
if (existingOverlay) {
existingOverlay.onContentChanged(data.error)
} else {
document.body.innerHTML += '<elm-error-overlay></elm-error-overlay>'
document.querySelector('elm-error-overlay').onContentChanged(data.error)
}
})
import.meta.hot.on('elm:success', () => {
let existingOverlay = document.querySelector('elm-error-overlay')
if (existingOverlay) {
existingOverlay.remove()
}
})
}
let startApp = ({ Interop }) => {
// Grab environment variables, but remove the "ELM_LAND_" prefix
let env = Object.keys(import.meta.env).reduce((env, key) => {
if (key.startsWith('ELM_LAND_')) {
env[key.slice('ELM_LAND_'.length)] = import.meta.env[key]
}
return env
}, {})
let flags = undefined
if (Interop.flags) {
flags = Interop.flags({ env })
}
if (Elm && Elm.Main && Elm.Main.init) {
let app = Elm.Main.init({
node: document.getElementById('app'),
flags
})
if (Interop.onReady) {
Interop.onReady({ app, env })
}
}
if (import.meta.env.DEV) {
import.meta.hot.send('elm:client-ready')
}
}
try {
// Attempt to find "interop.ts" file
let interopFiles = import.meta.glob('./src/interop.ts', { eager: true })
startApp({ Interop: interopFiles['./src/interop.ts'] })
} catch (_) {
try {
// Attempt to find "interop.js" file
let interopFiles = import.meta.glob('./src/interop.js', { eager: true })
startApp({ Interop: interopFiles['./src/interop.js'] })
} catch (_) {
// Run application without an interop file
startApp({ Interop: {} })
}
}