@flanksource/page-runtime 0.0.0-stage → 0.1.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/LICENSE +17 -0
- package/README.md +494 -2
- package/dist/build/README.md +96 -0
- package/dist/build/cjs-export-names.mjs +24 -0
- package/dist/build/dependencies.mjs +279 -0
- package/dist/build/dependencies.mjs.map +1 -0
- package/dist/build/dependency-cache-Zg-FGasE.mjs +2130 -0
- package/dist/build/dependency-cache-Zg-FGasE.mjs.map +1 -0
- package/dist/build/index.mjs +42 -0
- package/dist/build/index.mjs.map +1 -0
- package/dist/build/package.json +19 -0
- package/dist/build/types/activity-schema.d.ts +62 -0
- package/dist/build/types/build/barrels.d.ts +70 -0
- package/dist/build/types/build/base.d.ts +2 -0
- package/dist/build/types/build/browser-runtime-exports.d.ts +19 -0
- package/dist/build/types/build/check-fragment.d.ts +5 -0
- package/dist/build/types/build/commonjs-exports.d.ts +5 -0
- package/dist/build/types/build/convert-commonjs.d.ts +12 -0
- package/dist/build/types/build/dependencies.d.ts +6 -0
- package/dist/build/types/build/dependency-barrel-index.d.ts +5 -0
- package/dist/build/types/build/dependency-barrels.d.ts +7 -0
- package/dist/build/types/build/dependency-cache.d.ts +7 -0
- package/dist/build/types/build/dependency-optional.d.ts +4 -0
- package/dist/build/types/build/dependency-plugin.d.ts +8 -0
- package/dist/build/types/build/dependency-runtime.d.ts +20 -0
- package/dist/build/types/build/dependency-workers.d.ts +8 -0
- package/dist/build/types/build/errors.d.ts +9 -0
- package/dist/build/types/build/fragment.d.ts +13 -0
- package/dist/build/types/build/index.d.ts +7 -0
- package/dist/build/types/build/library-runtime.d.ts +21 -0
- package/dist/build/types/build/module-utils.d.ts +3 -0
- package/dist/build/types/build/recipe-dependencies.d.ts +2 -0
- package/dist/build/types/build/runtime-entries.d.ts +4 -0
- package/dist/build/types/build/runtime-layer.d.ts +17 -0
- package/dist/build/types/build/trace.d.ts +140 -0
- package/dist/build/types/design-schema.d.ts +98 -0
- package/dist/build/types/host/agent-source.d.ts +2 -0
- package/dist/build/types/host/agent-validation.d.ts +3 -0
- package/dist/build/types/host/design-validation.d.ts +2 -0
- package/dist/build/types/host/message-values.d.ts +8 -0
- package/dist/build/types/host/review/validation.d.ts +5 -0
- package/dist/build/types/host/validation.d.ts +6 -0
- package/dist/build/types/link/page-sources.d.ts +31 -0
- package/dist/build/types/link/types.d.ts +62 -0
- package/dist/build/types/link/validation.d.ts +9 -0
- package/dist/build/types/markers.d.ts +10 -0
- package/dist/build/types/review-schema.d.ts +60 -0
- package/dist/build/types/schema.d.ts +319 -0
- package/dist/build/types/sw/barrels.d.ts +9 -0
- package/dist/build/types/sw/compiler-plugins.d.ts +6 -0
- package/dist/build/types/sw/composition.d.ts +8 -0
- package/dist/build/types/sw/module-transform.d.ts +68 -0
- package/dist/build/types/sw/profile.d.ts +7 -0
- package/dist/build/types/sw/source-provider.d.ts +7 -0
- package/dist/build/types/sw/versions.d.ts +10 -0
- package/dist/node/catalog.mjs +1 -0
- package/dist/node/chunks/publication-RchcQvxD.mjs +229 -0
- package/dist/node/chunks/publication-RchcQvxD.mjs.map +1 -0
- package/dist/node/chunks/schema-CaRTfg2x.mjs +12 -0
- package/dist/node/chunks/schema-CaRTfg2x.mjs.map +1 -0
- package/dist/node/dev.mjs +410 -0
- package/dist/node/dev.mjs.map +1 -0
- package/dist/node/publication/cli.mjs +13 -0
- package/dist/node/publication/cli.mjs.map +1 -0
- package/dist/node/publication.mjs +1 -0
- package/dist/node/schema.mjs +1 -0
- package/dist/node/styles.mjs +49 -0
- package/dist/node/styles.mjs.map +1 -0
- package/dist/static/boot.js +40938 -0
- package/dist/static/boot.js.map +1 -0
- package/dist/static/bootstrap.html +1 -0
- package/dist/static/devtools-hook.js +118 -0
- package/dist/static/document-bundle.js +1 -0
- package/dist/static/document.js +2621 -0
- package/dist/static/editing.js +37 -0
- package/dist/static/export-compiler.js +81521 -0
- package/dist/static/export-compiler.js.map +1 -0
- package/dist/static/page-agent.js +17193 -0
- package/dist/static/page-agent.js.map +1 -0
- package/dist/static/sw.js +86112 -0
- package/dist/static/sw.js.map +1 -0
- package/package.json +110 -3
- package/scripts/build-builder.ts +27 -0
- package/scripts/build-library-runtime.ts +9 -0
- package/scripts/build-node.ts +26 -0
- package/scripts/build-static.ts +86 -0
- package/scripts/build.ts +7 -0
- package/scripts/check-fragment.ts +12 -0
- package/scripts/local-workspaces.ts +55 -0
- package/scripts/react-externals.ts +18 -0
- package/scripts/release-version.ts +47 -0
- package/scripts/run-package-build.ts +8 -0
- package/scripts/serve-packages.ts +8 -0
- package/src/activity-schema.ts +92 -0
- package/src/build/README.md +96 -0
- package/src/build/barrels.ts +306 -0
- package/src/build/base.ts +2 -0
- package/src/build/browser-runtime-exports.ts +52 -0
- package/src/build/check-fragment.ts +45 -0
- package/src/build/cjs-export-names.mjs +24 -0
- package/src/build/commonjs-exports.ts +24 -0
- package/src/build/convert-commonjs.ts +183 -0
- package/src/build/dependencies.ts +6 -0
- package/src/build/dependency-barrel-index.ts +48 -0
- package/src/build/dependency-barrels.ts +80 -0
- package/src/build/dependency-cache.ts +40 -0
- package/src/build/dependency-optional.ts +15 -0
- package/src/build/dependency-plugin.ts +58 -0
- package/src/build/dependency-runtime.ts +182 -0
- package/src/build/dependency-workers.ts +41 -0
- package/src/build/errors.ts +9 -0
- package/src/build/fragment.ts +89 -0
- package/src/build/index.ts +7 -0
- package/src/build/library-runtime.ts +101 -0
- package/src/build/module-utils.ts +11 -0
- package/src/build/recipe-dependencies.ts +14 -0
- package/src/build/runtime-entries.ts +10 -0
- package/src/build/runtime-layer.ts +99 -0
- package/src/build/trace.ts +431 -0
- package/src/design-schema.ts +25 -0
- package/src/dev/README.md +78 -0
- package/src/dev/catalog.ts +79 -0
- package/src/dev/cli.ts +25 -0
- package/src/dev/index.ts +118 -0
- package/src/dev/runtime-origin.ts +63 -0
- package/src/editing.ts +25 -0
- package/src/form/entry-source.ts +68 -0
- package/src/form/form-page.ts +63 -0
- package/src/form/index.ts +2 -0
- package/src/form/source-update.ts +17 -0
- package/src/frame/activity.browser.ts +34 -0
- package/src/frame/activity.ts +52 -0
- package/src/frame/agent/README.md +47 -0
- package/src/frame/agent/agent.browser.ts +127 -0
- package/src/frame/agent/design.ts +62 -0
- package/src/frame/agent/frame-layout.ts +54 -0
- package/src/frame/agent/index.ts +151 -0
- package/src/frame/agent/overlay.ts +104 -0
- package/src/frame/agent/owner.ts +61 -0
- package/src/frame/agent/provider.tsx +14 -0
- package/src/frame/agent/react-override.ts +55 -0
- package/src/frame/agent/review.browser.ts +65 -0
- package/src/frame/agent/review.ts +108 -0
- package/src/frame/agent/testing/callsite.tsx +5 -0
- package/src/frame/agent/testing/client.ts +99 -0
- package/src/frame/agent/testing/library.ts +3 -0
- package/src/frame/agent/testing/modal.tsx +5 -0
- package/src/frame/agent/testing/review-client.ts +43 -0
- package/src/frame/agent/testing/review-server.ts +31 -0
- package/src/frame/agent/testing/server.ts +72 -0
- package/src/frame/agent/testing/vocabulary-host.ts +20 -0
- package/src/frame/agent/testing/vocabulary.tsx +10 -0
- package/src/frame/agent/vitest.browser.config.ts +6 -0
- package/src/frame/agent/vocabulary.browser.ts +30 -0
- package/src/frame/agent/vocabulary.ts +13 -0
- package/src/frame/agent.ts +3 -0
- package/src/frame/boot-styles.ts +50 -0
- package/src/frame/boot.ts +269 -0
- package/src/frame/bridge.ts +27 -0
- package/src/frame/devtools-hook.ts +1 -0
- package/src/frame/document-assets.ts +76 -0
- package/src/frame/document-entry.ts +68 -0
- package/src/frame/document-modules.ts +113 -0
- package/src/frame/document-mount.ts +32 -0
- package/src/frame/entry.ts +4 -0
- package/src/frame/export-compiler.ts +18 -0
- package/src/frame/export-page.ts +61 -0
- package/src/frame/export.ts +89 -0
- package/src/frame/index.ts +8 -0
- package/src/frame/local-runtime.browser.ts +36 -0
- package/src/frame/mount-page.ts +36 -0
- package/src/frame/page-agent.ts +2 -0
- package/src/frame/readiness.ts +51 -0
- package/src/frame/render-job.ts +101 -0
- package/src/frame/review-integration.browser.ts +48 -0
- package/src/frame/runtime.browser.ts +90 -0
- package/src/frame/tailwind/README.md +49 -0
- package/src/frame/tailwind/boot-styles.browser.ts +18 -0
- package/src/frame/tailwind/cache.ts +20 -0
- package/src/frame/tailwind/load.ts +75 -0
- package/src/frame/tailwind/recipe.ts +76 -0
- package/src/frame/tailwind/runtime.ts +113 -0
- package/src/frame/tailwind/store.ts +55 -0
- package/src/frame/tailwind/stylesheets.ts +42 -0
- package/src/frame/tailwind/tailwind.browser.ts +71 -0
- package/src/frame/tailwind/testing/client.ts +133 -0
- package/src/frame/tailwind/testing/compare.ts +91 -0
- package/src/frame/tailwind/testing/pages.ts +14 -0
- package/src/frame/tailwind/testing/server.ts +89 -0
- package/src/frame/tailwind/vitest.browser.config.ts +7 -0
- package/src/frame/testing/boot-layout.ts +3 -0
- package/src/frame/testing/boot-page.ts +3 -0
- package/src/frame/testing/host.ts +78 -0
- package/src/frame/testing/local-host.tsx +33 -0
- package/src/frame/testing/server.ts +135 -0
- package/src/host/BuildLog.tsx +28 -0
- package/src/host/DEBUG_CONSOLE.md +13 -0
- package/src/host/PageForm.tsx +63 -0
- package/src/host/PageFrame.tsx +49 -0
- package/src/host/RuntimeActivity.tsx +77 -0
- package/src/host/RuntimeDebugPanel.tsx +18 -0
- package/src/host/activity-state.ts +69 -0
- package/src/host/agent-source.ts +14 -0
- package/src/host/agent-validation.ts +73 -0
- package/src/host/bridge.ts +56 -0
- package/src/host/build-log.ts +29 -0
- package/src/host/clear-cache.ts +66 -0
- package/src/host/debug-runtime.tsx +17 -0
- package/src/host/design-validation.ts +96 -0
- package/src/host/index.ts +12 -0
- package/src/host/link/index.ts +66 -0
- package/src/host/link/transport.ts +68 -0
- package/src/host/link/watch.ts +62 -0
- package/src/host/message-values.ts +55 -0
- package/src/host/origin.ts +7 -0
- package/src/host/review/README.md +32 -0
- package/src/host/review/index.ts +43 -0
- package/src/host/review/validation.ts +55 -0
- package/src/host/runtime-diagnostics.ts +41 -0
- package/src/host/validation.ts +148 -0
- package/src/link/README.md +115 -0
- package/src/link/barrels.ts +5 -0
- package/src/link/build-cli.ts +12 -0
- package/src/link/build.ts +99 -0
- package/src/link/commonjs.ts +39 -0
- package/src/link/examples/server.ts +18 -0
- package/src/link/index.ts +6 -0
- package/src/link/link.browser.ts +66 -0
- package/src/link/local-relay.browser.ts +22 -0
- package/src/link/overrides.ts +69 -0
- package/src/link/page-sources.ts +69 -0
- package/src/link/source.ts +25 -0
- package/src/link/storage.ts +32 -0
- package/src/link/testing/client.ts +96 -0
- package/src/link/testing/folder.ts +19 -0
- package/src/link/testing/local-client.ts +30 -0
- package/src/link/testing/local-server.ts +26 -0
- package/src/link/testing/local-worker.ts +17 -0
- package/src/link/testing/server.ts +131 -0
- package/src/link/testing/worker.ts +32 -0
- package/src/link/testing-cache.ts +20 -0
- package/src/link/testing-fixtures.ts +16 -0
- package/src/link/types.ts +27 -0
- package/src/link/validation.ts +44 -0
- package/src/link/vitest.browser.config.ts +3 -0
- package/src/markers.ts +18 -0
- package/src/publication/build.ts +27 -0
- package/src/publication/fixtures/build.mjs +2 -0
- package/src/publication/index.ts +3 -0
- package/src/publication/lock.ts +32 -0
- package/src/publication/manifest.ts +14 -0
- package/src/publication/publish.ts +80 -0
- package/src/publication/runtime.ts +28 -0
- package/src/publication/schema.ts +13 -0
- package/src/review-schema.ts +17 -0
- package/src/schema.ts +175 -0
- package/src/storage/db.ts +64 -0
- package/src/styles.ts +40 -0
- package/src/sw/activity.ts +66 -0
- package/src/sw/barrels.ts +44 -0
- package/src/sw/candidates.ts +14 -0
- package/src/sw/compile.ts +92 -0
- package/src/sw/compiler-columns.ts +44 -0
- package/src/sw/compiler-internals.d.ts +8 -0
- package/src/sw/compiler-plugins.ts +18 -0
- package/src/sw/compiler.ts +93 -0
- package/src/sw/composition.ts +86 -0
- package/src/sw/content-types.ts +28 -0
- package/src/sw/converted.browser.ts +31 -0
- package/src/sw/converted.ts +64 -0
- package/src/sw/editable-sites.ts +42 -0
- package/src/sw/errors.ts +30 -0
- package/src/sw/esm-sh-fixtures/browser-client.ts +18 -0
- package/src/sw/esm-sh-fixtures/browser-server.ts +104 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-entry.headers +15 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-module.headers +13 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-module.mjs.txt +142 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-entry.headers +15 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-module.headers +11 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-module.mjs.txt +222 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-entry.headers +14 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-module.headers +11 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-module.mjs.txt +76 -0
- package/src/sw/esm-sh-fixtures/fixtures.ts +54 -0
- package/src/sw/esm-sh-fixtures/redact.ts +1 -0
- package/src/sw/index.ts +7 -0
- package/src/sw/integrity.ts +10 -0
- package/src/sw/legacy-storage.ts +18 -0
- package/src/sw/link-activity.ts +21 -0
- package/src/sw/link-cache.ts +19 -0
- package/src/sw/link-compiler.ts +120 -0
- package/src/sw/link-folder.ts +29 -0
- package/src/sw/link-relay.ts +40 -0
- package/src/sw/link-route.ts +239 -0
- package/src/sw/link-source.ts +41 -0
- package/src/sw/markers.ts +128 -0
- package/src/sw/module-transform.ts +364 -0
- package/src/sw/page-clients.ts +45 -0
- package/src/sw/profile.ts +90 -0
- package/src/sw/resolver.ts +224 -0
- package/src/sw/router.ts +360 -0
- package/src/sw/runtime-exports.ts +39 -0
- package/src/sw/runtime-module.ts +13 -0
- package/src/sw/source-hash.ts +37 -0
- package/src/sw/source-provider.ts +20 -0
- package/src/sw/versions.ts +96 -0
- package/src/sw/worker.ts +91 -0
- package/static/README.md +15 -0
- package/static/profile.example.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
|
16
|
+
|
|
17
|
+
Copyright (c) Flanksource
|
package/README.md
CHANGED
|
@@ -1,3 +1,495 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @flanksource/page-runtime
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A browser-only runtime for facet and clicky-ui pages. Pages written as `.mdx` or `.tsx` are resolved, fetched, compiled and linked entirely in the browser: a service worker on a dedicated runtime origin acts as the module server and compiler, a sandboxed frame boots React from a shared, integrity-checked runtime set, and the host application embeds the frame with a single `PageFrame` component. No per-page bundler, dev server or server-side build is involved.
|
|
4
|
+
|
|
5
|
+
- [Benefits](#benefits)
|
|
6
|
+
- [Architecture](#architecture)
|
|
7
|
+
- [How to use](#how-to-use)
|
|
8
|
+
- [Structure of a runtime package](#structure-of-a-runtime-package)
|
|
9
|
+
- [How it differs from other packaging formats](#how-it-differs-from-other-packaging-formats)
|
|
10
|
+
- [The compiler in the worker](#the-compiler-in-the-worker)
|
|
11
|
+
- [WebAssembly](#webassembly)
|
|
12
|
+
- [Security model](#security-model)
|
|
13
|
+
- [Caching and storage](#caching-and-storage)
|
|
14
|
+
- [Testing](#testing)
|
|
15
|
+
- [Further reading](#further-reading)
|
|
16
|
+
|
|
17
|
+
## Benefits
|
|
18
|
+
|
|
19
|
+
- **No build per page.** Page source goes in, a rendered page comes out. An edit recompiles only the changed module; linked-source edits become visible in roughly 150 ms (p50).
|
|
20
|
+
- **One React for everything.** clicky-ui, facet, every other library and the page itself share a single React, React DOM and renderer instance. Hooks and context work across library boundaries, and a duplicated React is treated as a build failure rather than a runtime surprise.
|
|
21
|
+
- **Exact npm semantics in the browser.** Installed versions, nested dependency versions and package `exports` conditions are preserved. A page sees the same dependency graph the library was built and tested against.
|
|
22
|
+
- **Reproducible.** Every opened page produces a serializable `PageLock` (versions, import map, integrity, remaps, barrel index). Supplying the lock again skips registry resolution entirely, so a saved page renders identically later.
|
|
23
|
+
- **Integrity everywhere.** Every traced and converted file carries a `sha384` subresource-integrity hash, verified when the worker fetches it.
|
|
24
|
+
- **Fine-grained loading.** Barrel imports (`import { Button } from "@flanksource/clicky-ui/components"`) are rewritten to the defining modules when the package's `sideEffects` allows it, cutting requests by around 90%. Warm loads of real facet and clicky-ui pages land in 60–100 ms.
|
|
25
|
+
- **Isolation by default.** Pages run on a separate origin under a strict nonce-based CSP with no `eval` and no WebAssembly, behind an origin- and session-validated message bridge, with an explicit network egress allowlist and a dependency policy.
|
|
26
|
+
- **Runtime Tailwind v4.** Pages that opt in get Tailwind utilities generated in the browser with no computed-style differences against a build-time Tailwind build, cached across loads.
|
|
27
|
+
- **Developer-grade output in every frame.** React always runs as its development build, and the compiler stamps `data-source` locations into host JSX. Owner metadata, source locations and warnings power element picking, commenting and agent edits even in deployed apps.
|
|
28
|
+
- **Local checkout links.** A developer can swap a published library for a local checkout for one session, without republishing anything.
|
|
29
|
+
- **Static, cacheable artifacts.** Libraries publish plain files under `dist/runtime/`; any unpkg-layout origin (unpkg, a mirror, or the bundled local origin) can serve them.
|
|
30
|
+
|
|
31
|
+
## Architecture
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
Host application (app.example.com)
|
|
35
|
+
┌───────────────────────────────────────────────┐
|
|
36
|
+
│ <PageFrame runtimeOrigin open theme data /> │
|
|
37
|
+
│ ▲ postMessage bridge "page-runtime-v1" │
|
|
38
|
+
└────────┼──────────────────────────────────────┘
|
|
39
|
+
│ origin + session validated envelopes
|
|
40
|
+
┌────────▼──────────────────────────────────────┐ Runtime origin (pages.example.com)
|
|
41
|
+
│ Frame /frame/<page> │
|
|
42
|
+
│ bootstrap.html → boot.js → page module │
|
|
43
|
+
│ page-agent.js (picking, comments, review) │
|
|
44
|
+
└────────┬──────────────────────────────────────┘
|
|
45
|
+
│ fetch /src/ /npm/ /link/ /__runtime/
|
|
46
|
+
┌────────▼──────────────────────────────────────┐
|
|
47
|
+
│ Service worker sw.js (scope /frame/) │
|
|
48
|
+
│ resolve → fetch + verify → compile → cache │
|
|
49
|
+
└────────┬───────────────────────┬──────────────┘
|
|
50
|
+
│ │
|
|
51
|
+
package origin npm registry
|
|
52
|
+
/<name>@<ver>/<file> metadata for resolution
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The host never loads page code. It sends `open` (page path, folder manifest, source files, optional lock), `theme` and `data` over the bridge. The frame asks the worker for the page entry; the worker resolves bare specifiers through the composed import map, fetches package files from the package origin, verifies integrity, compiles source, rewrites imports to canonical URLs and serves the result. The frame mounts the page's default export with `{ data, host }`, waits for readiness signals and reports status back.
|
|
56
|
+
|
|
57
|
+
Static assets produced by `pnpm --filter @flanksource/page-runtime build` in `dist/static/`:
|
|
58
|
+
|
|
59
|
+
| File | Role |
|
|
60
|
+
|---|---|
|
|
61
|
+
| `bootstrap.html` | Frame document served for `/frame/*`; registers the worker on first visit and reloads once |
|
|
62
|
+
| `sw.js` | Service worker: resolver, compiler, module router, caches |
|
|
63
|
+
| `boot.js` | Frame bootstrap: bridge, React runtime set, styles, page mount, readiness |
|
|
64
|
+
| `page-agent.js` | Element picking, comments, review and agent overlays inside the frame |
|
|
65
|
+
| `export-compiler.js` | Loaded only for HTML/PDF export; runs library-declared remark plugins |
|
|
66
|
+
| `devtools-hook.js` | React devtools hook installed before React loads |
|
|
67
|
+
| `editing.js` | Shared editing helpers served at `/__runtime/editing.js` |
|
|
68
|
+
|
|
69
|
+
## How to use
|
|
70
|
+
|
|
71
|
+
### Install
|
|
72
|
+
|
|
73
|
+
```sh
|
|
74
|
+
pnpm add @flanksource/page-runtime @flanksource/clicky-ui react react-dom
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`@flanksource/clicky-ui` (`^0.3.57`), `react` and `react-dom` (18.3 or 19) are peer dependencies. The library-build half is published separately as `@flanksource/page-runtime-build`, so build machines don't pull browser host dependencies.
|
|
78
|
+
|
|
79
|
+
### 1. Build and host the runtime origin
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
pnpm --filter @flanksource/page-runtime build # uses installed @flanksource/* releases
|
|
83
|
+
pnpm --filter @flanksource/page-runtime build -- --dev # bundles sibling workspace checkouts instead
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Serve `dist/static/` over HTTPS on an origin separate from the host (localhost is fine for development):
|
|
87
|
+
|
|
88
|
+
- `bootstrap.html` for every `/frame/*` path.
|
|
89
|
+
- `/sw.js`, `/boot.js`, `/page-agent.js`, `/export-compiler.js`, `/devtools-hook.js` and `/profile.json` from the origin root.
|
|
90
|
+
- Keep `/frame/` reserved for runtime documents — it is the worker's scope.
|
|
91
|
+
|
|
92
|
+
### 2. Configure `profile.json`
|
|
93
|
+
|
|
94
|
+
The profile is required and validated on worker start; a change requires a worker upgrade/reload.
|
|
95
|
+
|
|
96
|
+
```json
|
|
97
|
+
{
|
|
98
|
+
"runtimeDomain": "pages.example.com",
|
|
99
|
+
"embedders": ["https://app.example.com"],
|
|
100
|
+
"packageOrigin": "https://unpkg.com",
|
|
101
|
+
"registry": "https://registry.npmjs.org",
|
|
102
|
+
"managed": { "@flanksource/clicky-ui": "^0.3", "@flanksource/facet": "^0.1.74" },
|
|
103
|
+
"policy": { "dependencies": "strict", "allow": ["date-fns", "d3-*", "zod"] },
|
|
104
|
+
"convert": { "origin": "https://esm.internal.example" },
|
|
105
|
+
"minReleaseAgeDays": 3,
|
|
106
|
+
"network": { "allow": ["https://api.example.com/*"] },
|
|
107
|
+
"csp": { "add": {} },
|
|
108
|
+
"links": "deny",
|
|
109
|
+
"sources": ["host", "opfs", "folder", "url", "hash"],
|
|
110
|
+
"readinessTimeoutMs": 15000
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
| Field | Meaning |
|
|
115
|
+
|---|---|
|
|
116
|
+
| `runtimeDomain` | The origin this runtime is served from |
|
|
117
|
+
| `embedders` | Host origins allowed to embed frames |
|
|
118
|
+
| `packageOrigin` | unpkg-layout origin serving `/<name>@<exact-version>/<file>` (and `?meta` listings) |
|
|
119
|
+
| `registry` | npm registry used to resolve version ranges |
|
|
120
|
+
| `managed` | Libraries that publish runtime fragments, with allowed ranges; they define the page's shared import map |
|
|
121
|
+
| `dependencies` | Optional extra page dependencies with ranges |
|
|
122
|
+
| `policy` | `any` allows any declared package; `ask` and `strict` allow only names or `*` globs in `allow` |
|
|
123
|
+
| `convert` | Optional CommonJS→ESM converter origin for unmanaged packages without ESM entries (also added to `connect-src`) |
|
|
124
|
+
| `minReleaseAgeDays` | Refuse versions published more recently than this |
|
|
125
|
+
| `network.allow` | Egress allowlist for page `fetch`es |
|
|
126
|
+
| `csp` | `add` appends sources to any directive; `wasm: true` opts frames into WebAssembly (see [WebAssembly](#webassembly)) |
|
|
127
|
+
| `links` | `allow` permits developer checkout links (also needs the host's `developerOverrides`) |
|
|
128
|
+
| `sources` | Where page sources may come from |
|
|
129
|
+
| `readinessTimeoutMs` | Upper bound for each readiness step (data, fonts, stylesheets, diagrams…) |
|
|
130
|
+
|
|
131
|
+
### 3. Embed a page in the host
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
import { PageFrame, type PageBridge } from "@flanksource/page-runtime/host";
|
|
135
|
+
import type { PageLock } from "@flanksource/page-runtime/schema";
|
|
136
|
+
|
|
137
|
+
function Report({ source, lock, saveLock }: { source: string; lock?: PageLock; saveLock: (l: PageLock) => void }) {
|
|
138
|
+
const [bridge, setBridge] = useState<PageBridge | null>(null);
|
|
139
|
+
return (
|
|
140
|
+
<PageFrame
|
|
141
|
+
runtimeOrigin="https://pages.example.com"
|
|
142
|
+
open={{
|
|
143
|
+
page: "report.mdx",
|
|
144
|
+
manifest: { dependencies: { "@flanksource/facet": "^0.1.74" } },
|
|
145
|
+
files: { "report.mdx": source },
|
|
146
|
+
...(lock ? { lock } : {}),
|
|
147
|
+
}}
|
|
148
|
+
theme={{ theme: "light", density: "comfortable" }}
|
|
149
|
+
data={{ cluster: "prod-eu", window: "24h" }}
|
|
150
|
+
onMessage={(m) => { if (m.type === "lock") saveLock(m.lock); }}
|
|
151
|
+
onError={(error) => console.error(error)}
|
|
152
|
+
onBridge={setBridge}
|
|
153
|
+
/>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
- **Lifecycle.** The frame reports `status` messages: `connecting` (the frame asks for `open`), `booting` (it asks for `theme` and `data`), then readiness steps (`mounted`, `fonts`, `images`, `stylesheets`, `diagrams`, `hook`, `quiescence`) and finally `ready`.
|
|
159
|
+
- **Locks.** On first open the frame emits a validated `lock`. Persist it next to the page source (or in a URL/hash, or browser state) and pass it back as `open.lock` for reproducible renders.
|
|
160
|
+
- **Live updates.** Changing `theme` or `data` props sends them over the bridge without reloading. To change source, call `bridge.send({ type: "sources", files: { "report.mdx": next } })`.
|
|
161
|
+
- **Persistent frames.** Keep `PageFrame` mounted across edits. Data updates retain React component state; `await bridge.request({ type: "data", data: next })` completes after rendering and readiness. With imperative data updates, set `dataUpdates={false}` to avoid also sending prop changes; the initial boot still receives the latest `data` prop. Source updates compile versioned local modules and reconcile them through the same React root without reloading the document, worker or agent. Optional `sources.data` supplies data for that same source update. The previous render stays visible during compilation; an edited component can remount, while unchanged layouts are retained for page-only edits. Dependency changes still require opening the page again.
|
|
162
|
+
- **Layouts.** Optional `open.layout` names a JavaScript/TypeScript module in `open.files`. Its default React component receives `{ data, children }` and wraps both the live page and its HTML/print export. The layout can declare `meta.tailwind`; page metadata overrides matching layout keys. Keep Markdown/MDX as `open.page` when it needs export-time remark plugins, rather than importing it through a TSX entry.
|
|
163
|
+
- **CSS updates.** Source messages containing only `.css` files replace loaded stylesheets after their new version loads, preserving the mounted React tree. Library base stylesheets load in their recipe's cascade layer, so ordinary page CSS can override them consistently with the Node build. HTML export retains those layers.
|
|
164
|
+
- **Export.** After `ready`, `bridge.send({ type: "export", format: "html" })` returns a `snapshot` message; `format: "print"` prints to PDF.
|
|
165
|
+
- **Errors** arrive as `{ type: "error", error: { phase, message, file, line?, column? } }`, where `phase` is one of `resolve`, `fetch`, `compile`, `link`, `render`, `readiness`.
|
|
166
|
+
|
|
167
|
+
`RuntimeActivity`, `BuildLog` and `clearPageRuntimeCache` from the same entry give hosts a live compile/fetch log and a cache reset.
|
|
168
|
+
|
|
169
|
+
#### Page-to-host messages
|
|
170
|
+
|
|
171
|
+
Besides `lock`, `status`, `error`, `snapshot`, `activity` and the agent messages, a frame sends two data messages. Both are validated by the host bridge, and the payload of `emit` must be JSON.
|
|
172
|
+
|
|
173
|
+
| Frame message | Sent when | Host receives |
|
|
174
|
+
|---|---|---|
|
|
175
|
+
| `{ type: "emit", event, payload }` | the page calls `host.emit(event, payload)`; `host` is the second prop of the page's default export, next to `data` | the message, through `PageFrame`'s `onMessage` |
|
|
176
|
+
| `{ type: "size", height }` | the page root's rendered height changes (a `ResizeObserver` on `#root`, rounded up to whole pixels; the first value is sent when the page mounts) | the message, through `onMessage`; set the iframe's `height` from it to make a frame follow its content |
|
|
177
|
+
|
|
178
|
+
Host messages are unchanged; `emit` and `size` are rejected if they arrive from the host direction.
|
|
179
|
+
|
|
180
|
+
### Forms with runtime extensions
|
|
181
|
+
|
|
182
|
+
`PageForm` renders a clicky-ui `JsonSchemaForm` inside a frame and lets the form be extended by `FormExtension` modules (formats, lookups, layouts, slots, pre/post hooks) that are compiled in the browser. The whole form, including its extensions, runs inside the frame, so extensions are ordinary functions in the same React as the form; the sandbox, lock and integrity model are unchanged.
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
import { PageForm } from "@flanksource/page-runtime/host";
|
|
186
|
+
|
|
187
|
+
<PageForm
|
|
188
|
+
runtimeOrigin="https://pages.example.com"
|
|
189
|
+
theme={{ theme: "light", density: "comfortable" }}
|
|
190
|
+
manifest={{ dependencies: { "@flanksource/clicky-ui": "^0.3.57" } }}
|
|
191
|
+
schema={schema}
|
|
192
|
+
value={value}
|
|
193
|
+
onChange={setValue}
|
|
194
|
+
formProps={{ layout: { mode: "cards" } }}
|
|
195
|
+
extensions={[{ tsx: inlineSource, name: "notes" }]} // host-supplied; the schema may declare its own too
|
|
196
|
+
files={{ "extensions/service.tsx": serviceSource }} // serves the schema's `{ ref: "extensions/service.tsx" }`
|
|
197
|
+
onError={(error) => console.error(error)}
|
|
198
|
+
/>
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
- **Extension sources.** `{ tsx }` is inline source, placed at `__form/ext-<i>.tsx`. `{ ref }` is a path relative to the project root, served by `files` or, through the approved path index, by `sourceProvider`; a ref neither can serve throws when `PageForm` renders. Each module's default export must be a `FormExtension` (`defineFormExtension` from `@flanksource/clicky-ui/components`). The generated `__form/entry.tsx` checks every default export when it loads, so a bad export fails the page with a named error instead of being ignored.
|
|
202
|
+
- **Schema-declared sources.** A schema's root can declare its own extensions, `"x-clicky-extensions": [{ "ref": "extensions/service.tsx" }, { "tsx": "…", "name": "…" }]`, so one `schema.json` describes a whole form. `PageForm` reads the keyword and appends those sources after the host's `extensions`; a source's index in the combined list fixes its file name (`__form/ext-<i>.tsx`), and they are validated like host sources (a ref must be served by `files` or `sourceProvider`, an inline source must not be empty, a malformed keyword throws). The generated entry hands host sources to the form's `extensions` prop and schema-declared ones only to its `extensionLoader`, by declared index, so nothing is registered twice and duplicate or reserved names still throw. The framed form always has a loader, so a schema-declared form just works in `PageForm`; a plain `JsonSchemaForm` loads them only when the host passes its own `extensionLoader`.
|
|
203
|
+
- **Data.** Only serializable `JsonSchemaForm` props cross the bridge (`formProps`, typed `FormDataProps`: the props minus `onChange`, `pre`, `post`, `extensions`, `lookupFetcher` and `expressionEvaluator`). A host-side `lookupFetcher` cannot reach a framed form, so lookups must be extension handlers; they run in the frame under the profile's `network.allow` and CSP.
|
|
204
|
+
- **Value.** The frame keeps the value in local state for typing latency and reports every edit as `emit` event `change`, which `PageForm` maps to `onChange`. Feeding the value back through `value` is safe: the frame reseeds from `value` only when it is not one of the values it emitted and has not yet seen echoed back.
|
|
205
|
+
- **Height.** `size` messages set the iframe's height, so the frame grows and shrinks with the form.
|
|
206
|
+
- **Live edits.** Changing the extension sources, or the sources the schema declares, sends a `sources` message with only the changed files once the frame is ready, without remounting the iframe. Changing the manifest, `sourceProvider` or `policy` re-sends `open` instead, because a worker session cannot swap those.
|
|
207
|
+
- **Without React.** `formPageOpen` from `@flanksource/page-runtime/form` is the pure generator behind `PageForm` (pass `schema` to include its declared sources); it returns the `open` message (`{ page, manifest, files, sourceProvider?, policy? }`) for use with `PageFrame` directly, and `formSourceUpdate` computes the `sources`/`open` message between two such messages.
|
|
208
|
+
|
|
209
|
+
### 4. Write a page
|
|
210
|
+
|
|
211
|
+
A page is an `.mdx` or `.tsx` module whose default export receives `{ data, host }` (`host.emit(event, payload)` sends a JSON payload to the embedding host, see [Page-to-host messages](#page-to-host-messages)). It imports managed libraries and allowed dependencies by their normal npm specifiers:
|
|
212
|
+
|
|
213
|
+
```mdx
|
|
214
|
+
import { Gauge } from "@flanksource/clicky-ui/data";
|
|
215
|
+
import { DataFlowDiagram } from "@flanksource/facet";
|
|
216
|
+
|
|
217
|
+
export const meta = { tailwind: "dynamic" };
|
|
218
|
+
|
|
219
|
+
# Cluster {props.data.cluster}
|
|
220
|
+
|
|
221
|
+
<Gauge value={72} max={100} className="w-48" />
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
`export const meta = { tailwind: "dynamic" }` opts the page into runtime Tailwind; without it the page is styled by its libraries' published stylesheets alone. A page may also export `ready` (a promise or function) to extend readiness.
|
|
225
|
+
|
|
226
|
+
### 5. Publish a library as a runtime package
|
|
227
|
+
|
|
228
|
+
Build the library's normal ESM `dist`, then generate its fragment:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
import { buildLibraryRuntime } from "@flanksource/page-runtime-build";
|
|
232
|
+
|
|
233
|
+
await buildLibraryRuntime({
|
|
234
|
+
libraryDir: "/workspace/my-library",
|
|
235
|
+
outDir: "/workspace/my-library/dist/runtime",
|
|
236
|
+
base: clickyUiFragment, // inherit clicky-ui's React family
|
|
237
|
+
aliases: { "@example": "my-library" }, // optional page-facing aliases
|
|
238
|
+
});
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Or from this package's directory:
|
|
242
|
+
|
|
243
|
+
```sh
|
|
244
|
+
pnpm exec tsx scripts/build-library-runtime.ts /workspace/my-library ../ui/dist/runtime/importmap.json
|
|
245
|
+
pnpm run check-fragment /workspace/my-library ../ui/dist/runtime/importmap.json
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
The checker re-traces `dist` and fails on missing mappings, Node built-ins, unconverted CommonJS or a base remap. Libraries can additionally declare export-time remark plugins (`pageRuntime.compiler` in `package.json`) and a `./page-styles` recipe for runtime Tailwind. See [src/build/README.md](src/build/README.md) and [src/frame/tailwind/README.md](src/frame/tailwind/README.md).
|
|
249
|
+
|
|
250
|
+
### Local development
|
|
251
|
+
|
|
252
|
+
- **Local package origin.** `pnpm --filter @flanksource/page-runtime run serve-packages --package ../ui --package ../../../facet` serves built workspace packages in the registry and unpkg layouts, proxying everything else upstream. See [src/dev/README.md](src/dev/README.md) and [PACKAGE_PUBLICATIONS.md](PACKAGE_PUBLICATIONS.md) for atomic, generation-pinned publications.
|
|
253
|
+
- **Same-origin mode.** `origin="same-origin-local"` on `PageFrame` allows `runtimeOrigin={location.origin}` on localhost only. It is a convenience, not an isolation boundary.
|
|
254
|
+
- **Checkout links.** Replace a managed library with checkout source for one session; see [src/link/README.md](src/link/README.md).
|
|
255
|
+
|
|
256
|
+
### Subpath exports
|
|
257
|
+
|
|
258
|
+
| Export | Contents |
|
|
259
|
+
|---|---|
|
|
260
|
+
| `./host` | `PageFrame`, `PageForm`, bridge, validation, activity/build log, cache clearing, remote review |
|
|
261
|
+
| `./form` | `formPageOpen`, `formSourceUpdate`: the pure generator for pages that render a `JsonSchemaForm` with runtime extensions |
|
|
262
|
+
| `./host/link` | Host side of developer checkout links |
|
|
263
|
+
| `./frame`, `./frame/agent` | Frame bootstrap and in-frame agent (bundled into `dist/static`) |
|
|
264
|
+
| `./sw` | Service worker (bundled into `dist/static/sw.js`) |
|
|
265
|
+
| `./build`, `./build/dependencies` | Library fragment builder and installed-dependency runtimes (also `@flanksource/page-runtime-build`) |
|
|
266
|
+
| `./publication`, `./publication/cli`, `./catalog` | Atomic local package publications and their catalog schema |
|
|
267
|
+
| `./dev` | Node package registry/origin for development |
|
|
268
|
+
| `./link`, `./link/build` | Link channel and link builder |
|
|
269
|
+
| `./schema` | Shared contracts: `Fragment`, `PageLock`, `RuntimeProfile`, bridge messages |
|
|
270
|
+
| `./markers`, `./editing` | Source-location markers and editing helpers |
|
|
271
|
+
| `./static/*` | Built runtime origin files |
|
|
272
|
+
|
|
273
|
+
## Structure of a runtime package
|
|
274
|
+
|
|
275
|
+
A runtime package is **an ordinary npm package plus a `dist/runtime/` directory**. Nothing about the package's own ESM files changes: the browser loads them byte-for-byte from the package origin. What the runtime directory adds is the metadata a browser needs to link those files into an exact, locked dependency graph, plus ESM conversions of any CommonJS dependencies.
|
|
276
|
+
|
|
277
|
+
```
|
|
278
|
+
@flanksource/clicky-ui@0.3.57/
|
|
279
|
+
├── package.json # exports, sideEffects, pageRuntime.compiler
|
|
280
|
+
├── dist/
|
|
281
|
+
│ ├── index.js, data.js, components.js, … # normal ESM build, served as-is
|
|
282
|
+
│ ├── styles.css
|
|
283
|
+
│ └── runtime/
|
|
284
|
+
│ ├── importmap.json # the fragment
|
|
285
|
+
│ ├── barrels.json # barrel export index
|
|
286
|
+
│ ├── react@18.3.1/ # CommonJS dependencies converted to ESM
|
|
287
|
+
│ ├── react-dom@18.3.1/
|
|
288
|
+
│ ├── scheduler@0.23.2/
|
|
289
|
+
│ ├── prop-types@15.8.1/
|
|
290
|
+
│ ├── …
|
|
291
|
+
│ └── _chunks/ # shared chunks of the converted set
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### The fragment (`importmap.json`)
|
|
295
|
+
|
|
296
|
+
```jsonc
|
|
297
|
+
{
|
|
298
|
+
"fragment": 1,
|
|
299
|
+
"name": "@flanksource/clicky-ui",
|
|
300
|
+
// top-level specifiers a page may import → package-relative URL at an exact version
|
|
301
|
+
"imports": {
|
|
302
|
+
"@flanksource/clicky-ui/data": "@flanksource/clicky-ui@0.3.57/dist/data.js",
|
|
303
|
+
"react": "@flanksource/clicky-ui@0.3.57/dist/runtime/react@18.3.1/index.js"
|
|
304
|
+
},
|
|
305
|
+
// one scope per package version: the bare imports that package's own files make
|
|
306
|
+
"scopes": {
|
|
307
|
+
"@ai-sdk/gateway@3.0.127/": {
|
|
308
|
+
"@ai-sdk/provider": "@ai-sdk/provider@3.0.10/dist/index.mjs",
|
|
309
|
+
"zod": "zod@4.6.7/index.js"
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
// ESM files that import their own CommonJS build by relative path → converted URL
|
|
313
|
+
"remaps": {
|
|
314
|
+
"@opentelemetry/api@1.9.1/build/esm/api/context": "@opentelemetry/api@1.9.1/build/esm/api/context.js"
|
|
315
|
+
},
|
|
316
|
+
"aliases": {}, // page-facing prefixes, e.g. facet's "@facet"
|
|
317
|
+
"inherits": [], // specifiers left to a base fragment's top level (facet inherits React from clicky-ui)
|
|
318
|
+
"integrity": { // sha384 for every traced and converted file
|
|
319
|
+
"@ai-sdk/gateway@3.0.127/dist/index.mjs": "sha384-d7rdZc8N…"
|
|
320
|
+
},
|
|
321
|
+
"compiler": { "remark": [] } // optional export-time remark plugins
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
For scale, clicky-ui's fragment currently has 40 top-level imports, 203 scopes, 329 remaps and about 4,600 integrity entries.
|
|
326
|
+
|
|
327
|
+
| Field | Why it exists |
|
|
328
|
+
|---|---|
|
|
329
|
+
| `imports` | The public surface: every subpath export plus the shared React runtime set |
|
|
330
|
+
| `scopes` | npm installs nested versions (`react-is@16` and `react-is@17` side by side). A flat import map can't express that; per-package-version scopes can |
|
|
331
|
+
| `remaps` | Published packages often contain extensionless or CommonJS-targeting relative imports that name no real file; remaps point them at the real or converted file |
|
|
332
|
+
| `aliases` | Lets a library expose a short page-facing prefix without a second package |
|
|
333
|
+
| `inherits` | Declares which specifiers a dependent library takes from its base, so it can't ship a second React |
|
|
334
|
+
| `integrity` | Doubles as the file listing the resolver checks against, and the SRI hash the worker verifies |
|
|
335
|
+
| `compiler` | Remark plugins applied only when exporting HTML/PDF |
|
|
336
|
+
|
|
337
|
+
### The barrel index (`barrels.json`)
|
|
338
|
+
|
|
339
|
+
```jsonc
|
|
340
|
+
// positions are illustrative
|
|
341
|
+
{
|
|
342
|
+
"modules": ["@flanksource/clicky-ui@0.3.57/dist/lib/utils.js", "…"],
|
|
343
|
+
"barrels": {
|
|
344
|
+
"@flanksource/clicky-ui/data": { "Gauge": 412, "Badge": 37, "Tag": [55, "Badge"] }
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
For each entry barrel, every named export maps to its defining module (`modules[pos]`) or to `[pos, importName]` when re-exported under another name. The worker rewrites `import { Gauge } from "@flanksource/clicky-ui/data"` to import the defining module directly, so a page touching one component doesn't load the whole barrel graph. This happens only where the package's `sideEffects` declaration permits skipping the forwarding module; a direct side-effect import keeps the module intact.
|
|
350
|
+
|
|
351
|
+
### The base set and inheritance
|
|
352
|
+
|
|
353
|
+
clicky-ui's fragment defines the base set: React, `react/jsx-runtime`, `react/jsx-dev-runtime`, React DOM and `react-dom/client`, scheduler, React Query and jotai, always built with `NODE_ENV=development`. A library built with `base: clickyUiFragment` lists those specifiers under `inherits` and imports them through the base; it can't remap them. That is what guarantees one React and one renderer across every library on a page.
|
|
354
|
+
|
|
355
|
+
### Composition into a `PageLock`
|
|
356
|
+
|
|
357
|
+
When a page opens without a lock, the worker resolves each `managed` library and page dependency against the registry and profile policy, fetches the fragments, and merges them in deterministic library order (a base before the libraries that extend it, ties by name) into a lock:
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
type PageLock = {
|
|
361
|
+
lock: 2;
|
|
362
|
+
packageOrigin: string; // generation-bound origin used for every fetch
|
|
363
|
+
page: string;
|
|
364
|
+
versions: Record<string, string>; // exact resolved versions
|
|
365
|
+
map: { imports; scopes }; // the composed browser import map
|
|
366
|
+
integrity: Record<string, string>;
|
|
367
|
+
remaps: Record<string, string>;
|
|
368
|
+
barrels: BarrelIndex;
|
|
369
|
+
listings?: Record<string, string>; // file listings for plain ESM extras
|
|
370
|
+
compiler?: Record<string, CompilerPlugins>;
|
|
371
|
+
};
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
Unmanaged ESM dependencies (for example `date-fns`) don't need a fragment: the worker reads their `?meta` file listing from the package origin and resolves their files directly.
|
|
375
|
+
|
|
376
|
+
## How it differs from other packaging formats
|
|
377
|
+
|
|
378
|
+
The runtime package is **not a new module format**. Its modules are standard ESM. The difference is where linking happens and what metadata travels with the package.
|
|
379
|
+
|
|
380
|
+
| | Unit shipped | Who resolves bare specifiers | Nested versions | Shared singletons (one React) | Integrity | CommonJS | Granularity | Build per page |
|
|
381
|
+
|---|---|---|---|---|---|---|---|---|
|
|
382
|
+
| **Plain ESM package** | Source modules | A bundler or Node at install/build time | Yes, via `node_modules` | Only after bundling/dedupe | Lockfile, not in the browser | Unsupported in browsers | Per module, after bundling | Yes |
|
|
383
|
+
| **CommonJS package** | `require` modules | Node/bundler | Yes | Bundler dedupe | Lockfile | Native | Whole file | Yes |
|
|
384
|
+
| **UMD / IIFE bundle** | One file per package, globals | Nobody (globals) | No | Fragile (`window.React`) | Optional SRI per file | Pre-bundled | Whole bundle | No, but no tree-shaking or versioning |
|
|
385
|
+
| **App bundle (Vite/webpack)** | Chunks of the whole app | The bundler, ahead of time | Flattened/deduped | Yes, within one bundle | Per chunk if configured | Converted at build | Tree-shaken | Yes, every change |
|
|
386
|
+
| **Transforming CDN (esm.sh-style)** | Per-request rebuilt ESM | The CDN, at request time | Partly; CDN-chosen versions | Needs `?external`/`?deps` hints | Rarely | Converted on the fly by the CDN | Per package | No, but output is CDN-controlled |
|
|
387
|
+
| **Import-map CDN (jspm-style)** | Original ESM + generated map | The browser, via an import map | Yes, via scopes | Only if the map is generated together | Optional | CDN conversion | Per module | No |
|
|
388
|
+
| **Runtime package** | Original ESM + fragment + converted CJS | The browser, via the composed `PageLock` map | Yes, one scope per package version | Guaranteed by the base set and `inherits` | `sha384` on every file, verified by the worker | Converted once at library build time, published with the library | Per defining module, via the barrel index | No |
|
|
389
|
+
|
|
390
|
+
The practical differences:
|
|
391
|
+
|
|
392
|
+
- **Linking moves to the browser, but stays deterministic.** Unlike a bundle, nothing is precompiled per page. Unlike a transforming CDN, the bytes the browser runs are exactly the published package files or conversions the library author built and tested, with hashes to prove it.
|
|
393
|
+
- **The library author owns its closure.** The fragment records the dependency closure as installed by the library's own lockfile, nested versions included. A page can't accidentally get a different `react-is` than the library was tested with.
|
|
394
|
+
- **Singletons are a contract, not a dedupe heuristic.** `inherits` plus the base set make "one React" a property checked at build time (`check-fragment`), rather than something a bundler's dedupe may or may not achieve.
|
|
395
|
+
- **CommonJS is handled once, offline.** Converted modules ship inside `dist/runtime/` with their own integrity; there is no converter in the hot path unless the profile configures `convert` for unmanaged packages.
|
|
396
|
+
- **Tree-shaking without a bundler.** The barrel index gives the browser the information a bundler would use, at the granularity of the defining module.
|
|
397
|
+
- **Composable.** Fragments stack: facet inherits clicky-ui, a product library can inherit facet, and the worker composes them per page.
|
|
398
|
+
|
|
399
|
+
## The compiler in the worker
|
|
400
|
+
|
|
401
|
+
The service worker compiles page sources and linked library sources on request:
|
|
402
|
+
|
|
403
|
+
1. **Lexing.** `es-module-lexer` finds every static and dynamic import.
|
|
404
|
+
2. **TS/TSX/JSX.** Sucrase strips types and transforms JSX in development mode. Before Sucrase runs, a pass over Sucrase's tokenizer writes `data-source="file:line:col"` into each host JSX element.
|
|
405
|
+
3. **MDX.** `@mdx-js/mdx` with `remark-gfm`, `remark-frontmatter`, GitHub alerts and `rehype-raw` compiles straight to JavaScript with its own `jsxDEV` positions; Sucrase never runs after MDX, so `.mdx` source positions survive.
|
|
406
|
+
4. **JSON and CSS** become modules inside the worker.
|
|
407
|
+
5. **Import rewriting.** Specifiers are resolved through the lock's import map, scopes, remaps and barrel index, and rewritten to canonical runtime URLs (`/src/…`, `/npm/…`, `/link/…`).
|
|
408
|
+
6. **Tailwind candidates.** Class candidates are extracted per module and stored so the frame can generate CSS before first render.
|
|
409
|
+
|
|
410
|
+
Compiled output is cached by source hash, so an edit recompiles one module.
|
|
411
|
+
|
|
412
|
+
## WebAssembly
|
|
413
|
+
|
|
414
|
+
WebAssembly is used where it is safe and fast, kept out of frames by default, and available to pages that explicitly opt in.
|
|
415
|
+
|
|
416
|
+
### Inside the runtime
|
|
417
|
+
|
|
418
|
+
- **Service worker: WASM.** The worker uses `es-module-lexer`'s WebAssembly build (`await init()`, then `parse`) to scan imports for every compiled module and every converted file. The worker is not governed by the frame's CSP, so it gets the fast lexer.
|
|
419
|
+
- **Frames: no WASM.** Frame documents ship with `script-src 'self' 'nonce-…'` and no `'wasm-unsafe-eval'`. Anything that runs inside a frame therefore must not compile WebAssembly:
|
|
420
|
+
- `export-compiler.js` is built against `es-module-lexer/js`, the asm.js build of the same lexer, through a resolve plugin in `scripts/build-static.ts`.
|
|
421
|
+
- Runtime Tailwind uses the pure-JavaScript `tailwindcss` compiler in the frame, not the native/WASM oxide scanner. Candidates come from the worker's lexer pass instead of a filesystem scan.
|
|
422
|
+
- **Library builds: native, not WASM.** `@flanksource/page-runtime-build` traces dependency closures with rolldown's native resolver and parser in Node. None of that reaches the browser.
|
|
423
|
+
|
|
424
|
+
The runtime itself never needs `'wasm-unsafe-eval'`.
|
|
425
|
+
|
|
426
|
+
### Letting pages use WebAssembly
|
|
427
|
+
|
|
428
|
+
Page code that depends on WebAssembly (a parser, a compression or image library, a query engine) can be enabled per deployment in `profile.json`:
|
|
429
|
+
|
|
430
|
+
```json
|
|
431
|
+
{ "csp": { "add": {}, "wasm": true } }
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
This adds `'wasm-unsafe-eval'` to the frame's `script-src`. It permits `WebAssembly.compile`/`instantiate` only; it never enables JavaScript `eval` or `new Function`. It is off when omitted, and because the profile is read at worker start, changing it requires a worker upgrade/reload.
|
|
435
|
+
|
|
436
|
+
`.wasm` files from packages are served with `Content-Type: application/wasm`, so streaming instantiation works:
|
|
437
|
+
|
|
438
|
+
```tsx
|
|
439
|
+
import wasmUrl from "some-wasm-package/dist/engine.wasm?url";
|
|
440
|
+
|
|
441
|
+
export default function Page() {
|
|
442
|
+
const [result, setResult] = useState<number>();
|
|
443
|
+
useEffect(() => {
|
|
444
|
+
WebAssembly.instantiateStreaming(fetch(wasmUrl)).then(({ instance }) =>
|
|
445
|
+
setResult((instance.exports.compute as (n: number) => number)(42)));
|
|
446
|
+
}, []);
|
|
447
|
+
return <p>{result}</p>;
|
|
448
|
+
}
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
Treat `wasm: true` as a widening of the sandbox: enable it only for deployments whose pages need it, and keep `network.allow` tight, since WebAssembly modules can still `fetch` through their JavaScript glue.
|
|
452
|
+
|
|
453
|
+
## Security model
|
|
454
|
+
|
|
455
|
+
- **Separate runtime origin.** Page code never runs on the host origin; the frame is sandboxed (`allow-scripts allow-same-origin allow-modals`) on `runtimeDomain`, embeddable only by `embedders`.
|
|
456
|
+
- **Strict CSP.** `script-src 'self'` plus a per-document nonce; no `eval`, no WebAssembly unless `csp.wasm`; extra sources only through `csp.add`.
|
|
457
|
+
- **Validated bridge.** Every message is a `page-runtime-v1` envelope checked for exact origin, source window and a fresh per-embedding session.
|
|
458
|
+
- **Egress control.** Page network access is limited to `network.allow`.
|
|
459
|
+
- **Supply chain.** `policy` restricts which packages may be resolved, `minReleaseAgeDays` refuses freshly published versions, and integrity is verified on every file.
|
|
460
|
+
- **Links are double-gated.** Checkout links require both `profile.links: "allow"` and the host's `developerOverrides: true`, and they are session-only.
|
|
461
|
+
|
|
462
|
+
Hosting details are in [static/README.md](static/README.md).
|
|
463
|
+
|
|
464
|
+
## Caching and storage
|
|
465
|
+
|
|
466
|
+
- **Cache Storage**, versioned by runtime version: opened pages and their locks (reused while the folder manifest is unchanged), fetched and compiled modules, and generated Tailwind CSS under `/__runtime/tailwind/<key>.css`. Old versions' caches are deleted when a new worker activates.
|
|
467
|
+
- **IndexedDB `page-runtime`**, shared by worker and frame: per-module Tailwind candidates and Tailwind CSS records with bounded per-library eviction.
|
|
468
|
+
- **Partitioning.** Browsers partition the frame's storage by the embedding site, so caches are per (host site, runtime origin), not global.
|
|
469
|
+
|
|
470
|
+
`clearPageRuntimeCache` from `./host` resets them. Details: [src/frame/tailwind/README.md](src/frame/tailwind/README.md).
|
|
471
|
+
|
|
472
|
+
## Testing
|
|
473
|
+
|
|
474
|
+
From `packages/page-runtime`:
|
|
475
|
+
|
|
476
|
+
```sh
|
|
477
|
+
pnpm run check # tsc + oxlint
|
|
478
|
+
pnpm test # vitest unit tests (jsdom, fake-indexeddb)
|
|
479
|
+
pnpm test tailwind # one slice by name
|
|
480
|
+
pnpm run test:browser # real-browser tests (worker, frame, links, Tailwind parity)
|
|
481
|
+
gavel test --work-dir . --framework vitest --recursive=false -- src/build
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
The browser suites need built `packages/ui/dist` (and facet's `dist` for the Tailwind parity pages).
|
|
485
|
+
|
|
486
|
+
## Further reading
|
|
487
|
+
|
|
488
|
+
- [src/build/README.md](src/build/README.md) — library fragments, dependency runtimes, export compiler plugins, publishing `@flanksource/page-runtime-build`
|
|
489
|
+
- [static/README.md](static/README.md) — hosting the runtime origin, profile, CSP, bridge, locks
|
|
490
|
+
- [src/frame/tailwind/README.md](src/frame/tailwind/README.md) — page-styles recipes, runtime Tailwind, cache identity and storage
|
|
491
|
+
- [src/link/README.md](src/link/README.md) — developer checkout links
|
|
492
|
+
- [src/dev/README.md](src/dev/README.md) — local registry and package origin
|
|
493
|
+
- [PACKAGE_PUBLICATIONS.md](PACKAGE_PUBLICATIONS.md) — atomic local package publications and generations
|
|
494
|
+
- [src/frame/agent/README.md](src/frame/agent/README.md) — in-frame agent, picking and design edits
|
|
495
|
+
- [src/host/review/README.md](src/host/review/README.md) — remote review
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Library runtime build
|
|
2
|
+
|
|
3
|
+
The shared builder ships compiled JavaScript and declarations as `@flanksource/page-runtime-build`. Build its distribution from the clicky-ui workspace before packing it:
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm --dir packages/page-runtime run build:builder
|
|
7
|
+
mkdir -p .tmp
|
|
8
|
+
pnpm --dir packages/page-runtime/dist/build --config.ignoreScripts=true pack --out ../../../../.tmp/page-runtime-build.tgz
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The package is assembled from `builder-package.json`; it excludes browser host dependencies. The parent clicky-ui repository publishes it through `.github/workflows/release.yml` on pushes to `main`, alongside the Clicky UI release matrix. The workflow selects the next stable patch from registry history, updates `builder-package.json`, builds and tests the compiler, publishes `dist/build` with provenance, then commits the version and creates a `page-runtime-build@<version>` tag. First publication uses the version already declared in the manifest. Registry authentication and network failures stop the release.
|
|
12
|
+
|
|
13
|
+
The parent CI workflow's `runtime-builder` job builds and tests the same package on pull requests and uploads `page-runtime-build.tgz` for inspection. Downstream dependency and lockfile updates follow a successful CI publication. The workspace's `@flanksource/page-runtime/build` export uses the same compiled implementation.
|
|
14
|
+
|
|
15
|
+
Configure the npm trusted publisher for `@flanksource/page-runtime-build` with organization `flanksource`, repository `clicky-ui`, and workflow filename `release.yml`. The package's repository metadata matches that parent repository for provenance. See [npm trusted publishing](https://docs.npmjs.com/trusted-publishers/).
|
|
16
|
+
|
|
17
|
+
## Installed dependency runtimes
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { buildDependencyRuntime, runtimeEntries } from "@flanksource/page-runtime-build/dependencies";
|
|
21
|
+
|
|
22
|
+
const root = "/workspace/render-environment";
|
|
23
|
+
await buildDependencyRuntime({
|
|
24
|
+
root,
|
|
25
|
+
outDir: `${root}/page-runtime/browser`,
|
|
26
|
+
entries: runtimeEntries({ root, libraries: ["@flanksource/facet"] }),
|
|
27
|
+
target: "browser",
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`root` contains the installed dependency environment and its lockfile. `entries` contains package specifiers and subpaths; `runtimeEntries` combines the React base set with a library's published fragment. `target` selects Node or browser resolution conditions. `outDir` receives relocatable ESM chunks, styles, assets and `runtime.json` with entry mappings, integrity and input hashes. CommonJS entry facades expose both named exports and the original default value. `external` maps shared base entries to precompiled modules, preserving React identity across additional dependency closures. `exports` optionally limits an entry to requested bindings; barrel indexes keep unrelated optional features outside that closure. The compiler uses published indexes where available and derives them from package source otherwise, honoring the package's side-effect declarations.
|
|
32
|
+
|
|
33
|
+
Absent dependencies declared through `optionalDependencies` or optional peer metadata remain external only for dynamic `import()` calls. The import rejects at runtime if called, so the library retains control of its optional feature. Missing static imports and undeclared dynamic dependencies fail compilation.
|
|
34
|
+
|
|
35
|
+
`ensureDependencyRuntime` takes the same options with `cacheDir` in place of `outDir`. It serializes competing processes, keys compilation by the lockfile, compiler, ABI, target and entry selection, validates library input hashes on reuse, and publishes immutable generations. Older generations remain available to active readers. Compilation failures leave the previous pointer intact.
|
|
36
|
+
|
|
37
|
+
`dependencyRuntimePlugin({ root, cacheDir, base })` connects a prepared runtime to Vite or Rolldown. The base includes its manifest plus `root`. Node builds import its modules externally; browser development serves its ESM modules directly. Additional package imports compile into the shared cache and reuse the base's React family. CSS imports are passed to the host's stylesheet pipeline.
|
|
38
|
+
|
|
39
|
+
## Library fragments
|
|
40
|
+
|
|
41
|
+
Build the library's normal ESM dist first, then call `buildLibraryRuntime` from `@flanksource/page-runtime-build`. It traces the installed dependency closure, preserving installed versions and nested dependency scopes. Converted CommonJS packages import other packages through the fragment, so the React family stays shared. Browser files under dist are checked even when an entry barrel does not reach them.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { buildLibraryRuntime } from "@flanksource/page-runtime-build";
|
|
45
|
+
|
|
46
|
+
const { fragment, barrels } = await buildLibraryRuntime({
|
|
47
|
+
libraryDir: "/workspace/my-library",
|
|
48
|
+
outDir: "/workspace/my-library/dist/runtime",
|
|
49
|
+
base: clickyUiFragment,
|
|
50
|
+
aliases: { "@example": "my-library" },
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`libraryDir` contains package.json, the built dist and the installed dependencies selected by the library's lockfile. `outDir` receives importmap.json, barrels.json and development modules. Publish that directory as dist/runtime; fragment URLs name the actual published dist/runtime paths. `base` is the optional fragment whose top-level packages this library inherits. A dependent library cannot replace those packages. `aliases` declares optional page-facing aliases for this library's own specifier prefixes.
|
|
55
|
+
|
|
56
|
+
The clicky-ui package defines the base set: React, its JSX runtimes, React DOM and its client entry, scheduler, React Query and jotai. Normal npm exports and React peers keep their existing behavior. Barrel shortcuts require the containing package's sideEffects declaration to permit skipping the forwarding module; a direct side-effect import keeps that module intact.
|
|
57
|
+
|
|
58
|
+
The runtime always builds React and its dependency closure with `NODE_ENV=development`, including published runtime sets and frames embedded in deployed applications. Runtime modules live directly under `dist/runtime/`, without environment subdirectories or URL placeholders. Owner metadata, source locations and warnings remain available in every frame.
|
|
59
|
+
|
|
60
|
+
Run from packages/page-runtime:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
pnpm exec tsx scripts/build-library-runtime.ts ../ui
|
|
64
|
+
pnpm run check-fragment ../ui
|
|
65
|
+
gavel test --work-dir . --framework vitest --recursive=false -- src/build
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
A library inheriting clicky-ui can pass its fragment to either CLI:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
pnpm exec tsx scripts/build-library-runtime.ts /workspace/my-library ../ui/dist/runtime/importmap.json
|
|
72
|
+
pnpm run check-fragment /workspace/my-library ../ui/dist/runtime/importmap.json
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The checker validates the written fragment against a fresh dist trace and checks converted files' integrity and imports. Without a base argument, declared inherited specifiers count as mapped; with a base it also verifies that the base provides them. Missing mappings, Node built-ins and unconverted CommonJS fail the check. Build failures use NodeBuiltinError, CommonJsConversionError and BaseSetRemapError for those named failure classes.
|
|
76
|
+
|
|
77
|
+
## Export compiler plugins
|
|
78
|
+
|
|
79
|
+
A library can contribute remark plugins that run when a Markdown or MDX page is exported to HTML or PDF. Live previews never run them. Declare them in package.json under `pageRuntime.compiler`. Each `module` must be one of the library's own `exports`, and its default export is the plugin. `options` is the JSON value the plugin is applied with:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"exports": { "./remark/facet-redact": "./remark/facet-redact.mjs" },
|
|
84
|
+
"pageRuntime": {
|
|
85
|
+
"compiler": {
|
|
86
|
+
"remark": [{ "module": "@flanksource/facet/remark/facet-redact", "options": { "allow": {} } }]
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The build traces each plugin module, even outside dist, and writes the declaration to the fragment's `compiler` field. A page's lock collects every library's declaration in library order. A local checkout link replaces its library's entry. On export, the frame loads the export compiler and each plugin through the page's import map, with integrity checks. It compiles the page with the base plugins followed by the declared ones, and renders that result in place of the live page for the snapshot or print. Exporting a page that imports other Markdown fails, because the worker compiles imported Markdown without these plugins. An undeclared module, an empty list, or another package's module fails the build.
|
|
93
|
+
|
|
94
|
+
A library exporting `./page-styles` declares `runtimeDependencies: string[]` on its default or named `pageStyles` recipe. List the package specifiers and recipe-relative files its CSS compiler fetches, including stylesheets, Tailwind plugins and presets. The build imports the recipe from dist and traces these dependencies from that module, including transitive CSS imports and assets, scoped mappings and integrity. The checker reads the same declaration. The recipe's `base` stylesheets are linked on every page and its `entry` holds only compiler inputs; see `src/frame/tailwind/README.md`. Browser field mappings apply before built-in rejection: `false` maps to a generated empty module, while a string resolves to its browser target.
|
|
95
|
+
|
|
96
|
+
Clicky UI's build appends the runtime step after both Vite passes and stylesheet generation, because the ESM Vite pass empties dist. `pnpm --filter @flanksource/clicky-ui test:runtime-set` runs only its Chromium probe through Gavel. It tests hooks and Clicky UI theme context, counts one React and one renderer in the development build, and checks that a deliberately duplicated React fails.
|