@jay-framework/compiler-jay-html 0.11.0 → 0.13.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 (3) hide show
  1. package/dist/index.d.cts +213 -4
  2. package/dist/index.js +16410 -8048
  3. package/package.json +10 -9
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
- import { JayType, WithValidations, PluginComponentResolution, JayEnumType, RefsTree, CompilerSourceFile, SourceFileFormat, JayImportLink, MainRuntimeModes, GenerateTarget, Imports } from '@jay-framework/compiler-shared';
1
+ import { JayType, WithValidations, PluginComponentResolution, PluginManifest, JayEnumType, RefsTree, CompilerSourceFile, SourceFileFormat, JayImportLink, MainRuntimeModes, GenerateTarget, Imports, JayObjectType } from '@jay-framework/compiler-shared';
2
2
  import { HTMLElement } from 'node-html-parser';
3
3
  import { ResolveTsConfigOptions } from '@jay-framework/compiler-analyze-exported-types';
4
+ import { Coordinate } from '@jay-framework/runtime';
4
5
 
5
6
  declare enum ContractTagType {
6
7
  data = 0,
@@ -23,20 +24,72 @@ interface ContractTag {
23
24
  async?: boolean;
24
25
  phase?: RenderingPhase;
25
26
  }
27
+ interface ContractProp {
28
+ name: string;
29
+ dataType: JayType;
30
+ required?: boolean;
31
+ description?: Array<string>;
32
+ default?: string;
33
+ }
34
+ /** URL/load params for a page (e.g. [slug]). Always string in generated type (UrlParams = Record<string, string>). Design Log #85. */
35
+ interface ContractParam {
36
+ name: string;
37
+ }
26
38
  interface Contract {
27
39
  name: string;
28
40
  tags: Array<ContractTag>;
41
+ props?: Array<ContractProp>;
42
+ /** URL/load params (e.g. slug for [slug] route). Generates Params extends UrlParams. */
43
+ params?: Array<ContractParam>;
29
44
  }
30
45
 
31
46
  declare function compileContract(contractWithValidations: WithValidations<Contract>, contractFilePath: string, jayImportResolver: JayImportResolver): Promise<WithValidations<string>>;
32
47
 
33
48
  declare function parseContract(contractYaml: string, fileName: string): WithValidations<Contract>;
34
49
 
50
+ /**
51
+ * Get the effective phase for a tag (explicit or default)
52
+ */
53
+ declare function getEffectivePhase(tag: ContractTag, parentPhase?: RenderingPhase): RenderingPhase;
54
+ /**
55
+ * Check if a tag should be included in a specific phase's ViewState
56
+ * A tag is included if its effective phase exactly matches the target phase
57
+ */
58
+ declare function isTagInPhase(tag: ContractTag, targetPhase: RenderingPhase, parentPhase?: RenderingPhase): boolean;
59
+ /**
60
+ * Validate phase constraints across the entire contract
61
+ */
62
+ declare function validateContractPhases(contract: Contract): string[];
63
+ /**
64
+ * Filter tags recursively by phase
65
+ * Returns only tags that should be included in the target phase's ViewState
66
+ */
67
+ declare function filterTagsByPhase(tags: ContractTag[], targetPhase: RenderingPhase, parentPhase?: RenderingPhase): ContractTag[];
68
+ /**
69
+ * Create a phase-specific contract
70
+ * Returns a contract with only tags that belong to the target phase
71
+ */
72
+ declare function createPhaseContract(contract: Contract, targetPhase: RenderingPhase): Contract;
73
+
35
74
  interface JayImportResolver {
36
75
  resolveLink(importingModuleDir: string, link: string): string;
76
+ /** Load a contract from an absolute file path */
37
77
  loadContract(fullPath: string): WithValidations<Contract>;
38
78
  analyzeExportedTypes(fullPath: string, options: ResolveTsConfigOptions): JayType[];
39
79
  resolvePluginComponent(pluginName: string, contractName: string, projectRoot: string): WithValidations<PluginComponentResolution>;
80
+ /**
81
+ * Load a contract from a plugin, handling both static and dynamic contracts.
82
+ * For static contracts, loads from the plugin's contract file.
83
+ * For dynamic contracts, loads from materialized location (build/materialized-contracts/).
84
+ *
85
+ * Returns the contract, path, and optional metadata (for dynamic contracts).
86
+ */
87
+ loadPluginContract(pluginName: string, contractName: string, projectRoot: string): WithValidations<{
88
+ contract: Contract;
89
+ contractPath: string;
90
+ metadata?: Record<string, unknown>;
91
+ }>;
92
+ resolvePluginManifest(pluginName: string, projectRoot: string): WithValidations<PluginManifest>;
40
93
  }
41
94
  declare const JAY_IMPORT_RESOLVER: JayImportResolver;
42
95
 
@@ -71,17 +124,38 @@ interface PhaseViewStates {
71
124
  */
72
125
  declare function contractToAllPhaseViewStates(contract: Contract, contractFilePath: string, jayImportResolver: JayImportResolver): Promise<WithValidations<PhaseViewStates>>;
73
126
 
127
+ /**
128
+ * Load a linked contract using the import resolver.
129
+ * Returns the parsed contract or null if not found/failed.
130
+ *
131
+ * @param linkPath - The link path from the contract (e.g., "./product-card")
132
+ * @param baseContractDir - Directory of the contract containing the link
133
+ * @param importResolver - Import resolver for loading contracts
134
+ */
135
+ declare function loadLinkedContract(linkPath: string, baseContractDir: string, importResolver: JayImportResolver): Contract | null;
136
+ /**
137
+ * Get the directory of a linked contract for resolving nested links.
138
+ *
139
+ * @param linkPath - The link path from the contract (e.g., "./product-card")
140
+ * @param baseContractDir - Directory of the contract containing the link
141
+ * @param importResolver - Import resolver for path resolution
142
+ */
143
+ declare function getLinkedContractDir(linkPath: string, baseContractDir: string, importResolver: JayImportResolver): string;
144
+
74
145
  interface JayHtmlNamespace {
75
146
  prefix: string;
76
147
  namespace: string;
77
148
  }
78
149
  interface JayHeadlessImports {
79
- key: string;
150
+ key?: string;
151
+ contractName: string;
80
152
  refs: RefsTree;
81
153
  rootType: JayType;
82
154
  contractLinks: JayImportLink[];
83
155
  codeLink: JayImportLink;
84
156
  contract?: Contract;
157
+ contractPath?: string;
158
+ metadata?: Record<string, unknown>;
85
159
  }
86
160
  interface JayHtmlHeadLink {
87
161
  rel: string;
@@ -138,6 +212,71 @@ declare function renderRefsType(refs: RefsTree, refsType: string, generateTarget
138
212
 
139
213
  declare function generateTypes(types: JayType): string;
140
214
 
215
+ /**
216
+ * Parser for .jay-action files (compact jay-type notation).
217
+ *
218
+ * Produces JayType from the compact format used by jay-html data scripts:
219
+ * - Primitives: string, number, boolean → JayAtomicType
220
+ * - Enums: enum(a | b | c) → JayEnumType
221
+ * - Arrays: YAML list / type[] shorthand → JayArrayType
222
+ * - Objects: nested YAML maps → JayObjectType (with optionalProps tracking)
223
+ * - Optional: ? suffix on property keys → tracked in JayObjectType.optionalProps
224
+ * - Contract imports: import: block → JayImportedType
225
+ */
226
+
227
+ /**
228
+ * Parsed action definition from a .jay-action file.
229
+ * Uses JayType for the type representation.
230
+ */
231
+ interface ActionDefinition {
232
+ name: string;
233
+ description: string;
234
+ /** Import aliases: alias → contract subpath (e.g., productCard → product-card.jay-contract) */
235
+ imports: Record<string, string>;
236
+ /** Input schema as a JayObjectType. Optional props tracked in optionalProps. */
237
+ inputType: JayObjectType;
238
+ /** Output type (any JayType shape, or undefined if no output). */
239
+ outputType?: JayType;
240
+ }
241
+ /**
242
+ * Parses a .jay-action YAML string into an ActionDefinition using JayType.
243
+ */
244
+ declare function parseAction(actionYaml: string, fileName: string): WithValidations<ActionDefinition>;
245
+
246
+ /**
247
+ * Compiler for .jay-action files → .jay-action.d.ts
248
+ *
249
+ * Generates TypeScript Input/Output interfaces from JayType trees,
250
+ * including import statements for contract references (JayImportedType).
251
+ */
252
+
253
+ interface ContractImportInfo {
254
+ importPath: string;
255
+ viewStateName: string;
256
+ }
257
+ type ContractResolver = (contractSubpath: string) => ContractImportInfo | null;
258
+ /**
259
+ * Default contract resolver that derives names from the subpath.
260
+ */
261
+ declare function defaultContractResolver(contractSubpath: string): ContractImportInfo;
262
+ /**
263
+ * Compiles an ActionDefinition (with JayType) into a TypeScript .d.ts string.
264
+ */
265
+ declare function compileAction(actionWithValidations: WithValidations<ActionDefinition>, contractResolver?: ContractResolver): WithValidations<string>;
266
+
267
+ /**
268
+ * Headless contract with its key (used for property path prefix)
269
+ */
270
+ interface HeadlessContractInfo {
271
+ /** The key attribute from the headless script tag */
272
+ key: string;
273
+ /** The parsed contract */
274
+ contract: Contract;
275
+ /** Path to the contract file (used to resolve linked sub-contracts) */
276
+ contractPath?: string;
277
+ /** Optional metadata from the generator (for dynamic contracts) */
278
+ metadata?: Record<string, unknown>;
279
+ }
141
280
  /**
142
281
  * Input for slow render transformation
143
282
  */
@@ -146,14 +285,24 @@ interface SlowRenderInput {
146
285
  jayHtmlContent: string;
147
286
  /** Slow phase view state data */
148
287
  slowViewState: Record<string, unknown>;
149
- /** Contract metadata for phase detection */
288
+ /** Contract metadata for phase detection (page's main contract) */
150
289
  contract?: Contract;
290
+ /**
291
+ * Headless component contracts, keyed by their `key` attribute.
292
+ * These contracts provide phase info for properties like `productSearch.categoryName`.
293
+ */
294
+ headlessContracts?: HeadlessContractInfo[];
151
295
  /**
152
296
  * Source directory of the original jay-html file.
153
297
  * Used to resolve relative paths (contracts, CSS, components) to absolute paths
154
298
  * so the pre-rendered file can be placed in a different directory.
155
299
  */
156
300
  sourceDir?: string;
301
+ /**
302
+ * Import resolver for loading linked sub-contracts.
303
+ * If not provided, linked sub-contracts will not be resolved.
304
+ */
305
+ importResolver?: JayImportResolver;
157
306
  }
158
307
  /**
159
308
  * Output of slow render transformation
@@ -171,9 +320,69 @@ interface SlowRenderOutput {
171
320
  * @returns The pre-rendered jay-html with slow bindings resolved
172
321
  */
173
322
  declare function slowRenderTransform(input: SlowRenderInput): WithValidations<SlowRenderOutput>;
323
+ /**
324
+ * Discovered headless component instance in pre-rendered jay-html.
325
+ * Found after Pass 1 (page bindings resolved, slow forEach unrolled).
326
+ */
327
+ interface DiscoveredHeadlessInstance {
328
+ /** Contract name from tag name (e.g., "product-card" from <jay:product-card>) */
329
+ contractName: string;
330
+ /** Props extracted from element attributes (camelCased keys, string values) */
331
+ props: Record<string, string>;
332
+ /**
333
+ * Coordinate that uniquely identifies this instance in the page tree.
334
+ * Built from ancestor slowForEach jayTrackBy values + "contractName:localIndex".
335
+ */
336
+ coordinate: Coordinate;
337
+ }
338
+ /**
339
+ * Resolved data for a headless component instance.
340
+ * Provided by the dev server after calling slowlyRender for each discovered instance.
341
+ */
342
+ interface HeadlessInstanceResolvedData {
343
+ /** Coordinate matching the discovered instance */
344
+ coordinate: Coordinate;
345
+ /** The component's contract (for phase detection) */
346
+ contract: Contract;
347
+ /** Slow phase ViewState data for this instance */
348
+ slowViewState: Record<string, unknown>;
349
+ }
350
+ /**
351
+ * Build the full coordinate key string for a <jay:xxx> element.
352
+ * Format: "jayTrackBy1/jayTrackBy2/.../contractName:ref"
353
+ *
354
+ * Uses the element's `ref` attribute if present. If the element has no ref,
355
+ * a coordinateCounters map must be provided to auto-assign an index.
356
+ */
357
+ declare function buildInstanceCoordinateKey(element: HTMLElement, contractName: string, coordinateCounters?: Map<string, number>): string;
358
+ /**
359
+ * A headless instance found inside a preserved forEach (fast phase).
360
+ * These instances have unresolved prop bindings and need per-item rendering
361
+ * at fast phase time. They are only allowed if the component has no slow phase.
362
+ */
363
+ interface ForEachHeadlessInstance {
364
+ contractName: string;
365
+ /** The forEach attribute path on the ancestor element (e.g., "allProducts.items") */
366
+ forEachPath: string;
367
+ /** TrackBy key for the forEach (e.g., "_id") */
368
+ trackBy: string;
369
+ /** Prop bindings referencing forEach item fields (e.g., { productId: "{_id}" }) */
370
+ propBindings: Record<string, string>;
371
+ /** Coordinate suffix after trackBy values (e.g., "product-card:0") */
372
+ coordinateSuffix: string;
373
+ }
374
+ interface HeadlessInstanceDiscoveryResult {
375
+ instances: DiscoveredHeadlessInstance[];
376
+ /** Headless instances inside preserved forEach elements (need server-time validation) */
377
+ forEachInstances: ForEachHeadlessInstance[];
378
+ /** HTML with auto-generated ref attributes embedded on <jay:xxx> elements */
379
+ preRenderedJayHtml: string;
380
+ }
381
+ declare function discoverHeadlessInstances(preRenderedJayHtml: string): HeadlessInstanceDiscoveryResult;
382
+ declare function resolveHeadlessInstances(preRenderedJayHtml: string, instanceData: HeadlessInstanceResolvedData[], importResolver?: JayImportResolver): WithValidations<string>;
174
383
  /**
175
384
  * Check if a jay-html file has any slow-phase properties that can be pre-rendered
176
385
  */
177
386
  declare function hasSlowPhaseProperties(contract: Contract | undefined): boolean;
178
387
 
179
- export { type Contract, type ContractTag, ContractTagType, type EnumToImport, JAY_IMPORT_RESOLVER, type JayContractImportLink, type JayHtmlSourceFile, type JayImportResolver, type PhaseViewStates, type RenderingPhase, type SlowRenderInput, type SlowRenderOutput, compileContract, contractToAllPhaseViewStates, contractToImportsViewStateAndRefs, contractToPhaseViewState, generateElementBridgeFile, generateElementDefinitionFile, generateElementFile, generateElementFileReactTarget, generateSandboxRootFile, generateTypes, getJayHtmlImports, hasSlowPhaseProperties, parseContract, parseEnumValues, parseIsEnum, parseJayFile, renderRefsType, slowRenderTransform };
388
+ export { type ActionDefinition, type Contract, type ContractImportInfo, type ContractParam, type ContractProp, type ContractResolver, type ContractTag, ContractTagType, type DiscoveredHeadlessInstance, type EnumToImport, type ForEachHeadlessInstance, type HeadlessContractInfo, type HeadlessInstanceDiscoveryResult, type HeadlessInstanceResolvedData, JAY_IMPORT_RESOLVER, type JayContractImportLink, type JayHtmlSourceFile, type JayImportResolver, type PhaseViewStates, type RenderingPhase, type SlowRenderInput, type SlowRenderOutput, buildInstanceCoordinateKey, compileAction, compileContract, contractToAllPhaseViewStates, contractToImportsViewStateAndRefs, contractToPhaseViewState, createPhaseContract, defaultContractResolver, discoverHeadlessInstances, filterTagsByPhase, generateElementBridgeFile, generateElementDefinitionFile, generateElementFile, generateElementFileReactTarget, generateSandboxRootFile, generateTypes, getEffectivePhase, getJayHtmlImports, getLinkedContractDir, hasSlowPhaseProperties, isTagInPhase, loadLinkedContract, parseAction, parseContract, parseEnumValues, parseIsEnum, parseJayFile, renderRefsType, resolveHeadlessInstances, slowRenderTransform, validateContractPhases };