From 0a05548c15b517ace07819051418adfcb283b983 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 26 Jun 2023 16:39:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96HMR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 14 ++++---------- lib/dev.js | 5 ++--- lib/utils.js | 5 ++++- lib/ws.js | 19 ++++++++++++------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index ca0927f..299a0f9 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -408,22 +408,16 @@ function render(_ctx, _cache) { /** * 解析模板html */ -export function parseHtml( - html, - { page, imports, entry, LEGACY_MODE, session } -) { +export function parseHtml(html, { page, imports, entry, LEGACY_MODE }) { return html .replace(/\r\n/g, '\n') .replace( '', `${ process.env.NODE_ENV === 'development' - ? ` \n` + ? ` \n` : '' }` ) diff --git a/lib/dev.js b/lib/dev.js index 1bc2b4b..b4aa41b 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -7,7 +7,7 @@ import socket from './ws.js' import chokidar from 'chokidar' import { red } from 'kolorist' -import { friendlyErrors, defaultCustomElement, md5, gzip } from './utils.js' +import { friendlyErrors, defaultCustomElement, gzip } from './utils.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' @@ -183,8 +183,7 @@ export default async function createServer(root = '', conf = {}) { page, imports: conf.imports, entry, - LEGACY_MODE, - session: md5(page.entry) + LEGACY_MODE }) } diff --git a/lib/utils.js b/lib/utils.js index b2c8808..34645ab 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -39,7 +39,7 @@ export function friendlyErrors(pathname, ext = '') { export function createHmrScript(legacy, session = '') { return ` !(function vue_live_hmr(){ - var ws = new WebSocket(\`ws\${location.protocol === 'https:' ? 's' : ''}://\${location.host}/ws-fite-hmr?session=${session}\`) + var ws = new WebSocket(\`ws\${location.protocol === 'https:' ? 's' : ''}://\${location.host}/ws-fite-hmr?session=\${btoa(location.pathname).replace(/[=\+\/]/g, '')}&lock=\${localStorage.getItem(location.pathname) || 0}\`) ws.addEventListener('open', function (r) { if(vue_live_hmr.closed){ @@ -51,6 +51,9 @@ export function createHmrScript(legacy, session = '') { ws.addEventListener('close', function(){ vue_live_hmr.closed = true + if (localStorage.getItem(location.pathname) === '1') { + return + } setTimeout(vue_live_hmr, 2000) }) diff --git a/lib/ws.js b/lib/ws.js index 208f45f..73c3b85 100644 --- a/lib/ws.js +++ b/lib/ws.js @@ -15,16 +15,21 @@ class WebSocket { conn.on('connection', (client, req) => { let params = new URLSearchParams(req.url.slice(req.url.indexOf('?'))) let session = params.get('session') + let lock = +params.get('lock') - this.#clients.set(session, client) + if (lock === 1) { + client.close() + } else { + this.#clients.set(session, client) - client.once('close', _ => { - this.#clients.delete(session) - }) + client.once('close', _ => { + this.#clients.delete(session) + }) - while (this.#queue.length) { - let msg = this.#queue.shift() - this.send(msg) + while (this.#queue.length) { + let msg = this.#queue.shift() + this.send(msg) + } } }) }