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
| 1 | /** |
| 2 | * Post-pass over Bun's output. |
| 3 | * |
| 4 | * Bun's `minify` does whitespace, identifiers and syntax, but cannot mangle |
| 5 | * property names. Terser can, which is worth ~6% gzipped, but only internal |
| 6 | * names may be renamed: anything read off a user-supplied object (`props`, |
| 7 | * `styles`, `shadow`, `scoped`, `reflect`, `type`, `equals`, …) must survive. |
| 8 | * |
| 9 | * So the mangle list is closed and explicit: |
| 10 | * - `_$`-prefixed names, our own internals (Lit's convention), |
| 11 | * - alien-signals' reactive-graph fields, which never appear in user data. |
| 12 | * |
| 13 | * One class is barred from the `_$` convention: properties of `Symbol.for` |
| 14 | * singleton records (pebble's invokers/fetch registries; a component's |
| 15 | * once-only install record, if it keeps one). Those records are created by |
| 16 | * whichever bundle loads first and read by every other copy of the module, so |
| 17 | * their names cross build boundaries; mangling is per-build, and two bundles |
| 18 | * built separately from source could mangle them differently and silently |
| 19 | * fail to share the singleton. They stay unprefixed. |
| 20 | */ |
| 21 | import { minify } from "terser"; |
| 22 | |
| 23 | /** alien-signals graph internals (see node_modules/alien-signals/esm/index.mjs). */ |
| 24 | const ALIEN_INTERNALS = [ |
| 25 | "depsTail", "nextSub", "nextDep", "prevDep", "subsTail", "pendingValue", "currentValue", |
| 26 | "getter", "flags", "deps", "subs", "dep", "sub", "prevSub", "cleanup", "version", |
| 27 | ]; |
| 28 | |
| 29 | export const MANGLE_PROPERTIES = new RegExp(`^(_\\$|${ALIEN_INTERNALS.join("$|")}$)`); |
| 30 | |
| 31 | export async function shrink(code: string): Promise<string> { |
| 32 | const result = await minify(code, { |
| 33 | module: true, |
| 34 | compress: { passes: 3 }, |
| 35 | format: { comments: false }, |
| 36 | // `builtins: true` lets the whitelist reach names that are also JS builtins |
| 37 | // (`flags` appears 35× in our bundle but is skipped otherwise, because of |
| 38 | // RegExp.prototype.flags). Safe only because the regex is a closed list. |
| 39 | mangle: { properties: { regex: MANGLE_PROPERTIES, builtins: true } }, |
| 40 | }); |
| 41 | if (!result.code) throw new Error("terser produced no output"); |
| 42 | return result.code; |
| 43 | } |
| 44 | |
| 45 | /** `shrink` that also re-chains an existing source map onto the output. */ |
| 46 | export async function shrinkWithMap( |
| 47 | code: string, |
| 48 | previousMap?: string, |
| 49 | filename?: string, |
| 50 | ): Promise<{ code: string; map?: string }> { |
| 51 | const result = await minify(code, { |
| 52 | module: true, |
| 53 | compress: { passes: 3 }, |
| 54 | format: { comments: false }, |
| 55 | mangle: { properties: { regex: MANGLE_PROPERTIES, builtins: true } }, |
| 56 | sourceMap: previousMap ? { content: previousMap, url: filename } : undefined, |
| 57 | }); |
| 58 | if (!result.code) throw new Error("terser produced no output"); |
| 59 | return { code: result.code, map: typeof result.map === "string" ? result.map : undefined }; |
| 60 | } |