Skip to content
← Public packages

@kentcdodds/cloudflare-connect-pse-hyperframes

I Built Your Personal Software Ecosystem: Kent C. Dodds's Cloudflare Connect 2026 talk deck, built with HyperFrames. How Kody gives every user one of everything on Cloudflare.

FollowForkOpen app

Files

build.mjs

155 lines · 9.0 KB · JavaScript
// Builds the deck in three layers:
// 1. composition/index.html: the HyperFrames composition (one scene per src/slides/*.html,
//    in file-name order, plus the slideshow island). Relative asset paths, so
//    `hyperframes lint composition` and `hyperframes present composition` work on it.
// 2. dist/index.html + dist/composition.html: the slideshow wrapper and the composition with
//    every font, image and script inlined, so the deck makes no network requests.
// 3. app/assets.generated.ts: those two pages and the favicon, served by app/router.ts.
// Fails on an em dash (U+2014) anywhere we own or ship, on past-tense words in slide text,
// and on any external or relative resource left in the shipped pages.
import { copyFileSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'
import { extname, join, relative } from 'node:path'
import QRCode from 'qrcode'

const root = new URL('..', import.meta.url).pathname
const src = join(root, 'src')
const comp = join(root, 'composition')
const EM_DASH = '\u2014'
export const DECK_URL = 'https://cloudflare-connect-pse-hyperframes--kentcdodds.kody.run'

const walk = (dir) =>
	readdirSync(dir).flatMap((name) => {
		const p = join(dir, name)
		return statSync(p).isDirectory() ? walk(p) : [p]
	})
const textExt = new Set(['.html', '.css', '.js', '.mjs', '.ts', '.json', '.md', '.svg'])
const owned = [...walk(src), ...walk(join(root, 'scripts')), join(root, 'app/router.ts'), join(root, 'README.md'), join(root, 'AGENTS.md'), join(root, 'package.json')]
const dashHits = owned
	.filter((f) => textExt.has(extname(f)))
	.flatMap((f) => readFileSync(f, 'utf8').split('\n').flatMap((line, i) => (line.includes(EM_DASH) ? [`${relative(root, f)}:${i + 1}`] : [])))
if (dashHits.length) throw new Error(`em dash (U+2014) found, use a comma, colon or period instead:\n${dashHits.join('\n')}`)

// The QR code on the closing slides points at the hosted deck.
mkdirSync(join(comp, 'assets/img'), { recursive: true })
writeFileSync(join(comp, 'assets/img/qr-deck.svg'), await QRCode.toString(DECK_URL, { type: 'svg', margin: 1, errorCorrectionLevel: 'M', color: { dark: '#111111', light: '#ffffff' } }))

// Slides: <section data-id data-label [data-steps]> with an optional <style> and <aside class="notes">.
const banned = /\b(used to|now|switched|removed|we moved)\b/i
const visibleText = (html) =>
	html
		.replace(/<style>[\s\S]*?<\/style>/g, ' ')
		.replace(/<pre[\s\S]*?<\/pre>/g, ' ')
		.replace(/<[^>]+>/g, ' ')
const SCENE_SECONDS = 4
const STEP_SECONDS = 2
const slideFiles = readdirSync(join(src, 'slides')).filter((f) => f.endsWith('.html')).sort()
const seen = new Set()
const problems = []
const styles = []
const scenes = []
const island = { slides: [] }
let clock = 0
for (const file of slideFiles) {
	let html = readFileSync(join(src, 'slides', file), 'utf8').trim()
	const open = html.match(/^<section ([^>]*)>/)
	if (!open) throw new Error(`${file}: must start with <section ...>`)
	const attr = (name) => open[1].match(new RegExp(`${name}="([^"]*)"`))?.[1]
	const id = attr('data-id')
	const label = attr('data-label')
	if (!id || !label) throw new Error(`${file}: needs data-id and data-label`)
	if (seen.has(id)) throw new Error(`${file}: duplicate id ${id}`)
	seen.add(id)
	const steps = Number(attr('data-steps') ?? 1)
	const notes = (html.match(/<aside class="notes">([\s\S]*?)<\/aside>/)?.[1] ?? '').replace(/\s+/g, ' ').trim()
	if (!notes) problems.push(`${file}: no speaker notes`)
	html = html.replace(/\s*<aside class="notes">[\s\S]*?<\/aside>/, '')
	html = html.replace(/\s*<style>([\s\S]*?)<\/style>/g, (_, css) => {
		styles.push(css.replace(/^\t\t/gm, ''))
		return ''
	})
	if (!/class="[^"]*\bconnect\b/.test(open[0])) {
		const pad = (k) => String(k).padStart(2, '0')
		html = html.replace(/<\/section>\s*$/, `\t<p class="folio" aria-hidden="true">${pad(scenes.length + 1)} / ${pad(slideFiles.length)}</p>\n</section>`)
	}
	const hit = visibleText(html).match(banned) || notes.match(banned)
	if (hit) problems.push(`${file}: "${hit[0]}" (keep it present tense)`)
	const duration = steps > 1 ? STEP_SECONDS * steps + 2 : SCENE_SECONDS
	const start = clock
	clock += duration
	const fragments = steps > 1 ? Array.from({ length: steps }, (_, k) => start + 1 + STEP_SECONDS * k) : undefined
	island.slides.push({ sceneId: id, notes, ...(fragments ? { fragments } : {}) })
	const section = html
		.replace(/^<section ([^>]*)>/, (_, a) => `<section id="slide-${id}" ${a.replace(/\s*data-(id|label|steps)="[^"]*"/g, '').trim()} data-start="${start}" data-duration="${duration}" data-track-index="1">`)
		.replace(/class="([^"]*)" data-start/, 'class="clip $1" data-start')
	scenes.push(
		`<div id="scene-${id}" class="scene-frame" data-composition-id="${id}" data-start="${start}" data-duration="${duration}" data-label="${label}" data-width="1920" data-height="1080">\n${section}\n</div>`,
	)
}
if (problems.length) throw new Error(`slide problems:\n${problems.join('\n')}`)
const islandJson = JSON.stringify(island, null, '\t')
const islandTag = `<script type="application/hyperframes-slideshow+json">\n${islandJson}\n</script>`

// Layer 1: the composition.
mkdirSync(join(comp, 'vendor'), { recursive: true })
copyFileSync(join(root, 'node_modules/gsap/dist/gsap.min.js'), join(comp, 'vendor/gsap.min.js'))
const composition = readFileSync(join(src, 'composition.html'), 'utf8')
	.replace('/* deck.css */', () => readFileSync(join(src, 'deck.css'), 'utf8'))
	.replace('<!-- slide styles -->', () => `<style>\n${styles.join('\n')}\n</style>`)
	.replace('<!-- island -->', () => islandTag)
	.replace('<!-- scenes -->', () => `<div id="deck" data-composition-id="deck" data-start="0" data-duration="${clock}" data-width="1920" data-height="1080">\n${scenes.join('\n')}\n</div>`)
	.replace('/* flows.js */', () => readFileSync(join(src, 'flows.js'), 'utf8'))
	.replace('/* runtime.js */', () => readFileSync(join(src, 'runtime.js'), 'utf8'))
writeFileSync(join(comp, 'index.html'), composition)

// Layer 2: self-contained pages.
const types = { '.png': 'image/png', '.svg': 'image/svg+xml', '.woff2': 'font/woff2', '.ico': 'image/x-icon', '.webp': 'image/webp' }
const dataUri = (rel) => {
	const file = join(comp, rel)
	const type = types[extname(file)]
	if (!type) throw new Error(`No content type for ${rel}`)
	return `data:${type};base64,${readFileSync(file).toString('base64')}`
}
// Inlined JS must not close its <script> early; vendored bundles may carry an em dash in a
// string or comment, which ships as the \u2014 escape instead.
const inlineJs = (t) => t.replace(/<\/script/gi, '<\\/script').replace(/^\/\/# sourceMappingURL=.*$/gm, '').replaceAll(EM_DASH, '\\u2014')
const inlineAssets = (html) =>
	html
		.replace(/url\((['"]?)(assets\/[\w./-]+)\1\)/g, (_, q, p) => `url('${dataUri(p)}')`)
		.replace(/(src|href)="(assets\/[\w./-]+)"/g, (_, a, p) => `${a}="${dataUri(p)}"`)
const distComposition = inlineAssets(composition).replace(/<script src="([\w./-]+)"><\/script>/g, (_, f) => `<script>\n${inlineJs(readFileSync(join(comp, f), 'utf8'))}\n</script>`)
const playerDist = join(root, 'node_modules/@hyperframes/player/dist')
const wrapper = readFileSync(join(src, 'index.html'), 'utf8')
	.replace('/* player.js */', () => inlineJs(readFileSync(join(playerDist, 'hyperframes-player.global.js'), 'utf8')))
	.replace('/* slideshow.js */', () => inlineJs(readFileSync(join(playerDist, 'slideshow/hyperframes-slideshow.global.js'), 'utf8')))
	.replace('<!-- island -->', () => islandTag)
	.replace('href="favicon.ico"', `href="data:image/png;base64,${readFileSync(join(src, 'icons/favicon-32x32.png')).toString('base64')}"`)

for (const [name, page] of [['index.html', wrapper], ['composition.html', distComposition]]) {
	const markup = page.replace(/<script>[\s\S]*?<\/script>/g, '').replace(/<a ([^>]*?)href="https:\/\/[^"]+"/g, '<a $1')
	const bad = markup.match(/(?:src|href)="(?!data:|#|composition\.html)[^"]*"|url\((?!['"]?data:)[^)]*\)/)
	if (bad) throw new Error(`${name} still references an external or relative resource: ${bad[0].slice(0, 120)}`)
	if (page.includes(EM_DASH)) throw new Error(`${name} contains an em dash (U+2014)`)
}
mkdirSync(join(root, 'dist'), { recursive: true })
writeFileSync(join(root, 'dist/index.html'), wrapper)
writeFileSync(join(root, 'dist/composition.html'), distComposition)

// Layer 3: the app's assets.
const assets = {
	'index.html': { type: 'text/html; charset=utf-8', text: wrapper },
	'composition.html': { type: 'text/html; charset=utf-8', text: distComposition },
	'favicon.ico': { type: 'image/x-icon', base64: readFileSync(join(src, 'icons/favicon.ico')).toString('base64') },
}
mkdirSync(join(root, 'app'), { recursive: true })
writeFileSync(
	join(root, 'app/assets.generated.ts'),
	`// Generated by scripts/build.mjs. Do not edit by hand.
export type Asset = { type: string; text?: string; base64?: string }

export const slideCount = ${scenes.length}

export const assets: Record<string, Asset> = ${JSON.stringify(assets, null, '\t')}
`,
)
const kib = (s) => (Buffer.byteLength(s) / 1024).toFixed(0)
console.log(`Built ${scenes.length} slides (${clock}s timeline). dist/index.html ${kib(wrapper)} KiB, dist/composition.html ${kib(distComposition)} KiB.`)