@pithyjs/core 0.1.0-beta.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 (255) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +513 -0
  3. package/dist/a11y/focus-trap.d.ts +109 -0
  4. package/dist/a11y/focus-trap.d.ts.map +1 -0
  5. package/dist/a11y/focus-trap.js +246 -0
  6. package/dist/a11y/focus-trap.js.map +1 -0
  7. package/dist/a11y/index.d.ts +12 -0
  8. package/dist/a11y/index.d.ts.map +1 -0
  9. package/dist/a11y/index.js +16 -0
  10. package/dist/a11y/index.js.map +1 -0
  11. package/dist/a11y/keyboard-utils.d.ts +165 -0
  12. package/dist/a11y/keyboard-utils.d.ts.map +1 -0
  13. package/dist/a11y/keyboard-utils.js +321 -0
  14. package/dist/a11y/keyboard-utils.js.map +1 -0
  15. package/dist/a11y/roving-tabindex.d.ts +118 -0
  16. package/dist/a11y/roving-tabindex.d.ts.map +1 -0
  17. package/dist/a11y/roving-tabindex.js +249 -0
  18. package/dist/a11y/roving-tabindex.js.map +1 -0
  19. package/dist/a11y/skip-link.d.ts +55 -0
  20. package/dist/a11y/skip-link.d.ts.map +1 -0
  21. package/dist/a11y/skip-link.js +164 -0
  22. package/dist/a11y/skip-link.js.map +1 -0
  23. package/dist/compile/bindSwitchCases.d.ts +16 -0
  24. package/dist/compile/bindSwitchCases.d.ts.map +1 -0
  25. package/dist/compile/bindSwitchCases.js +323 -0
  26. package/dist/compile/bindSwitchCases.js.map +1 -0
  27. package/dist/compile/binds/bindBoundAttributes.d.ts +14 -0
  28. package/dist/compile/binds/bindBoundAttributes.d.ts.map +1 -0
  29. package/dist/compile/binds/bindBoundAttributes.js +57 -0
  30. package/dist/compile/binds/bindBoundAttributes.js.map +1 -0
  31. package/dist/compile/binds/bindClassDirectives.d.ts +20 -0
  32. package/dist/compile/binds/bindClassDirectives.d.ts.map +1 -0
  33. package/dist/compile/binds/bindClassDirectives.js +94 -0
  34. package/dist/compile/binds/bindClassDirectives.js.map +1 -0
  35. package/dist/compile/binds/bindComponentsTags.d.ts +17 -0
  36. package/dist/compile/binds/bindComponentsTags.d.ts.map +1 -0
  37. package/dist/compile/binds/bindComponentsTags.js +355 -0
  38. package/dist/compile/binds/bindComponentsTags.js.map +1 -0
  39. package/dist/compile/binds/bindConditionals.d.ts +16 -0
  40. package/dist/compile/binds/bindConditionals.d.ts.map +1 -0
  41. package/dist/compile/binds/bindConditionals.js +102 -0
  42. package/dist/compile/binds/bindConditionals.js.map +1 -0
  43. package/dist/compile/binds/bindDefaultTwoWay.d.ts +35 -0
  44. package/dist/compile/binds/bindDefaultTwoWay.d.ts.map +1 -0
  45. package/dist/compile/binds/bindDefaultTwoWay.js +128 -0
  46. package/dist/compile/binds/bindDefaultTwoWay.js.map +1 -0
  47. package/dist/compile/binds/bindDeferDirectives.d.ts +21 -0
  48. package/dist/compile/binds/bindDeferDirectives.d.ts.map +1 -0
  49. package/dist/compile/binds/bindDeferDirectives.js +465 -0
  50. package/dist/compile/binds/bindDeferDirectives.js.map +1 -0
  51. package/dist/compile/binds/bindErrorBoundary.d.ts +71 -0
  52. package/dist/compile/binds/bindErrorBoundary.d.ts.map +1 -0
  53. package/dist/compile/binds/bindErrorBoundary.js +389 -0
  54. package/dist/compile/binds/bindErrorBoundary.js.map +1 -0
  55. package/dist/compile/binds/bindEventHandlers.d.ts +17 -0
  56. package/dist/compile/binds/bindEventHandlers.d.ts.map +1 -0
  57. package/dist/compile/binds/bindEventHandlers.js +174 -0
  58. package/dist/compile/binds/bindEventHandlers.js.map +1 -0
  59. package/dist/compile/binds/bindRefDirectives.d.ts +14 -0
  60. package/dist/compile/binds/bindRefDirectives.d.ts.map +1 -0
  61. package/dist/compile/binds/bindRefDirectives.js +35 -0
  62. package/dist/compile/binds/bindRefDirectives.js.map +1 -0
  63. package/dist/compile/binds/bindTextInterpolations.d.ts +16 -0
  64. package/dist/compile/binds/bindTextInterpolations.d.ts.map +1 -0
  65. package/dist/compile/binds/bindTextInterpolations.js +408 -0
  66. package/dist/compile/binds/bindTextInterpolations.js.map +1 -0
  67. package/dist/compile/binds/directiveRegistry.d.ts +46 -0
  68. package/dist/compile/binds/directiveRegistry.d.ts.map +1 -0
  69. package/dist/compile/binds/directiveRegistry.js +50 -0
  70. package/dist/compile/binds/directiveRegistry.js.map +1 -0
  71. package/dist/compile/compileExpression.d.ts +10 -0
  72. package/dist/compile/compileExpression.d.ts.map +1 -0
  73. package/dist/compile/compileExpression.js +26 -0
  74. package/dist/compile/compileExpression.js.map +1 -0
  75. package/dist/compile/compileNodes.d.ts +32 -0
  76. package/dist/compile/compileNodes.d.ts.map +1 -0
  77. package/dist/compile/compileNodes.js +104 -0
  78. package/dist/compile/compileNodes.js.map +1 -0
  79. package/dist/compile/compileTemplate.d.ts +64 -0
  80. package/dist/compile/compileTemplate.d.ts.map +1 -0
  81. package/dist/compile/compileTemplate.js +157 -0
  82. package/dist/compile/compileTemplate.js.map +1 -0
  83. package/dist/compile/contentProjection.d.ts +29 -0
  84. package/dist/compile/contentProjection.d.ts.map +1 -0
  85. package/dist/compile/contentProjection.js +210 -0
  86. package/dist/compile/contentProjection.js.map +1 -0
  87. package/dist/compile/forLoops/bindForLoops.d.ts +21 -0
  88. package/dist/compile/forLoops/bindForLoops.d.ts.map +1 -0
  89. package/dist/compile/forLoops/bindForLoops.js +151 -0
  90. package/dist/compile/forLoops/bindForLoops.js.map +1 -0
  91. package/dist/compile/forLoops/createLoopManager.d.ts +51 -0
  92. package/dist/compile/forLoops/createLoopManager.d.ts.map +1 -0
  93. package/dist/compile/forLoops/createLoopManager.js +429 -0
  94. package/dist/compile/forLoops/createLoopManager.js.map +1 -0
  95. package/dist/compile/forLoops/index.d.ts +6 -0
  96. package/dist/compile/forLoops/index.d.ts.map +1 -0
  97. package/dist/compile/forLoops/index.js +8 -0
  98. package/dist/compile/forLoops/index.js.map +1 -0
  99. package/dist/compile/forLoops/loopHelpers.d.ts +33 -0
  100. package/dist/compile/forLoops/loopHelpers.d.ts.map +1 -0
  101. package/dist/compile/forLoops/loopHelpers.js +69 -0
  102. package/dist/compile/forLoops/loopHelpers.js.map +1 -0
  103. package/dist/compile/forLoops/loopSignals.d.ts +36 -0
  104. package/dist/compile/forLoops/loopSignals.d.ts.map +1 -0
  105. package/dist/compile/forLoops/loopSignals.js +64 -0
  106. package/dist/compile/forLoops/loopSignals.js.map +1 -0
  107. package/dist/compile/forLoops/loopTraversal.d.ts +18 -0
  108. package/dist/compile/forLoops/loopTraversal.d.ts.map +1 -0
  109. package/dist/compile/forLoops/loopTraversal.js +38 -0
  110. package/dist/compile/forLoops/loopTraversal.js.map +1 -0
  111. package/dist/compile/handleConditionalChain.d.ts +15 -0
  112. package/dist/compile/handleConditionalChain.d.ts.map +1 -0
  113. package/dist/compile/handleConditionalChain.js +181 -0
  114. package/dist/compile/handleConditionalChain.js.map +1 -0
  115. package/dist/compile/index.d.ts +15 -0
  116. package/dist/compile/index.d.ts.map +1 -0
  117. package/dist/compile/index.js +19 -0
  118. package/dist/compile/index.js.map +1 -0
  119. package/dist/compile/injectScopedStyle.d.ts +16 -0
  120. package/dist/compile/injectScopedStyle.d.ts.map +1 -0
  121. package/dist/compile/injectScopedStyle.js +50 -0
  122. package/dist/compile/injectScopedStyle.js.map +1 -0
  123. package/dist/compile/nodeExpando.d.ts +12 -0
  124. package/dist/compile/nodeExpando.d.ts.map +1 -0
  125. package/dist/compile/nodeExpando.js +4 -0
  126. package/dist/compile/nodeExpando.js.map +1 -0
  127. package/dist/compile/parseHtml.d.ts +70 -0
  128. package/dist/compile/parseHtml.d.ts.map +1 -0
  129. package/dist/compile/parseHtml.js +164 -0
  130. package/dist/compile/parseHtml.js.map +1 -0
  131. package/dist/compile/pithyTreeToDom.d.ts +16 -0
  132. package/dist/compile/pithyTreeToDom.d.ts.map +1 -0
  133. package/dist/compile/pithyTreeToDom.js +118 -0
  134. package/dist/compile/pithyTreeToDom.js.map +1 -0
  135. package/dist/compile/renderChildren.d.ts +50 -0
  136. package/dist/compile/renderChildren.d.ts.map +1 -0
  137. package/dist/compile/renderChildren.js +76 -0
  138. package/dist/compile/renderChildren.js.map +1 -0
  139. package/dist/compile/ssrMode.d.ts +17 -0
  140. package/dist/compile/ssrMode.d.ts.map +1 -0
  141. package/dist/compile/ssrMode.js +20 -0
  142. package/dist/compile/ssrMode.js.map +1 -0
  143. package/dist/compile/textValue.d.ts +28 -0
  144. package/dist/compile/textValue.d.ts.map +1 -0
  145. package/dist/compile/textValue.js +30 -0
  146. package/dist/compile/textValue.js.map +1 -0
  147. package/dist/config/index.d.ts +37 -0
  148. package/dist/config/index.d.ts.map +1 -0
  149. package/dist/config/index.js +16 -0
  150. package/dist/config/index.js.map +1 -0
  151. package/dist/index.d.ts +7 -0
  152. package/dist/index.d.ts.map +1 -0
  153. package/dist/index.js +7 -0
  154. package/dist/index.js.map +1 -0
  155. package/dist/interfaces/index.d.ts +3 -0
  156. package/dist/interfaces/index.d.ts.map +1 -0
  157. package/dist/interfaces/index.js +3 -0
  158. package/dist/interfaces/index.js.map +1 -0
  159. package/dist/interfaces/ref-signal.interface.d.ts +6 -0
  160. package/dist/interfaces/ref-signal.interface.d.ts.map +1 -0
  161. package/dist/interfaces/ref-signal.interface.js +2 -0
  162. package/dist/interfaces/ref-signal.interface.js.map +1 -0
  163. package/dist/interfaces/ref.interface.d.ts +5 -0
  164. package/dist/interfaces/ref.interface.d.ts.map +1 -0
  165. package/dist/interfaces/ref.interface.js +2 -0
  166. package/dist/interfaces/ref.interface.js.map +1 -0
  167. package/dist/runtime/applyBindings.d.ts +36 -0
  168. package/dist/runtime/applyBindings.d.ts.map +1 -0
  169. package/dist/runtime/applyBindings.js +165 -0
  170. package/dist/runtime/applyBindings.js.map +1 -0
  171. package/dist/runtime/bindings/applyComponent.d.ts +3 -0
  172. package/dist/runtime/bindings/applyComponent.d.ts.map +1 -0
  173. package/dist/runtime/bindings/applyComponent.js +190 -0
  174. package/dist/runtime/bindings/applyComponent.js.map +1 -0
  175. package/dist/runtime/bindings/applyDefer.d.ts +11 -0
  176. package/dist/runtime/bindings/applyDefer.d.ts.map +1 -0
  177. package/dist/runtime/bindings/applyDefer.js +422 -0
  178. package/dist/runtime/bindings/applyDefer.js.map +1 -0
  179. package/dist/runtime/bindings/applySimple.d.ts +11 -0
  180. package/dist/runtime/bindings/applySimple.d.ts.map +1 -0
  181. package/dist/runtime/bindings/applySimple.js +346 -0
  182. package/dist/runtime/bindings/applySimple.js.map +1 -0
  183. package/dist/runtime/bindings/applyStructural.d.ts +5 -0
  184. package/dist/runtime/bindings/applyStructural.d.ts.map +1 -0
  185. package/dist/runtime/bindings/applyStructural.js +312 -0
  186. package/dist/runtime/bindings/applyStructural.js.map +1 -0
  187. package/dist/runtime/bindings/helpers.d.ts +56 -0
  188. package/dist/runtime/bindings/helpers.d.ts.map +1 -0
  189. package/dist/runtime/bindings/helpers.js +121 -0
  190. package/dist/runtime/bindings/helpers.js.map +1 -0
  191. package/dist/runtime/bindings/index.d.ts +7 -0
  192. package/dist/runtime/bindings/index.d.ts.map +1 -0
  193. package/dist/runtime/bindings/index.js +6 -0
  194. package/dist/runtime/bindings/index.js.map +1 -0
  195. package/dist/runtime/bindings/types.d.ts +105 -0
  196. package/dist/runtime/bindings/types.d.ts.map +1 -0
  197. package/dist/runtime/bindings/types.js +5 -0
  198. package/dist/runtime/bindings/types.js.map +1 -0
  199. package/dist/runtime/bootstrap.d.ts +59 -0
  200. package/dist/runtime/bootstrap.d.ts.map +1 -0
  201. package/dist/runtime/bootstrap.js +82 -0
  202. package/dist/runtime/bootstrap.js.map +1 -0
  203. package/dist/runtime/createContextProxy.d.ts +10 -0
  204. package/dist/runtime/createContextProxy.d.ts.map +1 -0
  205. package/dist/runtime/createContextProxy.js +45 -0
  206. package/dist/runtime/createContextProxy.js.map +1 -0
  207. package/dist/runtime/deferHelpers.d.ts +49 -0
  208. package/dist/runtime/deferHelpers.d.ts.map +1 -0
  209. package/dist/runtime/deferHelpers.js +65 -0
  210. package/dist/runtime/deferHelpers.js.map +1 -0
  211. package/dist/runtime/html.d.ts +23 -0
  212. package/dist/runtime/html.d.ts.map +1 -0
  213. package/dist/runtime/html.js +97 -0
  214. package/dist/runtime/html.js.map +1 -0
  215. package/dist/runtime/index.d.ts +8 -0
  216. package/dist/runtime/index.d.ts.map +1 -0
  217. package/dist/runtime/index.js +21 -0
  218. package/dist/runtime/index.js.map +1 -0
  219. package/dist/runtime/lazy.d.ts +144 -0
  220. package/dist/runtime/lazy.d.ts.map +1 -0
  221. package/dist/runtime/lazy.js +295 -0
  222. package/dist/runtime/lazy.js.map +1 -0
  223. package/dist/runtime/module-cache.d.ts +78 -0
  224. package/dist/runtime/module-cache.d.ts.map +1 -0
  225. package/dist/runtime/module-cache.js +109 -0
  226. package/dist/runtime/module-cache.js.map +1 -0
  227. package/dist/runtime/onDestroy.d.ts +29 -0
  228. package/dist/runtime/onDestroy.d.ts.map +1 -0
  229. package/dist/runtime/onDestroy.js +66 -0
  230. package/dist/runtime/onDestroy.js.map +1 -0
  231. package/dist/runtime/parseIdentifiersFromTemplate.d.ts +17 -0
  232. package/dist/runtime/parseIdentifiersFromTemplate.d.ts.map +1 -0
  233. package/dist/runtime/parseIdentifiersFromTemplate.js +68 -0
  234. package/dist/runtime/parseIdentifiersFromTemplate.js.map +1 -0
  235. package/dist/types/globals.d.ts +18 -0
  236. package/dist/types/globals.d.ts.map +1 -0
  237. package/dist/types/globals.js +4 -0
  238. package/dist/types/globals.js.map +1 -0
  239. package/dist/types/index.d.ts +11 -0
  240. package/dist/types/index.d.ts.map +1 -0
  241. package/dist/types/index.js +2 -0
  242. package/dist/types/index.js.map +1 -0
  243. package/dist/utilities/index.d.ts +3 -0
  244. package/dist/utilities/index.d.ts.map +1 -0
  245. package/dist/utilities/index.js +3 -0
  246. package/dist/utilities/index.js.map +1 -0
  247. package/dist/utilities/ref-signal.utility.d.ts +32 -0
  248. package/dist/utilities/ref-signal.utility.d.ts.map +1 -0
  249. package/dist/utilities/ref-signal.utility.js +46 -0
  250. package/dist/utilities/ref-signal.utility.js.map +1 -0
  251. package/dist/utilities/ref.untility.d.ts +37 -0
  252. package/dist/utilities/ref.untility.d.ts.map +1 -0
  253. package/dist/utilities/ref.untility.js +47 -0
  254. package/dist/utilities/ref.untility.js.map +1 -0
  255. package/package.json +55 -0
@@ -0,0 +1,295 @@
1
+ /** @codex
2
+ {
3
+ "id": "pithy.core.lazy",
4
+ "title": "Lazy Loading Runtime Helper",
5
+ "category": "runtime"
6
+ }
7
+ */
8
+ /**
9
+ * Lazy Loading Runtime
10
+ *
11
+ * Provides the `lazy()` function for component-level lazy loading.
12
+ * Components wrapped with lazy() are only loaded when triggered by @defer directive.
13
+ *
14
+ * @module runtime/lazy
15
+ */
16
+ import { signal } from '@pithyjs/signals';
17
+ import { DEFAULT_LAZY_TIMEOUT } from '@pithyjs/compiler';
18
+ import { loadModule, invalidateModule, getModuleRecord } from './module-cache';
19
+ /** Symbol used to identify LazyHandle instances (internal) */
20
+ const LAZY_HANDLE_SYMBOL = Symbol('pithy-lazy-handle');
21
+ /** Counter for generating unique keys when path isn't available */
22
+ let lazyIdCounter = 0;
23
+ /**
24
+ * Wrap a dynamic import for lazy loading.
25
+ *
26
+ * Creates a lazy handle that can be used with the @defer directive
27
+ * to load components on-demand.
28
+ *
29
+ * @param loader - Function returning a dynamic import promise
30
+ * @param options - Configuration options
31
+ * @returns LazyHandle for controlling the lazy component
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * // Basic usage
36
+ * const UserCard = lazy(() => import('./UserCard.pithy'));
37
+ *
38
+ * // With options
39
+ * const HeavyChart = lazy(() => import('./HeavyChart.pithy'), {
40
+ * timeoutMs: 10000,
41
+ * onError: (err) => console.error('Failed to load chart', err),
42
+ * });
43
+ * ```
44
+ *
45
+ * In template with the `@defer` directive:
46
+ *
47
+ * ```html
48
+ * <div @defer="visible" @load="UserCard" @placeholder="Skeleton"></div>
49
+ * ```
50
+ */
51
+ /** @codexApi {"parent":"pithy.core.lazy","name":"lazy","stability":"stable","signature":"<T>(loader: () => Promise<T>, options?: LazyOptions) => LazyHandle<T>"} */
52
+ export function lazy(loader, options = {}) {
53
+ const { key: customKey, onError, timeoutMs = DEFAULT_LAZY_TIMEOUT, priority = 'low', } = options;
54
+ // Generate unique key for this lazy component
55
+ // Use custom key if provided (for predictable chunk naming)
56
+ // Otherwise use a counter-based key
57
+ const key = customKey
58
+ ? `component:${customKey}`
59
+ : `component:lazy-${++lazyIdCounter}`;
60
+ // Create reactive state signal
61
+ const state = signal('idle');
62
+ // Internal state
63
+ const internal = {
64
+ handle: null, // Set below
65
+ abortController: null,
66
+ timeoutId: null,
67
+ loadPromise: null,
68
+ retryCount: 0,
69
+ };
70
+ /**
71
+ * Cancel in-flight load
72
+ */
73
+ function cancel() {
74
+ if (internal.abortController) {
75
+ internal.abortController.abort();
76
+ internal.abortController = null;
77
+ }
78
+ if (internal.timeoutId) {
79
+ clearTimeout(internal.timeoutId);
80
+ internal.timeoutId = null;
81
+ }
82
+ // Only reset to idle if we were loading
83
+ if (state() === 'loading') {
84
+ state.set('idle');
85
+ }
86
+ internal.loadPromise = null;
87
+ }
88
+ /**
89
+ * Load the component
90
+ */
91
+ function load() {
92
+ // If already loaded, return cached module
93
+ const cached = getModuleRecord(key);
94
+ if (cached?.status === 'resolved' && cached.value !== undefined) {
95
+ internal.handle.module = cached.value;
96
+ state.set('loaded');
97
+ return Promise.resolve(cached.value);
98
+ }
99
+ // If already loading, return existing promise
100
+ if (internal.loadPromise && state() === 'loading') {
101
+ return internal.loadPromise;
102
+ }
103
+ // If there was an error, need to call retry() instead
104
+ if (cached?.status === 'rejected') {
105
+ return retry();
106
+ }
107
+ // Start new load
108
+ state.set('loading');
109
+ internal.abortController = new AbortController();
110
+ // Set up timeout
111
+ if (timeoutMs > 0) {
112
+ internal.timeoutId = setTimeout(() => {
113
+ const error = new Error(`Lazy load timeout: Component did not load within ${timeoutMs}ms`);
114
+ handleError(error);
115
+ }, timeoutMs);
116
+ }
117
+ // Create load promise
118
+ internal.loadPromise = loadModule(key, loader)
119
+ .then(module => {
120
+ // Clear timeout on success
121
+ if (internal.timeoutId) {
122
+ clearTimeout(internal.timeoutId);
123
+ internal.timeoutId = null;
124
+ }
125
+ // Check if cancelled
126
+ if (internal.abortController?.signal.aborted) {
127
+ throw new Error('Load cancelled');
128
+ }
129
+ // Update handle
130
+ internal.handle.module = module;
131
+ internal.handle.error = undefined;
132
+ state.set('loaded');
133
+ return module;
134
+ })
135
+ .catch(err => {
136
+ handleError(err);
137
+ throw err;
138
+ });
139
+ return internal.loadPromise;
140
+ }
141
+ /**
142
+ * Handle load error
143
+ */
144
+ function handleError(err) {
145
+ // Clear timeout
146
+ if (internal.timeoutId) {
147
+ clearTimeout(internal.timeoutId);
148
+ internal.timeoutId = null;
149
+ }
150
+ // Don't update state if cancelled
151
+ if (internal.abortController?.signal.aborted) {
152
+ return;
153
+ }
154
+ // Update handle
155
+ const error = err instanceof Error ? err : new Error(String(err));
156
+ internal.handle.error = error;
157
+ state.set('error');
158
+ // Call error callback
159
+ if (onError) {
160
+ try {
161
+ onError(error);
162
+ }
163
+ catch (callbackErr) {
164
+ if (import.meta.env?.DEV) {
165
+ console.error('[lazy] Error in onError callback:', callbackErr);
166
+ }
167
+ }
168
+ }
169
+ internal.loadPromise = null;
170
+ }
171
+ /**
172
+ * Retry after error
173
+ */
174
+ function retry() {
175
+ // Extract URL from error message for cache busting
176
+ // Browser caches dynamic imports, so we need to bust the cache with a new URL
177
+ let cacheBustUrl = null;
178
+ if (internal.handle.error) {
179
+ const errorMsg = internal.handle.error.message || '';
180
+ // Extract URL from "Failed to fetch dynamically imported module: <url>"
181
+ const urlMatch = errorMsg.match(/module:\s*(.+)$/);
182
+ if (urlMatch) {
183
+ const originalUrl = urlMatch[1].trim();
184
+ // Add or update cache-bust parameter
185
+ try {
186
+ const url = new URL(originalUrl);
187
+ url.searchParams.set('_retry', Date.now().toString());
188
+ cacheBustUrl = url.href;
189
+ }
190
+ catch {
191
+ // URL parsing failed, fall back to normal retry
192
+ }
193
+ }
194
+ }
195
+ // Clear previous error state
196
+ internal.handle.error = undefined;
197
+ // Invalidate cached error in our module cache
198
+ invalidateModule(key);
199
+ // Increment retry count
200
+ internal.retryCount++;
201
+ // Reset state
202
+ state.set('idle');
203
+ internal.loadPromise = null;
204
+ // If we have a cache-bust URL, use it to bypass browser's import cache
205
+ if (cacheBustUrl) {
206
+ return loadWithCacheBust(cacheBustUrl);
207
+ }
208
+ // Fall back to normal load
209
+ return load();
210
+ }
211
+ /**
212
+ * Load with a cache-busted URL (for retry after failure)
213
+ */
214
+ function loadWithCacheBust(url) {
215
+ // Start new load
216
+ state.set('loading');
217
+ internal.abortController = new AbortController();
218
+ // Set up timeout
219
+ if (timeoutMs > 0) {
220
+ internal.timeoutId = setTimeout(() => {
221
+ const error = new Error(`Lazy load timeout: Component did not load within ${timeoutMs}ms`);
222
+ handleError(error);
223
+ }, timeoutMs);
224
+ }
225
+ // Use dynamic import with cache-busted URL
226
+ // This bypasses both our cache and the browser's import cache
227
+ internal.loadPromise = import(/* @vite-ignore */ url)
228
+ .then(module => {
229
+ // Clear timeout on success
230
+ if (internal.timeoutId) {
231
+ clearTimeout(internal.timeoutId);
232
+ internal.timeoutId = null;
233
+ }
234
+ // Check if cancelled
235
+ if (internal.abortController?.signal.aborted) {
236
+ throw new Error('Load cancelled');
237
+ }
238
+ // Update handle BEFORE setting state (effects run synchronously)
239
+ internal.handle.module = module;
240
+ internal.handle.error = undefined;
241
+ // Update our module cache BEFORE setting state to 'loaded'
242
+ // This is critical because effects run synchronously and may call load()
243
+ // which needs to see the cached module
244
+ loadModule(key, () => Promise.resolve(module));
245
+ // Now set state to loaded (this triggers effects)
246
+ state.set('loaded');
247
+ return module;
248
+ })
249
+ .catch(err => {
250
+ handleError(err);
251
+ throw err;
252
+ });
253
+ return internal.loadPromise;
254
+ }
255
+ // Create the handle with symbol brand
256
+ const handle = {
257
+ [LAZY_HANDLE_SYMBOL]: true,
258
+ key,
259
+ load,
260
+ state,
261
+ module: undefined,
262
+ error: undefined,
263
+ cancel,
264
+ retry,
265
+ priority,
266
+ };
267
+ // Store reference for internal use
268
+ internal.handle = handle;
269
+ return handle;
270
+ }
271
+ /**
272
+ * Check if a value is a LazyHandle.
273
+ * Uses Symbol-based branding for reliable type checking.
274
+ *
275
+ * @param value - Value to check
276
+ * @returns True if value is a LazyHandle
277
+ */
278
+ /** @codexApi {"parent":"pithy.core.lazy","name":"isLazyHandle","stability":"stable","signature":"(value: unknown) => value is LazyHandle<unknown>"} */
279
+ export function isLazyHandle(value) {
280
+ return (typeof value === 'object' &&
281
+ value !== null &&
282
+ value[LAZY_HANDLE_SYMBOL] === true);
283
+ }
284
+ /**
285
+ * Preload a lazy component without mounting it.
286
+ * Useful for prefetching on hover or when idle.
287
+ *
288
+ * @param handle - LazyHandle to preload
289
+ * @returns Promise that resolves when loaded
290
+ */
291
+ /** @codexApi {"parent":"pithy.core.lazy","name":"preloadLazy","stability":"stable","signature":"<T>(handle: LazyHandle<T>) => Promise<T>"} */
292
+ export function preloadLazy(handle) {
293
+ return handle.load();
294
+ }
295
+ //# sourceMappingURL=lazy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy.js","sourceRoot":"","sources":["../../src/runtime/lazy.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF;;;;;;;GAOG;AAEH,OAAO,EAAE,MAAM,EAAe,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAwB,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE/E,8DAA8D;AAC9D,MAAM,kBAAkB,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAsGvD,mEAAmE;AACnE,IAAI,aAAa,GAAG,CAAC,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;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AG,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAW,CAAC,CAAC,CAAC;YAEpD,kDAAkD;YAClD,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAEpB,OAAO,MAAW,CAAC;QACrB,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,WAAW,CAAC,GAAG,CAAC,CAAC;YACjB,MAAM,GAAG,CAAC;QACZ,CAAC,CAAC,CAAC;QAEL,OAAO,QAAQ,CAAC,WAAW,CAAC;IAC9B,CAAC;IAED,sCAAsC;IACtC,MAAM,MAAM,GAAG;QACb,CAAC,kBAAkB,CAAC,EAAE,IAAI;QAC1B,GAAG;QACH,IAAI;QACJ,KAAK;QACL,MAAM,EAAE,SAAS;QACjB,KAAK,EAAE,SAAS;QAChB,MAAM;QACN,KAAK;QACL,QAAQ;KACQ,CAAC;IAEnB,mCAAmC;IACnC,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;IAEzB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,uJAAuJ;AACvJ,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAAiC,CAAC,kBAAkB,CAAC,KAAK,IAAI,CAChE,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,8IAA8I;AAC9I,MAAM,UAAU,WAAW,CAAI,MAAqB;IAClD,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;AACvB,CAAC"}
@@ -0,0 +1,78 @@
1
+ /** @codex
2
+ {
3
+ "id": "pithy.core.module-cache",
4
+ "title": "Shared Module Cache for Dynamic Imports",
5
+ "category": "runtime"
6
+ }
7
+ */
8
+ /**
9
+ * Shared Module Cache
10
+ *
11
+ * Provides a centralized cache for all dynamic imports across the framework.
12
+ * Used by router, prefetch, and lazy components to prevent duplicate loads.
13
+ *
14
+ * @module runtime/module-cache
15
+ */
16
+ /** Status of a cached module */
17
+ type ModuleStatus = 'pending' | 'resolved' | 'rejected';
18
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"ModuleRecord","stability":"stable","signature":"interface ModuleRecord<T>"} */
19
+ export interface ModuleRecord<T = unknown> {
20
+ status: ModuleStatus;
21
+ promise: Promise<T>;
22
+ value?: T;
23
+ error?: unknown;
24
+ /** Timestamp when the module was loaded (for cache invalidation) */
25
+ loadedAt?: number;
26
+ }
27
+ /**
28
+ * Load a module by dynamic import with caching.
29
+ * Same module (by key) is only loaded once.
30
+ *
31
+ * @param key - Unique identifier for the module (e.g., 'route:/dashboard', 'component:HeavyChart')
32
+ * @param loader - Function that returns the dynamic import promise
33
+ * @returns Promise resolving to the loaded module
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * // Router uses it for route chunks
38
+ * loadModule('route:/dashboard', () => import('./routes/dashboard.pithy'));
39
+ *
40
+ * // lazy() uses it for component chunks
41
+ * loadModule('component:HeavyChart', () => import('./HeavyChart.pithy'));
42
+ * ```
43
+ */
44
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"loadModule","stability":"stable","signature":"<T>(key: string, loader: () => Promise<T>) => Promise<T>"} */
45
+ export declare function loadModule<T>(key: string, loader: () => Promise<T>): Promise<T>;
46
+ /**
47
+ * Get current status of a cached module.
48
+ *
49
+ * @param key - Module identifier
50
+ * @returns Module record or undefined if not cached
51
+ */
52
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"getModuleRecord","stability":"stable","signature":"<T>(key: string) => ModuleRecord<T> | undefined"} */
53
+ export declare function getModuleRecord<T = unknown>(key: string): ModuleRecord<T> | undefined;
54
+ /**
55
+ * Invalidate a specific module from the cache.
56
+ * Used for HMR to force reload of updated modules.
57
+ *
58
+ * @param key - Module identifier to invalidate
59
+ * @returns True if module was found and removed
60
+ */
61
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"invalidateModule","stability":"stable","signature":"(key: string) => boolean"} */
62
+ export declare function invalidateModule(key: string): boolean;
63
+ /**
64
+ * Get all resolved module keys.
65
+ * Used for SSR to track which chunks were used during render.
66
+ *
67
+ * @returns Array of module keys that have been successfully loaded
68
+ */
69
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"getResolvedModuleKeys","stability":"stable","signature":"() => string[]"} */
70
+ export declare function getResolvedModuleKeys(): string[];
71
+ /**
72
+ * Clear entire module cache.
73
+ * Use with caution - typically only for testing or full app reload.
74
+ */
75
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"clearModuleCache","stability":"stable","signature":"() => void"} */
76
+ export declare function clearModuleCache(): void;
77
+ export {};
78
+ //# sourceMappingURL=module-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module-cache.d.ts","sourceRoot":"","sources":["../../src/runtime/module-cache.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF;;;;;;;GAOG;AAEH,gCAAgC;AAChC,KAAK,YAAY,GAAG,SAAS,GAAG,UAAU,GAAG,UAAU,CAAC;AAExD,wIAAwI;AACxI,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACvC,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAKD;;;;;;;;;;;;;;;;GAgBG;AACH,qKAAqK;AACrK,wBAAgB,UAAU,CAAC,CAAC,EAC1B,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,CAAC,CAAC,CAkCZ;AAED;;;;;GAKG;AACH,iKAAiK;AACjK,wBAAgB,eAAe,CAAC,CAAC,GAAG,OAAO,EACzC,GAAG,EAAE,MAAM,GACV,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,CAE7B;AAED;;;;;;GAMG;AACH,2IAA2I;AAC3I,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;GAKG;AACH,sIAAsI;AACtI,wBAAgB,qBAAqB,IAAI,MAAM,EAAE,CAQhD;AAED;;;GAGG;AACH,6HAA6H;AAC7H,wBAAgB,gBAAgB,IAAI,IAAI,CAEvC"}
@@ -0,0 +1,109 @@
1
+ /** @codex
2
+ {
3
+ "id": "pithy.core.module-cache",
4
+ "title": "Shared Module Cache for Dynamic Imports",
5
+ "category": "runtime"
6
+ }
7
+ */
8
+ /** Global module cache - shared across all features */
9
+ const moduleCache = new Map();
10
+ /**
11
+ * Load a module by dynamic import with caching.
12
+ * Same module (by key) is only loaded once.
13
+ *
14
+ * @param key - Unique identifier for the module (e.g., 'route:/dashboard', 'component:HeavyChart')
15
+ * @param loader - Function that returns the dynamic import promise
16
+ * @returns Promise resolving to the loaded module
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * // Router uses it for route chunks
21
+ * loadModule('route:/dashboard', () => import('./routes/dashboard.pithy'));
22
+ *
23
+ * // lazy() uses it for component chunks
24
+ * loadModule('component:HeavyChart', () => import('./HeavyChart.pithy'));
25
+ * ```
26
+ */
27
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"loadModule","stability":"stable","signature":"<T>(key: string, loader: () => Promise<T>) => Promise<T>"} */
28
+ export function loadModule(key, loader) {
29
+ let record = moduleCache.get(key);
30
+ if (record) {
31
+ // Return cached promise (handles pending, resolved, or rejected)
32
+ return record.promise;
33
+ }
34
+ // Create new record with pending status
35
+ const promise = loader()
36
+ .then(value => {
37
+ // Update record on success
38
+ const rec = moduleCache.get(key);
39
+ if (rec) {
40
+ rec.status = 'resolved';
41
+ rec.value = value;
42
+ rec.loadedAt = Date.now();
43
+ }
44
+ return value;
45
+ })
46
+ .catch(err => {
47
+ // Update record on failure
48
+ const rec = moduleCache.get(key);
49
+ if (rec) {
50
+ rec.status = 'rejected';
51
+ rec.error = err;
52
+ }
53
+ throw err;
54
+ });
55
+ record = { status: 'pending', promise };
56
+ moduleCache.set(key, record);
57
+ return promise;
58
+ }
59
+ /**
60
+ * Get current status of a cached module.
61
+ *
62
+ * @param key - Module identifier
63
+ * @returns Module record or undefined if not cached
64
+ */
65
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"getModuleRecord","stability":"stable","signature":"<T>(key: string) => ModuleRecord<T> | undefined"} */
66
+ export function getModuleRecord(key) {
67
+ return moduleCache.get(key);
68
+ }
69
+ /**
70
+ * Invalidate a specific module from the cache.
71
+ * Used for HMR to force reload of updated modules.
72
+ *
73
+ * @param key - Module identifier to invalidate
74
+ * @returns True if module was found and removed
75
+ */
76
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"invalidateModule","stability":"stable","signature":"(key: string) => boolean"} */
77
+ export function invalidateModule(key) {
78
+ return moduleCache.delete(key);
79
+ }
80
+ /**
81
+ * Get all resolved module keys.
82
+ * Used for SSR to track which chunks were used during render.
83
+ *
84
+ * @returns Array of module keys that have been successfully loaded
85
+ */
86
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"getResolvedModuleKeys","stability":"stable","signature":"() => string[]"} */
87
+ export function getResolvedModuleKeys() {
88
+ const keys = [];
89
+ for (const [key, record] of moduleCache.entries()) {
90
+ if (record.status === 'resolved') {
91
+ keys.push(key);
92
+ }
93
+ }
94
+ return keys;
95
+ }
96
+ /**
97
+ * Clear entire module cache.
98
+ * Use with caution - typically only for testing or full app reload.
99
+ */
100
+ /** @codexApi {"parent":"pithy.core.module-cache","name":"clearModuleCache","stability":"stable","signature":"() => void"} */
101
+ export function clearModuleCache() {
102
+ moduleCache.clear();
103
+ }
104
+ // Development-only logging
105
+ if (import.meta.env?.DEV) {
106
+ // Expose cache for debugging
107
+ globalThis.__PITHY_MODULE_CACHE__ = moduleCache;
108
+ }
109
+ //# sourceMappingURL=module-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module-cache.js","sourceRoot":"","s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AKG;AACH,iKAAiK;AACjK,MAAM,UAAU,eAAe,CAC7B,GAAW;IAEX,OAAO,WAAW,CAAC,GAAG,CAAC,GAAG,CAAgC,CAAC;AAC7D,CAAC;AAED;;;;;;GAMG;AACH,2IAA2I;AAC3I,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,OAAO,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACjC,CAAC;AAED;;;;;GAKG;AACH,sIAAsI;AACtI,MAAM,UAAU,qBAAqB;IACnC,MAAM,IAAI,GAAa,EAAE,CAAC;IAC1B,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC;QAClD,IAAI,MAAM,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;YACjC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,6HAA6H;AAC7H,MAAM,UAAU,gBAAgB;IAC9B,WAAW,CAAC,KAAK,EAAE,CAAC;AACtB,CAAC;AAED,2BAA2B;AAC3B,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;IACzB,6BAA6B;IAC5B,UAAsC,CAAC,sBAAsB,GAAG,WAAW,CAAC;AAC/E,CAAC"}
@@ -0,0 +1,29 @@
1
+ /** @codex
2
+ {
3
+ "id": "pithy.runtime.onDestroy",
4
+ "title": "Component Destroy Lifecycle Hook",
5
+ "category": "runtime"
6
+ }
7
+ */
8
+ /**
9
+ * Registers a cleanup callback at component scope. When the component's
10
+ * destroy() is called, all registered callbacks run in LIFO order
11
+ * (last-registered first), after template disposers have executed.
12
+ *
13
+ * Must be called synchronously during component function execution,
14
+ * before the template entry point. Pending callbacks are drained by
15
+ * `html()`, `__applyBindings()` (AOT), or `compileNodes()` — whichever
16
+ * runs next — and incorporated into the returned `destroy()` function.
17
+ *
18
+ * @param fn - Cleanup function to run on component destroy
19
+ */
20
+ /** @codexApi {"parent":"pithy.runtime.onDestroy","name":"onDestroy","stability":"stable","signature":"(fn: () => void) => void"} */
21
+ export declare function onDestroy(fn: () => void): void;
22
+ /**
23
+ * Drains all pending destroy callbacks and returns them.
24
+ * Replaces the internal array so the next component starts fresh.
25
+ *
26
+ * @internal Called by html(), __applyBindings(), and compileNodes() before processing
27
+ */
28
+ export declare function drainDestroyCallbacks(): (() => void)[];
29
+ //# sourceMappingURL=onDestroy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onDestroy.d.ts","sourceRoot":"","sources":["../../src/runtime/onDestroy.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAmCF;;;;;;;;;;;GAWG;AACH,oIAAoI;AACpI,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,IAAI,CAE9C;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE,CAItD"}
@@ -0,0 +1,66 @@
1
+ /** @codex
2
+ {
3
+ "id": "pithy.runtime.onDestroy",
4
+ "title": "Component Destroy Lifecycle Hook",
5
+ "category": "runtime"
6
+ }
7
+ */
8
+ import { pithyGlobals } from '../types/globals';
9
+ /**
10
+ * Module-scoped fallback for browser (single-threaded, no isolation needed).
11
+ * In SSR, @pithyjs/ssr installs AsyncLocalStorage-backed getters via
12
+ * globalThis.__pithyGetDestroyCallbacks / __pithySetDestroyCallbacks
13
+ * to isolate pending callbacks per request.
14
+ */
15
+ let _defaultCallbacks = [];
16
+ /** Get the current pending callbacks (per-request in SSR, module-scoped in browser) */
17
+ function getPending() {
18
+ const getter = pithyGlobals().__pithyGetDestroyCallbacks;
19
+ if (typeof getter === 'function') {
20
+ const result = getter();
21
+ if (result)
22
+ return result;
23
+ }
24
+ return _defaultCallbacks;
25
+ }
26
+ /** Replace the current pending callbacks array */
27
+ function setPending(cbs) {
28
+ const g = pithyGlobals();
29
+ const getter = g.__pithyGetDestroyCallbacks;
30
+ // Only use SSR setter when inside an active render scope (getter returns non-null).
31
+ // Outside a render scope the setter is a no-op, so fall back to module-scoped array.
32
+ if (typeof getter === 'function' && getter() != null) {
33
+ g.__pithySetDestroyCallbacks(cbs);
34
+ }
35
+ else {
36
+ _defaultCallbacks = cbs;
37
+ }
38
+ }
39
+ /**
40
+ * Registers a cleanup callback at component scope. When the component's
41
+ * destroy() is called, all registered callbacks run in LIFO order
42
+ * (last-registered first), after template disposers have executed.
43
+ *
44
+ * Must be called synchronously during component function execution,
45
+ * before the template entry point. Pending callbacks are drained by
46
+ * `html()`, `__applyBindings()` (AOT), or `compileNodes()` — whichever
47
+ * runs next — and incorporated into the returned `destroy()` function.
48
+ *
49
+ * @param fn - Cleanup function to run on component destroy
50
+ */
51
+ /** @codexApi {"parent":"pithy.runtime.onDestroy","name":"onDestroy","stability":"stable","signature":"(fn: () => void) => void"} */
52
+ export function onDestroy(fn) {
53
+ getPending().push(fn);
54
+ }
55
+ /**
56
+ * Drains all pending destroy callbacks and returns them.
57
+ * Replaces the internal array so the next component starts fresh.
58
+ *
59
+ * @internal Called by html(), __applyBindings(), and compileNodes() before processing
60
+ */
61
+ export function drainDestroyCallbacks() {
62
+ const drained = getPending();
63
+ setPending([]);
64
+ return drained;
65
+ }
66
+ //# sourceMappingURL=onDestroy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onDestroy.js","sourceRoot":"","sources":["../../src/runtime/onDestroy.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD;;;;;GAKG;AACH,IAAI,iBAAiB,GAAmB,EAAE,CAAC;AAE3C,uFAAuF;AACvF,SAAS,UAAU;IACjB,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC,0BAA0B,CAAC;IACzD,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,CAAC;QACjC,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;QACxB,IAAI,MAAM;YAAE,OAAO,MAAM,CAAC;IAC5B,CAAC;IACD,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAED,kDAAkD;AAClD,SAAS,UAAU,CAAC,GAAmB;IACrC,MAAM,CAAC,GAAG,YAAY,EAAE,CAAC;IACzB,MAAM,MAAM,GAAG,CAAC,CAAC,0BAA0B,CAAC;IAC5C,oFAAoF;IACpF,qFAAqF;IACrF,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,EAAE,IAAI,IAAI,EAAE,CAAC;QACrD,CAAC,CAAC,0BAA2B,CAAC,GAAG,CAAC,CAAC;IACrC,CAAC;SAAM,CAAC;QACN,iBAAiB,GAAG,GAAG,CAAC;IAC1B,CAAC;AACH,CAAC;AAED;;;;;;;;;;;GAWG;AACH,oIAAoI;AACpI,MAAM,UAAU,SAAS,CAAC,EAAc;IACtC,UAAU,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACxB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB;IACnC,MAAM,OAAO,GAAG,UAAU,EAAE,CAAC;IAC7B,UAAU,CAAC,EAAE,CAAC,CAAC;IACf,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,17 @@
1
+ /** @codex
2
+ {
3
+ "id": "pithy.compiler.identifier-parser",
4
+ "title": "Template Identifier Parser",
5
+ "category": "compiler"
6
+ }
7
+ */
8
+ /**
9
+ * Extract variable identifiers from an HTML template string.
10
+ * DEV-only helper used by `html()` fallback when the Babel plugin hasn't
11
+ * injected identifiers. Returns an empty set if parse5 is not loaded
12
+ * (intentional — throwing would crash AOT apps in dev mode).
13
+ * @internal
14
+ */
15
+ /** @codexApi {"parent":"pithy.compiler.identifier-parser","name":"parseIdentifiersFromTemplate","stability":"internal","signature":"(template: string) => Set<string>"} */
16
+ export declare function parseIdentifiersFromTemplate(template: string): Set<string>;
17
+ //# sourceMappingURL=parseIdentifiersFromTemplate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseIdentifiersFromTemplate.d.ts","sourceRoot":"","sources":["../../src/runtime/parseIdentifiersFromTemplate.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAKF;;;;;;GAMG;AACH,2KAA2K;AAC3K,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CA+D1E"}