@lokascript/vite-plugin 1.0.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,379 @@
1
+ import { Plugin } from 'vite';
2
+ import { ASTNode } from '@lokascript/core/parser/hybrid/ast-types';
3
+
4
+ /**
5
+ * Vite Plugin Types
6
+ *
7
+ * Type definitions for the HyperFixi Vite plugin.
8
+ */
9
+ /**
10
+ * Options for the HyperFixi Vite plugin
11
+ */
12
+ interface HyperfixiPluginOptions {
13
+ /**
14
+ * Bundle generation mode:
15
+ * - 'interpret': Generate minimal bundle with parser (default, ~8KB gzip)
16
+ * - 'compile': Pre-compile hyperscript to JS at build time (~500 bytes gzip)
17
+ *
18
+ * Compile mode limitations:
19
+ * - No dynamic hyperscript (runtime execute() won't work)
20
+ * - No block commands (if, for, repeat, while, fetch)
21
+ * - HTML must be transformed to use data-h attributes
22
+ *
23
+ * Use compile mode when:
24
+ * - Bundle size is critical (<1KB target)
25
+ * - All hyperscript is static (no dynamic generation)
26
+ * - Only simple commands are used (toggle, add, remove, set, etc.)
27
+ */
28
+ mode?: 'interpret' | 'compile';
29
+ /**
30
+ * File patterns to scan for hyperscript usage.
31
+ * Defaults to common web file extensions.
32
+ */
33
+ include?: RegExp | string[];
34
+ /**
35
+ * File patterns to exclude from scanning.
36
+ * Defaults to node_modules.
37
+ */
38
+ exclude?: RegExp | string[];
39
+ /**
40
+ * Extra commands to always include in the bundle.
41
+ * Use this for dynamically generated hyperscript that can't be detected.
42
+ */
43
+ extraCommands?: string[];
44
+ /**
45
+ * Extra blocks to always include in the bundle.
46
+ */
47
+ extraBlocks?: string[];
48
+ /**
49
+ * Always include positional expressions (first, last, next, closest, parent).
50
+ * Set to true if your dynamic code uses these.
51
+ */
52
+ positional?: boolean;
53
+ /**
54
+ * Enable HTMX integration (auto-process after htmx:afterSettle).
55
+ */
56
+ htmx?: boolean;
57
+ /**
58
+ * Development mode fallback strategy.
59
+ * - 'hybrid-complete': Use pre-built hybrid-complete bundle for faster dev (default)
60
+ * - 'full': Use full bundle for complete compatibility
61
+ * - 'auto': Generate minimal bundle even in dev
62
+ */
63
+ devFallback?: 'hybrid-complete' | 'full' | 'auto';
64
+ /**
65
+ * Global variable name for the hyperfixi API.
66
+ * Defaults to 'hyperfixi'.
67
+ */
68
+ globalName?: string;
69
+ /**
70
+ * Bundle name for generated code comments.
71
+ * Defaults to 'ViteAutoGenerated'.
72
+ */
73
+ bundleName?: string;
74
+ /**
75
+ * Enable verbose logging during development.
76
+ */
77
+ debug?: boolean;
78
+ /**
79
+ * Enable semantic parsing for multilingual support.
80
+ * - false: No semantic parsing (default, current behavior)
81
+ * - true: Auto-detect languages from templates
82
+ * - 'en': English semantic only (synonyms, flexible syntax)
83
+ * - 'auto': Same as true
84
+ */
85
+ semantic?: boolean | 'en' | 'auto';
86
+ /**
87
+ * Explicit language list (overrides auto-detection).
88
+ * Use ISO 639-1 codes: 'en', 'es', 'ja', 'ko', 'zh', 'ar', etc.
89
+ */
90
+ languages?: string[];
91
+ /**
92
+ * Regional bundle shorthand (alternative to explicit languages).
93
+ * - 'western': en, es, pt, fr, de, it
94
+ * - 'east-asian': ja, zh, ko
95
+ * - 'slavic': pl, ru, uk
96
+ * - 'south-asian': hi, bn
97
+ * - 'priority': 13 most common languages
98
+ * - 'all': All 21 supported languages
99
+ */
100
+ region?: 'western' | 'east-asian' | 'slavic' | 'south-asian' | 'priority' | 'all';
101
+ /**
102
+ * Enable grammar transformation for native word order.
103
+ * When true, semantic is automatically enabled.
104
+ * Adds support for SOV (Japanese, Korean) and VSO (Arabic) word orders.
105
+ */
106
+ grammar?: boolean;
107
+ /**
108
+ * Always include these languages in addition to detected ones.
109
+ * Useful for dynamic content not detectable at build time.
110
+ */
111
+ extraLanguages?: string[];
112
+ /**
113
+ * Custom language keyword definitions.
114
+ * Use this to add new languages or extend/override existing keyword detection.
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * hyperfixi({
119
+ * customKeywords: {
120
+ * // Add a new language
121
+ * 'my-lang': {
122
+ * keywords: new Set(['mytoggle', 'myadd', 'myremove']),
123
+ * isNonLatin: false,
124
+ * },
125
+ * // Extend an existing language with additional keywords
126
+ * 'es': {
127
+ * keywords: new Set(['conmutar', 'intercambiar']), // Additional Spanish keywords
128
+ * extend: true, // Merge with existing keywords
129
+ * },
130
+ * },
131
+ * })
132
+ * ```
133
+ */
134
+ customKeywords?: Record<string, CustomLanguageKeywords>;
135
+ }
136
+ /**
137
+ * Custom language keyword configuration.
138
+ */
139
+ interface CustomLanguageKeywords {
140
+ /**
141
+ * Set of keywords for this language.
142
+ */
143
+ keywords: Set<string>;
144
+ /**
145
+ * Whether this language uses non-Latin script.
146
+ * Non-Latin scripts use simple substring matching (no word boundaries).
147
+ * Latin scripts use word boundary matching to avoid false positives.
148
+ */
149
+ isNonLatin?: boolean;
150
+ /**
151
+ * If true, merge these keywords with existing ones for this language.
152
+ * If false (default), replace existing keywords entirely.
153
+ */
154
+ extend?: boolean;
155
+ }
156
+ /**
157
+ * Usage information detected from a single file
158
+ */
159
+ interface FileUsage {
160
+ /** Commands used in this file */
161
+ commands: Set<string>;
162
+ /** Block types used (if, repeat, for, while, fetch) */
163
+ blocks: Set<string>;
164
+ /** Whether positional expressions are used */
165
+ positional: boolean;
166
+ /** Non-English languages detected in hyperscript (ISO 639-1 codes) */
167
+ detectedLanguages: Set<string>;
168
+ }
169
+ /**
170
+ * Aggregated usage information across all files
171
+ */
172
+ interface AggregatedUsage {
173
+ /** All commands detected across all files */
174
+ commands: Set<string>;
175
+ /** All blocks detected across all files */
176
+ blocks: Set<string>;
177
+ /** Whether any file uses positional expressions */
178
+ positional: boolean;
179
+ /** All non-English languages detected across all files */
180
+ detectedLanguages: Set<string>;
181
+ /** Map of file paths to their usage */
182
+ fileUsage: Map<string, FileUsage>;
183
+ }
184
+
185
+ /**
186
+ * Compiler
187
+ *
188
+ * Compiles hyperscript AST to JavaScript at build time.
189
+ * Eliminates the need for runtime parsing.
190
+ *
191
+ * Multilingual support:
192
+ * - For English: Uses HybridParser directly
193
+ * - For other languages: Uses semantic parser to translate → AST → JavaScript
194
+ * - Semantic parsing happens at BUILD time (zero runtime overhead)
195
+ */
196
+
197
+ /**
198
+ * Semantic analyzer interface for multilingual support.
199
+ * This matches the interface from @lokascript/semantic.
200
+ */
201
+ interface SemanticAnalysisResult {
202
+ confidence: number;
203
+ node?: unknown;
204
+ errors?: string[];
205
+ }
206
+ interface SemanticAnalyzer {
207
+ analyze(input: string, language: string): SemanticAnalysisResult;
208
+ supportsLanguage(language: string): boolean;
209
+ }
210
+ type BuildASTFn = (node: unknown) => {
211
+ ast: ASTNode;
212
+ warnings: string[];
213
+ };
214
+ /**
215
+ * Configure the semantic parser for multilingual compilation.
216
+ * Call this with the semantic analyzer from @lokascript/semantic.
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * import { createSemanticAnalyzer, buildAST } from '@lokascript/semantic';
221
+ * import { setSemanticParser } from '@lokascript/vite-plugin';
222
+ *
223
+ * setSemanticParser(createSemanticAnalyzer(), buildAST);
224
+ * ```
225
+ */
226
+ declare function setSemanticParser(analyzer: SemanticAnalyzer, buildAST: BuildASTFn): void;
227
+ /**
228
+ * Clear the semantic parser (for testing).
229
+ */
230
+ declare function clearSemanticParser(): void;
231
+ /**
232
+ * Check if semantic parser is available.
233
+ */
234
+ declare function hasSemanticParser(): boolean;
235
+ interface CompiledHandler {
236
+ /** Unique handler ID (h0, h1, etc.) */
237
+ id: string;
238
+ /** Event name (click, input, etc.) */
239
+ event: string;
240
+ /** Event modifiers */
241
+ modifiers: {
242
+ prevent?: boolean;
243
+ stop?: boolean;
244
+ once?: boolean;
245
+ debounce?: number;
246
+ throttle?: number;
247
+ };
248
+ /** Compiled JavaScript code */
249
+ code: string;
250
+ /** Whether handler needs the mini-evaluator for dynamic expressions */
251
+ needsEvaluator: boolean;
252
+ /** Original hyperscript for debugging */
253
+ original: string;
254
+ }
255
+ /**
256
+ * Compile options for multilingual support.
257
+ */
258
+ interface CompileOptions {
259
+ /** Language code (ISO 639-1). Defaults to 'en'. */
260
+ language?: string;
261
+ /** Enable debug logging. */
262
+ debug?: boolean;
263
+ }
264
+
265
+ /**
266
+ * Language Keywords for Detection
267
+ *
268
+ * Maps of keywords for each of the 21 supported languages.
269
+ * Used by the scanner to detect which languages are used in hyperscript templates.
270
+ *
271
+ * Note: These are a representative subset of keywords - enough to reliably
272
+ * detect language usage without including every possible keyword variant.
273
+ */
274
+
275
+ /**
276
+ * All supported language codes.
277
+ */
278
+ declare const SUPPORTED_LANGUAGES: readonly ["en", "es", "pt", "fr", "de", "it", "vi", "pl", "ru", "uk", "ja", "zh", "ko", "ar", "hi", "bn", "th", "tr", "id", "sw", "qu", "tl"];
279
+ type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];
280
+ /**
281
+ * Regional bundle mappings.
282
+ */
283
+ declare const REGIONS: {
284
+ western: SupportedLanguage[];
285
+ 'east-asian': SupportedLanguage[];
286
+ slavic: SupportedLanguage[];
287
+ 'south-asian': SupportedLanguage[];
288
+ priority: SupportedLanguage[];
289
+ all: SupportedLanguage[];
290
+ };
291
+ /**
292
+ * Detect all languages used in a hyperscript string.
293
+ * Returns a Set of language codes found.
294
+ *
295
+ * Note: English is never detected (it's the default).
296
+ * Only non-English languages are detected.
297
+ */
298
+ declare function detectLanguages(script: string): Set<SupportedLanguage>;
299
+ /**
300
+ * Register custom keywords for a language.
301
+ * Call this before scanning to add or extend language detection.
302
+ *
303
+ * @param code - Language code (e.g., 'es', 'my-lang')
304
+ * @param config - Keyword configuration
305
+ */
306
+ declare function registerCustomKeywords(code: string, config: CustomLanguageKeywords): void;
307
+ /**
308
+ * Get keywords for a language, including custom registrations.
309
+ */
310
+ declare function getKeywordsForLanguage(code: string): Set<string> | undefined;
311
+ /**
312
+ * Check if a language uses non-Latin script.
313
+ */
314
+ declare function isNonLatinLanguage(code: string): boolean;
315
+ /**
316
+ * Get all registered language codes (built-in + custom).
317
+ */
318
+ declare function getAllLanguageCodes(): string[];
319
+ /**
320
+ * Clear all custom keyword registrations.
321
+ */
322
+ declare function clearCustomKeywords(): void;
323
+ /**
324
+ * Get the optimal regional bundle for a set of detected languages.
325
+ * Returns the smallest bundle that covers all detected languages.
326
+ */
327
+ declare function getOptimalRegion(languages: Set<SupportedLanguage>): 'western' | 'east-asian' | 'slavic' | 'south-asian' | 'priority' | 'all' | null;
328
+
329
+ /**
330
+ * @lokascript/vite-plugin
331
+ *
332
+ * Zero-config Vite plugin that automatically generates minimal HyperFixi bundles
333
+ * based on detected hyperscript usage in your source files.
334
+ *
335
+ * @example
336
+ * ```javascript
337
+ * // vite.config.js
338
+ * import { hyperfixi } from '@lokascript/vite-plugin';
339
+ *
340
+ * export default {
341
+ * plugins: [hyperfixi()]
342
+ * };
343
+ * ```
344
+ *
345
+ * The plugin automatically:
346
+ * - Scans HTML, Vue, Svelte, JSX/TSX files for `_="..."` attributes
347
+ * - Detects which commands, blocks, and expressions are used
348
+ * - Generates a minimal bundle with only needed features
349
+ * - Re-generates on file changes (HMR)
350
+ *
351
+ * @example
352
+ * ```javascript
353
+ * // With options
354
+ * hyperfixi({
355
+ * extraCommands: ['fetch', 'put'], // Always include these commands
356
+ * htmx: true, // Enable htmx integration
357
+ * debug: true, // Enable verbose logging
358
+ * })
359
+ * ```
360
+ *
361
+ * @example
362
+ * ```javascript
363
+ * // Compile mode for smallest bundles (~500 bytes)
364
+ * hyperfixi({
365
+ * mode: 'compile', // Pre-compile hyperscript to JS
366
+ * debug: true,
367
+ * })
368
+ * ```
369
+ */
370
+
371
+ /**
372
+ * Create the HyperFixi Vite plugin
373
+ *
374
+ * @param options Plugin options
375
+ * @returns Vite plugin
376
+ */
377
+ declare function hyperfixi(options?: HyperfixiPluginOptions): Plugin;
378
+
379
+ export { type AggregatedUsage, type CompileOptions, type CompiledHandler, type CustomLanguageKeywords, type FileUsage, type HyperfixiPluginOptions, REGIONS, SUPPORTED_LANGUAGES, type SupportedLanguage, clearCustomKeywords, clearSemanticParser, hyperfixi as default, detectLanguages, getAllLanguageCodes, getKeywordsForLanguage, getOptimalRegion, hasSemanticParser, hyperfixi, isNonLatinLanguage, registerCustomKeywords, setSemanticParser };