svelte-origin 0.0.0 → 1.0.0-next.49

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 (49) hide show
  1. package/LLM.md +545 -0
  2. package/README.md +678 -1
  3. package/dist/aliases.d.ts +45 -0
  4. package/dist/cli.d.ts +20 -0
  5. package/dist/cli.js +163 -0
  6. package/dist/generate-dts.d.ts +31 -0
  7. package/dist/globals.d.ts +649 -0
  8. package/dist/index.d.ts +9 -0
  9. package/dist/index.js +108 -0
  10. package/dist/plugin.d.ts +176 -0
  11. package/dist/plugin.js +81 -0
  12. package/dist/post-process.d.ts +31 -0
  13. package/dist/post-process.js +99 -0
  14. package/dist/preprocess.d.ts +119 -0
  15. package/dist/preprocess.js +81 -0
  16. package/dist/runtime/attrs.d.ts +65 -0
  17. package/dist/runtime/index.d.ts +8 -0
  18. package/dist/runtime/index.js +28 -0
  19. package/dist/runtime/origin.d.ts +170 -0
  20. package/dist/runtime/types.d.ts +193 -0
  21. package/dist/runtime-inline.d.ts +117 -0
  22. package/dist/runtime-inline.min.js +1 -0
  23. package/dist/schema-resolver.d.ts +61 -0
  24. package/dist/shared-state.d.ts +47 -0
  25. package/dist/transform/ast-parser.d.ts +282 -0
  26. package/dist/transform/attrs-for-transform.d.ts +17 -0
  27. package/dist/transform/attrs-origin-transform.d.ts +38 -0
  28. package/dist/transform/attrs-schema.d.ts +63 -0
  29. package/dist/transform/codegen.d.ts +15 -0
  30. package/dist/transform/comments.d.ts +31 -0
  31. package/dist/transform/core.d.ts +37 -0
  32. package/dist/transform/declaration-parser.d.ts +77 -0
  33. package/dist/transform/element-types.d.ts +10 -0
  34. package/dist/transform/expression-utils.d.ts +59 -0
  35. package/dist/transform/origin-body-helpers.d.ts +143 -0
  36. package/dist/transform/origin-create-transform.d.ts +32 -0
  37. package/dist/transform/origin-destructure-transform.d.ts +62 -0
  38. package/dist/transform/origin-transform.d.ts +51 -0
  39. package/dist/transform/patterns.d.ts +86 -0
  40. package/dist/transform/reserved-keywords.d.ts +87 -0
  41. package/dist/transform/schema-codegen.d.ts +105 -0
  42. package/dist/transform/schema-parse.d.ts +50 -0
  43. package/dist/transform/schema-types.d.ts +40 -0
  44. package/dist/transform/schema.d.ts +11 -0
  45. package/dist/transform/standalone-attrs-transform.d.ts +36 -0
  46. package/dist/utils.d.ts +21 -0
  47. package/dist/vite-dts.d.ts +17 -0
  48. package/dist/vite-dts.js +24 -0
  49. package/package.json +79 -3
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Core types for svelte-origin runtime
3
+ *
4
+ * This file defines the type system for origins.
5
+ *
6
+ * Key concepts:
7
+ * - Origins are defined with $origin({...}) macro using `this` context
8
+ * - The macro transforms to a factory with __attrSchema and __create
9
+ * - __create receives a reactive attrs object and returns an instance
10
+ */
11
+ /**
12
+ * Marker type for bindable attrs.
13
+ * Used inside $origin({ props: $origin.props({ count: $bindable(0) }) })
14
+ *
15
+ * At macro expansion time, $bindable is replaced with __bindable()
16
+ * to mark attrs that should become $bindable() in the component.
17
+ */
18
+ export interface Bindable<T = unknown> {
19
+ readonly __bindable: true;
20
+ readonly default: T;
21
+ }
22
+ /**
23
+ * Schema describing a single attr's metadata.
24
+ * Extracted at transformation time from the attrs definition.
25
+ */
26
+ export interface AttrSchemaEntry {
27
+ /** The default value (undefined if no default) */
28
+ default: unknown;
29
+ /** Whether this attr is bindable (two-way binding) */
30
+ bindable: boolean;
31
+ /** Whether this attr has a default value */
32
+ hasDefault: boolean;
33
+ }
34
+ /**
35
+ * Full attr schema for an origin.
36
+ * Maps attr name -> schema entry.
37
+ */
38
+ export interface AttrSchema {
39
+ [key: string]: AttrSchemaEntry;
40
+ }
41
+ /**
42
+ * The reactive attrs object passed to the origin factory's __create function.
43
+ * This is created by the component with getters/setters that link to the
44
+ * actual $props() destructured variables.
45
+ *
46
+ * @example
47
+ * // In component after transformation:
48
+ * let { label = 'Counter', count = $bindable(0) } = $props()
49
+ * let __reactiveAttrs = {
50
+ * get label() { return label },
51
+ * get count() { return count },
52
+ * set count(v) { count = v }
53
+ * }
54
+ * let counter = Counter.__create(__reactiveAttrs)
55
+ */
56
+ export type ReactiveAttrs<T> = T extends Record<string, any> ? {
57
+ [K in keyof T]: UnwrapBindable<T[K]>;
58
+ } : Record<string, never>;
59
+ /**
60
+ * Unwrap Bindable<T> to T, pass through other types
61
+ */
62
+ export type UnwrapBindable<T> = T extends Bindable<infer U> ? U : T;
63
+ /**
64
+ * The factory object created by the $origin macro transformation.
65
+ *
66
+ * Contains:
67
+ * - __origin: marker for identification
68
+ * - __attrSchema: metadata about attrs (defaults, bindable markers)
69
+ * - __parents: parent origin factories for inheritance
70
+ * - __create: function that receives reactive attrs and returns instance
71
+ * - __onInit: optional callback that runs after instance creation, can receive extra args
72
+ *
73
+ * The factory is also callable for convenience: `Counter(attrs)` calls `Counter.__create(attrs)`
74
+ * Factory supports: `Counter(attrs?, ...initArgs?)` where initArgs are passed to __onInit
75
+ */
76
+ export interface OriginFactory<TInstance = unknown, TAttrs = Record<string, any>> {
77
+ /** Create an instance with the given reactive attrs and optional init args */
78
+ (attrs?: TAttrs | null, ...initArgs: any[]): TInstance & OriginInstanceExtras;
79
+ /** Marker to identify origin factories */
80
+ readonly __origin: true;
81
+ /** The attr schema extracted from the definition */
82
+ readonly __attrSchema: AttrSchema;
83
+ /** Parent origin factories (for inheritance) */
84
+ readonly __parents: OriginFactory<any, any>[];
85
+ /** The actual factory function that creates instances */
86
+ readonly __create: (attrs: TAttrs, superInstances?: any[]) => TInstance;
87
+ /** Optional initialization callback that runs after instance creation, receives any extra args passed to factory */
88
+ readonly __onInit?: (this: TInstance, ...args: any[]) => void | (() => void);
89
+ }
90
+ /**
91
+ * Standalone attrs schema factory created by $origin.props({...}) outside of $origin.
92
+ * Can be used to define reusable attr schemas.
93
+ *
94
+ * @example
95
+ * // Define reusable attrs
96
+ * export const BaseAttrs = $origin.props({
97
+ * label: 'Default',
98
+ * disabled: false
99
+ * })
100
+ *
101
+ * // Extend in origin
102
+ * export const Widget = $origin({
103
+ * props: $origin.props([BaseAttrs], {
104
+ * value: $bindable(0)
105
+ * })
106
+ * })
107
+ */
108
+ export interface AttrsFactory<_TAttrs = Record<string, any>> {
109
+ /** Marker to identify attrs factories */
110
+ readonly __attrs: true;
111
+ /** The attr schema */
112
+ readonly __attrSchema: AttrSchema;
113
+ /** Parent attr factories (for inheritance) */
114
+ readonly __parents: AttrsFactory<any>[];
115
+ }
116
+ /**
117
+ * Attachment registry returned by $attachments getter.
118
+ * Maps Symbol keys to attachment functions.
119
+ */
120
+ export type AttachmentRegistry = Record<symbol, (element: Element) => void | (() => void)>;
121
+ /**
122
+ * Properties added to all origin instances at runtime.
123
+ * These are injected by __createOrigin after __create returns.
124
+ */
125
+ export interface OriginInstanceExtras {
126
+ /**
127
+ * Getter that returns attachment functions that can be spread onto elements.
128
+ * Returns an object with Symbol keys mapping to attachment functions.
129
+ *
130
+ * @example
131
+ * <div {...instance.$attachments}>
132
+ */
133
+ readonly $attachments: AttachmentRegistry;
134
+ }
135
+ /**
136
+ * The origin instance has:
137
+ * - attrs: reactive attrs object (passed in)
138
+ * - super: parent instance (if extending)
139
+ * - $attachments: attachment registry (injected by runtime)
140
+ * - All other members from the definition (state, getters, methods)
141
+ */
142
+ export interface OriginInstance<TAttrs = Record<string, any>> extends OriginInstanceExtras {
143
+ /** Reactive attrs object */
144
+ readonly attrs: TAttrs;
145
+ /** Parent instance (if extending) */
146
+ readonly super?: any;
147
+ /** Additional members from definition */
148
+ [key: string]: any;
149
+ }
150
+ /**
151
+ * Extract the component attrs type from an origin's attr schema.
152
+ * This is what the component will accept as attrs.
153
+ */
154
+ export type ComponentAttrs<T extends OriginFactory<any, any>> = T extends OriginFactory<any, infer A> ? Partial<A> : Record<string, any>;
155
+ /**
156
+ * Extract the instance type from an origin factory
157
+ */
158
+ export type InstanceOf<T extends OriginFactory<any, any>> = T extends OriginFactory<infer I, any> ? I : never;
159
+ /**
160
+ * Merge parent origins with child definition.
161
+ * The child's attrs are merged with parent attrs.
162
+ * The child gets access to `this.super` for parent methods.
163
+ */
164
+ export type MergedOrigin<Parents extends OriginFactory<any, any>[], TInstance> = TInstance & {
165
+ super: Parents extends [OriginFactory<infer P, any>] ? P : Parents extends [OriginFactory<infer P1, any>, ...infer _Rest] ? P1 : never;
166
+ };
167
+ /**
168
+ * Attrs for a child origin (used with $origin.props.for).
169
+ * Extracts the attrs type from the origin's schema.
170
+ */
171
+ export type AttrsFor<T extends OriginFactory<any, any>> = T extends OriginFactory<any, infer A> ? A : Record<string, any>;
172
+ /**
173
+ * Attr mapping for $origin.props.for with custom mappings
174
+ */
175
+ export type AttrMapping<TSource = any, TTarget = any> = {
176
+ [K in keyof TTarget]?: (source: TSource) => TTarget[K];
177
+ };
178
+ /**
179
+ * Valid HTML element names for $origin.for('element').
180
+ * At compile time, this maps to svelte/elements types.
181
+ */
182
+ export type HTMLElementName = keyof HTMLElementTagNameMap | string;
183
+ /**
184
+ * Placeholder type for element attrs.
185
+ * The real type resolution happens at compile time using svelte/elements.
186
+ */
187
+ export type ElementAttrs<_K extends HTMLElementName> = Record<string, any>;
188
+ /**
189
+ * An attachment function that runs when an element is attached.
190
+ * Can return a cleanup function that runs when the element is detached
191
+ * or when the attachment re-runs.
192
+ */
193
+ export type AttachmentFn = (this: any, element: Element) => void | (() => void);
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Minimal inlined runtime for svelte-origin
3
+ *
4
+ * ┌─────────────────────────────────────────────────────────────────────────────┐
5
+ * │ │
6
+ * │ WHY THIS FILE EXISTS │
7
+ * │ ════════════════════ │
8
+ * │ │
9
+ * │ When library authors publish packages using svelte-origin, they have │
10
+ * │ two choices for how consumers interact with their library: │
11
+ * │ │
12
+ * │ ┌─────────────────────────────────────────────────────────────────────┐ │
13
+ * │ │ OPTION A: Zero Dependencies (Default) │ │
14
+ * │ │ ───────────────────────────────────────────────────────────────── │ │
15
+ * │ │ $ svelte-package && svelte-origin │ │
16
+ * │ │ │ │
17
+ * │ │ This INLINES this file (minified) into the library's dist/ │ │
18
+ * │ │ as `__svelte-origin-runtime.js` (~4KB). Consumers only need │ │
19
+ * │ │ Svelte — NO svelte-origin dependency required. │ │
20
+ * │ │ │ │
21
+ * │ │ How it works: │ │
22
+ * │ │ 1. svelte-package builds the library normally │ │
23
+ * │ │ 2. post-process.ts finds `import { ... } from 'svelte-origin/...'` │ │
24
+ * │ │ 3. Replaces with `import { ... } from './__svelte-origin-runtime'` │ │
25
+ * │ │ 4. Copies this file (minified) to dist/__svelte-origin-runtime.js │ │
26
+ * │ └─────────────────────────────────────────────────────────────────────┘ │
27
+ * │ │
28
+ * │ ┌─────────────────────────────────────────────────────────────────────┐ │
29
+ * │ │ OPTION B: Keep Dependency │ │
30
+ * │ │ ───────────────────────────────────────────────────────────────── │ │
31
+ * │ │ $ svelte-package && svelte-origin --dependency │ │
32
+ * │ │ │ │
33
+ * │ │ This keeps `import { ... } from 'svelte-origin/runtime'` as-is. │ │
34
+ * │ │ Consumers must install svelte-origin as a dependency. │ │
35
+ * │ │ │ │
36
+ * │ │ Use this when: │ │
37
+ * │ │ - Multiple libraries use svelte-origin (share runtime) │ │
38
+ * │ │ - You need advanced runtime features │ │
39
+ * │ │ - You prefer smaller per-library bundle size │ │
40
+ * │ └─────────────────────────────────────────────────────────────────────┘ │
41
+ * │ │
42
+ * │ RELATIONSHIP TO runtime/origin.ts │
43
+ * │ ═════════════════════════════════ │
44
+ * │ │
45
+ * │ This file is NOT a copy of runtime/origin.ts — it's a MINIMAL version: │
46
+ * │ │
47
+ * │ ┌────────────────────────┬────────────────────────────────────────────┐ │
48
+ * │ │ runtime/origin.ts │ runtime-inline.ts (this file) │ │
49
+ * │ ├────────────────────────┼────────────────────────────────────────────┤ │
50
+ * │ │ ~500 lines │ ~280 lines │ │
51
+ * │ │ Full JSDoc comments │ No comments (stripped for size) │ │
52
+ * │ │ Debug warnings │ Minimal error messages │ │
53
+ * │ │ getMergedAttrSchema* │ getMergedAttrSchema* (essential) │ │
54
+ * │ │ __createOrigin │ __createOrigin (essential) │ │
55
+ * │ │ __attrsFor │ __attrsFor (essential) │ │
56
+ * │ │ __createAttrs │ __createAttrs (essential) │ │
57
+ * │ │ __createOriginInstance│ __createOriginInstance (essential) │ │
58
+ * │ │ bindable, isBindable │ (not needed at runtime) │ │
59
+ * │ │ extractAttrSchema │ (not needed at runtime) │ │
60
+ * │ │ isAttrsFactory │ (not needed at runtime) │ │
61
+ * │ │ generateAttrKeys │ (not needed at runtime) │ │
62
+ * │ └────────────────────────┴────────────────────────────────────────────┘ │
63
+ * │ │
64
+ * │ The transforms compile `$origin`, `$origin.props`, `$bindable` into calls to │
65
+ * │ `__createOrigin`, `__createAttrs`, `__attrsFor` with pre-computed │
66
+ * │ `__attrSchema` objects — so runtime doesn't need schema extraction. │
67
+ * │ │
68
+ * │ IMPORTANT: Changes to runtime logic must be synced between both files! │
69
+ * │ │
70
+ * └─────────────────────────────────────────────────────────────────────────────┘
71
+ *
72
+ * @see post-process.ts - The CLI command that inlines this runtime
73
+ * @see runtime/origin.ts - The full runtime with all utilities
74
+ */
75
+ interface AttrSchemaEntry {
76
+ default: unknown;
77
+ bindable: boolean;
78
+ hasDefault: boolean;
79
+ }
80
+ type AttrSchema = Record<string, AttrSchemaEntry>;
81
+ interface OriginFactory<TInstance = any, TAttrs = any> {
82
+ (attrs?: TAttrs, ...initArgs: any[]): TInstance;
83
+ __origin: true;
84
+ __attrSchema: AttrSchema;
85
+ __parents: OriginFactory<any, any>[];
86
+ __create: (attrs: TAttrs, superInstances?: any[]) => TInstance;
87
+ __onInit?: (this: TInstance, ...args: any[]) => void | (() => void);
88
+ }
89
+ interface AttrsFactory<TAttrs = any> {
90
+ __attrs: true;
91
+ __attrSchema: AttrSchema;
92
+ __parents: AttrsFactory<any>[];
93
+ }
94
+ type AttachmentFn = (element: Element) => void | (() => void);
95
+ export declare function __createOrigin<TInstance, TAttrs extends Record<string, any>>(config: {
96
+ __attrSchema: AttrSchema;
97
+ __parents?: OriginFactory<any, any>[];
98
+ __create: (attrs: TAttrs, superInstances?: any[]) => TInstance;
99
+ __onInit?: (this: TInstance, ...args: any[]) => void | (() => void);
100
+ __attachments?: Record<string, AttachmentFn>;
101
+ }): OriginFactory<TInstance, TAttrs>;
102
+ export declare function __attrsFor<T extends {
103
+ __origin: true;
104
+ }>(factory: T, rawAttrs: Record<string, any>): Record<string, any>;
105
+ export declare function __createAttrs<TAttrs extends Record<string, any>>(config: {
106
+ __attrSchema: AttrSchema;
107
+ __parents?: AttrsFactory<any>[];
108
+ }): AttrsFactory<TAttrs>;
109
+ declare const BINDABLE_MARKER: unique symbol;
110
+ interface BindableMarker<T> {
111
+ [BINDABLE_MARKER]: true;
112
+ get: () => T;
113
+ set: (value: T) => void;
114
+ }
115
+ export declare function __bindable<T>(get: () => T, set: (value: T) => void): BindableMarker<T>;
116
+ export declare function __createOriginInstance<T extends OriginFactory<any, any>>(factory: T, props: Record<string, any>): ReturnType<T>;
117
+ export {};
@@ -0,0 +1 @@
1
+ import{createAttachmentKey as P}from"svelte/attachments";function V(H,J){let C=J&&typeof J==="object"?J:{},U={},X=new Set;return new Proxy(C,{get(Q,z){if(X.has(z))return U[z];if(typeof z==="symbol")return Q[z];let Y=Q[z];if(Y===void 0&&z in H&&H[z].hasDefault)return H[z].default;return Y},set(Q,z,Y){U[z]=Y;try{Q[z]=Y}catch{X.add(z)}return!0},has(Q,z){return typeof z==="symbol"?z in Q:(z in Q)||(z in H)},ownKeys(Q){return[...new Set([...Reflect.ownKeys(Q),...Object.keys(H)])]},getOwnPropertyDescriptor(Q,z){if(typeof z==="symbol")return Object.getOwnPropertyDescriptor(Q,z);if(z in Q)return Object.getOwnPropertyDescriptor(Q,z);if(z in H)return{configurable:!0,enumerable:!0,value:H[z].hasDefault?H[z].default:void 0};return}})}function E(H,J=new Set){if(J.has(H))return{};J.add(H);let C=H.__parents||[],U={};for(let X of C)Object.assign(U,E(X,J));return Object.assign(U,H.__attrSchema),U}function S(H,J,C=new Set){let U={};for(let X of J){if(C.has(X))continue;Object.assign(U,E(X,new Set(C)))}return Object.assign(U,H),U}function k(H){let{__attrSchema:J,__parents:C=[],__create:U,__onInit:X,__attachments:Q={}}=H;function z(Y,...N){let G,q;if(Y===null||Y===void 0)G={},q=N;else if(typeof Y!=="object"||Array.isArray(Y))G={},q=[Y,...N];else G=Y,q=N;let j=S(J,C),b=V(j,G),B=C.map((Z)=>Z(b)),F=B.length===1?B[0]:B.length>1?B:void 0,D=U(b,F);if(D===null||D===void 0)throw Error("[svelte-origin] __create returned null/undefined");let R=D,T=F?new Proxy(R,{get(Z,$,L){if($ in Z){let x=Reflect.get(Z,$,L);return typeof x==="function"?x.bind(L):x}if(F&&$ in F){let x=Reflect.get(F,$,F);return typeof x==="function"?x.bind(F):x}return},has(Z,$){return $ in Z||F&&$ in F},ownKeys(Z){let $=Reflect.ownKeys(Z);return F?[...new Set([...$,...Reflect.ownKeys(F)])]:$},getOwnPropertyDescriptor(Z,$){return Reflect.getOwnPropertyDescriptor(Z,$)||(F?Reflect.getOwnPropertyDescriptor(F,$):void 0)}}):R;if(X){let Z=X.call(T,...q);if(typeof Z==="function")Object.defineProperty(T,"__cleanup",{value:Z,writable:!0,enumerable:!0,configurable:!0})}let K=new Map;for(let Z of Object.keys(Q))K.set(Z,P());return Object.defineProperty(T,"$attachments",{get(){let Z={};for(let[$,L]of Object.entries(Q))Z[K.get($)]=(x)=>L.call(T,x);for(let $ of Object.getOwnPropertySymbols(G)){let L=G[$];if(typeof L==="function")Z[$]=L}return Z},enumerable:!0,configurable:!0}),T}if(Object.defineProperty(z,"__origin",{value:!0,writable:!1,enumerable:!0}),Object.defineProperty(z,"__attrSchema",{value:J,writable:!1,enumerable:!0}),Object.defineProperty(z,"__parents",{value:C,writable:!1,enumerable:!0}),Object.defineProperty(z,"__create",{value:U,writable:!1,enumerable:!0}),X)Object.defineProperty(z,"__onInit",{value:X,writable:!1,enumerable:!0});return z}function _(H,J){let U=E(H),X={},Q=new Set,z=(G,q)=>{X[G]=q;try{return J[G]=q,!0}catch{return Q.add(G),!1}},Y={},N=(G,q)=>{if(Object.prototype.hasOwnProperty.call(Y,G))return;let j={get(){return Q.has(G)?X[G]:J[G]},enumerable:!0,configurable:!0};if(q.bindable)j.set=function(b){z(G,b)};Object.defineProperty(Y,G,j)};for(let[G,q]of Object.entries(U)){let j=J[G];if(!q.bindable||!q.hasDefault||j!==void 0)N(G,q)}for(let G of Object.getOwnPropertySymbols(J))Object.defineProperty(Y,G,{get(){return J[G]},enumerable:!1,configurable:!0});return new Proxy(Y,{get(G,q){if(typeof q==="string"){if(Q.has(q))return X[q];if(q in U)return J[q]}return q in G?G[q]:J[q]},set(G,q,j){if(typeof q==="string"&&q in U){if(!Object.prototype.hasOwnProperty.call(Y,q))N(q,U[q]);if(U[q].bindable)z(q,j);return!0}if(q in G){try{G[q]=j}catch{}return!0}try{J[q]=j}catch{if(typeof q==="string")X[q]=j,Q.add(q)}return!0},has(G,q){return q in G||q in J},ownKeys(){return[...Object.keys(Y),...Object.getOwnPropertySymbols(J)]},getOwnPropertyDescriptor(G,q){return q in G?Object.getOwnPropertyDescriptor(G,q):Object.getOwnPropertyDescriptor(J,q)}})}function w(H){return{__attrs:!0,__attrSchema:H.__attrSchema,__parents:H.__parents||[]}}var M=Symbol.for("svelte-origin:bindable");function I(H,J){return{[M]:!0,get:H,set:J}}function W(H){return H!==null&&typeof H==="object"&&M in H}function d(H,J){let C={};for(let[U,X]of Object.entries(J))if(W(X))Object.defineProperty(C,U,{get(){return X.get()},set(Q){X.set(Q)},enumerable:!0,configurable:!0});else Object.defineProperty(C,U,{get(){return X},enumerable:!0,configurable:!0});return H(C)}export{d as __createOriginInstance,k as __createOrigin,w as __createAttrs,I as __bindable,_ as __attrsFor};
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Shared schema resolution logic for svelte-origin
3
+ *
4
+ * This module provides the core schema resolution functionality used by both
5
+ * the Svelte preprocessor and the Vite plugin. Extracting this into a shared
6
+ * module ensures:
7
+ *
8
+ * 1. Single source of truth - changes reflect in both preprocessor and plugin
9
+ * 2. Reduced code duplication - ~400 lines of duplicated code eliminated
10
+ * 3. Consistent behavior - both tools resolve schemas identically
11
+ *
12
+ * The schema resolver reads origin/attrs factory definitions from source files
13
+ * and recursively resolves parent schemas to build complete prop type information.
14
+ */
15
+ import { type ParsedOriginSchema, type ParsedAttrInfo, type SchemaResolver } from './transform/schema';
16
+ export type { ParsedOriginSchema, ParsedAttrInfo, SchemaResolver };
17
+ export interface SchemaResolverOptions {
18
+ /** Enable debug logging */
19
+ debug?: boolean;
20
+ /** Resolved alias mappings for import resolution */
21
+ aliases?: Record<string, string>;
22
+ /**
23
+ * Pre-cached imports from the module script.
24
+ * Maps local name (e.g., "TableState" or alias) to import path.
25
+ * This enables schema resolution when the import is in the module script
26
+ * but the $origin.component() call is in the component script.
27
+ */
28
+ moduleImports?: Map<string, string>;
29
+ }
30
+ /**
31
+ * Invalidate cache entries for a specific file.
32
+ * This is called when a file changes to ensure stale schemas are not served.
33
+ *
34
+ * @param filePath The absolute path of the file that changed
35
+ * @param cache The schema cache to invalidate entries from
36
+ */
37
+ export declare function invalidateCacheForFile(filePath: string, cache: Map<string, ParsedOriginSchema | null>): void;
38
+ /**
39
+ * Reset mtime tracking for testing purposes.
40
+ * This clears all cached mtime data to ensure clean test state.
41
+ */
42
+ export declare function resetMtimeTracking(): void;
43
+ /**
44
+ * Resolve a schema from an import path, recursively resolving parent schemas.
45
+ * This is the core schema resolution logic shared between preprocessor and plugin.
46
+ */
47
+ export declare function resolveSchema(importPath: string, exportName: string, importerId: string, cache: Map<string, ParsedOriginSchema | null>, options?: SchemaResolverOptions): Promise<ParsedOriginSchema | null>;
48
+ /**
49
+ * Resolve an $origin.props factory schema from an import path.
50
+ * Similar to resolveSchema but handles $origin.props factories instead of $origin.
51
+ */
52
+ export declare function resolveAttrsSchema(importPath: string, exportName: string, importerId: string, cache: Map<string, ParsedOriginSchema | null>, options?: SchemaResolverOptions): Promise<ParsedOriginSchema | null>;
53
+ /**
54
+ * Create a schema resolver bound to a specific file and cache.
55
+ * This creates the SchemaResolver interface expected by transform functions.
56
+ *
57
+ * Resolution priority when importPath is null:
58
+ * 1. Check moduleImports (from preprocessor's module script cache)
59
+ * 2. Attempt sibling file resolution (guessing by factory name)
60
+ */
61
+ export declare function createSchemaResolver(importerId: string, cache: Map<string, ParsedOriginSchema | null>, options?: SchemaResolverOptions): SchemaResolver;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Shared state between preprocessor and plugin
3
+ *
4
+ * This module allows the preprocessor to signal to the plugin that it's active,
5
+ * so the plugin knows whether it should handle .svelte files or not.
6
+ *
7
+ * It also shares output settings from the plugin to the preprocessor,
8
+ * so both can write transformed output to the same directory.
9
+ *
10
+ * This works because both the preprocessor (via svelte.config.js) and the plugin
11
+ * (via vite.config.ts) run in the same Node.js process during Vite startup.
12
+ */
13
+ /**
14
+ * Shared output settings from the plugin
15
+ */
16
+ export interface OutputSettings {
17
+ /** Whether to output transformed files */
18
+ outputTransformed: boolean;
19
+ /** Directory for transformed output (absolute path) */
20
+ outputDir?: string;
21
+ /** Enable debug logging */
22
+ debug: boolean;
23
+ }
24
+ /**
25
+ * Mark the preprocessor as active.
26
+ * Called by svelteOriginPreprocess() when it initializes.
27
+ */
28
+ export declare function markPreprocessorActive(): void;
29
+ /**
30
+ * Check if the preprocessor is active.
31
+ * Called by svelteOrigin() plugin to decide if it should handle .svelte files.
32
+ */
33
+ export declare function isPreprocessorActive(): boolean;
34
+ /**
35
+ * Set output settings from the plugin.
36
+ * Called by svelteOrigin() plugin during initialization.
37
+ */
38
+ export declare function setOutputSettings(settings: Partial<OutputSettings>): void;
39
+ /**
40
+ * Get current output settings.
41
+ * Used by the preprocessor to write transformed output.
42
+ */
43
+ export declare function getOutputSettings(): OutputSettings;
44
+ /**
45
+ * Reset the state (for testing purposes only)
46
+ */
47
+ export declare function resetState(): void;