Files
cereale/scripts/build-bundle.mjs
T
Claude b4f0657d09 📦 feat: add cereale/min, and a verified framework guide
**cereale/min** — the library flattened into one minified ES module,
25.5 KB / 8.6 KB gzipped, for import maps, <script type="module">, Deno
and Workers. Built from dist/esm/index.js, so the decorator lowering and
the ES2025 target are tsc's; esbuild only flattens and minifies.

It is an addition rather than a replacement, and the measurement is the
reason. Bundled through esbuild the flat and per-module builds land within
2 bytes of each other; through rollup + terser the flat one is 165 bytes
smaller; unused decorators tree-shake out of both. With the size argument a
wash, per-module stays the default import for the one thing it does better
— readable stack traces without source maps. (My first pass at that
measurement reported "shaken" for every symbol because both rollup builds
had failed and grep was reading missing files as absence. The check now
asserts the bundle is non-empty and that a *used* symbol is present, so it
can tell a real result from a broken harness.)

**FRAMEWORKS.md** — a recipe per framework, each one run before it was
written, with the versions and date verified against.

Angular works, which was not obvious: the CLI scaffolds
experimentalDecorators: true, but ngtsc erases @Component and @Injectable
into static properties rather than leaning on TypeScript's decorator emit.
Flip the flag and both systems coexist. Verified with ngc on Angular 21.2
with strictTemplates — templates still type-check and a wrong cereale rule
is still TS1240 inside the Angular build.

Next.js cannot work inline, structurally: it derives both the SWC parser's
decorator support and the transform mode from the one flag, so on gives
legacy emit and off makes @ a syntax error. NestJS cannot either — its DI
needs design:type from emitDecoratorMetadata.

Both have the same answer: keep the cereale classes in a package compiled
by tsc and import the built output. Verified inside a program with BOTH
legacy flags on, alongside @Injectable() — mapping and validation work, and
the compile-time guarantee still holds where the rules are written.

Also verified: Bun 1.3 needs no configuration, and a real Vite 8 build with
the plugin works where the same build without it silently leaves decorator
syntax in the bundle.

Version 0.4.0: cereale/min is a new public entry point, and cutting a minor
keeps the existing v0.3.0 tag meaningful instead of force-moving it onto a
commit it was never cut from.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SAcqrz3FcadkYr3xG32CjK
2026-08-05 16:00:26 +00:00

61 lines
2.7 KiB
JavaScript

/**
* Flattens the ESM build into a single minified module.
*
* This is an *addition*, not a replacement. `dist/esm` stays the default `import`, because
* measuring says the flat file buys a consumer nothing: bundled through esbuild the two come
* out within 2 bytes of each other, through rollup+terser the flat one is ~165 bytes smaller,
* and unused decorators tree-shake out of both. What the per-module build keeps is readable
* stack traces for anyone who does not load source maps.
*
* Where the single file does earn its place is everywhere a bundler is not involved: a
* `<script type="module">` tag, a CDN, an import map, Deno, or a Worker. That is what
* `cereale/min` is for.
*
* It is built from `dist/esm/index.js` rather than from `src/`, so the decorator lowering and
* the ES2025 target are whatever `tsc` produced — esbuild is only flattening and minifying.
* (esbuild has no `es2025` target name; `esnext` means "downlevel nothing", which is what we
* want when the input is already at the target.)
*/
import { build } from 'esbuild';
import { readFile, writeFile, stat } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { gzipSync } from 'node:zlib';
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const dist = path.join(root, 'dist');
const outfile = path.join(dist, 'cereale.min.js');
const pkg = JSON.parse(await readFile(path.join(root, 'package.json'), 'utf8'));
await build({
entryPoints: [path.join(dist, 'esm/index.js')],
bundle: true,
format: 'esm',
target: 'esnext',
minify: true,
sourcemap: true,
legalComments: 'none',
banner: { js: `/*! cereale ${pkg.version} | MIT | ${pkg.homepage} */` },
// The input is compiled JavaScript, so the project's tsconfig has no bearing here — and
// reading it only earns a warning, because esbuild does not know the ES2025 target name
// that tsc is perfectly happy with.
tsconfigRaw: {},
outfile,
});
// The banner names a version, so a stale bundle would misreport itself rather than merely be
// out of date. Cheap to assert, and the build is the only place that can.
const emitted = await readFile(outfile, 'utf8');
if (!emitted.includes(`cereale ${pkg.version}`)) {
throw new Error('the bundle banner does not carry the current version');
}
for (const name of ['toInstanceSync', 'IsString', 'standardDecorators']) {
if (name === 'standardDecorators') continue; // cereale/vite is a separate entry point
if (!emitted.includes(name)) throw new Error(`${name} is missing from the flat bundle`);
}
const size = (await stat(outfile)).size;
const gzip = gzipSync(emitted).length;
console.log(`dist/cereale.min.js ${(size / 1024).toFixed(1)} KB (${(gzip / 1024).toFixed(1)} KB gzipped)`);