From b7da9d573fc4e0008f6d675706a0916c3fcbb04c Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Fri, 26 Mar 2021 22:58:49 +0100 Subject: [PATCH] chore: cleanup --- packages/webpack5/src/bin/devServer.ts | 127 ++++++++---------- packages/webpack5/src/bin/index.ts | 4 - .../nativescript-hot-loader/hmr.runtime.ts | 109 +++------------ 3 files changed, 71 insertions(+), 169 deletions(-) diff --git a/packages/webpack5/src/bin/devServer.ts b/packages/webpack5/src/bin/devServer.ts index e60854243..22dd01e44 100644 --- a/packages/webpack5/src/bin/devServer.ts +++ b/packages/webpack5/src/bin/devServer.ts @@ -1,71 +1,56 @@ -import { createServer } from 'http' -// import { spawn } from 'child_process' -// yarn --cwd /Users/rigor789/Code/echo-server start - -// let statuses: { -// [hash: string]: IHMRStatusData -// } = {} - -export interface IHMRStatusData { - seq: number - uuid: string, - hash: string - status: string -} - -export function run() { - createServer((req, res) => { - if (req.url === '/ping') { - console.log('PING -> PONG!') - return res.end("Pong."); - } - - if (req.method !== 'POST') { - res.statusCode = 400; - return res.end("Unsupported method."); - } - - let data = ""; - req.on("data", chunk => { - data += chunk; - }); - - req.on("end", () => { - try { - const signal = JSON.parse(data) as IHMRStatusData; - // if (!statuses[signal.hash] || statuses[signal.hash].seq < signal.seq) { - // statuses[signal.hash] = signal - // } - if (process.send) { - process.send({ - type: 'hmr-status', - version: 1, - hash: signal.hash, - data: signal - }, (error) => { - if (error) { - console.error(`Process Send Error: `, error); - } - - return null; - }); - } - - res.end('ok.'); - } catch (e) { - res.statusCode = 400; - res.end("Invalid JSON."); - } - }); - }).listen(8238) - - // spawn('/Users/rigor789/Code/echo-server/node_modules/.bin/ts-node', ['/Users/rigor789/Code/echo-server/index.ts'], { - // cwd: '/Users/rigor789/Code/echo-server/', - // stdio: ['inherit', 'inherit', 'inherit', 'ipc'], - // }).on('message', function (data) { - // console.log({ - // messageFromEchoServer: data - // }); - // }); -} - +// import { createServer } from 'http' +// +// export interface IHMRStatusData { +// seq: number +// uuid: string, +// hash: string +// status: string +// } +// +// export function run() { +// createServer((req, res) => { +// if (req.url === '/ping') { +// console.log('PING -> PONG!') +// return res.end("Pong."); +// } +// +// if (req.method !== 'POST') { +// res.statusCode = 400; +// return res.end("Unsupported method."); +// } +// +// let data = ""; +// req.on("data", chunk => { +// data += chunk; +// }); +// +// req.on("end", () => { +// try { +// const signal = JSON.parse(data) as IHMRStatusData; +// // if (!statuses[signal.hash] || statuses[signal.hash].seq < signal.seq) { +// // statuses[signal.hash] = signal +// // } +// if (process.send) { +// process.send({ +// type: 'hmr-status', +// version: 1, +// hash: signal.hash, +// data: signal +// }, (error) => { +// if (error) { +// console.error(`Process Send Error: `, error); +// } +// +// return null; +// }); +// } +// +// res.end('ok.'); +// } catch (e) { +// res.statusCode = 400; +// res.end("Invalid JSON."); +// } +// }); +// }).listen(8238) +// } +// diff --git a/packages/webpack5/src/bin/index.ts b/packages/webpack5/src/bin/index.ts index 6710cf255..852d0cbd2 100644 --- a/packages/webpack5/src/bin/index.ts +++ b/packages/webpack5/src/bin/index.ts @@ -8,7 +8,6 @@ import path from 'path'; import fs from 'fs'; import { parseEnvFlags } from '../cli/parseEnvFlags'; -import { run } from "./devServer"; const defaultConfig = path.resolve( __dirname, @@ -108,9 +107,6 @@ program } if (options.watch) { - // run dev server - run(); - console.log('webpack is watching the files...') compiler.watch( configuration.watchOptions ?? {}, diff --git a/packages/webpack5/src/loaders/nativescript-hot-loader/hmr.runtime.ts b/packages/webpack5/src/loaders/nativescript-hot-loader/hmr.runtime.ts index cb1e9d26e..7a5e8a020 100644 --- a/packages/webpack5/src/loaders/nativescript-hot-loader/hmr.runtime.ts +++ b/packages/webpack5/src/loaders/nativescript-hot-loader/hmr.runtime.ts @@ -1,125 +1,46 @@ // @ts-nocheck // This is a runtime module - included by nativescript-hot-loader -// todo: log correct message format for CLI to pick up -// todo: build CLI service to listen for state changes +// this file should not include external dependencies // --- -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__ - .map(ip => `http://${ip}:8238/`) - .map(async url => { - await Http.request({ - method: 'get', - url: url + 'ping' - }) - - return url; - })).then(winner => { - __NS_DEV_HOST_URL__ = winner -}) - -let __SEQ = 0; - if (module.hot) { - module.hot.dispose(() => { - console.log('Disposing entry file?!') - // require('@nativescript/core').Application.resetRootView() - }) - - const send = (content: object) => { - if (__NS_DEV_HOST_URL__) { - Http.request({ - method: 'post', - url: __NS_DEV_HOST_URL__, - content: JSON.stringify(content) - }).catch(err => { - console.log(err) - }) - } + const setStatus = (hash: string, status: 'success' | 'failure') => { + // format is important - CLI expects this exact format + console.log(`[HMR][${hash}] ${status}`) } - - 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) - }) - + const originalOnLiveSync = global.__onLiveSync global.__onLiveSync = async function () { - // 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) + setStatus(hash, 'failure') }); - - log('checked') - log('applying') - await module.hot.apply({ ignoreUnaccepted: false, ignoreDeclined: false, ignoreErrored: false, - onDeclined(info) { - log('declined', info) - sendStatus('failure', hash); + setStatus(hash, 'failure') }, onUnaccepted(info) { - log('unaccepted', info) - sendStatus('failure', hash); + setStatus(hash, 'failure') }, onAccepted(info) { - log('accepted', info) + // console.log('accepted', info) }, onDisposed(info) { - log('disposed', info) + // console.log('disposed', info) }, onErrored(info) { - log('errored', info) - sendStatus('failure', hash); + setStatus(hash, 'failure') } }).then(() => { - sendStatus('success', hash) + setStatus(hash, 'success') }).catch((err) => { - sendStatus('failure', hash) - log('applying-failed', err) + setStatus(hash, 'failure') }) - // log('applying') - // await module.hot.apply() - log('applying-done') - // await module.hot.apply() setTimeout(() => { - orig(); - }); + originalOnLiveSync(); + }) }; - - // global.__onLiveSync() }