binja 0.1.0 → 0.1.1
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 +149 -102
- package/dist/compiler/index.d.ts +32 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/filters/index.d.ts.map +1 -1
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +969 -310
- package/dist/runtime/context.d.ts +4 -0
- package/dist/runtime/context.d.ts.map +1 -1
- package/dist/runtime/index.d.ts +34 -22
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/tests/index.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
| Feature | binja | Other JS engines |
|
|
27
27
|
|---------|-----------|------------------|
|
|
28
|
+
| **AOT Compilation** | ✅ 160x faster | ❌ |
|
|
28
29
|
| Django DTL Compatible | ✅ 100% | ❌ Partial |
|
|
29
30
|
| Jinja2 Compatible | ✅ Full | ⚠️ Limited |
|
|
30
31
|
| Template Inheritance | ✅ | ⚠️ |
|
|
@@ -37,107 +38,32 @@
|
|
|
37
38
|
|
|
38
39
|
## Benchmarks
|
|
39
40
|
|
|
40
|
-
Tested on
|
|
41
|
+
Tested on Mac Studio M1 Max, Bun 1.3.5, 10,000 iterations.
|
|
41
42
|
|
|
42
|
-
###
|
|
43
|
-
```
|
|
44
|
-
{{ name }} - {{ title|upper }}
|
|
45
|
-
```
|
|
43
|
+
### Two Rendering Modes
|
|
46
44
|
|
|
47
|
-
|
|
|
48
|
-
|
|
49
|
-
| **
|
|
50
|
-
|
|
|
51
|
-
| EJS | 38,461 | 3.7x slower |
|
|
52
|
-
| Handlebars | 52,631 | 2.7x slower |
|
|
45
|
+
| Mode | Function | Best For | vs Nunjucks |
|
|
46
|
+
|------|----------|----------|-------------|
|
|
47
|
+
| **Runtime** | `render()` | Development | **3.7x faster** |
|
|
48
|
+
| **AOT** | `compile()` | Production | **160x faster** |
|
|
53
49
|
|
|
54
|
-
###
|
|
55
|
-
```django
|
|
56
|
-
{% for item in items %}
|
|
57
|
-
{% if item.active %}
|
|
58
|
-
{{ item.name|title }} - ${{ item.price|floatformat:2 }}
|
|
59
|
-
{% endif %}
|
|
60
|
-
{% endfor %}
|
|
61
|
-
```
|
|
50
|
+
### Performance Comparison
|
|
62
51
|
|
|
63
|
-
|
|
|
64
|
-
|
|
65
|
-
|
|
|
66
|
-
|
|
|
67
|
-
|
|
|
68
|
-
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
```django
|
|
72
|
-
{% extends "base.html" %}
|
|
73
|
-
{% block content %}...{% endblock %}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
| Engine | Ops/sec | Relative |
|
|
77
|
-
|--------|---------|----------|
|
|
78
|
-
| **binja** | **18,518** | **1.0x** |
|
|
79
|
-
| Nunjucks | 6,250 | 3.0x slower |
|
|
80
|
-
| EJS | N/A | Not supported |
|
|
81
|
-
| Handlebars | N/A | Not supported |
|
|
82
|
-
|
|
83
|
-
### Memory Usage
|
|
84
|
-
|
|
85
|
-
| Engine | Heap (MB) | RSS (MB) |
|
|
86
|
-
|--------|-----------|----------|
|
|
87
|
-
| **binja** | **12.4** | **45.2** |
|
|
88
|
-
| Nunjucks | 28.6 | 89.4 |
|
|
89
|
-
| EJS | 18.2 | 62.1 |
|
|
52
|
+
| Benchmark | Nunjucks | binja Runtime | binja AOT |
|
|
53
|
+
|-----------|----------|---------------|-----------|
|
|
54
|
+
| Simple Template | 95K ops/s | 290K ops/s | **14.3M ops/s** |
|
|
55
|
+
| Complex Template | 28K ops/s | 103K ops/s | **1.07M ops/s** |
|
|
56
|
+
| Nested Loops | 27K ops/s | 130K ops/s | **1.75M ops/s** |
|
|
57
|
+
| HTML Escaping | 65K ops/s | 241K ops/s | **2.23M ops/s** |
|
|
58
|
+
| Conditionals | 27K ops/s | 126K ops/s | **22.8M ops/s** |
|
|
59
|
+
| Large Dataset (100 items) | 21K ops/s | 36K ops/s | **202K ops/s** |
|
|
90
60
|
|
|
91
61
|
### Run Benchmarks
|
|
92
62
|
|
|
93
63
|
```bash
|
|
94
|
-
bun run benchmark
|
|
64
|
+
bun run full-benchmark.ts
|
|
95
65
|
```
|
|
96
66
|
|
|
97
|
-
<details>
|
|
98
|
-
<summary>📊 Full Benchmark Code</summary>
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
import { Environment } from 'binja'
|
|
102
|
-
|
|
103
|
-
const env = new Environment()
|
|
104
|
-
const iterations = 1000
|
|
105
|
-
|
|
106
|
-
// Simple benchmark
|
|
107
|
-
const simpleTemplate = '{{ name }} - {{ title|upper }}'
|
|
108
|
-
const simpleContext = { name: 'John', title: 'hello world' }
|
|
109
|
-
|
|
110
|
-
console.time('Simple Template')
|
|
111
|
-
for (let i = 0; i < iterations; i++) {
|
|
112
|
-
await env.renderString(simpleTemplate, simpleContext)
|
|
113
|
-
}
|
|
114
|
-
console.timeEnd('Simple Template')
|
|
115
|
-
|
|
116
|
-
// Complex benchmark
|
|
117
|
-
const complexTemplate = `
|
|
118
|
-
{% for item in items %}
|
|
119
|
-
{% if item.active %}
|
|
120
|
-
{{ item.name|title }} - ${{ item.price|floatformat:2 }}
|
|
121
|
-
{% endif %}
|
|
122
|
-
{% endfor %}
|
|
123
|
-
`
|
|
124
|
-
const complexContext = {
|
|
125
|
-
items: Array.from({ length: 50 }, (_, i) => ({
|
|
126
|
-
name: `product ${i}`,
|
|
127
|
-
price: Math.random() * 100,
|
|
128
|
-
active: Math.random() > 0.3
|
|
129
|
-
}))
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
console.time('Complex Template')
|
|
133
|
-
for (let i = 0; i < iterations; i++) {
|
|
134
|
-
await env.renderString(complexTemplate, complexContext)
|
|
135
|
-
}
|
|
136
|
-
console.timeEnd('Complex Template')
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
</details>
|
|
140
|
-
|
|
141
67
|
---
|
|
142
68
|
|
|
143
69
|
## Installation
|
|
@@ -181,6 +107,37 @@ const html = await env.render('pages/home.html', {
|
|
|
181
107
|
})
|
|
182
108
|
```
|
|
183
109
|
|
|
110
|
+
### AOT Compilation (Maximum Performance)
|
|
111
|
+
|
|
112
|
+
For production, use `compile()` for **160x faster** rendering:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { compile } from 'binja'
|
|
116
|
+
|
|
117
|
+
// Compile once at startup
|
|
118
|
+
const renderUser = compile('<h1>{{ name|upper }}</h1>')
|
|
119
|
+
|
|
120
|
+
// Use many times (sync, extremely fast!)
|
|
121
|
+
const html = renderUser({ name: 'john' })
|
|
122
|
+
// Output: <h1>JOHN</h1>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Production example:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { compile } from 'binja'
|
|
129
|
+
|
|
130
|
+
// Pre-compile all templates at server startup
|
|
131
|
+
const templates = {
|
|
132
|
+
home: compile(await Bun.file('./views/home.html').text()),
|
|
133
|
+
user: compile(await Bun.file('./views/user.html').text()),
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Rendering is now synchronous and extremely fast
|
|
137
|
+
app.get('/', () => templates.home({ title: 'Welcome' }))
|
|
138
|
+
app.get('/user/:id', ({ params }) => templates.user({ id: params.id }))
|
|
139
|
+
```
|
|
140
|
+
|
|
184
141
|
---
|
|
185
142
|
|
|
186
143
|
## Features
|
|
@@ -351,6 +308,43 @@ const html = await env.render('pages/home.html', {
|
|
|
351
308
|
|
|
352
309
|
---
|
|
353
310
|
|
|
311
|
+
## Tests (is operator)
|
|
312
|
+
|
|
313
|
+
Tests check values using the `is` operator (Jinja2 syntax):
|
|
314
|
+
|
|
315
|
+
```django
|
|
316
|
+
{% if value is defined %}...{% endif %}
|
|
317
|
+
{% if num is even %}...{% endif %}
|
|
318
|
+
{% if num is divisibleby(3) %}...{% endif %}
|
|
319
|
+
{% if items is empty %}...{% endif %}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Built-in Tests
|
|
323
|
+
|
|
324
|
+
| Test | Description |
|
|
325
|
+
|------|-------------|
|
|
326
|
+
| `divisibleby(n)` | Divisible by n |
|
|
327
|
+
| `even` / `odd` | Even/odd integer |
|
|
328
|
+
| `number` / `integer` / `float` | Type checks |
|
|
329
|
+
| `defined` / `undefined` | Variable exists |
|
|
330
|
+
| `none` | Is null |
|
|
331
|
+
| `empty` | Empty array/string/object |
|
|
332
|
+
| `truthy` / `falsy` | Truthiness checks |
|
|
333
|
+
| `string` / `mapping` / `iterable` | Type checks |
|
|
334
|
+
| `gt(n)` / `lt(n)` / `ge(n)` / `le(n)` | Comparisons |
|
|
335
|
+
| `eq(v)` / `ne(v)` / `sameas(v)` | Equality |
|
|
336
|
+
| `upper` / `lower` | String case checks |
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
import { builtinTests } from 'binja'
|
|
340
|
+
|
|
341
|
+
// All 30+ built-in tests
|
|
342
|
+
console.log(Object.keys(builtinTests))
|
|
343
|
+
// ['divisibleby', 'even', 'odd', 'number', 'integer', ...]
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
354
348
|
## Django Compatibility
|
|
355
349
|
|
|
356
350
|
binja is designed to be a drop-in replacement for Django templates:
|
|
@@ -459,31 +453,84 @@ await render('{{ script }}', {
|
|
|
459
453
|
|
|
460
454
|
## Performance Tips
|
|
461
455
|
|
|
462
|
-
1. **
|
|
463
|
-
2. **
|
|
464
|
-
3. **
|
|
456
|
+
1. **Use AOT in Production** - `compile()` is 160x faster than Nunjucks
|
|
457
|
+
2. **Pre-compile at Startup** - Compile templates once, use many times
|
|
458
|
+
3. **Reuse Environment** - For templates with `{% extends %}`, create once
|
|
459
|
+
4. **Enable caching** - Templates are cached automatically
|
|
460
|
+
|
|
461
|
+
```typescript
|
|
462
|
+
import { compile } from 'binja'
|
|
463
|
+
|
|
464
|
+
// Best: AOT compilation for static templates
|
|
465
|
+
const templates = {
|
|
466
|
+
home: compile(await Bun.file('./views/home.html').text()),
|
|
467
|
+
user: compile(await Bun.file('./views/user.html').text()),
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Sync rendering, extremely fast
|
|
471
|
+
app.get('/', () => templates.home({ title: 'Home' }))
|
|
472
|
+
app.get('/user/:id', () => templates.user({ id: params.id }))
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
For templates with inheritance (`{% extends %}`):
|
|
465
476
|
|
|
466
477
|
```typescript
|
|
467
|
-
|
|
468
|
-
|
|
478
|
+
import { Environment } from 'binja'
|
|
479
|
+
|
|
480
|
+
// Environment with cache for inherited templates
|
|
481
|
+
const env = new Environment({ templates: './views', cache: true })
|
|
469
482
|
|
|
470
|
-
//
|
|
471
|
-
|
|
472
|
-
|
|
483
|
+
// Pre-warm cache at startup
|
|
484
|
+
await env.loadTemplate('base.html')
|
|
485
|
+
await env.loadTemplate('home.html')
|
|
473
486
|
```
|
|
474
487
|
|
|
475
488
|
---
|
|
476
489
|
|
|
477
490
|
## API Reference
|
|
478
491
|
|
|
479
|
-
### `render(template, context)`
|
|
492
|
+
### `render(template, context)` - Runtime Mode
|
|
480
493
|
|
|
481
|
-
Render a template string with context.
|
|
494
|
+
Render a template string with context (async, easy development).
|
|
482
495
|
|
|
483
496
|
```typescript
|
|
497
|
+
import { render } from 'binja'
|
|
498
|
+
|
|
484
499
|
const html = await render('Hello {{ name }}', { name: 'World' })
|
|
485
500
|
```
|
|
486
501
|
|
|
502
|
+
### `compile(template, options?)` - AOT Mode
|
|
503
|
+
|
|
504
|
+
Compile a template to an optimized function (sync, **160x faster**).
|
|
505
|
+
|
|
506
|
+
```typescript
|
|
507
|
+
import { compile } from 'binja'
|
|
508
|
+
|
|
509
|
+
// Compile once
|
|
510
|
+
const renderGreeting = compile('<h1>{{ name|upper }}</h1>')
|
|
511
|
+
|
|
512
|
+
// Use many times (sync!)
|
|
513
|
+
const html = renderGreeting({ name: 'world' }) // <h1>WORLD</h1>
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
**Supported:** Variables, filters, conditions, loops, set/with, comments.
|
|
517
|
+
**Not supported:** `{% extends %}`, `{% include %}` (use Environment for these).
|
|
518
|
+
|
|
519
|
+
### `compileToCode(template, options?)`
|
|
520
|
+
|
|
521
|
+
Generate JavaScript code string for build tools.
|
|
522
|
+
|
|
523
|
+
```typescript
|
|
524
|
+
import { compileToCode } from 'binja'
|
|
525
|
+
|
|
526
|
+
const code = compileToCode('<h1>{{ title }}</h1>', {
|
|
527
|
+
functionName: 'renderHeader'
|
|
528
|
+
})
|
|
529
|
+
|
|
530
|
+
// Save to file for bundling
|
|
531
|
+
await Bun.write('./compiled/header.js', code)
|
|
532
|
+
```
|
|
533
|
+
|
|
487
534
|
### `Environment`
|
|
488
535
|
|
|
489
536
|
Create a configured template environment.
|
|
@@ -496,6 +543,7 @@ env.render(name, context) // Render template file
|
|
|
496
543
|
env.renderString(str, context) // Render template string
|
|
497
544
|
env.addFilter(name, fn) // Add custom filter
|
|
498
545
|
env.addGlobal(name, value) // Add global variable
|
|
546
|
+
env.loadTemplate(name) // Pre-load template (for cache warming)
|
|
499
547
|
```
|
|
500
548
|
|
|
501
549
|
---
|
|
@@ -725,4 +773,3 @@ See [LICENSE](./LICENSE) for details.
|
|
|
725
773
|
<p align="center">
|
|
726
774
|
Made with ❤️ for the Bun ecosystem
|
|
727
775
|
</p>
|
|
728
|
-
# binja
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AOT Compiler for binja templates
|
|
3
|
+
* Generates optimized JavaScript functions from AST
|
|
4
|
+
*/
|
|
5
|
+
import type { TemplateNode } from '../parser/nodes';
|
|
6
|
+
export interface CompileOptions {
|
|
7
|
+
/** Function name (default: 'render') */
|
|
8
|
+
functionName?: string;
|
|
9
|
+
/** Include runtime helpers inline (default: true) */
|
|
10
|
+
inlineHelpers?: boolean;
|
|
11
|
+
/** Minify output (default: false) */
|
|
12
|
+
minify?: boolean;
|
|
13
|
+
/** Auto-escape HTML (default: true) */
|
|
14
|
+
autoescape?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Compile AST to JavaScript code string
|
|
18
|
+
*/
|
|
19
|
+
export declare function compileToString(ast: TemplateNode, options?: CompileOptions): string;
|
|
20
|
+
/**
|
|
21
|
+
* Compile AST to executable function
|
|
22
|
+
*/
|
|
23
|
+
export declare function compileToFunction(ast: TemplateNode, options?: CompileOptions): (ctx: Record<string, any>) => string;
|
|
24
|
+
declare const runtimeHelpers: {
|
|
25
|
+
escape: (value: any) => string;
|
|
26
|
+
isTruthy: (value: any) => boolean;
|
|
27
|
+
toArray: (value: any) => any[];
|
|
28
|
+
applyFilter: (name: string, value: any, ...args: any[]) => any;
|
|
29
|
+
applyTest: (name: string, value: any, ...args: any[]) => boolean;
|
|
30
|
+
};
|
|
31
|
+
export { runtimeHelpers };
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compiler/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAEV,YAAY,EAqBb,MAAM,iBAAiB,CAAA;AAIxB,MAAM,WAAW,cAAc;IAC7B,wCAAwC;IACxC,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,qDAAqD;IACrD,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,qCAAqC;IACrC,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,uCAAuC;IACvC,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAE,cAAmB,GAAG,MAAM,CAGvF;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAE,cAAmB,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,MAAM,CAUvH;AAGD,QAAA,MAAM,cAAc;oBACF,GAAG,KAAG,MAAM;sBAQV,GAAG,KAAG,OAAO;qBAad,GAAG,KAAG,GAAG,EAAE;wBAWR,MAAM,SAAS,GAAG,WAAW,GAAG,EAAE,KAAG,GAAG;sBAM1C,MAAM,SAAS,GAAG,WAAW,GAAG,EAAE,KAAG,OAAO;CAK/D,CAAA;AAyaD,OAAO,EAAE,cAAc,EAAE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAA;AAuBhE,eAAO,MAAM,KAAK,EAAE,cAAuD,CAAA;AAE3E,eAAO,MAAM,KAAK,EAAE,cAAuD,CAAA;AAE3E,eAAO,MAAM,UAAU,EAAE,cAGxB,CAAA;AAED,eAAO,MAAM,QAAQ,EAAE,cAGtB,CAAA;AAED,eAAO,MAAM,KAAK,EAAE,cACwC,CAAA;AAE5D,eAAO,MAAM,IAAI,EAAE,cAAgD,CAAA;AAEnE,eAAO,MAAM,SAAS,EAAE,cACoB,CAAA;AAE5C,eAAO,MAAM,MAAM,EAAE,cAWpB,CAAA;AAED,eAAO,MAAM,IAAI,EAAE,cAKlB,CAAA;AAED,eAAO,MAAM,QAAQ,EAAE,cACqB,CAAA;AAE5C,eAAO,MAAM,UAAU,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AAgB,EAAE,cAG9B,CAAA;AAGD,eAAO,MAAM,OAAO,EAAE,cAUrB,CAAA;AAoCD,eAAO,MAAM,IAAI,EAAE,cAKlB,CAAA;AAED,eAAO,MAAM,IAAI,EAAE,cAA+D,CAAA;AAElF,eAAO,MAAM,SAAS,EAAE,cAqBvB,CAAA;AAED,eAAO,MAAM,SAAS,EAAE,cAqBvB,CAAA;AAKD,QAAA,MAAM,aAAa,EAAE,cAKpB,CAAA;AACD,OAAO,EAAE,aAAa,IAAI,OAAO,EAAE,CAAA;AAGnC,eAAO,MAAM,eAAe,EAAE,cACgC,CAAA;AAG9D,eAAO,MAAM,KAAK,EAAE,cAKnB,CAAA;AAGD,eAAO,MAAM,SAAS,EAAE,cAIvB,CAAA;AAID,eAAO,MAAM,SAAS,EAAE,cAA6D,CAAA;AAErF,eAAO,MAAM,MAAM,EAAE,cAMpB,CAAA;AAID,eAAO,MAAM,IAAI,EAAE,cAUlB,CAAA;AAKD,eAAO,MAAM,MAAM,EAAE,cAKpB,CAAA;AAGD,eAAO,MAAM,KAAK,EAAE,cAcnB,CAAA;AAGD,eAAO,MAAM,OAAO,EAAE,cAiBrB,CAAA;AAID,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CA0EzD,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
*/
|
|
22
22
|
import { TemplateNode } from './parser';
|
|
23
23
|
import { FilterFunction } from './filters';
|
|
24
|
+
import { CompileOptions } from './compiler';
|
|
24
25
|
export interface EnvironmentOptions {
|
|
25
26
|
/** Template directory path */
|
|
26
27
|
templates?: string;
|
|
@@ -95,6 +96,28 @@ export declare function render(source: string, context?: Record<string, any>, op
|
|
|
95
96
|
export declare function Template(source: string, options?: EnvironmentOptions): {
|
|
96
97
|
render(context?: Record<string, any>): Promise<string>;
|
|
97
98
|
};
|
|
99
|
+
/**
|
|
100
|
+
* Compile a template to an optimized JavaScript function (AOT mode)
|
|
101
|
+
* Returns a sync function that is 10-50x faster than runtime rendering
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const renderUser = compile('<h1>{{ name|upper }}</h1>')
|
|
106
|
+
* const html = renderUser({ name: 'world' }) // <h1>WORLD</h1>
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function compile(source: string, options?: CompileOptions): (ctx: Record<string, any>) => string;
|
|
110
|
+
/**
|
|
111
|
+
* Compile a template to JavaScript code string (for build tools/CLI)
|
|
112
|
+
* The generated code can be saved to a file and imported directly
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const code = compileToCode('<h1>{{ name }}</h1>', { functionName: 'renderHeader' })
|
|
117
|
+
* // Returns: "function renderHeader(__ctx) { ... }"
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare function compileToCode(source: string, options?: CompileOptions): string;
|
|
98
121
|
export { Lexer, TokenType } from './lexer';
|
|
99
122
|
export type { Token } from './lexer';
|
|
100
123
|
export { Parser } from './parser';
|
|
@@ -102,4 +125,7 @@ export type { TemplateNode, ASTNode, ExpressionNode } from './parser/nodes';
|
|
|
102
125
|
export { Runtime, Context } from './runtime';
|
|
103
126
|
export { builtinFilters } from './filters';
|
|
104
127
|
export type { FilterFunction } from './filters';
|
|
128
|
+
export type { CompileOptions } from './compiler';
|
|
129
|
+
export { builtinTests } from './tests';
|
|
130
|
+
export type { TestFunction } from './tests';
|
|
105
131
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EAAU,YAAY,EAAE,MAAM,UAAU,CAAA;AAE/C,OAAO,EAAkB,cAAc,EAAE,MAAM,WAAW,CA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