From ac0647381d51d46d91a995a1c8a993ca667c0418 Mon Sep 17 00:00:00 2001 From: harlan Date: Wed, 3 Jul 2024 14:40:33 +1000 Subject: [PATCH] chore: better minification --- src/runtime/nitro-plugin.ts | 7 ++--- src/runtime/template/global.mjs | 45 +-------------------------------- src/runtime/template/replay.mjs | 14 +--------- src/template/global.mjs | 39 ++++++++++++++++++++++++++++ src/template/replay.mjs | 8 ++++++ 5 files changed, 51 insertions(+), 62 deletions(-) create mode 100644 src/template/global.mjs create mode 100644 src/template/replay.mjs diff --git a/src/runtime/nitro-plugin.ts b/src/runtime/nitro-plugin.ts index dfb9291..cc265f8 100644 --- a/src/runtime/nitro-plugin.ts +++ b/src/runtime/nitro-plugin.ts @@ -64,13 +64,10 @@ export default defineNitroPlugin((nitro) => { // insert the hydration API, maybe insert delay script htmlContext.bodyAppend.push(``) +`.replace(/\s+/g, ' ').replace(/[\n\r]/g, '')) }) }) diff --git a/src/runtime/template/global.mjs b/src/runtime/template/global.mjs index 7e3aa0a..b3b11fc 100644 --- a/src/runtime/template/global.mjs +++ b/src/runtime/template/global.mjs @@ -1,44 +1 @@ -if (!('requestIdleCallback' in w) || !('requestAnimationFrame' in w)) - return new Promise(resolve => resolve('not supported')) -function eventListeners() { - const c = new AbortController() - const p = new Promise((resolve) => { - const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(',') - function handler(e) { - hydrateOnEvents.forEach(e => w.removeEventListener(e, handler)) - requestAnimationFrame(() => resolve(e)) - } - hydrateOnEvents.forEach((e) => { - w.addEventListener(e, handler, { - capture: true, - once: true, - passive: true, - signal: c.signal, - }) - }) - }) - - return { c: () => c.abort(), p } -} - -function idleListener() { - let id - const p = new Promise((resolve) => { - const isMobile = w.innerWidth < 640 - const timeout = isMobile ? Number.parseInt('<%= options.postIdleTimeout.mobile %>') : Number.parseInt('<%= options.postIdleTimeout.desktop %>') - const timeoutDelay = () => setTimeout( - () => requestAnimationFrame(() => resolve('timeout')), - timeout, - ) - id = w.requestIdleCallback(timeoutDelay, { timeout: Number.parseInt('<%= options.idleCallbackTimeout %>') }) - }) - return { c: () => window.cancelIdleCallback(id), p } -} -const triggers = [idleListener(), eventListeners()] -const hydrationPromise = Promise.race( - triggers.map(t => t.p), -).finally(() => { - triggers.forEach(t => t.c()) -}) - -return hydrationPromise +function eventListeners() { const c = new AbortController(); const p = new Promise((resolve) => { const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(','); function handler(e) { hydrateOnEvents.forEach(e => w.removeEventListener(e, handler)); requestAnimationFrame(() => resolve(e)) }hydrateOnEvents.forEach((e) => { w.addEventListener(e, handler, { capture: true, once: true, passive: true, signal: c.signal }) }) }); return { c: () => c.abort(), p } } function idleListener() { let id; const p = new Promise((resolve) => { const isMobile = w.innerWidth < 640; const timeout = isMobile ? Number.parseInt('<%= options.postIdleTimeout.mobile %>') : Number.parseInt('<%= options.postIdleTimeout.desktop %>'); const timeoutDelay = () => setTimeout(() => requestAnimationFrame(() => resolve('timeout')), timeout); id = w.requestIdleCallback(timeoutDelay, { timeout: Number.parseInt('<%= options.idleCallbackTimeout %>') }) }); return { c: () => window.cancelIdleCallback(id), p } } const triggers = [idleListener(), eventListeners()]; const hydrationPromise = Promise.race(triggers.map(t => t.p)).finally(() => { triggers.forEach(t => t.c()) }) diff --git a/src/runtime/template/replay.mjs b/src/runtime/template/replay.mjs index b836d76..bbc2fea 100644 --- a/src/runtime/template/replay.mjs +++ b/src/runtime/template/replay.mjs @@ -1,13 +1 @@ -(() => { - w._$delayHydration.then((e) => { - if (!(e instanceof PointerEvent) && !(e instanceof MouseEvent) && !(window.TouchEvent && e instanceof TouchEvent)) - return - - if (e instanceof MouseEvent && e.type !== 'click') - return - setTimeout(() => - w.requestIdleCallback( - () => setTimeout(() => e.target?.click(), 500), - ), 50) - }) -})() +(() => { w._$delayHydration.then((e) => { if (!(e instanceof PointerEvent) && !(e instanceof MouseEvent) && !(window.TouchEvent && e instanceof TouchEvent) || e instanceof MouseEvent && e.type !== 'click') { return }setTimeout(() => w.requestIdleCallback(() => setTimeout(() => e.target && e.target.click(), 500)), 50) }) })() diff --git a/src/template/global.mjs b/src/template/global.mjs new file mode 100644 index 0000000..ea0a092 --- /dev/null +++ b/src/template/global.mjs @@ -0,0 +1,39 @@ +function eventListeners() { + const c = new AbortController() + const p = new Promise((resolve) => { + const hydrateOnEvents = '<%= options.hydrateOnEvents %>'.split(',') + function handler(e) { + hydrateOnEvents.forEach(e => w.removeEventListener(e, handler)) + requestAnimationFrame(() => resolve(e)) + } + hydrateOnEvents.forEach((e) => { + w.addEventListener(e, handler, { + capture: true, + once: true, + passive: true, + signal: c.signal, + }) + }) + }) + return { c: () => c.abort(), p } +} + +function idleListener() { + let id + const p = new Promise((resolve) => { + const isMobile = w.innerWidth < 640 + const timeout = isMobile ? Number.parseInt('<%= options.postIdleTimeout.mobile %>') : Number.parseInt('<%= options.postIdleTimeout.desktop %>') + const timeoutDelay = () => setTimeout( + () => requestAnimationFrame(() => resolve('timeout')), + timeout, + ) + id = w.requestIdleCallback(timeoutDelay, { timeout: Number.parseInt('<%= options.idleCallbackTimeout %>') }) + }) + return { c: () => window.cancelIdleCallback(id), p } +} +const triggers = [idleListener(), eventListeners()] +const hydrationPromise = Promise.race( + triggers.map(t => t.p), +).finally(() => { + triggers.forEach(t => t.c()) +}) diff --git a/src/template/replay.mjs b/src/template/replay.mjs new file mode 100644 index 0000000..2076398 --- /dev/null +++ b/src/template/replay.mjs @@ -0,0 +1,8 @@ +(() => { + w._$delayHydration.then((e) => { + if ((!(e instanceof PointerEvent) && !(e instanceof MouseEvent) && !(window.TouchEvent && e instanceof TouchEvent)) || e instanceof MouseEvent && e.type !== 'click') { + return + } + setTimeout(() => w.requestIdleCallback(() => setTimeout(() => e.target && e.target.click(), 500)), 50) + }) +})()