chore: implement basic devServer

This commit is contained in:
Igor Randjelovic
2021-03-26 22:16:36 +01:00
parent bb23bca3ce
commit 75e6009b8a
7 changed files with 198 additions and 58 deletions

View File

@@ -3,7 +3,13 @@
// todo: log correct message format for CLI to pick up
// todo: build CLI service to listen for state changes
// ---
import { Http } from '@nativescript/core'
import type { IHMRStatusData } from "../../bin/devServer";
import { Http, Device } from '@nativescript/core'
const uuid = Device.uuid;
console.log(`[HMR] uuid = ${uuid}`)
let __NS_DEV_HOST_URL__;
Promise.race(__NS_DEV_HOST_IPS__
@@ -11,7 +17,7 @@ Promise.race(__NS_DEV_HOST_IPS__
.map(async url => {
await Http.request({
method: 'get',
url
url: url + 'ping'
})
return url;
@@ -19,47 +25,63 @@ Promise.race(__NS_DEV_HOST_IPS__
__NS_DEV_HOST_URL__ = winner
})
if(module.hot) {
let __SEQ = 0;
if (module.hot) {
module.hot.dispose(() => {
console.log('Disposing entry file?!')
// require('@nativescript/core').Application.resetRootView()
})
const orig = global.__onLiveSync
const log = (type, info) => {
console.log(`[nds] HMR ${type}:`, info)
// console.log(__NS_DEV_HOST_IPS__[0])
if(__NS_DEV_HOST_URL__) {
const send = (content: object) => {
if (__NS_DEV_HOST_URL__) {
Http.request({
method: 'post',
url: __NS_DEV_HOST_URL__,
content: JSON.stringify({
type,
info
})
content: JSON.stringify(content)
}).catch(err => {
console.log(err)
})
}
}
const sendStatus = (status, hash) => {
send({
seq: __SEQ++,
uuid,
hash,
status
} as IHMRStatusData)
}
const orig = global.__onLiveSync
const log = (type, info) => {
// console.log(`[nds] HMR ${type}:`, info)
}
log('init')
module.hot.addStatusHandler(status => {
log('status', status)
// sendStatus(status)
})
global.__onLiveSync = async function () {
// handle hot updated on LiveSync
console.log('~~~ livesynced ~~~')
// handle hot updates on LiveSync
console.log('~~~ livesync ~~~')
log('checking')
const hash = __webpack_require__.h();
await module.hot.check().catch(err => {
log('checking-failed', err)
sendStatus('failure', hash)
});
log('checked')
log('applying')
await module.hot.apply({
ignoreUnaccepted: false,
ignoreDeclined: false,
@@ -67,9 +89,11 @@ if(module.hot) {
onDeclined(info) {
log('declined', info)
sendStatus('failure', hash);
},
onUnaccepted(info) {
log('unaccepted', info)
sendStatus('failure', hash);
},
onAccepted(info) {
log('accepted', info)
@@ -79,10 +103,15 @@ if(module.hot) {
},
onErrored(info) {
log('errored', info)
sendStatus('failure', hash);
}
}).then(() => {
sendStatus('success', hash)
}).catch((err) => {
sendStatus('failure', hash)
log('applying-failed', err)
})
// log('applying')
// await module.hot.apply()
log('applying-done')