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
70 lignes · 2926 octetsBrut · Historique
1/**
2 * Package discovery and the build-entry shims, shared by build.ts and sizes.ts.
3 *
4 * Every directory under packages/ with an index.ts(x) is a component:
5 * build.ts bundles it to dist/<name>.js and sizes.ts measures it. A package
6 * may also ship packages/<name>/<name>.css → dist/<name>.css (the raw sheet,
7 * for zero-JS <link> pages).
8 *
9 * Build entries are shims generated into .build/, never the package's own
10 * index: Bun elides the whole module graph when the entrypoint itself lives
11 * inside a package (the entry collapses to a re-export facade; pebble's
12 * scripts/invokers-entry.ts documents the bug — 3665 B vs 85 B). The shim is
13 * an external importer, and the graph bundles correctly.
14 *
15 * A component package must not set "sideEffects": false: the import IS the
16 * registration. build.ts refuses to build one that does.
17 */
18import { Glob } from "bun";
19import { existsSync, mkdirSync } from "node:fs";
20import { resolve } from "node:path";
21
22export const ROOT = resolve(`${import.meta.dir}/..`);
23const BUILD_DIR = `${ROOT}/.build`;
24
25export interface Package {
26 /** Directory name under packages/, and the dist basename: dist/<name>.js. */
27 name: string;
28 /** Absolute path of packages/<name>/index.ts(x). */
29 entry: string;
30 /** Absolute path of packages/<name>/<name>.css, when the package ships one. */
31 css?: string;
32}
33
34export async function discoverPackages(): Promise<Package[]> {
35 const packages = new Map<string, Package>();
36 for (const pattern of ["packages/*/index.ts", "packages/*/index.tsx"]) {
37 for (const path of new Glob(pattern).scanSync(ROOT)) {
38 const name = path.split("/")[1];
39 if (packages.has(name)) continue;
40 const meta = Bun.file(`${ROOT}/packages/${name}/package.json`);
41 if (await meta.exists()) {
42 const json = await meta.json();
43 if (json.sideEffects === false) {
44 throw new Error(
45 `packages/${name}/package.json sets "sideEffects": false — a component's import IS its registration; the flag lets bundlers drop it. Remove it.`,
46 );
47 }
48 }
49 const css = `${ROOT}/packages/${name}/${name}.css`;
50 packages.set(name, { name, entry: `${ROOT}/${path}`, ...(existsSync(css) ? { css } : {}) });
51 }
52 }
53 return [...packages.values()].sort((a, b) => a.name.localeCompare(b.name));
54}
55
56/**
57 * Writes the package's build entry to .build/<name>.entry.ts and returns its
58 * path. The shim is outside the package on purpose (see the header comment).
59 */
60export async function writeEntryShim(pkg: Package): Promise<string> {
61 mkdirSync(BUILD_DIR, { recursive: true });
62 const path = `${BUILD_DIR}/${pkg.name}.entry.ts`;
63 await Bun.write(
64 path,
65 `// Generated by scripts/packages.ts — the build entry for packages/${pkg.name}. Do not edit.\n` +
66 `import "../packages/${pkg.name}/index";\n` +
67 `export * from "../packages/${pkg.name}/index";\n`,
68 );
69 return path;
70}