@mednours/fronton 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,10 +9,13 @@ bun add @mednours/fronton
9
9
  Two separate entry points, deliberately not one:
10
10
 
11
11
  - **`@mednours/fronton`** (the root import) — Node-side, meant for `vite.config.ts` only: `obfuscateTailwind()`, `frontonCompression()`, `backonProxy()`.
12
- - **`@mednours/fronton/client`** — browser-safe, meant for application code: `resolveApiBaseUrl()`, `lazyDomAnimation()`/`lazyDomMax()`.
12
+ - **`@mednours/fronton/client`** — browser-safe, meant for application code: `resolveApiBaseUrl()`. Nothing here has a peer dependency, so importing it never obliges a project to install anything.
13
+ - **`@mednours/fronton/motion`** — `lazyDomAnimation()`/`lazyDomMax()`, which need the optional `motion` peer dependency installed.
13
14
 
14
15
  Mixing these into one entry point was the first version's actual design, and it broke in a specific, findable way: importing `resolveApiBaseUrl` from a component pulled the *entire* bundle — including `postcss`, `magic-string`, and `vite-plugin-compression2` — into the client dependency graph, since esbuild had already flattened everything into a single file before Vite's bundler ever got a chance to tree-shake it. Vite's dependency scanner then warned about externalizing `node:crypto`, `node:fs`, and `zlib` for the browser, and the shipped JS bundle was measurably bigger for it (in one test project, 338 KB vs. 191 KB after the split — that's not a rounding error). Splitting the entry point at the source fixes it structurally instead of relying on a bundler to clean it up.
15
16
 
17
+ The same mistake had a second, sharper version, which is why there are three entry points rather than two: `lazyDomAnimation`/`lazyDomMax` used to sit on `/client` alongside `resolveApiBaseUrl`. They reach `motion/react` through wrapper modules that import it *statically* (see below for why that indirection is unavoidable), and `motion` is an optional peer dependency — so in any project that hadn't installed it, Vite resolved the wrappers to an empty optional-peer stub and `vite build` failed outright with `MISSING_EXPORT: "domAnimation" is not exported by "__vite-optional-peer-dep:motion/react"`. Tree-shaking can't rescue this: resolution happens first, and merely *importing* the entry point is enough to put the wrappers in the graph. Every `create-fronton` project scaffolded next to a backon backend hit it, because the generated `App.tsx` imports `resolveApiBaseUrl` from `/client`. Moving the two loaders onto their own `/motion` entry point is what keeps `/client` honest about having nothing attached to it.
18
+
16
19
  ## `obfuscateTailwind(options?)`
17
20
 
18
21
  A build-only Vite plugin (`apply: "build"` — `bun run dev` stays fully readable). After Tailwind's CSS and the JS bundle are both fully generated, it collects every real class selector Tailwind actually emitted, hashes each one to a short opaque name, and rewrites both the stylesheet and known-safe class-bearing text in the built JS/HTML to match:
@@ -83,13 +86,13 @@ fetch(`${resolveApiBaseUrl()}/api/users`);
83
86
 
84
87
  Reads `import.meta.env.VITE_API_BASE_URL`, returning `""` (relative requests) when it's unset — correct for same-origin deployment or the dev-server proxy above. This exists because the obvious alternative, a `process.env` check (the pattern backon's own generated `generate-client` script uses, correctly, since that script runs under Node/Bun), does nothing useful in a browser bundle: `process` isn't defined there, so the check just silently falls through. Only `VITE_`-prefixed variables are ever exposed to client code — Vite drops everything else on purpose, so a backend secret sitting in the same `.env` file never leaks into the browser bundle.
85
88
 
86
- ## `lazyDomAnimation()` / `lazyDomMax()` (from `@mednours/fronton/client`)
89
+ ## `lazyDomAnimation()` / `lazyDomMax()` (from `@mednours/fronton/motion`)
87
90
 
88
91
  Pre-built `<LazyMotion features={...}>` loaders for [`motion`](https://motion.dev) (née Framer Motion):
89
92
 
90
93
  ```tsx
91
94
  import { LazyMotion, m } from "motion/react";
92
- import { lazyDomAnimation } from "@mednours/fronton/client";
95
+ import { lazyDomAnimation } from "@mednours/fronton/motion";
93
96
 
94
97
  <LazyMotion features={lazyDomAnimation}>
95
98
  <m.div animate={{ opacity: 1 }} initial={{ opacity: 0 }} />
@@ -98,9 +101,9 @@ import { lazyDomAnimation } from "@mednours/fronton/client";
98
101
 
99
102
  This exists because `motion`'s own `<LazyMotion features={domAnimation}>` example is a trap if `domAnimation` gets there via a plain, static `import { domAnimation } from "motion/react"` — that still pulls the whole animation engine into the main bundle, exactly as expensive as using `motion.div` directly. `features` only saves anything when it's a function that *dynamically* imports the bundle. Verified against a real Vite build: the static form produced one 265.81 KB chunk (85.91 KB gzip); switching to a real dynamic loader dropped the main chunk to 231.51 KB (74.58 KB gzip) and moved the rest into its own 37.42 KB chunk (14.18 KB gzip) that only loads once something on the page actually animates.
100
103
 
101
- The less obvious trap, also verified against a real build: dynamically importing `"motion/react"` itself doesn't work either. Every component using `<LazyMotion>`/`<m.div>` has to statically import `LazyMotion`/`m` from `"motion/react"` — there's no lazy way to get those — and Rollup refuses to split a module into its own chunk if *anything* in the build graph already imports that exact same specifier statically, even from an unrelated file (an "ineffective dynamic import" — collapses back into one chunk). `lazyDomAnimation`/`lazyDomMax` work around this by each dynamically importing a tiny dedicated internal module that statically re-exports just one feature bundle, giving Rollup a boundary that isn't already claimed elsewhere. Confirmed end-to-end, not just in theory: this package's own build (esbuild with `--splitting`) preserves that boundary as a real separate chunk file, and feeding its actual `dist/client.js` output into a real downstream Vite build reproduced the same 231.51/37.42 KB split.
104
+ The less obvious trap, also verified against a real build: dynamically importing `"motion/react"` itself doesn't work either. Every component using `<LazyMotion>`/`<m.div>` has to statically import `LazyMotion`/`m` from `"motion/react"` — there's no lazy way to get those — and Rollup refuses to split a module into its own chunk if *anything* in the build graph already imports that exact same specifier statically, even from an unrelated file (an "ineffective dynamic import" — collapses back into one chunk). `lazyDomAnimation`/`lazyDomMax` work around this by each dynamically importing a tiny dedicated internal module that statically re-exports just one feature bundle, giving Rollup a boundary that isn't already claimed elsewhere. Confirmed end-to-end, not just in theory: this package's own build (esbuild with `--splitting`) preserves that boundary as a real separate chunk file, and feeding its actual `dist/motion.js` output into a real downstream Vite build reproduced the same 231.51/37.42 KB split.
102
105
 
103
- `lazyDomAnimation` covers drag-free interactions (hover/tap/enter-exit/animate); `lazyDomMax` is a strict superset that adds layout and drag animations — same as `motion`'s own `domAnimation`/`domMax`. `motion` is an optional peer dependency (`bun add motion`) — install it only if you actually call one of these.
106
+ `lazyDomAnimation` covers drag-free interactions (hover/tap/enter-exit/animate); `lazyDomMax` is a strict superset that adds layout and drag animations — same as `motion`'s own `domAnimation`/`domMax`. `motion` is an optional peer dependency (`bun add motion`) — install it before importing this entry point at all, not just before calling one of these, since the loaders reach `motion/react` through statically-importing wrapper modules that a bundler has to resolve either way.
104
107
 
105
108
  ## `compressImages(options?)` and `compressVideos(options?)`
106
109
 
@@ -9,6 +9,20 @@ export interface BackonProxyOptions {
9
9
  * namespace of their own. Default true.
10
10
  */
11
11
  stripPrefix?: boolean;
12
+ /**
13
+ * Forward WebSocket upgrade requests on `prefix` to the backend as well.
14
+ * Default true, because backon ships `defineWebSocket()` and a route
15
+ * registered with it is reached over the same prefix as everything else —
16
+ * without this, such a route answers fine when hit directly on the
17
+ * backend's own port but hangs forever through the dev server, with no
18
+ * error on either side to explain it.
19
+ *
20
+ * This does not interfere with Vite's own HMR socket: Vite only hands an
21
+ * upgrade to a proxy entry whose path actually matches, and HMR's socket
22
+ * is not under `prefix`. Set false only if the backend has no WebSocket
23
+ * routes and you'd rather upgrade requests 404 than be forwarded.
24
+ */
25
+ ws?: boolean;
12
26
  }
13
27
  /**
14
28
  * Builds a Vite dev-server `server.proxy` entry pointed at a backon
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/backon/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AASzC,MAAM,WAAW,kBAAkB;IACjC,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAU1G"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/backon/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AASzC,MAAM,WAAW,kBAAkB;IACjC,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;;;;;;;;;OAYG;IACH,EAAE,CAAC,EAAE,OAAO,CAAC;CACd;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAmB1G"}
package/dist/client.d.ts CHANGED
@@ -18,56 +18,4 @@
18
18
  * are on different origins.
19
19
  */
20
20
  export declare function resolveApiBaseUrl(): string;
21
- /**
22
- * Pre-built `<LazyMotion features={...}>` loaders for `motion` (the
23
- * animation library, née Framer Motion) — `lazyDomAnimation` for
24
- * `domAnimation` (drag/hover/tap/enter-exit), `lazyDomMax` for `domMax`
25
- * (adds layout animations, on top of everything `domAnimation` covers).
26
- *
27
- * Why this exists: `motion`'s own `<LazyMotion features={domAnimation}>`
28
- * pattern only saves anything if `features` is a function that *dynamically*
29
- * imports the feature bundle — passing the bundle directly (a plain, static
30
- * `import { domAnimation } from "motion/react"`) still pulls the whole
31
- * animation engine into the main bundle, identical in cost to using
32
- * `motion.div` outright. Verified against a real Vite build: the static
33
- * form produced one 265.81 KB chunk (85.91 KB gzip); switching to a real
34
- * dynamic loader dropped the main chunk to 231.51 KB (74.58 KB gzip) and
35
- * moved the rest into its own 37.42 KB chunk (14.18 KB gzip) that only
36
- * loads once something on the page actually animates.
37
- *
38
- * The less obvious trap, also verified against a real build: it's not
39
- * enough for *this* function to dynamically import `"motion/react"` —
40
- * every component using `<LazyMotion>`/`<m.div>` also has to statically
41
- * import `LazyMotion`/`m` from `"motion/react"` (there's no lazy way to get
42
- * those), and Rollup refuses to split a module into its own chunk if
43
- * *anything* in the build graph already imports that exact same module
44
- * specifier statically (an "ineffective dynamic import" — confirmed by
45
- * reproducing it: dynamically importing `"motion/react"` directly, even
46
- * from a completely different file than the static import, still collapsed
47
- * back into one chunk). That's why `lazyDomAnimation`/`lazyDomMax` don't
48
- * dynamically import `"motion/react"` themselves — they each dynamically
49
- * import a tiny dedicated wrapper module (./motion/dom-animation,
50
- * ./motion/dom-max) that statically re-exports just that one feature bundle,
51
- * giving Rollup a module boundary that isn't already claimed elsewhere.
52
- * fronton's own build (esbuild with `--splitting`) preserves that boundary
53
- * as a real separate output chunk rather than inlining it back together —
54
- * confirmed by building this package for real and feeding its actual
55
- * `dist/client.js` output into a real downstream Vite build.
56
- *
57
- * Usage:
58
- * ```tsx
59
- * import { LazyMotion, m } from "motion/react";
60
- * import { lazyDomAnimation } from "@mednours/fronton/client";
61
- *
62
- * <LazyMotion features={lazyDomAnimation}>
63
- * <m.div animate={{ opacity: 1 }} initial={{ opacity: 0 }} />
64
- * </LazyMotion>
65
- * ```
66
- *
67
- * `motion` is an optional peer dependency (`bun add motion`) — these two
68
- * exports are the only things in this file that need it, and only if you
69
- * actually call one.
70
- */
71
- export declare const lazyDomAnimation: () => Promise<import("motion/react").FeatureBundle>;
72
- export declare const lazyDomMax: () => Promise<import("motion/react").FeatureBundle>;
73
21
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAG1C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,eAAO,MAAM,gBAAgB,QAAO,OAAO,CAAC,OAAO,cAAc,EAAE,aAAa,CACnB,CAAC;AAC9D,eAAO,MAAM,UAAU,QAAO,OAAO,CAAC,OAAO,cAAc,EAAE,aAAa,CACnB,CAAC"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAG1C"}
package/dist/client.js CHANGED
@@ -3,10 +3,6 @@ function resolveApiBaseUrl() {
3
3
  const value = import.meta.env?.VITE_API_BASE_URL;
4
4
  return typeof value === "string" && value.length > 0 ? value : "";
5
5
  }
6
- var lazyDomAnimation = () => import("./dom-animation-RYVDSRON.js").then((mod) => mod.default);
7
- var lazyDomMax = () => import("./dom-max-SN3JCHFZ.js").then((mod) => mod.default);
8
6
  export {
9
- lazyDomAnimation,
10
- lazyDomMax,
11
7
  resolveApiBaseUrl
12
8
  };
package/dist/index.js CHANGED
@@ -207,12 +207,19 @@ function frontonCompression(options = {}) {
207
207
 
208
208
  // src/backon/index.ts
209
209
  function backonProxy(target, options = {}) {
210
- const { prefix = "/api", stripPrefix = true } = options;
210
+ const { prefix = "/api", stripPrefix = true, ws = true } = options;
211
211
  return {
212
212
  [prefix]: {
213
213
  target,
214
214
  changeOrigin: true,
215
- ...stripPrefix ? { rewrite: (path) => path.replace(new RegExp(`^${prefix}`), "") } : {}
215
+ ws,
216
+ ...stripPrefix ? {
217
+ // Stripping a prefix off a request to the prefix itself leaves
218
+ // an empty path ("/api" -> ""), which is not a valid request
219
+ // target — fall back to "/" rather than relying on whatever the
220
+ // underlying HTTP client happens to coerce an empty string to.
221
+ rewrite: (path) => path.replace(new RegExp(`^${prefix}`), "") || "/"
222
+ } : {}
216
223
  }
217
224
  };
218
225
  }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Pre-built `<LazyMotion features={...}>` loaders for `motion` (the
3
+ * animation library, née Framer Motion) — `lazyDomAnimation` for
4
+ * `domAnimation` (drag/hover/tap/enter-exit), `lazyDomMax` for `domMax`
5
+ * (adds layout animations, on top of everything `domAnimation` covers).
6
+ *
7
+ * Why this exists: `motion`'s own `<LazyMotion features={domAnimation}>`
8
+ * pattern only saves anything if `features` is a function that *dynamically*
9
+ * imports the feature bundle — passing the bundle directly (a plain, static
10
+ * `import { domAnimation } from "motion/react"`) still pulls the whole
11
+ * animation engine into the main bundle, identical in cost to using
12
+ * `motion.div` outright. Verified against a real Vite build: the static
13
+ * form produced one 265.81 KB chunk (85.91 KB gzip); switching to a real
14
+ * dynamic loader dropped the main chunk to 231.51 KB (74.58 KB gzip) and
15
+ * moved the rest into its own 37.42 KB chunk (14.18 KB gzip) that only
16
+ * loads once something on the page actually animates.
17
+ *
18
+ * The less obvious trap, also verified against a real build: it's not
19
+ * enough for *this* function to dynamically import `"motion/react"` —
20
+ * every component using `<LazyMotion>`/`<m.div>` also has to statically
21
+ * import `LazyMotion`/`m` from `"motion/react"` (there's no lazy way to get
22
+ * those), and Rollup refuses to split a module into its own chunk if
23
+ * *anything* in the build graph already imports that exact same module
24
+ * specifier statically (an "ineffective dynamic import" — confirmed by
25
+ * reproducing it: dynamically importing `"motion/react"` directly, even
26
+ * from a completely different file than the static import, still collapsed
27
+ * back into one chunk). That's why `lazyDomAnimation`/`lazyDomMax` don't
28
+ * dynamically import `"motion/react"` themselves — they each dynamically
29
+ * import a tiny dedicated wrapper module (./dom-animation, ./dom-max) that
30
+ * statically re-exports just that one feature bundle, giving Rollup a
31
+ * module boundary that isn't already claimed elsewhere.
32
+ * fronton's own build (esbuild with `--splitting`) preserves that boundary
33
+ * as a real separate output chunk rather than inlining it back together —
34
+ * confirmed by building this package for real and feeding its actual
35
+ * output into a real downstream Vite build.
36
+ *
37
+ * That static re-export inside the wrappers is also why these two live on
38
+ * their own entry point (`@mednours/fronton/motion`) rather than on
39
+ * `/client` — see motion.ts's header comment for the build failure that
40
+ * caused.
41
+ *
42
+ * Usage:
43
+ * ```tsx
44
+ * import { LazyMotion, m } from "motion/react";
45
+ * import { lazyDomAnimation } from "@mednours/fronton/motion";
46
+ *
47
+ * <LazyMotion features={lazyDomAnimation}>
48
+ * <m.div animate={{ opacity: 1 }} initial={{ opacity: 0 }} />
49
+ * </LazyMotion>
50
+ * ```
51
+ *
52
+ * `motion` is an optional peer dependency (`bun add motion`) — install it
53
+ * before importing this entry point at all, not just before calling one of
54
+ * these.
55
+ */
56
+ export declare const lazyDomAnimation: () => Promise<import("motion/react").FeatureBundle>;
57
+ export declare const lazyDomMax: () => Promise<import("motion/react").FeatureBundle>;
58
+ //# sourceMappingURL=loaders.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loaders.d.ts","sourceRoot":"","sources":["../../src/motion/loaders.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,eAAO,MAAM,gBAAgB,QAAO,OAAO,CAAC,OAAO,cAAc,EAAE,aAAa,CAC1B,CAAC;AACvD,eAAO,MAAM,UAAU,QAAO,OAAO,CAAC,OAAO,cAAc,EAAE,aAAa,CAC1B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { lazyDomAnimation, lazyDomMax } from "./motion/loaders";
2
+ //# sourceMappingURL=motion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"motion.d.ts","sourceRoot":"","sources":["../src/motion.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
package/dist/motion.js ADDED
@@ -0,0 +1,7 @@
1
+ // src/motion/loaders.ts
2
+ var lazyDomAnimation = () => import("./dom-animation-RYVDSRON.js").then((mod) => mod.default);
3
+ var lazyDomMax = () => import("./dom-max-SN3JCHFZ.js").then((mod) => mod.default);
4
+ export {
5
+ lazyDomAnimation,
6
+ lazyDomMax
7
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mednours/fronton",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Opinionated runtime pieces for fronton-scaffolded Vite projects — Tailwind class obfuscation, gzip/brotli compression, and backon API wiring.",
6
6
  "license": "MIT",
@@ -27,12 +27,16 @@
27
27
  "./client": {
28
28
  "types": "./dist/client.d.ts",
29
29
  "import": "./dist/client.js"
30
+ },
31
+ "./motion": {
32
+ "types": "./dist/motion.d.ts",
33
+ "import": "./dist/motion.js"
30
34
  }
31
35
  },
32
36
  "scripts": {
33
37
  "test": "bun test",
34
38
  "typecheck": "tsc --noEmit",
35
- "build": "rm -rf dist && tsc -p tsconfig.json --emitDeclarationOnly && esbuild src/index.ts src/client.ts --bundle --platform=node --format=esm --target=node18 --packages=external --splitting --outdir=dist",
39
+ "build": "rm -rf dist && tsc -p tsconfig.json --emitDeclarationOnly && esbuild src/index.ts src/client.ts src/motion.ts --bundle --platform=node --format=esm --target=node18 --packages=external --splitting --outdir=dist",
36
40
  "prepublishOnly": "bun run typecheck && bun run test && bun run build"
37
41
  },
38
42
  "peerDependencies": {