Skip to content
← Public packages

@kentcdodds/cloudflare-connect-personal-software-ecosystem

I Built Your Personal Software Ecosystem: Kent C. Dodds's Cloudflare Connect 2026 lightning talk deck (Reveal.js). How Kody gives every user one of everything on Cloudflare.

FollowForkOpen app

Files

build.mjs

118 lines · 7.4 KB · JavaScript
// Builds one fully self-contained Reveal.js page from src/deck.html + src/slides/*.html
// (file-name order), grouped into Reveal vertical stacks by src/slides/groups.json. Reveal.js and its notes plugin come from the pinned npm package
// (node_modules/reveal.js) and are inlined with the fonts, images, CSS and JS.
// Writes dist/index.html (the offline build) and app/assets.generated.ts (served by the app).
// Fails on an em dash (U+2014) anywhere in the source or the output, and on banned words in
// slide text and notes.
import { readdirSync, readFileSync, writeFileSync, mkdirSync, statSync } from 'node:fs'
import { join, extname, relative } from 'node:path'

const root = new URL('..', import.meta.url).pathname
const src = join(root, 'src')
const types = { '.png': 'image/png', '.svg': 'image/svg+xml', '.woff2': 'font/woff2', '.ico': 'image/x-icon', '.webp': 'image/webp', '.jpg': 'image/jpeg' }
const EM_DASH = '\u2014'

// Every text file we own: src/, scripts/, app/router.ts, docs, package.json.
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')}`)

// c/<path> → data: URI, so the page never requests anything.
const dataUri = (rel) => {
	const file = join(src, 'c', rel)
	const type = types[extname(file)]
	if (!type) throw new Error(`No content type for ${rel}`)
	return `data:${type};base64,${readFileSync(file).toString('base64')}`
}

// Rewrites `@scope { a, b { ... } }` into rules prefixed with the slide's selector,
// so slide CSS stays isolated in browsers without @scope support.
function scopeCss(css, sel) {
	return css.replace(/@scope\s*\{([\s\S]*)\}\s*$/m, (_, body) =>
		body.replace(/([^{}]+)\{([^{}]*)\}/g, (_, selectors, decls) => {
			const out = selectors.split(',').map((s) => s.trim()).filter(Boolean).map((s) =>
				s.startsWith(':scope') ? sel + s.slice(6) : `${sel} ${s}`,
			)
			return `\n${out.join(', ')} {${decls}}`
		}),
	)
}

// Slide text rules: present tense, no "used to / now / switched / removed / we moved".
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 slideFiles = readdirSync(join(src, 'slides')).filter((f) => f.endsWith('.html')).sort()
const labels = new Set()
const wordProblems = []
const slideById = new Map()
for (const f of slideFiles) {
	const html = readFileSync(join(src, 'slides', f), 'utf8').trim()
	const label = html.match(/^<section id="([\w-]+)"/)?.[1]
	if (!label) throw new Error(`${f}: must start with <section id="...">`)
	if (labels.has(label)) throw new Error(`${f}: duplicate label ${label}`)
	labels.add(label)
	const hit = visibleText(html).match(banned)
	if (hit) wordProblems.push(`${f}: "${hit[0]}"`)
	// Descendant (not child) selector: slides sit inside their vertical stack's <section>.
	slideById.set(label, html.replace(/<style>([\s\S]*?)<\/style>/g, (_, css) => `<style>${scopeCss(css, `.reveal .slides section#${label}`)}</style>`))
}

// Vertical stacks: each group becomes an outer <section> holding its slides, like the source deck.
// Every slide is in exactly one group, and the groups read in file-name order, so the linear
// order (Space / N, slide numbers) matches the files.
const { groups } = JSON.parse(readFileSync(join(src, 'slides/groups.json'), 'utf8'))
const grouped = groups.flat()
const fileOrder = [...labels]
if (grouped.length !== new Set(grouped).size) throw new Error('groups.json lists a slide twice')
for (const id of fileOrder) if (!grouped.includes(id)) throw new Error(`groups.json is missing slide ${id}`)
for (const id of grouped) if (!labels.has(id)) throw new Error(`groups.json names unknown slide ${id}`)
if (grouped.join() !== fileOrder.join()) throw new Error('groups.json order must match src/slides file-name order')
const slidesHtml = groups.map((ids) => `<section>\n${ids.map((id) => slideById.get(id)).join('\n')}\n</section>`).join('\n')
if (wordProblems.length) throw new Error(`banned words in slide text or notes (keep it present tense):\n${wordProblems.join('\n')}`)

// `vendor:<path>` reads from node_modules (pinned in package.json); other names from src/c/.
const readAsset = (f) => (f.startsWith('vendor:') ? readFileSync(join(root, 'node_modules', f.slice(7)), 'utf8') : readFileSync(join(src, 'c', f), 'utf8'))
// Inlined JS must not close the <script> element early (the notes plugin carries HTML).
// The notes plugin's bundled markdown parser has a literal em dash inside a string; write it as the
// \u2014 escape so the page carries no U+2014 character and behaves the same.
const inlineJs = (t) => t.replace(/<\/script/gi, '<\\/script').replace(/^\/\/# sourceMappingURL=.*$/gm, '').replaceAll(EM_DASH, '\\u2014')
const inlineCss = (t) => t.replace(/^\/\*# sourceMappingURL=.*$/gm, '')
let page = readFileSync(join(src, 'deck.html'), 'utf8')
	.replace(/<link rel="stylesheet" href="([\w.:/-]+)">/g, (_, f) => `<style>\n${inlineCss(readAsset(f))}\n</style>`)
	.replace(/<script src="([\w.:/-]+)"><\/script>/g, (_, f) => `<script>\n${inlineJs(readAsset(f))}\n</script>`)
	.replace('<!-- slides -->', () => slidesHtml)
page = page
	.replace(/url\('fonts\/([\w.-]+)'\)/g, (_, f) => `url('${dataUri('fonts/' + f)}')`)
	.replace(/url\((['"]?)c\/([\w./-]+)\1\)/g, (_, q, p) => `url('${dataUri(p)}')`)
	.replace(/(src|href)="c\/([\w./-]+)"/g, (_, attr, p) => `${attr}="${dataUri(p)}"`)
// Check the markup and CSS (inline scripts are vendored code; the verify step blocks network anyway).
// Plain https links on <a> are navigation the presenter clicks, not resources the page loads.
const markup = page.replace(/<script>[\s\S]*?<\/script>/g, '').replace(/<a ([^>]*?)href="https:\/\/[^"]+"/g, '<a $1')
const bad = markup.match(/(?:src|href)="(?!data:|#)[^"]*"|url\((?!['"]?data:)[^)]*\)/)
if (bad) throw new Error(`page still references an external or relative resource: ${bad[0].slice(0, 120)}`)
if (page.includes(EM_DASH)) throw new Error('dist/index.html contains an em dash (U+2014), probably from a vendored file; fix it at the source')

mkdirSync(join(root, 'dist'), { recursive: true })
writeFileSync(join(root, 'dist/index.html'), page)
const assets = {
	'index.html': { type: 'text/html; charset=utf-8', text: page },
	'favicon.ico': { type: 'image/x-icon', base64: readFileSync(join(src, 'c/icons/favicon.ico')).toString('base64') },
}
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 = ${slideFiles.length}

export const assets: Record<string, Asset> = ${JSON.stringify(assets, null, '\t')}
`)
console.log(`Composed ${slideFiles.length} slides in ${groups.length} vertical stacks (${groups.map((g) => g.join(' / ')).join(' | ')}). dist/index.html is ${(page.length / 1024).toFixed(0)} KiB.`)