Skip to content

Commit 0aaabab

Browse files
committed
refactor(hub): reuse buildHub({ context }) instead of a bakeHubStatic concept
Rather than introduce a new "bake" verb/function, mirror initHub's existing `context` option: buildHub now accepts an already-mounted DevframeHubContext and bakes it (reading ctx.frames + ctx.views.buildStaticDirs), throwing DF8002 if both `context` and `devframes` are passed. The baking helpers fold back into build.ts as module-private functions; the public surface stays just buildHub.
1 parent c3a954f commit 0aaabab

9 files changed

Lines changed: 246 additions & 296 deletions

File tree

docs/content/1.guide/18.hub-initiate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,4 @@ const hub = initHub({ base: DEVFRAMES_HUB_BASE, context: ctx })
127127

128128
It then serves only hub-level endpoints and transport; serve each mounted devframe's meta from `hub.connectionMeta()` yourself.
129129

130-
The same context works for a static build: `bakeHubStatic(ctx, { outDir, base })` from `@devframes/hub/build` bakes an already-mounted context (`buildHub` is `createHubContext` + `mountDevframes` + `bakeHubStatic`). It reads `ctx.views.buildStaticDirs` for the statics to copy and `ctx.frames` for the frames to advertise, so a host that mounted its own context reuses the exact baker rather than reimplementing it. Pass `clean: false` to bake beside an app's own build output.
130+
The same `context` option works for a static build: `buildHub({ context: ctx, outDir })` bakes an already-mounted context instead of a `devframes` list, reading `ctx.frames` and `ctx.views.buildStaticDirs` for what to emit, so a host that mounted its own context reuses `buildHub` rather than reimplementing it. Pass `clean: false` to bake beside an app's own build output.

docs/content/6.errors/DF8002.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
title: 'DF8002: Both devframes and context Passed to initHub'
3-
description: 'initHub received both devframes and context; the two assembly modes are mutually exclusive.'
2+
title: 'DF8002: Both devframes and context Passed to initHub/buildHub'
3+
description: 'initHub/buildHub received both devframes and context; the two assembly modes are mutually exclusive.'
44
---
55

66
## Message
77

8-
> initHub received both `devframes` and `context`; the two assembly modes are mutually exclusive.
8+
> `initHub`/`buildHub` received both `devframes` and `context`; the two assembly modes are mutually exclusive.
99
1010
## Cause
1111

12-
`initHub` assembles a hub two ways: **declaratively** (`devframes: [...]`, where the instance creates the hub context and mounts each devframe under `<base><id>/`), or **from a pre-built context** (`context: ctx`, where your host framework already mounted the devframes and the instance serves only the hub-level endpoints and transport). A `devframes` list cannot be mounted into a context the instance doesn't own, so passing both contradicts.
12+
`initHub` (and `buildHub`) assembles a hub two ways: **declaratively** (`devframes: [...]`, where it creates the hub context and mounts each devframe under `<base><id>/`), or **from a pre-built context** (`context: ctx`, where your host framework already mounted the devframes). A `devframes` list cannot be mounted into a context it doesn't own, so passing both contradicts.
1313

1414
## Example
1515

@@ -33,3 +33,4 @@ Pick one mode. Use `configure(ctx)` on the declarative mode when you need post-m
3333
## Source
3434

3535
- [`packages/hub/src/node/initiate.ts`](https://github.com/devframes/devframe/blob/main/packages/hub/src/node/initiate.ts): `initHub` throws this during initialization when both options are present.
36+
- [`packages/hub/src/node/build.ts`](https://github.com/devframes/devframe/blob/main/packages/hub/src/node/build.ts): `buildHub` throws this when both options are present.

docs/content/6.errors/DF8006.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: 'A static hub build can only write mounts under its own base: "{url
99
1010
## Cause
1111

12-
`bakeHubStatic` (which `buildHub` runs) maps every mounted URL base to a directory under its `outDir` (which corresponds to the hub `base` at serve time), so a mount whose base lies outside the hub base has no on-disk location in the output. This happens when a devframe is installed with an explicit base outside the hub base, e.g. `ctx.install(devframe, { base: '/elsewhere/' })` from `configure`.
12+
`buildHub` maps every mounted URL base to a directory under its `outDir` (which corresponds to the hub `base` at serve time), so a mount whose base lies outside the hub base has no on-disk location in the output. This happens when a devframe is installed with an explicit base outside the hub base, e.g. `ctx.install(devframe, { base: '/elsewhere/' })` from `configure`.
1313

1414
## Example
1515

@@ -30,4 +30,4 @@ await buildHub({
3030

3131
## Source
3232

33-
- [`packages/hub/src/node/bake.ts`](https://github.com/devframes/devframe/blob/main/packages/hub/src/node/bake.ts): `bakeHubStatic()`'s mount-to-disk mapping throws this for any mount base outside the hub base.
33+
- [`packages/hub/src/node/build.ts`](https://github.com/devframes/devframe/blob/main/packages/hub/src/node/build.ts): `buildHub()`'s mount-to-disk mapping throws this for any mount base outside the hub base.

docs/content/8.references/6.hub-api.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,10 @@ The options of `buildHub()` from `@devframes/hub/build`: [Static builds](/guide/
9292
|---|---|
9393
| `outDir` | Output directory for the hub subtree; corresponds to `base` at serve time (build `base: '/__devframes/'` into `dist/__devframes`). |
9494
| `base` | Mount base baked into every absolute URL the build emits. Default `/__devframes/`. |
95+
| `context` | An already-mounted `DevframeHubContext` to bake instead of `devframes` (the build counterpart of `initHub({ context })`); reads `ctx.frames` and `ctx.views.buildStaticDirs`. Mutually exclusive with `devframes`. |
9596
| `clean` | Remove `outDir` before writing. Default `true`; set `false` to bake beside an app's own build output. |
9697
| `pretty` | Pretty-print RPC dump JSON shards. Default `false` (minified). |
9798

98-
## `bakeHubStatic` options
99-
100-
`bakeHubStatic(ctx, options)` from `@devframes/hub/build` is the second half of `buildHub`: it bakes an already-mounted `DevframeHubContext` a caller assembled itself (`createHubContext` + `ctx.install`, or a framework kit's own context), reading `ctx.views.buildStaticDirs` for the statics to copy and `ctx.frames` for the frames to advertise. `buildHub` is `createHubContext` + `mountDevframes` + `bakeHubStatic`. Options: `outDir`, `base`, `ui`, `renderers`, `name`, `version`, `pretty`, and `clean`, same contracts as their `buildHub` counterparts.
101-
10299
## Client runtime options
103100

104101
The options of `createDevframeClientRuntime()`: [The client runtime](/guide/client-context#the-client-runtime).

packages/hub/src/node/__tests__/build.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { join } from 'node:path'
55
import { createH3DevframeHost } from 'devframe/internal'
66
import { describe, expect, it } from 'vitest'
77
import { HUB_EVENTS } from '../../events'
8-
import { bakeHubStatic } from '../bake'
98
import { buildHub } from '../build'
109
import { createHubContext } from '../context'
1110

@@ -139,22 +138,22 @@ describe('buildHub', () => {
139138
expect(readFileSync(join(outDir, 'beta/index.html'), 'utf-8')).toContain('beta')
140139
})
141140

142-
it('bakes an externally-mounted context via bakeHubStatic', async () => {
143-
const outDir = join(mkdtempSync(join(tmpdir(), 'hub-bake-out-')), 'hub')
144-
const cwd = mkdtempSync(join(tmpdir(), 'hub-bake-cwd-'))
141+
it('bakes an externally-mounted context passed as `context`', async () => {
142+
const outDir = join(mkdtempSync(join(tmpdir(), 'hub-ctx-out-')), 'hub')
143+
const cwd = mkdtempSync(join(tmpdir(), 'hub-ctx-cwd-'))
145144

146145
// A host assembling the context itself: create + mount via `ctx.install`,
147-
// then hand the already-mounted context to the baker.
146+
// then hand the already-mounted context to `buildHub`.
148147
const host = createH3DevframeHost({ origin: 'http://localhost', appName: 'devframes', workspaceRoot: cwd, mount: () => {} })
149148
const ctx = await createHubContext({ cwd, workspaceRoot: cwd, mode: 'build', host })
150149
await ctx.install(makeFrame('alpha', { distDir: makeDist('<h1>alpha</h1>') }), { base: '/__hub/alpha/' })
151150

152151
expect(ctx.frames.map(frame => frame.id)).toEqual(['alpha'])
153152

154-
await bakeHubStatic(ctx, { outDir, base: '/__hub/' })
153+
await buildHub({ context: ctx, outDir, base: '/__hub/' })
155154

156-
// The baker copied the SPA from `ctx.views.buildStaticDirs`, wrote the
157-
// index from `ctx.frames`, and emitted the per-frame meta + shared dump.
155+
// The SPA was copied from `ctx.views.buildStaticDirs`, the index written
156+
// from `ctx.frames`, and the per-frame meta + shared dump emitted.
158157
expect(readFileSync(join(outDir, 'alpha/index.html'), 'utf-8')).toContain('alpha')
159158
const index = JSON.parse(readFileSync(join(outDir, '__index.json'), 'utf-8'))
160159
expect(index.frames.map((frame: { id: string }) => frame.id)).toEqual(['alpha'])

packages/hub/src/node/bake.ts

Lines changed: 0 additions & 224 deletions
This file was deleted.

0 commit comments

Comments
 (0)