@bake-native/core 0.0.0 → 0.1.0-rc.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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/THIRD_PARTY_NOTICES.txt +548 -0
  3. package/bake-prelude.js +189 -0
  4. package/batch-contract.json +21 -0
  5. package/batch-wire.js +674 -0
  6. package/compat.json +64 -0
  7. package/controls.css +108 -0
  8. package/determinism.js +175 -0
  9. package/devtools.d.ts +13 -0
  10. package/devtools.js +73 -0
  11. package/dialogs.js +59 -0
  12. package/direct-style.js +105 -0
  13. package/direct-table.js +261 -0
  14. package/dist/bake.mjs +2211 -0
  15. package/dist/build-CMsvCcZ0.mjs +1 -0
  16. package/dist/build-CkLSZw06.mjs +5514 -0
  17. package/dist/build-tools-Dxyl6XAf.mjs +80 -0
  18. package/dist/client-Cm7o6dQd.mjs +17544 -0
  19. package/dist/compiler.mjs +4 -0
  20. package/dist/config-B1W8gfZY.mjs +12 -0
  21. package/dist/dev-Cskd8Hl4.mjs +986 -0
  22. package/dist/direct-report-Bdda-k_Y.mjs +153 -0
  23. package/dist/direct-report-Du44nNWl.mjs +1 -0
  24. package/dist/index.d.mts +176 -0
  25. package/dist/index.mjs +1 -0
  26. package/dist/intl-ROnAp2qO.mjs +75 -0
  27. package/dist/jiti-stub-CKcaShwE.mjs +5 -0
  28. package/dist/native-views-BuzB9x5H.mjs +7849 -0
  29. package/dist/octane/package.json +5 -0
  30. package/dist/package-KkUSMoji.mjs +2784 -0
  31. package/dist/react/package.json +13 -0
  32. package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
  33. package/dist/run-D2aPm54a.mjs +6632 -0
  34. package/dist/scope-8W2aIuQ9.mjs +262 -0
  35. package/dist/screens/BakeNativeScreen.native.ts +9 -0
  36. package/dist/screens/package.json +5 -0
  37. package/dom-exception.js +31 -0
  38. package/element-api.js +158 -0
  39. package/first-screen.js +30 -0
  40. package/fonts/Geist-Italic-Variable.ttf +0 -0
  41. package/fonts/Geist-Variable.ttf +0 -0
  42. package/fonts/GeistMono-Italic-Variable.ttf +0 -0
  43. package/fonts/GeistMono-Variable.ttf +0 -0
  44. package/fonts/OFL.txt +93 -0
  45. package/fonts/README.md +27 -0
  46. package/fonts/geist-italic.css +8 -0
  47. package/fonts/geist-mono-italic.css +8 -0
  48. package/fonts/geist-mono.css +8 -0
  49. package/fonts/geist.css +8 -0
  50. package/header-icons.json +53 -0
  51. package/host-runtime.js +508 -0
  52. package/image-props.js +17 -0
  53. package/index.d.ts +8 -0
  54. package/index.js +7 -0
  55. package/licenses/Apache-2.0.txt +73 -0
  56. package/locale-entry.d.ts +1 -0
  57. package/locale-entry.js +1 -0
  58. package/locale.d.ts +28 -0
  59. package/locale.js +124 -0
  60. package/markup-support.js +87 -0
  61. package/media-element.js +115 -0
  62. package/native-args-entry.js +2 -0
  63. package/native-args.d.ts +7 -0
  64. package/native-args.js +155 -0
  65. package/native-entry.js +1 -0
  66. package/native-globals.d.ts +156 -0
  67. package/native-scenarios.d.ts +17 -0
  68. package/native-scenarios.js +46 -0
  69. package/native-support.json +349 -0
  70. package/native.d.ts +52 -0
  71. package/native.js +237 -0
  72. package/package.json +142 -4
  73. package/page-elements.js +123 -0
  74. package/runtime-provides.json +412 -0
  75. package/safe-area.d.ts +1 -0
  76. package/safe-area.js +44 -0
  77. package/swift-ui/modifiers.js +164 -0
  78. package/tab-icons.js +62 -0
  79. package/templates/create/tabs/AGENTS.md +22 -0
  80. package/templates/create/tabs/README.md +43 -0
  81. package/templates/create/tabs/_gitignore +13 -0
  82. package/templates/create/tabs/android/app/build.gradle.kts +79 -0
  83. package/templates/create/tabs/android/app/src/main/AndroidManifest.xml +39 -0
  84. package/templates/create/tabs/android/app/src/main/kotlin/MainActivity.kt +35 -0
  85. package/templates/create/tabs/android/app/src/main/res/values/themes.xml +8 -0
  86. package/templates/create/tabs/android/build.gradle.kts +5 -0
  87. package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  88. package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.properties +9 -0
  89. package/templates/create/tabs/android/gradle.properties +3 -0
  90. package/templates/create/tabs/android/gradlew +248 -0
  91. package/templates/create/tabs/android/settings.gradle.kts +47 -0
  92. package/templates/create/tabs/bake.config.ts +10 -0
  93. package/templates/create/tabs/ios/App/App.swift +30 -0
  94. package/templates/create/tabs/ios/build.sh +23 -0
  95. package/templates/create/tabs/ios/project.yml +57 -0
  96. package/templates/create/tabs/macos/Package.swift +24 -0
  97. package/templates/create/tabs/macos/Sources/__SWIFT_NAME__/App.swift +146 -0
  98. package/templates/create/tabs/macos/build.sh +4 -0
  99. package/templates/create/tabs/package.json +23 -0
  100. package/templates/create/tabs/pnpm-workspace.yaml +6 -0
  101. package/templates/create/tabs/src/App.tsrx +92 -0
  102. package/templates/create/tabs/src/App.tsx +67 -0
  103. package/templates/create/tabs/src/styles.css +32 -0
  104. package/templates/create/tabs/tsconfig.json +5 -0
  105. package/templates/create/tabs/windows/Cargo.toml +29 -0
  106. package/templates/create/tabs/windows/build.rs +9 -0
  107. package/templates/create/tabs/windows/src/main.rs +52 -0
  108. package/templates/create/tabs/windows/src/shell.rs +316 -0
  109. package/templates/module/__NAME__.kt +20 -0
  110. package/templates/module/__NAME__.native.ts +24 -0
  111. package/templates/module/__NAME__.swift +25 -0
  112. package/templates/module/__NAME__.web.ts +13 -0
  113. package/templates/module/__NAME__Test.kt +26 -0
  114. package/templates/module/__NAME__Tests.swift +25 -0
  115. package/templates/module/__SNAKE__.rs +43 -0
  116. package/text.d.ts +20 -0
  117. package/text.js +100 -0
  118. package/transform-entry.js +1 -0
  119. package/transform.d.ts +15 -0
  120. package/transform.js +98 -0
  121. package/tsconfig.native.json +16 -0
  122. package/ui/scope.js +103 -0
  123. package/ui/swift.js +106 -0
  124. package/ui/ui.css +272 -0
  125. package/view-ref-writer.js +268 -0
  126. package/view-ref.d.ts +26 -0
  127. package/view-ref.js +85 -0
  128. package/views/Video.native.ts +68 -0
  129. package/views/generated/VideoViewAndroid.h +68 -0
  130. package/views/generated/VideoViewJSI.h +94 -0
  131. package/views/generated/VideoViewSpec.kt +113 -0
  132. package/views/generated/apple/VideoViewJSI/VideoViewJSIApple.cpp +40 -0
  133. package/views/generated/apple/VideoViewJSI/include/VideoViewJSIApple.h +28 -0
  134. package/views/generated/apple/VideoViewSpec/VideoViewInterop.swift +39 -0
  135. package/views/generated/apple/VideoViewSpec/VideoViewSpec.swift +186 -0
  136. package/views/generated/video_view_spec.rs +189 -0
  137. package/views.d.ts +24 -0
  138. package/views.js +45 -0
  139. package/virtual-window.js +35 -0
  140. package/visibility.js +75 -0
  141. package/web-crypto.js +84 -0
  142. package/web-fetch.js +1071 -0
  143. package/README.md +0 -3
package/native.js ADDED
@@ -0,0 +1,237 @@
1
+ // Native modules: Swift functions the host installs into this runtime as JSI host functions
2
+ // (`globalThis.native.<Module>.<member>`, before the bundle evaluates). Calls are synchronous;
3
+ // `AsyncFunction`s return Promises.
4
+ //
5
+ // import { native, host } from '@bake-native/core';
6
+ // native.Haptics.impact('light');
7
+ // const info = native.Platform.info();
8
+ // const ms = await native.Platform.delay(100);
9
+ // const off = host.on('deeplink', (url) => …); // native -> JS: a host event
10
+ // native.OrderScreen.setTip(2); // JS -> native: a module the screen registers
11
+ // const stop = native.Battery.onChange((level) => …); // a module's event (spec: onX(listener))
12
+ //
13
+ // Events (`onX(listener: (value: T) => void): Unsubscribe` in a spec): the host installs
14
+ // `native.M.onX(active)`, which starts (true) or stops (false) the module's native source; the
15
+ // values arrive as the host event `M.onX`. Here `native.M.onX(listener)` counts the listeners of
16
+ // each event: the first one starts the source, the last unsubscribe stops it, so the native side
17
+ // subscribes once however many listeners there are. On the web the module's own `onX(emit)`
18
+ // (registerWebModule) is started the same way, once, and returns its own unsubscribe.
19
+ //
20
+ // Typed by the generated declarations (`bake native types`). On the web there is no host: members
21
+ // throw E_UNAVAILABLE unless a web implementation is registered with `registerWebModule`.
22
+
23
+ import { recordRead } from './first-screen.js';
24
+
25
+ // The host installs this data property before the bundle runs. Apps using other root exports can omit the read.
26
+ const installed = /* @__PURE__ */ Reflect.get(globalThis, 'native');
27
+ const webModules = new Map();
28
+
29
+ function error(message, code) {
30
+ const err = new Error(message);
31
+ err.code = code;
32
+ return err;
33
+ }
34
+
35
+ const inert = (key) => typeof key === 'symbol' || key === 'then' || key === 'toJSON' || key === '$$typeof';
36
+
37
+ /** First-party modules (compat.mjs FIRST_PARTY_MODULES): linked only when an app's bundles use them. */
38
+ const FIRST_PARTY = ['Clipboard', 'Linking', 'Share'];
39
+
40
+ // bake build defines __BAKE_TARGET__; a plain bundler or node is the web. A native bundle drops
41
+ // the web glue's hook (it never loads the glue).
42
+ const WEB = typeof __BAKE_TARGET__ === 'undefined' || __BAKE_TARGET__ === 'web';
43
+ const webSpecs = new Map();
44
+ const checked = new WeakMap();
45
+
46
+ function lookup(name) {
47
+ if (WEB && !installed?.[name] && webSpecs.has(name) && webModules.has(name)) {
48
+ // The web module as its spec's glue wraps it: calls check their arguments (native-args.js).
49
+ const impl = webModules.get(name);
50
+ let w = checked.get(impl);
51
+ if (!w) checked.set(impl, (w = webSpecs.get(name)(impl)));
52
+ return w;
53
+ }
54
+ return installed?.[name] ?? webModules.get(name);
55
+ }
56
+
57
+ function missingModule(name) {
58
+ if (!installed && typeof globalThis.__host === 'object') {
59
+ // A runtime without host modules: the build-time snapshot bake (bake build --bake).
60
+ return error(`native.${name} is not installed in this runtime (the snapshot bake runs without native modules: keep native calls out of render, e.g. in effects or handlers, or guard them with hasNative('${name}'))`, 'E_UNAVAILABLE');
61
+ }
62
+ if (!installed) {
63
+ return error(`native.${name} is not available on web (register one with registerWebModule('${name}', {...}))`, 'E_UNAVAILABLE');
64
+ }
65
+ const known = Object.keys(installed).join(', ') || 'none';
66
+ // A first-party module is linked only when the app's build registration includes it.
67
+ const firstParty = FIRST_PARTY.includes(name)
68
+ ? `; ${name} is a first-party module the host links through the registration \`bake build\` generates (BakeNativeAppModules): rebuild the app and pass \`BakeNativeAppModules.modules()\` to the host view (Android: register it) (guides/system-modules)`
69
+ : '';
70
+ return error(`native.${name} is not registered on this host (registered: ${known})${firstParty}`, 'E_UNKNOWN_MODULE');
71
+ }
72
+
73
+ const moduleProxies = new Map();
74
+ function moduleProxy(name) {
75
+ let proxy = moduleProxies.get(name);
76
+ if (!proxy) {
77
+ proxy = new Proxy({}, {
78
+ get(_, key) {
79
+ if (inert(key)) return undefined;
80
+ // While baking (no native modules): recorded, and a member that does nothing.
81
+ if (recordRead(`native.${name}.${String(key)}`)) return () => undefined;
82
+ const m = lookup(name);
83
+ if (!m) throw missingModule(name);
84
+ if (!(key in m)) {
85
+ throw error(`native.${name} has no member "${String(key)}" (has: ${Object.keys(m).join(', ') || 'nothing'})`, 'E_UNKNOWN_FUNCTION');
86
+ }
87
+ if (EVENT.test(key) && typeof m[key] === 'function') return eventMember(name, key);
88
+ return m[key];
89
+ },
90
+ has(_, key) {
91
+ if (recordRead(`hasNative('${name}.${String(key)}')`)) return false;
92
+ const m = lookup(name);
93
+ return !!m && key in m;
94
+ },
95
+ ownKeys() {
96
+ if (recordRead(`Reflect.ownKeys(native.${name})`)) return [];
97
+ return Reflect.ownKeys(lookup(name) ?? {});
98
+ },
99
+ getOwnPropertyDescriptor(_, key) {
100
+ if (recordRead(`hasNative('${name}.${String(key)}')`)) return undefined;
101
+ const m = lookup(name);
102
+ return m ? Reflect.getOwnPropertyDescriptor(m, key) : undefined;
103
+ },
104
+ });
105
+ moduleProxies.set(name, proxy);
106
+ }
107
+ return proxy;
108
+ }
109
+
110
+ // --- module events ---
111
+ /** An event member's name (`onChange`): the spec parser allows no other member named so. */
112
+ const EVENT = /^on[A-Z]/;
113
+ /** Per event path (`M.onX`): { count, stop } of its native source. */
114
+ const events = new Map();
115
+ const eventMembers = new Map();
116
+
117
+ /** `native.M.onX(listener)`: adds a listener; the first starts the module's source. */
118
+ function eventMember(name, key) {
119
+ const path = `${name}.${key}`;
120
+ let fn = eventMembers.get(path);
121
+ if (fn) return fn;
122
+ fn = (listener) => {
123
+ if (typeof listener !== 'function') throw error(`native.${path}: expected a listener function, got ${typeof listener}`, 'E_ARGUMENT');
124
+ let state = events.get(path);
125
+ if (!state) events.set(path, (state = { count: 0, stop: null }));
126
+ const off = host.on(path, listener);
127
+ if (state.count === 0) {
128
+ try {
129
+ state.stop = startEvent(name, key, path);
130
+ } catch (e) {
131
+ off();
132
+ throw e;
133
+ }
134
+ }
135
+ state.count++;
136
+ let removed = false;
137
+ return () => {
138
+ if (removed) return;
139
+ removed = true;
140
+ off();
141
+ if (--state.count === 0) {
142
+ const stop = state.stop;
143
+ state.stop = null;
144
+ stop?.();
145
+ }
146
+ };
147
+ };
148
+ eventMembers.set(path, fn);
149
+ return fn;
150
+ }
151
+
152
+ /** Starts event `path`'s source once; returns what stops it. */
153
+ function startEvent(name, key, path) {
154
+ const mod = installed?.[name];
155
+ if (mod) {
156
+ mod[key](true);
157
+ return () => mod[key](false);
158
+ }
159
+ // The web implementation: onX(emit) -> unsubscribe.
160
+ const stop = webModules.get(name)[key]((value) => dispatchHostEvent(path, value));
161
+ return typeof stop === 'function' ? stop : null;
162
+ }
163
+
164
+ /** The listeners of a web module's event (native ones are called by the host runtime). */
165
+ function dispatchHostEvent(name, payload) {
166
+ for (const handler of [...(globalThis.__bakeHostListeners?.get(name) ?? [])]) {
167
+ try { handler(payload); } catch (e) { setTimeout(() => { throw e; }); }
168
+ }
169
+ }
170
+
171
+ /** The number of listeners of each running event (`{ 'M.onX': 2 }`): for tests. */
172
+ export function __eventListenerCounts() {
173
+ return Object.fromEntries([...events].filter(([, s]) => s.count > 0).map(([p, s]) => [p, s.count]));
174
+ }
175
+
176
+ /** `native.<Module>.<member>`: validated access to the host's modules (clear errors for typos). */
177
+ export const native = /* @__PURE__ */ new Proxy({}, {
178
+ get(_, key) {
179
+ if (inert(key)) return undefined;
180
+ return moduleProxy(key);
181
+ },
182
+ has(_, key) {
183
+ if (recordRead(`hasNative('${String(key)}')`)) return false;
184
+ return !!lookup(key);
185
+ },
186
+ ownKeys() {
187
+ if (recordRead('Reflect.ownKeys(native)')) return [];
188
+ return [...new Set([...Object.keys(installed ?? {}), ...webModules.keys()])];
189
+ },
190
+ getOwnPropertyDescriptor(_, key) {
191
+ if (recordRead(`hasNative('${String(key)}')`)) return undefined;
192
+ return lookup(key) ? { value: moduleProxy(key), enumerable: true, configurable: true } : undefined;
193
+ },
194
+ });
195
+
196
+ /** `hasNative('Haptics')`, `hasNative('Haptics.impact')`. */
197
+ export function hasNative(path) {
198
+ // While baking (no native modules): recorded, since the device may have it: a first screen
199
+ // must not depend on what the host installed.
200
+ if (recordRead(`hasNative('${path}')`)) return false;
201
+ const [mod, member] = String(path).split('.');
202
+ const m = lookup(mod);
203
+ return member === undefined ? !!m : !!m && member in m;
204
+ }
205
+
206
+ /** Web implementation of a module (used only where the host installed none, i.e. on the web). */
207
+ export function registerWebModule(name, impl) {
208
+ webModules.set(name, impl);
209
+ }
210
+
211
+ /** Generated web glue (`<Name>Spec.js`): `wrap(impl)` is the module as JS calls it. */
212
+ export function checkWebModule(name, wrap) {
213
+ webSpecs.set(name, wrap);
214
+ }
215
+
216
+ // --- host events ---
217
+ export const host = {
218
+ /** Subscribes to `root.send(name, payload)`; returns an unsubscribe function. */
219
+ on(name, handler) {
220
+ const listeners = (globalThis.__bakeHostListeners ??= new Map());
221
+ let set = listeners.get(name);
222
+ if (!set) listeners.set(name, (set = new Set()));
223
+ set.add(handler);
224
+ return () => set.delete(handler);
225
+ },
226
+ /** Root component props passed by the host (`launch(bundle, props:)`). Not in the first
227
+ * render, which is baked without them: give the root component parameter defaults instead,
228
+ * which the host's props replace right after the first commit. */
229
+ get props() {
230
+ // While baking: recorded (the bake fails with every such read) and empty.
231
+ if (globalThis.__bakeBaking) {
232
+ // Each key read is recorded by name (host.props.<key>); the props are empty.
233
+ return new Proxy({}, { get: (_, key) => (typeof key === 'string' && recordRead(`host.props.${key}`), undefined) });
234
+ }
235
+ return globalThis.__bakeInitialProps ?? {};
236
+ },
237
+ };
package/package.json CHANGED
@@ -1,11 +1,149 @@
1
1
  {
2
2
  "name": "@bake-native/core",
3
- "version": "0.0.0",
4
- "description": "Placeholder for @bake-native/core. Releases start at 0.1.0.",
5
- "license": "MIT",
3
+ "version": "0.1.0-rc.0",
4
+ "description": "Build and run baked first frames on web, iOS, Android, macOS and Windows with the bake CLI.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./index.d.ts",
9
+ "default": "./index.js"
10
+ },
11
+ "./devtools": {
12
+ "types": "./devtools.d.ts",
13
+ "default": "./devtools.js"
14
+ },
15
+ "./testing/native-scenarios": {
16
+ "types": "./native-scenarios.d.ts",
17
+ "default": "./native-scenarios.js"
18
+ },
19
+ "./native-globals": {
20
+ "types": "./native-globals.d.ts"
21
+ },
22
+ "./tsconfig.native.json": "./tsconfig.native.json",
23
+ "./package.json": "./package.json",
24
+ "./cli": "./dist/bake.mjs",
25
+ "./compiler": "./dist/compiler.mjs",
26
+ "./markup-support": "./markup-support.js",
27
+ "./host-runtime": "./host-runtime.js",
28
+ "./direct-style": "./direct-style.js",
29
+ "./view-ref": "./view-ref.js",
30
+ "./virtual-window": "./virtual-window.js",
31
+ "./image-props": "./image-props.js",
32
+ "./safe-area": {
33
+ "types": "./safe-area.d.ts",
34
+ "default": "./safe-area.js"
35
+ },
36
+ "./ui/ui.css": "./ui/ui.css",
37
+ "./ui/swift.js": "./ui/swift.js",
38
+ "./swift-ui/modifiers.js": "./swift-ui/modifiers.js",
39
+ "./swift-ui/header-icons.json": "./header-icons.json",
40
+ "./swift-ui/tab-icons.js": "./tab-icons.js"
41
+ },
42
+ "bin": {
43
+ "bake": "./dist/bake.mjs"
44
+ },
45
+ "files": [
46
+ "index.js",
47
+ "index.d.ts",
48
+ "host-runtime.js",
49
+ "markup-support.js",
50
+ "batch-wire.js",
51
+ "determinism.js",
52
+ "web-fetch.js",
53
+ "web-crypto.js",
54
+ "dom-exception.js",
55
+ "dialogs.js",
56
+ "visibility.js",
57
+ "element-api.js",
58
+ "media-element.js",
59
+ "page-elements.js",
60
+ "bake-prelude.js",
61
+ "first-screen.js",
62
+ "native.js",
63
+ "native-entry.js",
64
+ "native.d.ts",
65
+ "native-args.js",
66
+ "native-args-entry.js",
67
+ "native-args.d.ts",
68
+ "native-scenarios.js",
69
+ "native-scenarios.d.ts",
70
+ "templates",
71
+ "controls.css",
72
+ "fonts",
73
+ "licenses",
74
+ "virtual-window.js",
75
+ "image-props.js",
76
+ "safe-area.js",
77
+ "safe-area.d.ts",
78
+ "swift-ui",
79
+ "ui",
80
+ "views.js",
81
+ "views.d.ts",
82
+ "views",
83
+ "tab-icons.js",
84
+ "devtools.js",
85
+ "devtools.d.ts",
86
+ "text.js",
87
+ "text.d.ts",
88
+ "locale.js",
89
+ "locale-entry.js",
90
+ "locale-entry.d.ts",
91
+ "locale.d.ts",
92
+ "transform.js",
93
+ "transform-entry.js",
94
+ "transform.d.ts",
95
+ "direct-table.js",
96
+ "direct-style.js",
97
+ "view-ref.js",
98
+ "view-ref.d.ts",
99
+ "view-ref-writer.js",
100
+ "native-support.json",
101
+ "header-icons.json",
102
+ "batch-contract.json",
103
+ "runtime-provides.json",
104
+ "native-globals.d.ts",
105
+ "tsconfig.native.json",
106
+ "compat.json",
107
+ "dist",
108
+ "!dist/internal.mjs",
109
+ "!dist/inspect.mjs",
110
+ "THIRD_PARTY_NOTICES.txt",
111
+ "!dist/third-party.json"
112
+ ],
6
113
  "repository": {
7
114
  "type": "git",
8
115
  "url": "git+https://github.com/gronxb/bake-native.git",
9
116
  "directory": "packages/core"
117
+ },
118
+ "license": "MIT",
119
+ "engines": {
120
+ "node": ">=22.22.2"
121
+ },
122
+ "dependencies": {
123
+ "@tsrx/oxc": "0.24.0"
124
+ },
125
+ "devDependencies": {
126
+ "@optique/core": "1.4.0",
127
+ "@optique/run": "1.4.0",
128
+ "@types/node": "^24.19.4",
129
+ "octane": "0.27.1",
130
+ "tsdown": "0.23.7",
131
+ "typescript": "6.0.3",
132
+ "unconfig": "7.5.0",
133
+ "vite": "8.3.8"
134
+ },
135
+ "types": "./index.d.ts",
136
+ "optionalDependencies": {
137
+ "@bake-native/toolchain-darwin-arm64": "0.1.0-rc.0",
138
+ "@bake-native/toolchain-darwin-x64": "0.1.0-rc.0",
139
+ "@bake-native/toolchain-linux-x64": "0.1.0-rc.0",
140
+ "@bake-native/toolchain-win32-x64": "0.1.0-rc.0"
141
+ },
142
+ "publishConfig": {
143
+ "access": "public"
144
+ },
145
+ "scripts": {
146
+ "build": "pnpm run typecheck && tsdown",
147
+ "typecheck": "tsc --noEmit"
10
148
  }
11
- }
149
+ }
@@ -0,0 +1,123 @@
1
+ // document.documentElement / document.body on native targets: the document's own elements.
2
+ // `bake build` rewrites the two to __bakePageHtml / __bakePageBody and includes this module only in
3
+ // bundles that name them (__BAKE_PAGE_ELEMENTS__, fail-closed.mjs usesPageElements). What JS sets
4
+ // on them (className, classList, id, attributes, the inline style) goes to the core once per
5
+ // microtask through `send` (host-runtime.js: an `update` of their reserved batch ids): their
6
+ // selectors match it and the page styles cascade it, so a scroll lock
7
+ // (`document.body.style.overflow = 'hidden'`, as Radix sets it) stops the page scrolling as in
8
+ // Chromium. Reads give back what was set (the computed style is getComputedStyle's).
9
+
10
+ /** Installs __bakePageHtml and __bakePageBody on `global`; `send(elements)` sends their props. */
11
+ export function installPageElements(global, send) {
12
+ const pendingPage = new Set();
13
+ function flush() {
14
+ const elements = [...pendingPage];
15
+ pendingPage.clear();
16
+ send(elements);
17
+ }
18
+ const kebab = (name) => (name.startsWith('--') ? name : name.replace(/[A-Z]/g, (c) => '-' + c.toLowerCase()));
19
+ const camelName = (name) => (name.startsWith('--') ? name : name.replace(/-([a-z])/g, (_, c) => c.toUpperCase()));
20
+ function pageElement(index, tagName) {
21
+ const classes = [];
22
+ const attrs = new Map();
23
+ const style = new Map(); // camelCase -> value
24
+ let id = '';
25
+ const el = {
26
+ __index: index,
27
+ tagName,
28
+ __props() {
29
+ const props = {};
30
+ if (classes.length) props.class = classes.join(' ');
31
+ if (id) props.id = id;
32
+ for (const [k, v] of attrs) props[k] = v;
33
+ if (style.size) props.style = Object.fromEntries(style);
34
+ return props;
35
+ },
36
+ };
37
+ const changed = () => {
38
+ if (pendingPage.size === 0) Promise.resolve().then(flush);
39
+ pendingPage.add(el);
40
+ };
41
+ const setClasses = (list) => {
42
+ const next = [];
43
+ for (const c of list) if (c && !next.includes(c)) next.push(c);
44
+ if (next.length === classes.length && next.every((c, i) => c === classes[i])) return;
45
+ classes.length = 0;
46
+ classes.push(...next);
47
+ changed();
48
+ };
49
+ const parseStyle = (text) => {
50
+ style.clear();
51
+ for (const decl of String(text).split(';')) {
52
+ const at = decl.indexOf(':');
53
+ if (at > 0) style.set(camelName(decl.slice(0, at).trim()), decl.slice(at + 1).trim());
54
+ }
55
+ changed();
56
+ };
57
+ const setStyle = (name, value) => {
58
+ const key = camelName(name);
59
+ const v = value == null ? '' : String(value);
60
+ if (v === '') { if (!style.delete(key)) return; } else { if (style.get(key) === v) return; style.set(key, v); }
61
+ changed();
62
+ };
63
+ const styleTarget = {
64
+ setProperty(name, value) { setStyle(name, value); },
65
+ removeProperty(name) { const old = style.get(camelName(name)) ?? ''; setStyle(name, ''); return old; },
66
+ getPropertyValue(name) { return style.get(camelName(name)) ?? ''; },
67
+ get cssText() { return [...style].map(([k, v]) => kebab(k) + ': ' + v + ';').join(' '); },
68
+ set cssText(text) { parseStyle(text); },
69
+ };
70
+ el.style = new Proxy(styleTarget, {
71
+ get(t, k) { return k in t ? t[k] : typeof k === 'string' ? (style.get(k) ?? '') : undefined; },
72
+ set(t, k, v) { if (k === 'cssText') t.cssText = v; else setStyle(String(k), v); return true; },
73
+ });
74
+ el.classList = {
75
+ add(...names) { setClasses([...classes, ...names]); },
76
+ remove(...names) { setClasses(classes.filter((c) => !names.includes(c))); },
77
+ toggle(name, force) {
78
+ const on = force === undefined ? !classes.includes(name) : !!force;
79
+ setClasses(on ? [...classes, name] : classes.filter((c) => c !== name));
80
+ return on;
81
+ },
82
+ contains(name) { return classes.includes(name); },
83
+ replace(a, b) { if (!classes.includes(a)) return false; setClasses(classes.map((c) => (c === a ? b : c))); return true; },
84
+ item(i) { return classes[i] ?? null; },
85
+ get length() { return classes.length; },
86
+ get value() { return classes.join(' '); },
87
+ toString() { return classes.join(' '); },
88
+ };
89
+ Object.defineProperty(el, 'className', {
90
+ get() { return classes.join(' '); },
91
+ set(v) { setClasses(String(v).split(/\s+/)); },
92
+ });
93
+ Object.defineProperty(el, 'id', {
94
+ get() { return id; },
95
+ set(v) { const next = String(v); if (next !== id) { id = next; changed(); } },
96
+ });
97
+ el.setAttribute = (name, value) => {
98
+ const n = String(name).toLowerCase();
99
+ if (n === 'class') el.className = value;
100
+ else if (n === 'id') el.id = value;
101
+ else if (n === 'style') parseStyle(value);
102
+ else if (attrs.get(n) !== String(value)) { attrs.set(n, String(value)); changed(); }
103
+ };
104
+ el.getAttribute = (name) => {
105
+ const n = String(name).toLowerCase();
106
+ if (n === 'class') return classes.length ? classes.join(' ') : null;
107
+ if (n === 'id') return id || null;
108
+ if (n === 'style') return style.size ? styleTarget.cssText : null;
109
+ return attrs.has(n) ? attrs.get(n) : null;
110
+ };
111
+ el.hasAttribute = (name) => el.getAttribute(name) !== null;
112
+ el.removeAttribute = (name) => {
113
+ const n = String(name).toLowerCase();
114
+ if (n === 'class') setClasses([]);
115
+ else if (n === 'id') el.id = '';
116
+ else if (n === 'style') { if (style.size) { style.clear(); changed(); } }
117
+ else if (attrs.delete(n)) changed();
118
+ };
119
+ return el;
120
+ }
121
+ global.__bakePageHtml = pageElement(0, 'HTML');
122
+ global.__bakePageBody = pageElement(1, 'BODY');
123
+ }