| 1 |
--- |
| 2 |
// 1. Configuration |
| 3 |
import pkg from '../../../package.json'; |
| 4 |
const CDN_BASE = `https://cdn.jsdelivr.net/npm/htmx.org@${pkg.version}/dist`; |
| 5 |
|
| 6 |
const EXTENSION_LIST = [ |
| 7 |
{name: 'websockets', comingSoon: true}, |
| 8 |
{name: 'webrtc', comingSoon: true}, |
| 9 |
]; |
| 10 |
|
| 11 |
// 2. Utility: Fetch file size (Server Side) |
| 12 |
async function getFileSize(url: string) { |
| 13 |
try { |
| 14 |
const res = await fetch(url, {method: 'HEAD'}); |
| 15 |
const bytes = parseInt(res.headers.get('content-length') || '0', 10); |
| 16 |
return bytes ? parseFloat((bytes / 1024).toFixed(1)) : 0; |
| 17 |
} catch (e) { |
| 18 |
return 0; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// 3. Execution: Parallel Fetching |
| 23 |
const [coreMin, coreFull] = await Promise.all([ |
| 24 |
getFileSize(`${CDN_BASE}/htmx.min.js`), |
| 25 |
getFileSize(`${CDN_BASE}/htmx.js`) |
| 26 |
]); |
| 27 |
|
| 28 |
const extensions = await Promise.all(EXTENSION_LIST.map(async (ext) => { |
| 29 |
// Skip fetching for "Coming Soon" items |
| 30 |
if (ext.comingSoon) return {...ext, size: 0, full: 0}; |
| 31 |
|
| 32 |
return { |
| 33 |
...ext, |
| 34 |
size: await getFileSize(`${CDN_BASE}/ext/${ext.name}.min.js`), |
| 35 |
full: await getFileSize(`${CDN_BASE}/ext/${ext.name}.js`) |
| 36 |
}; |
| 37 |
})); |
| 38 |
--- |
| 39 |
|
| 40 |
<section class="not-prose py-12 sm:py-16 font-sans"> |
| 41 |
|
| 42 |
<template id="script-tag"><span class='text-blue-700 dark:text-blue-350'><script</span> <span |
| 43 |
class='text-blue-800 dark:text-blue-200'>src</span>=<span |
| 44 |
class='text-red-700 dark:text-orange-300'>"{'${unescaped url}'}"</span><span |
| 45 |
class='text-blue-700 dark:text-blue-350'>></script></span></template> |
| 46 |
|
| 47 |
<div id="cdn-widget" |
| 48 |
class="max-w-[755px] mx-auto w-full px-6 flex flex-col gap-2" |
| 49 |
_={`init |
| 50 |
set :base to '${CDN_BASE}' |
| 51 |
set :minSize to ${coreMin} |
| 52 |
set :fullSize to ${coreFull} |
| 53 |
send updateCode to me |
| 54 |
|
| 55 |
on updateCode |
| 56 |
set min to #min.checked |
| 57 |
if min then set extStr to '.min' else set extStr to '' end |
| 58 |
if min then set total to :minSize else set total to :fullSize end |
| 59 |
set exts to <input.ext:checked/> |
| 60 |
|
| 61 |
-- Build core script tag |
| 62 |
render #script-tag with (url: :base + '/htmx' + extStr + '.js') |
| 63 |
set html to it |
| 64 |
|
| 65 |
-- Add extension script tags |
| 66 |
repeat for e in exts |
| 67 |
if min |
| 68 |
increment total by (e.dataset.min as Number) |
| 69 |
else |
| 70 |
increment total by (e.dataset.full as Number) |
| 71 |
end |
| 72 |
render #script-tag with (url: :base + '/ext/' + e.value + extStr + '.js') |
| 73 |
set html to html + '\n' + it |
| 74 |
end |
| 75 |
|
| 76 |
-- Render output |
| 77 |
put html into #code |
| 78 |
put total.toFixed(1) into #total |
| 79 |
|
| 80 |
-- Update dropdown label |
| 81 |
if exts.length > 0 |
| 82 |
put exts.length + ' added' into #count-lbl |
| 83 |
add .text-neutral-900 .dark:text-white to #count-lbl |
| 84 |
else |
| 85 |
put 'add extensions' into #count-lbl |
| 86 |
remove .text-neutral-900 .dark:text-white from #count-lbl |
| 87 |
end |
| 88 |
`}> |
| 89 |
|
| 90 |
<div class="border border-neutral-200 dark:border-neutral-800 rounded-[4px] bg-white dark:bg-neutral-950 shadow-xs"> |
| 91 |
<div class="flex gap-1.5 px-3 py-2 bg-neutral-50 dark:bg-neutral-920 border-b border-neutral-100 dark:border-neutral-900 rounded-t-[3px]"> |
| 92 |
{[0, 0, 0].map(() => |
| 93 |
<div class="size-2.5 rounded-full bg-neutral-200 dark:bg-neutral-800"/>)} |
| 94 |
</div> |
| 95 |
<div class="group relative pe-4 flex items-center gap-3"> |
| 96 |
<pre class="flex-1 overflow-x-auto text-xs leading-5 p-0! border-transparent! bg-transparent! text-neutral-900 dark:text-white"><code |
| 97 |
id="code" class="whitespace-pre block"></code></pre> |
| 98 |
|
| 99 |
<button class="relative text-neutral-400 interact:text-neutral-900 dark:interact:text-white active:scale-95 cursor-pointer" |
| 100 |
_={`on click |
| 101 |
writeText(#code.innerText) on navigator.clipboard |
| 102 |
add .hidden to .icon-\[radix-icons--copy\] in me |
| 103 |
remove .hidden from .check in me |
| 104 |
wait 2s |
| 105 |
remove .hidden from .icon-\[radix-icons--copy\] in me |
| 106 |
add .hidden to .check in me`}> |
| 107 |
<i class="icon-[radix-icons--copy] size-4"></i> |
| 108 |
<i class="icon-[radix-icons--check] size-4 hidden check text-green-600"></i> |
| 109 |
</button> |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
|
| 113 |
<div class="flex flex-col sm:flex-row gap-4 justify-between text-xs select-none relative px-1"> |
| 114 |
<div class="flex items-center gap-5"> |
| 115 |
|
| 116 |
<div class="relative" _="on click from elsewhere add .hidden to #dropdown"> |
| 117 |
<button class="flex items-center gap-1.5 text-neutral-600 dark:text-neutral-400 interact:text-black dark:interact:text-white cursor-pointer" |
| 118 |
_="on click toggle .hidden on #dropdown"> |
| 119 |
<i class="icon-[radix-icons--plus]"></i> |
| 120 |
<span class="font-chicago text-[0.625rem]" id="count-lbl">add extensions</span> |
| 121 |
</button> |
| 122 |
|
| 123 |
<div id="dropdown" |
| 124 |
class="hidden absolute top-full left-0 mt-1 min-w-[200px] bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 divide-y divide-neutral-75 dark:divide-neutral-800 font-chicago rounded-[4px] shadow-xs p-3 z-20 flex flex-col"> |
| 125 |
{extensions.map((e, i) => ( |
| 126 |
<label class={` |
| 127 |
group flex items-center justify-between p-1.5 first:pt-0 last:pb-0 rounded-[2px] select-none |
| 128 |
${e.comingSoon |
| 129 |
? 'opacity-60 cursor-not-allowed' |
| 130 |
: 'cursor-pointer interact:bg-blue-700 interact:text-white text-neutral-600 dark:text-neutral-400' |
| 131 |
} |
| 132 |
`}> |
| 133 |
<div class="flex items-center gap-2"> |
| 134 |
<div class={`relative size-3 rounded-[2px] border ${e.comingSoon ? 'border-neutral-300 dark:border-neutral-700 bg-neutral-100 dark:bg-neutral-800' : 'border-neutral-300 dark:border-neutral-600 bg-neutral-50 dark:bg-neutral-900 group-interact:bg-blue-500 group-interact:border-blue-600'}`}> |
| 135 |
|
| 136 |
<input type="checkbox" |
| 137 |
value={e.name} |
| 138 |
disabled={e.comingSoon} |
| 139 |
class="peer ext appearance-none absolute inset-0 cursor-pointer" |
| 140 |
data-min={e.size} |
| 141 |
data-full={e.full} |
| 142 |
_={e.comingSoon ? '' : "on change trigger updateCode"} |
| 143 |
/> |
| 144 |
<i class="icon-[radix-icons--check] absolute inset-0 m-auto size-2.5 text-neutral-900 dark:text-white group-interact:text-white opacity-0 peer-checked:opacity-100"></i> |
| 145 |
</div> |
| 146 |
<span class="text-[0.625rem]">{e.name}</span> |
| 147 |
</div> |
| 148 |
|
| 149 |
{e.comingSoon ? ( |
| 150 |
<span class="text-[9px] font-medium font-chicago uppercase tracking-wider text-amber-600 dark:text-amber-400 bg-amber-100 dark:bg-amber-400/10 px-1.5 py-0.25 rounded-[2px]"> |
| 151 |
Soon |
| 152 |
</span> |
| 153 |
) : ( |
| 154 |
<span class="font-mono text-[10px] text-neutral-400 group-interact:text-blue-100"> |
| 155 |
{e.size}K |
| 156 |
</span> |
| 157 |
)} |
| 158 |
</label> |
| 159 |
))} |
| 160 |
</div> |
| 161 |
</div> |
| 162 |
|
| 163 |
<label class="flex items-center gap-2 cursor-pointer group text-neutral-600 dark:text-neutral-400 interact:text-black dark:interact:text-white"> |
| 164 |
<div class="relative size-3.5 border border-neutral-300 dark:border-neutral-700 rounded-[3px] group-interact:border-neutral-400 bg-white dark:bg-neutral-950"> |
| 165 |
|
| 166 |
<input type="checkbox" id="min" checked |
| 167 |
class="peer appearance-none absolute inset-0 cursor-pointer" |
| 168 |
_="on change trigger updateCode"/> |
| 169 |
|
| 170 |
<i class="icon-[radix-icons--check] size-3.5 absolute -left-px text-neutral-900 dark:text-white opacity-0 peer-checked:opacity-100 pointer-events-none"></i> |
| 171 |
</div> |
| 172 |
<span class="font-chicago text-[0.625rem]">minify</span> |
| 173 |
</label> |
| 174 |
</div> |
| 175 |
|
| 176 |
<span class="font-mono text-neutral-400 dark:text-neutral-600 text-[11px]">~<span id="total" |
| 177 |
class="font-bold text-neutral-600 dark:text-neutral-400">0</span> |
| 178 |
KB</span> |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
</section> |