@lushly-dev/violet-runtime 0.0.0-stage → 0.3.0-preview.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../src/theme.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;GAEG;AACH,MAAM,WAAW,mBAAmB;IACnC,uBAAuB;IACvB,MAAM,EAAE,KAAK,EAAE,CAAC;IAEhB,4BAA4B;IAC5B,YAAY,EAAE,MAAM,CAAC;IAErB,kDAAkD;IAClD,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,+CAA+C;IAC/C,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B,+CAA+C;IAC/C,eAAe,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,YAAY;IACxB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,MAAM,CAAiC;IAC/C,OAAO,CAAC,YAAY,CAAsB;IAC1C,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,kBAAkB,CAAS;IACnC,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,SAAS,CAA0C;IAC3D,oFAAoF;IACpF,OAAO,CAAC,UAAU,CAAK;IACvB,6EAA6E;IAC7E,OAAO,CAAC,cAAc,CAAuB;IAC7C,8EAA8E;IAC9E,OAAO,CAAC,iBAAiB,CAAK;IAE9B,YAAY,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,mBAAmB,EAgB/D;IAED;;OAEG;IACH,SAAS,IAAI,KAAK,EAAE,CAEnB;IAED;;OAEG;IACH,eAAe,IAAI,KAAK,GAAG,IAAI,CAE9B;IAED;;OAEG;IACH,aAAa,IAAI,MAAM,GAAG,IAAI,CAS7B;IAED;;OAEG;IACH,OAAO,CAAC,SAAS;IAUjB;;OAEG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAwBzC;IAED;;OAEG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAwC1C;IAED;;OAEG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG1D;IAED;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAK3C;IAED;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAGhC;IAED;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,MAAM,IAAI,CAKrD;IAED;;;;;;;OAOG;IACH,mBAAmB,CAClB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,iBAAiB,CAAC,EAAE,MAAM,GACxB,MAAM,IAAI,CAsCZ;IAED;;OAEG;IACH,OAAO,CAAC,KAAK;IAKb;;OAEG;IACH,OAAO,CAAC,MAAM;IAId;;OAEG;IACH,OAAO,CAAC,aAAa;IAMrB;;OAEG;IACH,OAAO,CAAC,eAAe;IAUvB;;OAEG;IACH,OAAO,CAAC,KAAK;CAGb"}
package/dist/theme.js ADDED
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Theme Manager
3
+ *
4
+ * Manages theme switching with preloading and transitions.
5
+ */
6
+ /**
7
+ * ThemeManager - Manages theme switching with smooth transitions.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const violet = new VioletRuntime({ nodeId: "brand-light" });
12
+ * await violet.load();
13
+ *
14
+ * const themes = new ThemeManager(violet, {
15
+ * themes: [
16
+ * { nodeId: "brand-light", name: "Light" },
17
+ * { nodeId: "brand-dark", name: "Dark" },
18
+ * ],
19
+ * defaultTheme: "brand-light",
20
+ * storageKey: "preferred-theme",
21
+ * });
22
+ *
23
+ * // Get saved preference or default
24
+ * const initial = themes.getSavedTheme();
25
+ * await themes.apply(initial);
26
+ *
27
+ * // Switch theme
28
+ * await themes.switch("brand-dark");
29
+ *
30
+ * // Preload for instant switching
31
+ * await themes.preloadAll();
32
+ * ```
33
+ */
34
+ export class ThemeManager {
35
+ runtime;
36
+ themes = new Map();
37
+ currentTheme = null;
38
+ storageKey;
39
+ transitionDuration;
40
+ transitionClass;
41
+ listeners = new Set();
42
+ /** Incremented by every apply()/switch(); only the latest request updates state. */
43
+ requestSeq = 0;
44
+ /** Theme of the latest apply()/switch() while it is in flight, else null. */
45
+ requestedTheme = null;
46
+ /** Number of in-flight transitions holding the transition class on <html>. */
47
+ activeTransitions = 0;
48
+ constructor(runtime, options) {
49
+ this.runtime = runtime;
50
+ this.storageKey = options.storageKey ?? null;
51
+ this.transitionDuration = options.transitionDuration ?? 200;
52
+ this.transitionClass = options.transitionClass ?? 'theme-transitioning';
53
+ // Register themes
54
+ for (const theme of options.themes) {
55
+ this.themes.set(theme.nodeId, theme);
56
+ }
57
+ // Set initial theme from storage or default
58
+ // A stale saved preference (theme no longer registered) falls back to the default.
59
+ const saved = this.getSavedTheme();
60
+ this.currentTheme =
61
+ (saved ? this.themes.get(saved) : undefined) ?? this.themes.get(options.defaultTheme) ?? null;
62
+ }
63
+ /**
64
+ * Get all available themes.
65
+ */
66
+ getThemes() {
67
+ return Array.from(this.themes.values());
68
+ }
69
+ /**
70
+ * Get current theme.
71
+ */
72
+ getCurrentTheme() {
73
+ return this.currentTheme;
74
+ }
75
+ /**
76
+ * Get saved theme preference from storage.
77
+ */
78
+ getSavedTheme() {
79
+ if (!this.storageKey)
80
+ return null;
81
+ try {
82
+ // Even touching `localStorage` can throw (SecurityError) when storage is blocked.
83
+ if (typeof localStorage === 'undefined')
84
+ return null;
85
+ return localStorage.getItem(this.storageKey);
86
+ }
87
+ catch {
88
+ return null;
89
+ }
90
+ }
91
+ /**
92
+ * Save theme preference to storage (best-effort).
93
+ */
94
+ saveTheme(nodeId) {
95
+ if (!this.storageKey)
96
+ return;
97
+ try {
98
+ if (typeof localStorage === 'undefined')
99
+ return;
100
+ localStorage.setItem(this.storageKey, nodeId);
101
+ }
102
+ catch {
103
+ // Storage blocked or full - the preference simply isn't persisted.
104
+ }
105
+ }
106
+ /**
107
+ * Apply a theme (initial load).
108
+ */
109
+ async apply(nodeId) {
110
+ const theme = this.themes.get(nodeId);
111
+ if (!theme) {
112
+ throw new Error(`Theme not found: ${nodeId}`);
113
+ }
114
+ const seq = this.begin(nodeId);
115
+ try {
116
+ if (this.runtime.getCurrentNodeId() !== nodeId) {
117
+ await this.switchRuntime(theme);
118
+ }
119
+ else {
120
+ await this.runtime.load();
121
+ }
122
+ }
123
+ finally {
124
+ this.settle(seq);
125
+ }
126
+ // A newer apply()/switch() superseded this one: leave its state alone.
127
+ if (seq !== this.requestSeq)
128
+ return;
129
+ this.currentTheme = theme;
130
+ this.saveTheme(nodeId);
131
+ this.notifyListeners(theme);
132
+ }
133
+ /**
134
+ * Switch to a different theme with transition.
135
+ */
136
+ async switch(nodeId) {
137
+ const theme = this.themes.get(nodeId);
138
+ if (!theme) {
139
+ throw new Error(`Theme not found: ${nodeId}`);
140
+ }
141
+ // Already on this theme, or already switching to it. Compared with the latest
142
+ // request, not the committed theme, so a quick A -> B -> A ends on A.
143
+ if ((this.requestedTheme ?? this.currentTheme?.nodeId) === nodeId) {
144
+ return;
145
+ }
146
+ const seq = this.begin(nodeId);
147
+ // Add transition class (removed in `finally`, even if the switch fails)
148
+ if (typeof document !== 'undefined') {
149
+ document.documentElement.classList.add(this.transitionClass);
150
+ }
151
+ this.activeTransitions++;
152
+ try {
153
+ // Switch tokens
154
+ await this.switchRuntime(theme);
155
+ // Wait for transition duration
156
+ await this.delay(this.transitionDuration);
157
+ }
158
+ finally {
159
+ this.activeTransitions--;
160
+ if (this.activeTransitions === 0 && typeof document !== 'undefined') {
161
+ document.documentElement.classList.remove(this.transitionClass);
162
+ }
163
+ this.settle(seq);
164
+ }
165
+ // A newer apply()/switch() superseded this one: leave its state alone.
166
+ if (seq !== this.requestSeq)
167
+ return;
168
+ this.currentTheme = theme;
169
+ this.saveTheme(nodeId);
170
+ this.notifyListeners(theme);
171
+ }
172
+ /**
173
+ * Toggle between two themes.
174
+ */
175
+ async toggle(themeA, themeB) {
176
+ const nextTheme = this.currentTheme?.nodeId === themeA ? themeB : themeA;
177
+ await this.switch(nextTheme);
178
+ }
179
+ /**
180
+ * Preload a specific theme.
181
+ */
182
+ async preload(nodeId) {
183
+ if (!this.themes.has(nodeId)) {
184
+ throw new Error(`Theme not found: ${nodeId}`);
185
+ }
186
+ await this.runtime.preload(nodeId);
187
+ }
188
+ /**
189
+ * Preload all themes.
190
+ */
191
+ async preloadAll() {
192
+ const preloads = Array.from(this.themes.keys()).map((nodeId) => this.runtime.preload(nodeId));
193
+ await Promise.all(preloads);
194
+ }
195
+ /**
196
+ * Subscribe to theme changes.
197
+ */
198
+ onChange(listener) {
199
+ this.listeners.add(listener);
200
+ return () => {
201
+ this.listeners.delete(listener);
202
+ };
203
+ }
204
+ /**
205
+ * Set up system preference detection (prefers-color-scheme), and, when a
206
+ * high-contrast theme is given, prefers-contrast and forced-colors: the
207
+ * high-contrast theme wins whenever `(prefers-contrast: more)` or
208
+ * `(forced-colors: active)` matches, whatever the color scheme.
209
+ *
210
+ * Follows changes to every query until the returned function is called.
211
+ */
212
+ useSystemPreference(lightTheme, darkTheme, highContrastTheme) {
213
+ if (typeof window === 'undefined' || !window.matchMedia) {
214
+ return () => { };
215
+ }
216
+ const dark = window.matchMedia('(prefers-color-scheme: dark)');
217
+ const contrast = highContrastTheme
218
+ ? [
219
+ window.matchMedia('(prefers-contrast: more)'),
220
+ window.matchMedia('(forced-colors: active)'),
221
+ ]
222
+ : [];
223
+ // Read every query on each change, so one query's change never undoes another's.
224
+ const handler = () => {
225
+ const theme = highContrastTheme && contrast.some((query) => query.matches)
226
+ ? highContrastTheme
227
+ : dark.matches
228
+ ? darkTheme
229
+ : lightTheme;
230
+ this.switch(theme).catch(console.error);
231
+ };
232
+ // Set initial theme based on system preference
233
+ handler();
234
+ // Listen for changes
235
+ const queries = [dark, ...contrast];
236
+ for (const query of queries) {
237
+ query.addEventListener('change', handler);
238
+ }
239
+ return () => {
240
+ for (const query of queries) {
241
+ query.removeEventListener('change', handler);
242
+ }
243
+ };
244
+ }
245
+ /**
246
+ * Start a request: it becomes the latest, and its theme the requested one.
247
+ */
248
+ begin(nodeId) {
249
+ this.requestedTheme = nodeId;
250
+ return ++this.requestSeq;
251
+ }
252
+ /**
253
+ * End a request, successful or not. Only the latest clears the requested theme.
254
+ */
255
+ settle(seq) {
256
+ if (seq === this.requestSeq)
257
+ this.requestedTheme = null;
258
+ }
259
+ /**
260
+ * Switch the runtime to a theme, with the theme's preloaded tokens if it has any.
261
+ */
262
+ switchRuntime(theme) {
263
+ return theme.source
264
+ ? this.runtime.switchNode(theme.nodeId, theme.source)
265
+ : this.runtime.switchNode(theme.nodeId);
266
+ }
267
+ /**
268
+ * Notify listeners of theme change.
269
+ */
270
+ notifyListeners(theme) {
271
+ for (const listener of this.listeners) {
272
+ try {
273
+ listener(theme);
274
+ }
275
+ catch {
276
+ // Don't let listener errors break the manager
277
+ }
278
+ }
279
+ }
280
+ /**
281
+ * Delay helper.
282
+ */
283
+ delay(ms) {
284
+ return new Promise((resolve) => setTimeout(resolve, ms));
285
+ }
286
+ }
287
+ //# sourceMappingURL=theme.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"theme.js","sourceRoot":"","sources":["../src/theme.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAyBH;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,OAAO,YAAY;IAChB,OAAO,CAAgB;IACvB,MAAM,GAAuB,IAAI,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web SDK.
5
+ */
6
+ /**
7
+ * Configuration for VioletRuntime: tokens come either from a Violet token
8
+ * server ({@link VioletHttpConfig}) or from a preloaded source
9
+ * ({@link VioletSourceConfig}), never both.
10
+ */
11
+ export type VioletConfig = VioletHttpConfig | VioletSourceConfig;
12
+ /**
13
+ * Load tokens over HTTP from a Violet deployment.
14
+ */
15
+ export interface VioletHttpConfig extends VioletBaseConfig {
16
+ /**
17
+ * Base URL of your Violet deployment. Required when `apiKey` is set; without
18
+ * a key it defaults to the page's own origin (relative `/api/v1/...` requests).
19
+ */
20
+ apiUrl?: string;
21
+ /** API key for authentication. Needs `apiUrl`, so the key never goes to a default host */
22
+ apiKey?: string;
23
+ source?: never;
24
+ }
25
+ /**
26
+ * Apply tokens already in memory, such as a static build's resolved theme.
27
+ * No request is made and the cache is never read or written.
28
+ */
29
+ export interface VioletSourceConfig extends VioletBaseConfig {
30
+ /** Resolved tokens for `nodeId` */
31
+ source: VioletTokenSource;
32
+ apiUrl?: never;
33
+ apiKey?: never;
34
+ }
35
+ /**
36
+ * Preloaded tokens for one node.
37
+ */
38
+ export interface VioletTokenSource {
39
+ tokens: readonly SourceToken[];
40
+ }
41
+ /**
42
+ * A token in a preloaded source. `resolvedAt` may be omitted; it then
43
+ * defaults to the node the source was given for.
44
+ */
45
+ export type SourceToken = Omit<Token, 'resolvedAt'> & {
46
+ resolvedAt?: string;
47
+ };
48
+ /**
49
+ * Options shared by both token origins.
50
+ */
51
+ export interface VioletBaseConfig {
52
+ /** Node ID to load tokens from (e.g., "xbox-web-dark") */
53
+ nodeId: string;
54
+ /** Enable local caching (default: true). Ignored with a preloaded source */
55
+ cacheEnabled?: boolean;
56
+ /** Cache TTL in seconds (default: 3600 = 1 hour) */
57
+ cacheTtl?: number;
58
+ /** Auto-apply tokens as CSS variables on load (default: true) */
59
+ autoApply?: boolean;
60
+ /** CSS selector prefix for variables (default: empty) */
61
+ cssPrefix?: string;
62
+ /** Fetch timeout in milliseconds (default: 10000). Use 0 to disable. Unused with a preloaded source */
63
+ timeout?: number;
64
+ }
65
+ /**
66
+ * Resolved token with value and metadata
67
+ */
68
+ export interface Token {
69
+ /** Token ID (e.g., "color.brand.primary") */
70
+ id: string;
71
+ /** Resolved value */
72
+ value: string | number | Record<string, unknown>;
73
+ /** Node where token was resolved */
74
+ resolvedAt: string;
75
+ /** Token metadata */
76
+ metadata?: TokenMetadata;
77
+ }
78
+ /**
79
+ * Token metadata
80
+ */
81
+ export interface TokenMetadata {
82
+ description?: string;
83
+ deprecated?: boolean;
84
+ group?: string;
85
+ type?: string;
86
+ [key: string]: unknown;
87
+ }
88
+ /**
89
+ * Theme definition
90
+ */
91
+ export interface Theme {
92
+ /** Theme node ID */
93
+ nodeId: string;
94
+ /** Display name */
95
+ name: string;
96
+ /** Optional description */
97
+ description?: string;
98
+ /**
99
+ * Preloaded tokens for this theme, passed to `switchNode` when a
100
+ * ThemeManager switches to it. Needed with a runtime created with a
101
+ * `source`, except for the node that runtime already holds; refused by an
102
+ * HTTP runtime.
103
+ */
104
+ source?: VioletTokenSource;
105
+ }
106
+ /**
107
+ * Cache entry structure
108
+ */
109
+ export interface CacheEntry {
110
+ /** Cached tokens */
111
+ tokens: Token[];
112
+ /** Timestamp when cached */
113
+ cachedAt: number;
114
+ /** TTL in seconds */
115
+ ttl: number;
116
+ /** Node ID */
117
+ nodeId: string;
118
+ }
119
+ /**
120
+ * API response wrapper
121
+ */
122
+ export interface ApiResponse<T> {
123
+ success: boolean;
124
+ data?: T;
125
+ error?: {
126
+ code: string;
127
+ message: string;
128
+ suggestion?: string;
129
+ };
130
+ }
131
+ /**
132
+ * Token resolution response
133
+ */
134
+ export interface TokensResponse {
135
+ tokens: Token[];
136
+ nodeId: string;
137
+ ancestry: string[];
138
+ tokenCount: number;
139
+ }
140
+ /**
141
+ * Event types for runtime events
142
+ */
143
+ export type RuntimeEvent = 'load' | 'load:error' | 'theme:change' | 'cache:hit' | 'cache:miss' | 'cache:update';
144
+ /**
145
+ * Event listener callback
146
+ */
147
+ export type RuntimeEventListener = (event: RuntimeEventData) => void;
148
+ /**
149
+ * Event data payload
150
+ */
151
+ export interface RuntimeEventData {
152
+ type: RuntimeEvent;
153
+ nodeId: string;
154
+ timestamp: number;
155
+ data?: unknown;
156
+ }
157
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,gBAAgB,GAAG,kBAAkB,CAAC;AAEjE;;GAEG;AACH,MAAM,WAAW,gBAAiB,SAAQ,gBAAgB;IACzD;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,0FAA0F;IAC1F,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,MAAM,CAAC,EAAE,KAAK,CAAC;CACf;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAmB,SAAQ,gBAAgB;IAC3D,mCAAmC;IACnC,MAAM,EAAE,iBAAiB,CAAC;IAE1B,MAAM,CAAC,EAAE,KAAK,CAAC;IACf,MAAM,CAAC,EAAE,KAAK,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IACjC,MAAM,EAAE,SAAS,WAAW,EAAE,CAAC;CAC/B;AAED;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,EAAE,YAAY,CAAC,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9E;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC,0DAA0D;IAC1D,MAAM,EAAE,MAAM,CAAC;IAEf,4EAA4E;IAC5E,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB,oDAAoD;IACpD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,iEAAiE;IACjE,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,yDAAyD;IACzD,SAAS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b,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GACrB,MAAM,GACN,YAAY,GACZ,cAAc,GACd,WAAW,GACX,YAAY,GACZ,cAAc,CAAC;AAElB;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,OAAO,CAAC;CACf"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Violet Runtime Types
3
+ *
4
+ * Core type definitions for the Web SDK.
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
package/package.json CHANGED
@@ -1,6 +1,60 @@
1
1
  {
2
2
  "name": "@lushly-dev/violet-runtime",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.0-preview.0",
4
+ "license": "MIT",
5
+ "author": "Falkicon",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/lushly-dev/afd.git",
9
+ "directory": "foundations/violet/packages/runtime"
10
+ },
11
+ "homepage": "https://github.com/lushly-dev/afd#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/lushly-dev/afd/issues"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "keywords": [
19
+ "design-tokens",
20
+ "theming",
21
+ "css-variables",
22
+ "runtime",
23
+ "violet"
24
+ ],
25
+ "engines": {
26
+ "node": ">=24"
27
+ },
28
+ "description": "Violet Design Runtime - Web SDK for dynamic token loading and theme switching",
29
+ "type": "module",
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "import": "./dist/index.js",
35
+ "types": "./dist/index.d.ts"
36
+ }
37
+ },
38
+ "devDependencies": {
39
+ "@biomejs/biome": "^2.6.6",
40
+ "@types/node": "^26.6.6",
41
+ "@vitest/coverage-v8": "^5.1.0",
42
+ "happy-dom": "^21.0.4",
43
+ "typescript": "^7.0.2",
44
+ "vitest": "^5.1.0"
45
+ },
46
+ "files": [
47
+ "dist",
48
+ "README.md",
49
+ "LICENSE"
50
+ ],
51
+ "scripts": {
52
+ "build": "tsc",
53
+ "dev": "tsc --watch",
54
+ "lint": "biome check src/",
55
+ "test": "vitest run",
56
+ "test:coverage": "vitest run --coverage",
57
+ "test:watch": "vitest",
58
+ "clean": "rm -rf dist *.tsbuildinfo"
59
+ }
6
60
  }