@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.
- package/README.md +345 -0
- package/dist/index.cjs +3272 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +379 -0
- package/dist/index.d.ts +379 -0
- package/dist/index.js +3228 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -0
package/README.md
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
# @lokascript/vite-plugin
|
|
2
|
+
|
|
3
|
+
Zero-config Vite plugin that automatically generates minimal LokaScript bundles based on detected hyperscript usage.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Zero-config**: Just add the plugin and it works
|
|
8
|
+
- **Automatic detection**: Scans HTML, Vue, Svelte, JSX/TSX for `_="..."` attributes
|
|
9
|
+
- **Minimal bundles**: Only includes commands and blocks you actually use
|
|
10
|
+
- **HMR support**: Re-generates bundle when you add new hyperscript
|
|
11
|
+
- **Framework agnostic**: Works with any Vite-based project
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @lokascript/vite-plugin @lokascript/core
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
// vite.config.js
|
|
23
|
+
import { lokascript } from '@lokascript/vite-plugin';
|
|
24
|
+
|
|
25
|
+
export default {
|
|
26
|
+
plugins: [lokascript()],
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
// main.js
|
|
32
|
+
import 'lokascript'; // Auto-generated minimal bundle
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<!-- Just write hyperscript - plugin detects what you use -->
|
|
37
|
+
<button _="on click toggle .active">Toggle</button>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## How It Works
|
|
41
|
+
|
|
42
|
+
1. **Scans** your HTML/Vue/Svelte/JSX files for `_="..."` attributes
|
|
43
|
+
2. **Detects** which commands, blocks, and expressions you use
|
|
44
|
+
3. **Generates** a minimal bundle with only the features you need
|
|
45
|
+
4. **Regenerates** on HMR when you add new hyperscript
|
|
46
|
+
|
|
47
|
+
## Compile Mode (Minimal Bundle Size)
|
|
48
|
+
|
|
49
|
+
When bundle size is the priority, use **compile mode** to pre-compile hyperscript to JavaScript at build time:
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
lokascript({
|
|
53
|
+
mode: 'compile', // ~500 bytes gzip vs ~8KB for interpret mode
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Trade-offs:**
|
|
58
|
+
|
|
59
|
+
| Feature | Interpret (default) | Compile |
|
|
60
|
+
| ------------------- | ------------------- | --------------- |
|
|
61
|
+
| Bundle size | ~8 KB gzip | ~500 bytes gzip |
|
|
62
|
+
| Dynamic `execute()` | ✓ | ✗ |
|
|
63
|
+
| Block commands | ✓ | ✗ |
|
|
64
|
+
| Build complexity | Lower | Higher |
|
|
65
|
+
|
|
66
|
+
**When to use compile mode:**
|
|
67
|
+
|
|
68
|
+
- Landing pages with simple interactions (toggles, shows, hides)
|
|
69
|
+
- Performance-critical apps where every KB matters
|
|
70
|
+
- Static sites where hyperscript is just for UI polish
|
|
71
|
+
|
|
72
|
+
**When NOT to use compile mode:**
|
|
73
|
+
|
|
74
|
+
- Apps using `if`, `repeat`, `fetch`, or `for each` blocks
|
|
75
|
+
- Dynamic hyperscript generation at runtime via `execute()`
|
|
76
|
+
- Apps that need the full hyperscript power
|
|
77
|
+
|
|
78
|
+
**Supported commands in compile mode:**
|
|
79
|
+
toggle, add, remove, show, hide, focus, blur, set, get, put, increment, decrement, log, send, trigger, wait
|
|
80
|
+
|
|
81
|
+
**Positional expressions:** next, previous, parent, first, last, closest
|
|
82
|
+
|
|
83
|
+
**Animations:** Use CSS transitions - toggling classes triggers them automatically. No special `transition` command needed.
|
|
84
|
+
|
|
85
|
+
## Multilingual & Semantic Parsing
|
|
86
|
+
|
|
87
|
+
Enable semantic parsing for natural language hyperscript and multilingual support across 13 languages.
|
|
88
|
+
|
|
89
|
+
### Multilingual Options
|
|
90
|
+
|
|
91
|
+
| Option | Type | Default | Description |
|
|
92
|
+
| ---------------- | -------------------------------------------------- | ------- | --------------------------------------- |
|
|
93
|
+
| `semantic` | `boolean \| 'en' \| 'auto'` | `false` | Enable semantic parser |
|
|
94
|
+
| `languages` | `string[]` | `[]` | Explicit language codes to support |
|
|
95
|
+
| `region` | `'western' \| 'east-asian' \| 'priority' \| 'all'` | auto | Force a regional bundle |
|
|
96
|
+
| `grammar` | `boolean` | `false` | Enable grammar transformation (SOV/VSO) |
|
|
97
|
+
| `extraLanguages` | `string[]` | `[]` | Languages to always include |
|
|
98
|
+
|
|
99
|
+
### Supported Languages (13)
|
|
100
|
+
|
|
101
|
+
- **Western:** en, es, pt, fr, de
|
|
102
|
+
- **East Asian:** ja, zh, ko
|
|
103
|
+
- **Other:** ar, tr, id, sw, qu
|
|
104
|
+
|
|
105
|
+
### Usage Examples
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
// Auto-detect languages from source files
|
|
109
|
+
lokascript({ semantic: 'auto' });
|
|
110
|
+
|
|
111
|
+
// English synonyms only (smallest semantic bundle)
|
|
112
|
+
lokascript({ semantic: 'en' });
|
|
113
|
+
|
|
114
|
+
// Explicit languages (selects optimal regional bundle)
|
|
115
|
+
lokascript({ languages: ['en', 'es', 'ja'] });
|
|
116
|
+
|
|
117
|
+
// Force a specific regional bundle
|
|
118
|
+
lokascript({ region: 'western' });
|
|
119
|
+
|
|
120
|
+
// Full multilingual with grammar transformation
|
|
121
|
+
lokascript({ semantic: true, grammar: true });
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Language Auto-Detection
|
|
125
|
+
|
|
126
|
+
When `semantic: 'auto'` is set, the plugin scans your hyperscript for non-English keywords:
|
|
127
|
+
|
|
128
|
+
```html
|
|
129
|
+
<!-- Detected as Japanese -->
|
|
130
|
+
<button _="on click トグル .active">Toggle</button>
|
|
131
|
+
|
|
132
|
+
<!-- Detected as Spanish -->
|
|
133
|
+
<button _="on click alternar .active">Alternar</button>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The plugin automatically selects the smallest bundle that covers all detected languages.
|
|
137
|
+
|
|
138
|
+
### Semantic Bundle Sizes
|
|
139
|
+
|
|
140
|
+
| Bundle | Gzip Size | Languages |
|
|
141
|
+
| ---------------------- | --------- | --------------------- |
|
|
142
|
+
| `semantic: 'en'` | ~20 KB | English only |
|
|
143
|
+
| `semantic: 'es'` | ~16 KB | Spanish only |
|
|
144
|
+
| `region: 'es-en'` | ~25 KB | English + Spanish |
|
|
145
|
+
| `region: 'western'` | ~30 KB | en, es, pt, fr, de |
|
|
146
|
+
| `region: 'east-asian'` | ~24 KB | ja, zh, ko |
|
|
147
|
+
| `region: 'priority'` | ~48 KB | 11 priority languages |
|
|
148
|
+
| `region: 'all'` | ~61 KB | All 13 languages |
|
|
149
|
+
|
|
150
|
+
### Tiered Bundle Architecture
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
Level 0: Base HybridParser (default) 4-9 KB gzip
|
|
154
|
+
↓ semantic: true
|
|
155
|
+
Level 1: + Semantic English +20 KB
|
|
156
|
+
↓ languages detected/specified
|
|
157
|
+
Level 2: + Regional Semantic Bundle +16-61 KB
|
|
158
|
+
↓ grammar: true
|
|
159
|
+
Level 3: + Grammar Transformation +68 KB
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Options
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
lokascript({
|
|
166
|
+
// Bundle mode: 'interpret' (default) or 'compile'
|
|
167
|
+
mode: 'interpret',
|
|
168
|
+
|
|
169
|
+
// Extra commands to always include (for dynamic hyperscript)
|
|
170
|
+
extraCommands: ['fetch', 'put'],
|
|
171
|
+
extraBlocks: ['for'],
|
|
172
|
+
|
|
173
|
+
// Always include positional expressions
|
|
174
|
+
positional: true,
|
|
175
|
+
|
|
176
|
+
// Enable htmx attribute compatibility
|
|
177
|
+
htmx: true,
|
|
178
|
+
|
|
179
|
+
// Debug logging
|
|
180
|
+
debug: true,
|
|
181
|
+
|
|
182
|
+
// File patterns
|
|
183
|
+
include: /\.(html|vue|svelte|jsx|tsx)$/,
|
|
184
|
+
exclude: /node_modules/,
|
|
185
|
+
|
|
186
|
+
// Custom bundle name (shown in generated code comments)
|
|
187
|
+
bundleName: 'MyApp',
|
|
188
|
+
|
|
189
|
+
// Global variable name (default: 'lokascript')
|
|
190
|
+
globalName: 'lokascript',
|
|
191
|
+
|
|
192
|
+
// Multilingual options (see "Multilingual & Semantic Parsing" section)
|
|
193
|
+
semantic: false, // boolean | 'en' | 'auto'
|
|
194
|
+
languages: [], // ['en', 'es', 'ja']
|
|
195
|
+
region: undefined, // 'western' | 'east-asian' | 'priority' | 'all'
|
|
196
|
+
grammar: false, // Enable grammar transformation
|
|
197
|
+
extraLanguages: [], // Languages to always include
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Detected Features
|
|
202
|
+
|
|
203
|
+
### Commands (21)
|
|
204
|
+
|
|
205
|
+
toggle, add, remove, removeClass, show, hide, set, get, put, append, take,
|
|
206
|
+
increment, decrement, log, send, trigger, wait, transition, go, call, focus, blur, return
|
|
207
|
+
|
|
208
|
+
### Blocks (5)
|
|
209
|
+
|
|
210
|
+
if, repeat, for, while, fetch
|
|
211
|
+
|
|
212
|
+
### Positional Expressions (6)
|
|
213
|
+
|
|
214
|
+
first, last, next, previous, closest, parent
|
|
215
|
+
|
|
216
|
+
## Virtual Module
|
|
217
|
+
|
|
218
|
+
The plugin creates a virtual module that you can import:
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
// All of these work:
|
|
222
|
+
import 'lokascript';
|
|
223
|
+
import 'virtual:lokascript';
|
|
224
|
+
import '@lokascript/core'; // Redirects to virtual module
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## HMR Support
|
|
228
|
+
|
|
229
|
+
When you add new hyperscript to your HTML files, the plugin:
|
|
230
|
+
|
|
231
|
+
1. Re-scans the changed file
|
|
232
|
+
2. Updates the aggregated usage
|
|
233
|
+
3. Triggers a page reload if new commands are detected
|
|
234
|
+
|
|
235
|
+
## Bundle Size Comparison
|
|
236
|
+
|
|
237
|
+
| Usage | Generated Size (gzip) | vs Full Bundle |
|
|
238
|
+
| ------------------- | --------------------- | -------------- |
|
|
239
|
+
| 3 commands | ~5 KB | 90% smaller |
|
|
240
|
+
| 6 commands | ~6.5 KB | 85% smaller |
|
|
241
|
+
| 9 commands + blocks | ~8 KB | 80% smaller |
|
|
242
|
+
| All features | ~40 KB | same |
|
|
243
|
+
|
|
244
|
+
## Edge Cases
|
|
245
|
+
|
|
246
|
+
For dynamically generated hyperscript that can't be detected:
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
// This can't be detected by static analysis
|
|
250
|
+
element.setAttribute('_', `on click ${dynamicCommand} .active`);
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Use `extraCommands` to include those:
|
|
254
|
+
|
|
255
|
+
```javascript
|
|
256
|
+
lokascript({
|
|
257
|
+
extraCommands: ['toggle', 'add', 'remove'],
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Supported File Types
|
|
262
|
+
|
|
263
|
+
- `.html`, `.htm`
|
|
264
|
+
- `.vue` (single file components)
|
|
265
|
+
- `.svelte`
|
|
266
|
+
- `.jsx`, `.tsx`
|
|
267
|
+
- `.astro`
|
|
268
|
+
- `.php`, `.erb`, `.ejs`, `.hbs`
|
|
269
|
+
|
|
270
|
+
## Monorepo Development
|
|
271
|
+
|
|
272
|
+
When developing in the lokascript monorepo:
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
// vite.config.js
|
|
276
|
+
import { lokascript } from '../../packages/vite-plugin/src/index.ts';
|
|
277
|
+
import path from 'path';
|
|
278
|
+
|
|
279
|
+
export default {
|
|
280
|
+
plugins: [lokascript({ debug: true })],
|
|
281
|
+
resolve: {
|
|
282
|
+
alias: {
|
|
283
|
+
'@lokascript/core/parser/hybrid': path.resolve(
|
|
284
|
+
__dirname,
|
|
285
|
+
'../../packages/core/src/parser/hybrid'
|
|
286
|
+
),
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Examples
|
|
293
|
+
|
|
294
|
+
### Basic Example
|
|
295
|
+
|
|
296
|
+
See the [vite-plugin-test](../../examples/vite-plugin-test/) example for a basic demo:
|
|
297
|
+
|
|
298
|
+
```bash
|
|
299
|
+
cd examples/vite-plugin-test
|
|
300
|
+
npm install
|
|
301
|
+
npm run dev
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Multilingual Example
|
|
305
|
+
|
|
306
|
+
See the [vite-plugin-multilingual](../../examples/vite-plugin-multilingual/) example for multilingual features:
|
|
307
|
+
|
|
308
|
+
```bash
|
|
309
|
+
cd examples/vite-plugin-multilingual
|
|
310
|
+
npm install
|
|
311
|
+
npm run dev
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
This example demonstrates language auto-detection with Japanese, Spanish, and Korean keywords.
|
|
315
|
+
|
|
316
|
+
## API Reference
|
|
317
|
+
|
|
318
|
+
### Plugin Options
|
|
319
|
+
|
|
320
|
+
| Option | Type | Default | Description |
|
|
321
|
+
| ---------------- | --------------------------- | --------------------- | ------------------------------------- |
|
|
322
|
+
| `debug` | `boolean` | `false` | Enable debug logging |
|
|
323
|
+
| `include` | `RegExp \| string[]` | See below | File patterns to scan |
|
|
324
|
+
| `exclude` | `RegExp \| string[]` | `/node_modules/` | File patterns to exclude |
|
|
325
|
+
| `extraCommands` | `string[]` | `[]` | Commands to always include |
|
|
326
|
+
| `extraBlocks` | `string[]` | `[]` | Blocks to always include |
|
|
327
|
+
| `positional` | `boolean` | `false` | Always include positional expressions |
|
|
328
|
+
| `htmx` | `boolean` | `false` | Enable htmx integration |
|
|
329
|
+
| `bundleName` | `string` | `'ViteAutoGenerated'` | Bundle name in comments |
|
|
330
|
+
| `globalName` | `string` | `'lokascript'` | Window global name |
|
|
331
|
+
| `semantic` | `boolean \| 'en' \| 'auto'` | `false` | Enable semantic parser |
|
|
332
|
+
| `languages` | `string[]` | `[]` | Explicit language codes |
|
|
333
|
+
| `region` | `string` | auto | Force regional bundle |
|
|
334
|
+
| `grammar` | `boolean` | `false` | Enable grammar transformation |
|
|
335
|
+
| `extraLanguages` | `string[]` | `[]` | Languages to always include |
|
|
336
|
+
|
|
337
|
+
### Default Include Pattern
|
|
338
|
+
|
|
339
|
+
```regex
|
|
340
|
+
/\.(html?|vue|svelte|jsx?|tsx?|astro|php|erb|ejs|hbs|handlebars)$/
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## License
|
|
344
|
+
|
|
345
|
+
MIT
|