ecto 4.2.3 → 4.3.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 CHANGED
@@ -39,6 +39,7 @@ 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)
42
43
  * [How to Contribute](#how-to-contribute)
43
44
  * [License](#license)
44
45
 
@@ -563,6 +564,50 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front
563
564
  * `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
565
  * `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
565
566
 
567
+ # Caching on Rendering
568
+
569
+ 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`.
570
+ * `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.
571
+ * `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.
572
+
573
+ Here is an example of how to use the caching engine with `render`:
574
+
575
+ ```javascript
576
+ import { Ecto } from 'ecto';
577
+
578
+ const ecto = new Ecto({ cache: true });
579
+ const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
580
+ const data = { firstName: "John", lastName: "Doe" };
581
+ const result = await ecto.render(source, data);
582
+ console.log(result); // <h1>Hello John Doe!</h1>
583
+ ```
584
+
585
+ You can also use the `cacheSync` engine with `renderSync`:
586
+
587
+ ```javascript
588
+ import { Ecto } from 'ecto';
589
+ const ecto = new Ecto({ cacheSync: true });
590
+ const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
591
+ const data = { firstName: "John", lastName: "Doe" };
592
+ const result = ecto.renderSync(source, data);
593
+ console.log(result); // <h1>Hello John Doe!</h1>
594
+ ```
595
+
596
+ If you want to provide your own caching configuration, you can do so by passing `Cacheable` instance to the `Ecto` constructor:
597
+
598
+ ```javascript
599
+ import { Ecto } from 'ecto';
600
+ import { Cacheable } from 'cacheable';
601
+ const cacheable = new Cacheable({
602
+ // Your caching configuration here
603
+ });
604
+ const ecto = new Ecto({ cache: cacheable });
605
+ const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
606
+ const data = { firstName: "John", lastName: "Doe" };
607
+ const result = await ecto.render(source, data);
608
+ console.log(result); // <h1>Hello John Doe!</h1>
609
+ ```
610
+
566
611
  # How to Contribute
567
612
 
568
613
  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,5 @@
1
+ import { Cacheable, CacheableMemory } from 'cacheable';
2
+
1
3
  declare class EngineMap {
2
4
  private readonly _mappings;
3
5
  set(name: string, extensions: string[]): void;
@@ -88,10 +90,26 @@ type EctoOptions = {
88
90
  * }
89
91
  */
90
92
  engineOptions?: Record<string, Record<string, unknown>>;
93
+ /**
94
+ * Caching for async rendered templates. If set to true, it will use the default cacheable options.
95
+ * If set to Cacheable instantce, it will use the provided cacheable instance.
96
+ * @type {boolean | Cacheable}
97
+ * @default false
98
+ */
99
+ cache?: boolean | Cacheable;
100
+ /**
101
+ * If set to true, it will cache the rendered templates synchronously when running renderSync.
102
+ * If set to CacheableMemory instance, it will use the provided cacheable memory instance.
103
+ * @type {boolean | CacheableMemory}
104
+ * @default false
105
+ */
106
+ cacheSync?: boolean | CacheableMemory;
91
107
  };
92
108
  declare class Ecto {
93
109
  private readonly __mapping;
94
110
  private readonly __engines;
111
+ private __cache;
112
+ private __cacheSync;
95
113
  private __defaultEngine;
96
114
  private readonly __ejs;
97
115
  private readonly __markdown;
@@ -114,6 +132,26 @@ declare class Ecto {
114
132
  * @param {string} value the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
115
133
  */
116
134
  set defaultEngine(value: string);
135
+ /**
136
+ * Get the cacheable instance
137
+ * @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
138
+ */
139
+ get cache(): Cacheable | undefined;
140
+ /**
141
+ * Set the cacheable instance
142
+ * @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
143
+ */
144
+ set cache(value: Cacheable | undefined);
145
+ /**
146
+ * Get the cacheable memory instance
147
+ * @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
148
+ */
149
+ get cacheSync(): CacheableMemory | undefined;
150
+ /**
151
+ * Set the cacheable memory instance
152
+ * @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
153
+ */
154
+ set cacheSync(value: CacheableMemory | undefined);
117
155
  /**
118
156
  * Get the Engine Mappings. This is used to map file extensions to engines
119
157
  * @returns {EngineMap}
package/dist/ecto.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/ecto.ts
2
- import fs2 from "node:fs";
2
+ import fs2 from "fs";
3
3
  import { Writr as Writr2 } from "writr";
4
+ import { Cacheable, CacheableMemory } from "cacheable";
4
5
 
5
6
  // src/engine-map.ts
6
7
  var EngineMap = class {
@@ -101,7 +102,7 @@ var Markdown = class extends BaseEngine {
101
102
  };
102
103
 
103
104
  // src/engines/handlebars.ts
104
- import fs from "node:fs";
105
+ import fs from "fs";
105
106
  import * as _ from "underscore";
106
107
  import { helpers, handlebars } from "@jaredwray/fumanchu";
107
108
  var Handlebars = class extends BaseEngine {
@@ -174,16 +175,16 @@ var EJS = class extends BaseEngine {
174
175
  this.setExtensions(["ejs"]);
175
176
  }
176
177
  async render(source, data) {
177
- this.engine ||= ejs;
178
- this.opts ||= {};
178
+ this.engine ??= ejs;
179
+ this.opts ??= {};
179
180
  if (this.rootTemplatePath) {
180
181
  this.opts.root = this.rootTemplatePath;
181
182
  }
182
183
  return ejs.render(source, data, this.opts);
183
184
  }
184
185
  renderSync(source, data) {
185
- this.engine ||= ejs;
186
- this.opts ||= {};
186
+ this.engine ??= ejs;
187
+ this.opts ??= {};
187
188
  if (this.rootTemplatePath) {
188
189
  this.opts.root = this.rootTemplatePath;
189
190
  }
@@ -205,7 +206,7 @@ var Pug = class extends BaseEngine {
205
206
  }
206
207
  async render(source, data) {
207
208
  if (this.rootTemplatePath) {
208
- this.opts ||= {};
209
+ this.opts ??= {};
209
210
  this.opts.basedir = this.rootTemplatePath;
210
211
  }
211
212
  const template = pug.compile(source, this.opts);
@@ -213,7 +214,7 @@ var Pug = class extends BaseEngine {
213
214
  }
214
215
  renderSync(source, data) {
215
216
  if (this.rootTemplatePath) {
216
- this.opts ||= {};
217
+ this.opts ??= {};
217
218
  this.opts.basedir = this.rootTemplatePath;
218
219
  }
219
220
  const template = pug.compile(source, this.opts);
@@ -240,7 +241,7 @@ var Nunjucks = class extends BaseEngine {
240
241
  } else {
241
242
  nunjucks.configure(this.opts);
242
243
  }
243
- data ||= {};
244
+ data ??= {};
244
245
  return nunjucks.renderString(source, data);
245
246
  }
246
247
  renderSync(source, data) {
@@ -249,7 +250,7 @@ var Nunjucks = class extends BaseEngine {
249
250
  } else {
250
251
  nunjucks.configure(this.opts);
251
252
  }
252
- data ||= {};
253
+ data ??= {};
253
254
  return nunjucks.renderString(source, data);
254
255
  }
255
256
  };
@@ -267,7 +268,7 @@ var Liquid = class extends BaseEngine {
267
268
  }
268
269
  async render(source, data) {
269
270
  if (this.rootTemplatePath) {
270
- this.opts ||= {};
271
+ this.opts ??= {};
271
272
  this.opts.root = this.rootTemplatePath;
272
273
  }
273
274
  if (!this.engine) {
@@ -277,7 +278,7 @@ var Liquid = class extends BaseEngine {
277
278
  }
278
279
  renderSync(source, data) {
279
280
  if (this.rootTemplatePath) {
280
- this.opts ||= {};
281
+ this.opts ??= {};
281
282
  this.opts.root = this.rootTemplatePath;
282
283
  }
283
284
  if (!this.engine) {
@@ -291,6 +292,9 @@ var Liquid = class extends BaseEngine {
291
292
  var Ecto = class {
292
293
  __mapping = new EngineMap();
293
294
  __engines = new Array();
295
+ // Cacheable instance for caching rendered templates
296
+ __cache;
297
+ __cacheSync;
294
298
  __defaultEngine = "ejs";
295
299
  // Engines
296
300
  __ejs;
@@ -303,6 +307,7 @@ var Ecto = class {
303
307
  * Ecto constructor
304
308
  * @param {EctoOptions} [options] - The options for the ecto engine
305
309
  */
310
+ // eslint-disable-next-line complexity
306
311
  constructor(options) {
307
312
  this.__ejs = new EJS(options?.engineOptions?.ejs);
308
313
  this.__markdown = new Markdown(options?.engineOptions?.markdown);
@@ -312,6 +317,20 @@ var Ecto = class {
312
317
  this.__liquid = new Liquid(options?.engineOptions?.liquid);
313
318
  this.__engines.push(this.__ejs, this.__markdown, this.__pug, this.__nunjucks, this.__handlebars, this.__liquid);
314
319
  this.registerEngineMappings();
320
+ if (options?.cache !== void 0) {
321
+ if (typeof options.cache === "boolean") {
322
+ this.__cache = new Cacheable();
323
+ } else {
324
+ this.__cache = options.cache;
325
+ }
326
+ }
327
+ if (options?.cacheSync !== void 0) {
328
+ if (typeof options.cacheSync === "boolean") {
329
+ this.__cacheSync = new CacheableMemory();
330
+ } else {
331
+ this.__cacheSync = options.cacheSync;
332
+ }
333
+ }
315
334
  if (options !== void 0 && this.isValidEngine(options.defaultEngine)) {
316
335
  this.__defaultEngine = options.defaultEngine;
317
336
  }
@@ -333,6 +352,34 @@ var Ecto = class {
333
352
  this.__defaultEngine = value;
334
353
  }
335
354
  }
355
+ /**
356
+ * Get the cacheable instance
357
+ * @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
358
+ */
359
+ get cache() {
360
+ return this.__cache;
361
+ }
362
+ /**
363
+ * Set the cacheable instance
364
+ * @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
365
+ */
366
+ set cache(value) {
367
+ this.__cache = value;
368
+ }
369
+ /**
370
+ * Get the cacheable memory instance
371
+ * @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
372
+ */
373
+ get cacheSync() {
374
+ return this.__cacheSync;
375
+ }
376
+ /**
377
+ * Set the cacheable memory instance
378
+ * @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
379
+ */
380
+ set cacheSync(value) {
381
+ this.__cacheSync = value;
382
+ }
336
383
  /**
337
384
  * Get the Engine Mappings. This is used to map file extensions to engines
338
385
  * @returns {EngineMap}
@@ -393,6 +440,14 @@ var Ecto = class {
393
440
  */
394
441
  // eslint-disable-next-line max-params
395
442
  async render(source, data, engineName, rootTemplatePath, filePathOutput) {
443
+ const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
444
+ if (this.__cache) {
445
+ const cachedResult = await this.__cache.get(cacheKey);
446
+ if (cachedResult) {
447
+ await this.writeFile(filePathOutput, cachedResult);
448
+ return cachedResult;
449
+ }
450
+ }
396
451
  let result = "";
397
452
  let renderEngineName = this.__defaultEngine;
398
453
  if (this.isValidEngine(engineName) && engineName !== void 0) {
@@ -401,6 +456,9 @@ var Ecto = class {
401
456
  const renderEngine = this.getRenderEngine(renderEngineName);
402
457
  renderEngine.rootTemplatePath = rootTemplatePath;
403
458
  result = await renderEngine.render(source, data);
459
+ if (this.__cache) {
460
+ await this.__cache.set(cacheKey, result);
461
+ }
404
462
  await this.writeFile(filePathOutput, result);
405
463
  return result;
406
464
  }
@@ -415,6 +473,14 @@ var Ecto = class {
415
473
  */
416
474
  // eslint-disable-next-line max-params
417
475
  renderSync(source, data, engineName, rootTemplatePath, filePathOutput) {
476
+ const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
477
+ if (this.__cacheSync) {
478
+ const cachedResult = this.__cacheSync.get(cacheKey);
479
+ if (cachedResult) {
480
+ this.writeFileSync(filePathOutput, cachedResult);
481
+ return cachedResult;
482
+ }
483
+ }
418
484
  let result = "";
419
485
  let renderEngineName = this.__defaultEngine;
420
486
  if (this.isValidEngine(engineName) && engineName !== void 0) {
@@ -423,6 +489,9 @@ var Ecto = class {
423
489
  const renderEngine = this.getRenderEngine(renderEngineName);
424
490
  renderEngine.rootTemplatePath = rootTemplatePath;
425
491
  result = renderEngine.renderSync(source, data);
492
+ if (this.__cacheSync) {
493
+ this.__cacheSync.set(cacheKey, result);
494
+ }
426
495
  this.writeFileSync(filePathOutput, result);
427
496
  return result;
428
497
  }
@@ -438,7 +507,7 @@ var Ecto = class {
438
507
  // eslint-disable-next-line max-params
439
508
  async renderFromFile(filePath, data, rootTemplatePath, filePathOutput, engineName) {
440
509
  let result = "";
441
- engineName ||= this.getEngineByFilePath(filePath);
510
+ engineName ??= this.getEngineByFilePath(filePath);
442
511
  const source = await fs2.promises.readFile(filePath, "utf8");
443
512
  result = await this.render(source, data, engineName, rootTemplatePath, filePathOutput);
444
513
  return result;
@@ -455,7 +524,7 @@ var Ecto = class {
455
524
  // eslint-disable-next-line max-params
456
525
  renderFromFileSync(filePath, data, rootTemplatePath, filePathOutput, engineName) {
457
526
  let result = "";
458
- engineName ||= this.getEngineByFilePath(filePath);
527
+ engineName ??= this.getEngineByFilePath(filePath);
459
528
  const source = fs2.readFileSync(filePath, "utf8");
460
529
  result = this.renderSync(source, data, engineName, rootTemplatePath, filePathOutput);
461
530
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecto",
3
- "version": "4.2.3",
3
+ "version": "4.3.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,29 @@
46
46
  "liquidjs"
47
47
  ],
48
48
  "dependencies": {
49
- "@jaredwray/fumanchu": "^3.0.4",
49
+ "@jaredwray/fumanchu": "^3.1.1",
50
+ "cacheable": "^1.10.0",
50
51
  "ejs": "^3.1.10",
51
- "liquidjs": "^10.21.0",
52
+ "liquidjs": "^10.21.1",
52
53
  "nunjucks": "^3.2.4",
53
54
  "pug": "^3.0.3",
54
55
  "underscore": "^1.13.7",
55
- "writr": "^4.4.3"
56
+ "writr": "^4.4.4"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@types/ejs": "^3.1.5",
59
- "@types/node": "^22.15.17",
60
+ "@types/node": "^24.0.1",
60
61
  "@types/nunjucks": "^3.2.6",
61
62
  "@types/pug": "^2.0.10",
62
63
  "@types/underscore": "^1.13.0",
63
- "@vitest/coverage-v8": "^3.1.3",
64
- "docula": "^0.11.1",
64
+ "@vitest/coverage-v8": "^3.2.3",
65
+ "docula": "^0.12.2",
65
66
  "rimraf": "^6.0.1",
66
- "tsup": "^8.4.0",
67
+ "tsup": "^8.5.0",
67
68
  "typescript": "^5.8.3",
68
- "vitest": "^3.1.3",
69
- "webpack": "^5.99.8",
70
- "xo": "^0.60.0"
69
+ "vitest": "^3.2.3",
70
+ "webpack": "^5.99.9",
71
+ "xo": "^1.1.0"
71
72
  },
72
73
  "files": [
73
74
  "dist",