#!/usr/bin/env node // pnpm build:starter — packs reference/ into the starter bundle shipped in the tarball // (dist/starter/template.json.gz): the kept files, the manifest and the lockfile without the plugin. // Runs in prepublishOnly after `pnpm build`; fails when the reference site breaks a manifest rule. import { execFileSync } from 'node:child_process' import { mkdirSync, statSync, writeFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' const root = join(dirname(fileURLToPath(import.meta.url)), '..') const { bundleFlags, collectBundle, packBundle } = await import(pathToFileURL(join(root, 'dist', 'exports', 'starter.js')).href) const bundle = collectBundle(join(root, 'reference')) // provenance: the plugin commit this bundle was built from, so a generated site can be traced back (`-dirty` = uncommitted changes) const git = (args) => { try { return execFileSync('git', args, { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim() } catch { return '' } } const head = git(['rev-parse', 'HEAD']) bundle.source = { commit: head ? `${head}${git(['status', '--porcelain']) ? '-dirty' : ''}` : null } const out = join(root, 'dist', 'starter', 'template.json.gz') mkdirSync(dirname(out), { recursive: true }) writeFileSync(out, packBundle(bundle)) const raw = Object.values(bundle.files).reduce((sum, text) => sum + Buffer.byteLength(text), 0) + Buffer.byteLength(bundle.lockfile) console.log(`starter bundle: ${Object.keys(bundle.files).length} files + lockfile, ${(raw / 1024).toFixed(0)} KB raw → ${(statSync(out).size / 1024).toFixed(0)} KB gzipped (${out.slice(root.length + 1)})`) console.log(`flags used by the reference site: ${bundleFlags(bundle).join(', ')}`) console.log(`source: ${bundle.source.commit ?? 'not a Git checkout'}`)