#!/usr/bin/env node // Builds the published package in dist/: the toolkit compiled to JavaScript with its declarations, the declaration of // the font plugin's virtual module, README.md, CHANGELOG.md and a package.json whose exports name the compiled files. import { spawnSync } from 'node:child_process'; import { copyFileSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; const ROOT = import.meta.dirname; const DIST = join(ROOT, 'dist'); const ENTRIES = ['server', 'web', 'vite']; function fail(message) { console.error(`build: ${message}`); process.exit(1); } rmSync(DIST, { recursive: true, force: true }); const tsc = spawnSync(join(ROOT, 'node_modules', '.bin', 'tsc'), ['-p', join(ROOT, 'tsconfig.build.json')], { stdio: 'inherit' }); if (tsc.error) fail(`tsc: ${tsc.error.message}; run npm ci first`); if (tsc.status !== 0) process.exit(tsc.status ?? 1); // The declarations import the other declarations by their .js names, as the compiled JavaScript does. for (const entry of ENTRIES) { for (const file of readdirSync(join(DIST, entry)).filter((name) => name.endsWith('.d.ts'))) { const path = join(DIST, entry, file); writeFileSync(path, readFileSync(path, 'utf8').replace(/(from\s+|import\s+)(['"])(\.{1,2}\/[^'"]+)\.tsx?\2/g, '$1$2$3.js$2')); } } // The compiled renderApp keeps its import of virtual:font, so its declarations reference the module's declaration. copyFileSync(join(ROOT, 'web', 'virtual-font.d.ts'), join(DIST, 'web', 'virtual-font.d.ts')); const renderApp = join(DIST, 'web', 'renderApp.d.ts'); writeFileSync(renderApp, `/// \n${readFileSync(renderApp, 'utf8')}`); for (const file of ['README.md', 'CHANGELOG.md']) copyFileSync(join(ROOT, file), join(DIST, file)); const source = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8')); const published = { name: source.name, version: source.version, description: source.description, type: source.type, engines: source.engines, repository: source.repository, exports: Object.fromEntries(ENTRIES.map((entry) => [`./${entry}`, { types: `./${entry}/index.d.ts`, default: `./${entry}/index.js` }])), peerDependencies: source.peerDependencies, peerDependenciesMeta: source.peerDependenciesMeta, }; writeFileSync(join(DIST, 'package.json'), `${JSON.stringify(published, null, 2)}\n`); console.log(`${published.name}@${published.version} in ${DIST}`);