tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
78 lignes · 3802 octetsBrut · Historique
1/**
2 * Per-package prebuilt bundles. `bun run build` → dist/:
3 *
4 * dist/<name>.js packages/<name>/index.ts(x) through the release pipeline
5 * (scripts/release.ts): "pebble" is bundled IN — a prebuilt
6 * component needs nothing else; polyfill-style lazy dynamic
7 * imports stay split chunks. Bun bundles and minifies, then
8 * scripts/minify.ts mangles internal property names, which
9 * Bun cannot do. Source maps are re-chained onto the output.
10 * dist/<name>.d.ts tsc declarations (tsconfig.build.json), re-exported from
11 * dist/types/<name>/.
12 * dist/<name>.css when packages/<name>/<name>.css exists: the raw sheet for
13 * zero-JS <link> pages, minified by Bun's CSS engine.
14 *
15 * (The `bun build` CLI ignores plugins, hence a script.) An empty packages/
16 * is a valid empty dist/.
17 */
18import { brotliCompressSync, gzipSync } from "node:zlib";
19import { rmSync } from "node:fs";
20import { shrinkWithMap } from "./minify";
21import { ROOT, discoverPackages, writeEntryShim } from "./packages";
22import { bundleRelease } from "./release";
23
24const DIST = `${ROOT}/dist`;
25rmSync(DIST, { recursive: true, force: true });
26rmSync(`${ROOT}/.build`, { recursive: true, force: true });
27
28const packages = await discoverPackages();
29if (packages.length === 0) {
30 await Bun.write(`${DIST}/.gitkeep`, "");
31 console.log("dist/: no packages yet — empty dist");
32 process.exit(0);
33}
34
35for (const pkg of packages) {
36 const entry = await writeEntryShim(pkg);
37 const result = await bundleRelease([entry], DIST, "linked", `${pkg.name}.js`);
38 for (const out of result.outputs) {
39 let bytes: Uint8Array;
40 if (out.path.endsWith(".js")) {
41 const mapPath = `${out.path}.map`;
42 const mapFile = Bun.file(mapPath);
43 const previous = (await mapFile.exists()) ? await mapFile.text() : undefined;
44 const { code, map } = await shrinkWithMap(await out.text(), previous, `${out.path.split("/").pop()}.map`);
45 await Bun.write(out.path, code);
46 if (map) await Bun.write(mapPath, map);
47 bytes = new TextEncoder().encode(code);
48 } else {
49 bytes = new Uint8Array(await out.arrayBuffer());
50 }
51 const rel = out.path.replace(`${ROOT}/`, "");
52 console.log(`${pkg.name}\t${rel}\t${bytes.byteLength} B\t${gzipSync(bytes).byteLength} B gz\t${brotliCompressSync(bytes).byteLength} B br`);
53 }
54
55 // The raw sheet for zero-JS <link> pages, minified by the same engine as the
56 // component-CSS pipeline (`bun build --minify`) but written as .css.
57 if (pkg.css) {
58 const proc = Bun.spawn(["bun", "build", pkg.css, "--minify", "--target=browser"], { stdout: "pipe", stderr: "pipe" });
59 const [css, err, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
60 if (code !== 0) throw new Error(`CSS compile failed for ${pkg.css}:\n${err.trim()}`);
61 await Bun.write(`${DIST}/${pkg.name}.css`, `${css.trim()}\n`);
62 console.log(`${pkg.name}\tdist/${pkg.name}.css\t${new TextEncoder().encode(css.trim()).byteLength} B`);
63 }
64}
65
66// Type declarations, one tsc run for all packages (tsconfig.build.json):
67// dist/types/<name>/**, re-exported from dist/<name>.d.ts so a consumer
68// importing dist/<name>.js picks up its types.
69const tsc = Bun.spawnSync(["bunx", "tsc", "-p", "tsconfig.build.json"], { cwd: ROOT });
70if (tsc.exitCode !== 0) {
71 process.stdout.write(tsc.stdout);
72 process.stderr.write(tsc.stderr);
73 throw new Error("tsc declaration emit failed");
74}
75for (const pkg of packages) {
76 await Bun.write(`${DIST}/${pkg.name}.d.ts`, `export * from "./types/${pkg.name}/index";\n`);
77}
78console.log(`dist/: ${packages.map(p => `${p.name}.js`).join(", ")} + declarations`);