ecto 4.2.4 → 4.4.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 +79 -0
- package/dist/ecto.d.ts +48 -3
- package/dist/ecto.js +117 -22
- package/package.json +11 -9
package/README.md
CHANGED
|
@@ -39,6 +39,8 @@ Ecto is a modern template consolidation engine that enables the best template en
|
|
|
39
39
|
* [Handlebars](#handlebars)
|
|
40
40
|
* [Liquid](#liquid)
|
|
41
41
|
* [FrontMatter Helper Functions](#frontmatter-helper-functions)
|
|
42
|
+
* [Caching on Rendering](#caching-on-rendering)
|
|
43
|
+
* [Emitting Events](#emitting-events)
|
|
42
44
|
* [How to Contribute](#how-to-contribute)
|
|
43
45
|
* [License](#license)
|
|
44
46
|
|
|
@@ -563,6 +565,83 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front
|
|
|
563
565
|
* `setFrontMatter(source:string, data: Record<string, unknown>)` - This function sets the front matter even if it already exists and returns the full source with the new front matter.
|
|
564
566
|
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
|
|
565
567
|
|
|
568
|
+
# Caching on Rendering
|
|
569
|
+
|
|
570
|
+
Ecto has a built-in caching mechanism that is `disabled by default` that allows you to cache the rendered output of templates. This is useful for improving performance and reducing the number of times a template needs to be rendered. There are currently two caching engines available: `MemoryCache` and `FileCache`.
|
|
571
|
+
* `cache` - This is the default caching engine and it uses in-memory by default but is called `async` so you can use a storage layer such as Redis or PostgreSQL. This is done using `Cacheable` which is a high performance caching library.
|
|
572
|
+
* `cacheSync` - This is the synchronous version of the `cache` engine and uses `CacheableMemory`. It is useful for when you need to render a template synchronously and cache the output.
|
|
573
|
+
|
|
574
|
+
Here is an example of how to use the caching engine with `render`:
|
|
575
|
+
|
|
576
|
+
```javascript
|
|
577
|
+
import { Ecto } from 'ecto';
|
|
578
|
+
|
|
579
|
+
const ecto = new Ecto({ cache: true });
|
|
580
|
+
const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
|
|
581
|
+
const data = { firstName: "John", lastName: "Doe" };
|
|
582
|
+
const result = await ecto.render(source, data);
|
|
583
|
+
console.log(result); // <h1>Hello John Doe!</h1>
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
You can also use the `cacheSync` engine with `renderSync`:
|
|
587
|
+
|
|
588
|
+
```javascript
|
|
589
|
+
import { Ecto } from 'ecto';
|
|
590
|
+
const ecto = new Ecto({ cacheSync: true });
|
|
591
|
+
const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
|
|
592
|
+
const data = { firstName: "John", lastName: "Doe" };
|
|
593
|
+
const result = ecto.renderSync(source, data);
|
|
594
|
+
console.log(result); // <h1>Hello John Doe!</h1>
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
If you want to provide your own caching configuration, you can do so by passing `Cacheable` instance to the `Ecto` constructor:
|
|
598
|
+
|
|
599
|
+
```javascript
|
|
600
|
+
import { Ecto } from 'ecto';
|
|
601
|
+
import { Cacheable } from 'cacheable';
|
|
602
|
+
const cacheable = new Cacheable({
|
|
603
|
+
// Your caching configuration here
|
|
604
|
+
});
|
|
605
|
+
const ecto = new Ecto({ cache: cacheable });
|
|
606
|
+
const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
|
|
607
|
+
const data = { firstName: "John", lastName: "Doe" };
|
|
608
|
+
const result = await ecto.render(source, data);
|
|
609
|
+
console.log(result); // <h1>Hello John Doe!</h1>
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
# Emitting Events
|
|
613
|
+
|
|
614
|
+
Ecto has a built-in event emitter that allows you to listen for events that occur during the rendering process. This is useful for debugging and monitoring the rendering process. The following events are emitted:
|
|
615
|
+
|
|
616
|
+
- `cacheHit` - Emitted when a cached result is found and returned.
|
|
617
|
+
- `cacheMiss` - Emitted when a cached result is not found and the template needs to be rendered.
|
|
618
|
+
- `info` - Emitted for informational messages.
|
|
619
|
+
- `warn` - Emitted for warning messages.
|
|
620
|
+
- `error` - Emitted for error messages.
|
|
621
|
+
|
|
622
|
+
You can listen for these events by using the `on` method of the `Ecto` instance:
|
|
623
|
+
|
|
624
|
+
```javascript
|
|
625
|
+
import { Ecto } from 'ecto';
|
|
626
|
+
const ecto = new Ecto();
|
|
627
|
+
ecto.on('cacheHit', (data) => {
|
|
628
|
+
console.log('Cache hit:', data);
|
|
629
|
+
});
|
|
630
|
+
ecto.on('cacheMiss', (data) => {
|
|
631
|
+
console.log('Cache miss:', data);
|
|
632
|
+
});
|
|
633
|
+
ecto.on('warn', (data) => {
|
|
634
|
+
console.warn('Warning:', data);
|
|
635
|
+
});
|
|
636
|
+
ecto.on('error', (data) => {
|
|
637
|
+
console.error('Error:', data);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
|
|
641
|
+
const data = { firstName: "John", lastName: "Doe" };
|
|
642
|
+
await ecto.render(source, data); // <h1>Hello John Doe!</h1>
|
|
643
|
+
```
|
|
644
|
+
|
|
566
645
|
# How to Contribute
|
|
567
646
|
|
|
568
647
|
To contribute please ready our [CONTRIBUTING.md](CONTRIBUTING.md) guide. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
package/dist/ecto.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { HookifiedOptions, Hookified } from 'hookified';
|
|
2
|
+
import { Cacheable, CacheableMemory } from 'cacheable';
|
|
3
|
+
|
|
1
4
|
declare class EngineMap {
|
|
2
5
|
private readonly _mappings;
|
|
3
6
|
set(name: string, extensions: string[]): void;
|
|
@@ -88,10 +91,32 @@ type EctoOptions = {
|
|
|
88
91
|
* }
|
|
89
92
|
*/
|
|
90
93
|
engineOptions?: Record<string, Record<string, unknown>>;
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Caching for async rendered templates. If set to true, it will use the default cacheable options.
|
|
96
|
+
* If set to Cacheable instantce, it will use the provided cacheable instance.
|
|
97
|
+
* @type {boolean | Cacheable}
|
|
98
|
+
* @default false
|
|
99
|
+
*/
|
|
100
|
+
cache?: boolean | Cacheable;
|
|
101
|
+
/**
|
|
102
|
+
* If set to true, it will cache the rendered templates synchronously when running renderSync.
|
|
103
|
+
* If set to CacheableMemory instance, it will use the provided cacheable memory instance.
|
|
104
|
+
* @type {boolean | CacheableMemory}
|
|
105
|
+
* @default false
|
|
106
|
+
*/
|
|
107
|
+
cacheSync?: boolean | CacheableMemory;
|
|
108
|
+
} & HookifiedOptions;
|
|
109
|
+
declare enum EctoEvents {
|
|
110
|
+
cacheHit = "cacheHit",
|
|
111
|
+
cacheMiss = "cacheMiss",
|
|
112
|
+
warn = "warn",
|
|
113
|
+
error = "error"
|
|
114
|
+
}
|
|
115
|
+
declare class Ecto extends Hookified {
|
|
93
116
|
private readonly __mapping;
|
|
94
117
|
private readonly __engines;
|
|
118
|
+
private __cache;
|
|
119
|
+
private __cacheSync;
|
|
95
120
|
private __defaultEngine;
|
|
96
121
|
private readonly __ejs;
|
|
97
122
|
private readonly __markdown;
|
|
@@ -114,6 +139,26 @@ declare class Ecto {
|
|
|
114
139
|
* @param {string} value the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
|
|
115
140
|
*/
|
|
116
141
|
set defaultEngine(value: string);
|
|
142
|
+
/**
|
|
143
|
+
* Get the cacheable instance
|
|
144
|
+
* @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
|
|
145
|
+
*/
|
|
146
|
+
get cache(): Cacheable | undefined;
|
|
147
|
+
/**
|
|
148
|
+
* Set the cacheable instance
|
|
149
|
+
* @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
|
|
150
|
+
*/
|
|
151
|
+
set cache(value: Cacheable | undefined);
|
|
152
|
+
/**
|
|
153
|
+
* Get the cacheable memory instance
|
|
154
|
+
* @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
|
|
155
|
+
*/
|
|
156
|
+
get cacheSync(): CacheableMemory | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* Set the cacheable memory instance
|
|
159
|
+
* @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
|
|
160
|
+
*/
|
|
161
|
+
set cacheSync(value: CacheableMemory | undefined);
|
|
117
162
|
/**
|
|
118
163
|
* Get the Engine Mappings. This is used to map file extensions to engines
|
|
119
164
|
* @returns {EngineMap}
|
|
@@ -267,4 +312,4 @@ declare class Ecto {
|
|
|
267
312
|
private writeFileSync;
|
|
268
313
|
}
|
|
269
314
|
|
|
270
|
-
export { Ecto, type EctoOptions };
|
|
315
|
+
export { Ecto, EctoEvents, type EctoOptions };
|
package/dist/ecto.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// src/ecto.ts
|
|
2
2
|
import fs2 from "fs";
|
|
3
|
+
import { Hookified } from "hookified";
|
|
3
4
|
import { Writr as Writr2 } from "writr";
|
|
5
|
+
import { Cacheable, CacheableMemory } from "cacheable";
|
|
4
6
|
|
|
5
7
|
// src/engine-map.ts
|
|
6
8
|
var EngineMap = class {
|
|
@@ -288,9 +290,19 @@ var Liquid = class extends BaseEngine {
|
|
|
288
290
|
};
|
|
289
291
|
|
|
290
292
|
// src/ecto.ts
|
|
291
|
-
var
|
|
293
|
+
var EctoEvents = /* @__PURE__ */ ((EctoEvents2) => {
|
|
294
|
+
EctoEvents2["cacheHit"] = "cacheHit";
|
|
295
|
+
EctoEvents2["cacheMiss"] = "cacheMiss";
|
|
296
|
+
EctoEvents2["warn"] = "warn";
|
|
297
|
+
EctoEvents2["error"] = "error";
|
|
298
|
+
return EctoEvents2;
|
|
299
|
+
})(EctoEvents || {});
|
|
300
|
+
var Ecto = class extends Hookified {
|
|
292
301
|
__mapping = new EngineMap();
|
|
293
302
|
__engines = new Array();
|
|
303
|
+
// Cacheable instance for caching rendered templates
|
|
304
|
+
__cache;
|
|
305
|
+
__cacheSync;
|
|
294
306
|
__defaultEngine = "ejs";
|
|
295
307
|
// Engines
|
|
296
308
|
__ejs;
|
|
@@ -303,7 +315,9 @@ var Ecto = class {
|
|
|
303
315
|
* Ecto constructor
|
|
304
316
|
* @param {EctoOptions} [options] - The options for the ecto engine
|
|
305
317
|
*/
|
|
318
|
+
// eslint-disable-next-line complexity
|
|
306
319
|
constructor(options) {
|
|
320
|
+
super();
|
|
307
321
|
this.__ejs = new EJS(options?.engineOptions?.ejs);
|
|
308
322
|
this.__markdown = new Markdown(options?.engineOptions?.markdown);
|
|
309
323
|
this.__pug = new Pug(options?.engineOptions?.pug);
|
|
@@ -312,6 +326,20 @@ var Ecto = class {
|
|
|
312
326
|
this.__liquid = new Liquid(options?.engineOptions?.liquid);
|
|
313
327
|
this.__engines.push(this.__ejs, this.__markdown, this.__pug, this.__nunjucks, this.__handlebars, this.__liquid);
|
|
314
328
|
this.registerEngineMappings();
|
|
329
|
+
if (options?.cache !== void 0) {
|
|
330
|
+
if (typeof options.cache === "boolean") {
|
|
331
|
+
this.__cache = new Cacheable();
|
|
332
|
+
} else {
|
|
333
|
+
this.__cache = options.cache;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (options?.cacheSync !== void 0) {
|
|
337
|
+
if (typeof options.cacheSync === "boolean") {
|
|
338
|
+
this.__cacheSync = new CacheableMemory();
|
|
339
|
+
} else {
|
|
340
|
+
this.__cacheSync = options.cacheSync;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
315
343
|
if (options !== void 0 && this.isValidEngine(options.defaultEngine)) {
|
|
316
344
|
this.__defaultEngine = options.defaultEngine;
|
|
317
345
|
}
|
|
@@ -331,8 +359,38 @@ var Ecto = class {
|
|
|
331
359
|
value = value.toLowerCase().trim();
|
|
332
360
|
if (this.isValidEngine(value)) {
|
|
333
361
|
this.__defaultEngine = value;
|
|
362
|
+
} else {
|
|
363
|
+
this.emit("warn" /* warn */, `Invalid engine name: ${value}. Defaulting to ${this.__defaultEngine}.`);
|
|
334
364
|
}
|
|
335
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Get the cacheable instance
|
|
368
|
+
* @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
|
|
369
|
+
*/
|
|
370
|
+
get cache() {
|
|
371
|
+
return this.__cache;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Set the cacheable instance
|
|
375
|
+
* @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
|
|
376
|
+
*/
|
|
377
|
+
set cache(value) {
|
|
378
|
+
this.__cache = value;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Get the cacheable memory instance
|
|
382
|
+
* @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
|
|
383
|
+
*/
|
|
384
|
+
get cacheSync() {
|
|
385
|
+
return this.__cacheSync;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Set the cacheable memory instance
|
|
389
|
+
* @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
|
|
390
|
+
*/
|
|
391
|
+
set cacheSync(value) {
|
|
392
|
+
this.__cacheSync = value;
|
|
393
|
+
}
|
|
336
394
|
/**
|
|
337
395
|
* Get the Engine Mappings. This is used to map file extensions to engines
|
|
338
396
|
* @returns {EngineMap}
|
|
@@ -393,16 +451,34 @@ var Ecto = class {
|
|
|
393
451
|
*/
|
|
394
452
|
// eslint-disable-next-line max-params
|
|
395
453
|
async render(source, data, engineName, rootTemplatePath, filePathOutput) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
454
|
+
try {
|
|
455
|
+
const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
|
|
456
|
+
if (this.__cache) {
|
|
457
|
+
const cachedResult = await this.__cache.get(cacheKey);
|
|
458
|
+
if (cachedResult) {
|
|
459
|
+
this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
|
|
460
|
+
await this.writeFile(filePathOutput, cachedResult);
|
|
461
|
+
return cachedResult;
|
|
462
|
+
}
|
|
463
|
+
this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
|
|
464
|
+
}
|
|
465
|
+
let result = "";
|
|
466
|
+
let renderEngineName = this.__defaultEngine;
|
|
467
|
+
if (this.isValidEngine(engineName) && engineName !== void 0) {
|
|
468
|
+
renderEngineName = engineName;
|
|
469
|
+
}
|
|
470
|
+
const renderEngine = this.getRenderEngine(renderEngineName);
|
|
471
|
+
renderEngine.rootTemplatePath = rootTemplatePath;
|
|
472
|
+
result = await renderEngine.render(source, data);
|
|
473
|
+
if (this.__cache) {
|
|
474
|
+
await this.__cache.set(cacheKey, result);
|
|
475
|
+
}
|
|
476
|
+
await this.writeFile(filePathOutput, result);
|
|
477
|
+
return result;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
this.emit("error" /* error */, error);
|
|
480
|
+
return "";
|
|
481
|
+
}
|
|
406
482
|
}
|
|
407
483
|
/**
|
|
408
484
|
* Synchronously render the source with the data
|
|
@@ -415,16 +491,34 @@ var Ecto = class {
|
|
|
415
491
|
*/
|
|
416
492
|
// eslint-disable-next-line max-params
|
|
417
493
|
renderSync(source, data, engineName, rootTemplatePath, filePathOutput) {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
494
|
+
try {
|
|
495
|
+
const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
|
|
496
|
+
if (this.__cacheSync) {
|
|
497
|
+
const cachedResult = this.__cacheSync.get(cacheKey);
|
|
498
|
+
if (cachedResult) {
|
|
499
|
+
this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
|
|
500
|
+
this.writeFileSync(filePathOutput, cachedResult);
|
|
501
|
+
return cachedResult;
|
|
502
|
+
}
|
|
503
|
+
this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
|
|
504
|
+
}
|
|
505
|
+
let result = "";
|
|
506
|
+
let renderEngineName = this.__defaultEngine;
|
|
507
|
+
if (this.isValidEngine(engineName) && engineName !== void 0) {
|
|
508
|
+
renderEngineName = engineName;
|
|
509
|
+
}
|
|
510
|
+
const renderEngine = this.getRenderEngine(renderEngineName);
|
|
511
|
+
renderEngine.rootTemplatePath = rootTemplatePath;
|
|
512
|
+
result = renderEngine.renderSync(source, data);
|
|
513
|
+
if (this.__cacheSync) {
|
|
514
|
+
this.__cacheSync.set(cacheKey, result);
|
|
515
|
+
}
|
|
516
|
+
this.writeFileSync(filePathOutput, result);
|
|
517
|
+
return result;
|
|
518
|
+
} catch (error) {
|
|
519
|
+
this.emit("error" /* error */, error);
|
|
520
|
+
return "";
|
|
521
|
+
}
|
|
428
522
|
}
|
|
429
523
|
/**
|
|
430
524
|
* Render from a file path
|
|
@@ -654,5 +748,6 @@ var Ecto = class {
|
|
|
654
748
|
}
|
|
655
749
|
};
|
|
656
750
|
export {
|
|
657
|
-
Ecto
|
|
751
|
+
Ecto,
|
|
752
|
+
EctoEvents
|
|
658
753
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ecto",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/ecto.js",
|
|
@@ -46,28 +46,30 @@
|
|
|
46
46
|
"liquidjs"
|
|
47
47
|
],
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@jaredwray/fumanchu": "^3.1.
|
|
49
|
+
"@jaredwray/fumanchu": "^3.1.2",
|
|
50
|
+
"cacheable": "^1.10.1",
|
|
50
51
|
"ejs": "^3.1.10",
|
|
52
|
+
"hookified": "^1.10.0",
|
|
51
53
|
"liquidjs": "^10.21.1",
|
|
52
54
|
"nunjucks": "^3.2.4",
|
|
53
55
|
"pug": "^3.0.3",
|
|
54
56
|
"underscore": "^1.13.7",
|
|
55
|
-
"writr": "^4.4.
|
|
57
|
+
"writr": "^4.4.5"
|
|
56
58
|
},
|
|
57
59
|
"devDependencies": {
|
|
58
60
|
"@types/ejs": "^3.1.5",
|
|
59
|
-
"@types/node": "^
|
|
61
|
+
"@types/node": "^24.0.14",
|
|
60
62
|
"@types/nunjucks": "^3.2.6",
|
|
61
63
|
"@types/pug": "^2.0.10",
|
|
62
64
|
"@types/underscore": "^1.13.0",
|
|
63
|
-
"@vitest/coverage-v8": "^3.
|
|
64
|
-
"docula": "^0.
|
|
65
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
66
|
+
"docula": "^0.13.0",
|
|
65
67
|
"rimraf": "^6.0.1",
|
|
66
68
|
"tsup": "^8.5.0",
|
|
67
69
|
"typescript": "^5.8.3",
|
|
68
|
-
"vitest": "^3.
|
|
69
|
-
"webpack": "^5.
|
|
70
|
-
"xo": "^1.
|
|
70
|
+
"vitest": "^3.2.4",
|
|
71
|
+
"webpack": "^5.100.2",
|
|
72
|
+
"xo": "^1.2.0"
|
|
71
73
|
},
|
|
72
74
|
"files": [
|
|
73
75
|
"dist",
|