ecto 5.1.0 → 5.1.2

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
@@ -7,6 +7,7 @@
7
7
  [![GitHub license](https://img.shields.io/github/license/jaredwray/ecto)](https://github.com/jaredwray/ecto/blob/master/LICENSE)
8
8
  [![codecov](https://codecov.io/gh/jaredwray/ecto/branch/main/graph/badge.svg?token=dpbFqSW5Kh)](https://codecov.io/gh/jaredwray/ecto)
9
9
  [![npm](https://img.shields.io/npm/dm/ecto)](https://npmjs.com/package/ecto)
10
+ [![Drydock review](https://img.shields.io/endpoint?url=https%3A%2F%2Fdrydock.org%2Fpublic%2Fbadge%2Fnpm%2Fecto)](https://drydock.org/diff/ecto)
10
11
 
11
12
  -----
12
13
 
@@ -730,8 +731,8 @@ type RenderContext = {
730
731
  data?: Record<string, unknown>; // Template data (modifiable)
731
732
  engineName: string; // Engine being used
732
733
  rootTemplatePath?: string; // Root path for partials
733
- filePathOutput?: string; // Output file path
734
- cached: boolean; // True if result came from cache
734
+ filePathOutput?: string; // Output file path (modifiable; used when writing)
735
+ cached: boolean; // True when this call was served from cache
735
736
  };
736
737
 
737
738
  type RenderResult = {
@@ -764,9 +765,17 @@ ecto.onHook(EctoEvents.afterRender, (renderResult) => {
764
765
  const output = await ecto.render('<%= name %>', { name: 'World' });
765
766
  ```
766
767
 
768
+ ## Hooks and Caching
769
+
770
+ The async cache (`cache`) and sync cache (`cacheSync`) store the template engine output taken after the before-hook and before the after-hook. The cache key is the original source, data, and engine name.
771
+
772
+ On a cache hit, `beforeRender` / `beforeRenderSync` still run with `cached: true`, and the stored engine output is reused. Edits those hooks make to `source` or `data` are rendered on a cache miss. `afterRender` / `afterRenderSync` run on every call, including cache hits, and can still change `result` and the file written from `filePathOutput`.
773
+
774
+ `renderSync` uses Hookified's synchronous runner, which skips handlers declared with `async`.
775
+
767
776
  ## Hook Objects, IDs, and Errors
768
777
 
769
- Ecto extends [Hookified](https://hookified.org), so you can also register hooks via the `Hook` class. Every hook has a unique `id` (auto-generated unless supplied) for later lookup or removal across events. Pass `throwOnHookError: true` to the `Ecto` constructor to make hook errors fail the render instead of just emitting on `error`.
778
+ Ecto extends [Hookified](https://hookified.org), so constructor options such as `throwOnHookError` are passed through, and you can register hooks via the `Hook` class. Every hook has a unique `id` (auto-generated unless supplied) for later lookup or removal across events. The default is to emit `error` and continue rendering. Pass `throwOnHookError: true` to make a hook error reject `render` and throw from `renderSync`.
770
779
 
771
780
  ```javascript
772
781
  import { Ecto, EctoEvents } from 'ecto';
package/dist/ecto.d.mts CHANGED
@@ -33,14 +33,14 @@ declare class BaseEngine {
33
33
  }
34
34
  //#endregion
35
35
  //#region src/engines/ejs.d.ts
36
- declare class EJS extends BaseEngine implements EngineInterface {
36
+ export declare class EJS extends BaseEngine implements EngineInterface {
37
37
  constructor(options?: Record<string, unknown>);
38
38
  render(source: string, data?: Record<string, unknown>): Promise<string>;
39
39
  renderSync(source: string, data?: Record<string, unknown>): string;
40
40
  }
41
41
  //#endregion
42
42
  //#region src/engines/handlebars.d.ts
43
- declare class Handlebars extends BaseEngine implements EngineInterface {
43
+ export declare class Handlebars extends BaseEngine implements EngineInterface {
44
44
  partialsPath: string[];
45
45
  constructor(options?: Record<string, unknown>);
46
46
  render(source: string, data?: Record<string, unknown>): Promise<string>;
@@ -50,35 +50,35 @@ declare class Handlebars extends BaseEngine implements EngineInterface {
50
50
  }
51
51
  //#endregion
52
52
  //#region src/engines/liquid.d.ts
53
- declare class Liquid extends BaseEngine implements EngineInterface {
53
+ export declare class Liquid extends BaseEngine implements EngineInterface {
54
54
  constructor(options?: Record<string, unknown>);
55
55
  render(source: string, data?: Record<string, unknown>): Promise<string>;
56
56
  renderSync(source: string, data?: Record<string, unknown>): string;
57
57
  }
58
58
  //#endregion
59
59
  //#region src/engines/markdown.d.ts
60
- declare class Markdown extends BaseEngine implements EngineInterface {
60
+ export declare class Markdown extends BaseEngine implements EngineInterface {
61
61
  constructor(options?: Record<string, unknown>);
62
62
  render(source: string, data?: Record<string, unknown>): Promise<string>;
63
63
  renderSync(source: string, data?: Record<string, unknown>): string;
64
64
  }
65
65
  //#endregion
66
66
  //#region src/engines/nunjucks.d.ts
67
- declare class Nunjucks extends BaseEngine implements EngineInterface {
67
+ export declare class Nunjucks extends BaseEngine implements EngineInterface {
68
68
  constructor(options?: Record<string, unknown>);
69
69
  render(source: string, data?: Record<string, unknown>): Promise<string>;
70
70
  renderSync(source: string, data?: Record<string, unknown>): string;
71
71
  }
72
72
  //#endregion
73
73
  //#region src/engines/pug.d.ts
74
- declare class Pug extends BaseEngine implements EngineInterface {
74
+ export declare class Pug extends BaseEngine implements EngineInterface {
75
75
  constructor(options?: Record<string, unknown>);
76
76
  render(source: string, data?: Record<string, unknown>): Promise<string>;
77
77
  renderSync(source: string, data?: Record<string, unknown>): string;
78
78
  }
79
79
  //#endregion
80
80
  //#region src/ecto.d.ts
81
- type EctoOptions = {
81
+ export type EctoOptions = {
82
82
  /**
83
83
  * The default engine to use. This can be 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
84
84
  * @default 'ejs'
@@ -118,7 +118,7 @@ type EctoOptions = {
118
118
  /**
119
119
  * Context passed to beforeRender and beforeRenderSync hooks
120
120
  */
121
- type RenderContext = {
121
+ export type RenderContext = {
122
122
  source: string;
123
123
  data?: Record<string, unknown>;
124
124
  engineName: string;
@@ -129,11 +129,11 @@ type RenderContext = {
129
129
  /**
130
130
  * Result passed to afterRender and afterRenderSync hooks
131
131
  */
132
- type RenderResult = {
132
+ export type RenderResult = {
133
133
  result: string;
134
134
  context: RenderContext;
135
135
  };
136
- declare enum EctoEvents {
136
+ export declare enum EctoEvents {
137
137
  cacheHit = "cacheHit",
138
138
  cacheMiss = "cacheMiss",
139
139
  warn = "warn",
@@ -143,7 +143,7 @@ declare enum EctoEvents {
143
143
  beforeRenderSync = "beforeRenderSync",
144
144
  afterRenderSync = "afterRenderSync"
145
145
  }
146
- declare class Ecto extends Hookified {
146
+ export declare class Ecto extends Hookified {
147
147
  private readonly _mapping;
148
148
  private readonly _engines;
149
149
  private _cache;
@@ -226,7 +226,8 @@ declare class Ecto extends Hookified {
226
226
  */
227
227
  get liquid(): Liquid;
228
228
  /**
229
- * Asynchronously render a template source with data using the specified engine
229
+ * Asynchronously render a template source with data using the specified engine.
230
+ * `beforeRender` can modify the source and data. `afterRender` can modify the returned string.
230
231
  * @param {string} source - The template source string to render
231
232
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
232
233
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -238,7 +239,9 @@ declare class Ecto extends Hookified {
238
239
  */
239
240
  render(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): Promise<string>;
240
241
  /**
241
- * Synchronously render a template source with data using the specified engine
242
+ * Synchronously render a template source with data using the specified engine.
243
+ * `beforeRenderSync` can modify the source and data. `afterRenderSync` can modify the returned string.
244
+ * Async hook handlers are skipped.
242
245
  * @param {string} source - The template source string to render
243
246
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
244
247
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -381,6 +384,23 @@ declare class Ecto extends Hookified {
381
384
  * const body = ecto.removeFrontMatter('---\ntitle: Test\n---\nContent');
382
385
  */
383
386
  removeFrontMatter(source: string): string;
387
+ /**
388
+ * Run an async render hook.
389
+ * Hookified throws from `emit('error')` when no error listener is attached.
390
+ * A temporary listener keeps that from aborting the render unless
391
+ * `throwOnHookError` is set. Handler failures then reject the render.
392
+ * @param {EctoEvents} event - Hook event name
393
+ * @param {T} payload - Context or result object passed to handlers
394
+ * @returns {Promise<void>}
395
+ */
396
+ private invokeHook;
397
+ /**
398
+ * Run a synchronous render hook. Async handlers are skipped by Hookified.
399
+ * @param {EctoEvents} event - Hook event name
400
+ * @param {T} payload - Context or result object passed to handlers
401
+ * @returns {void}
402
+ */
403
+ private invokeHookSync;
384
404
  /**
385
405
  * Write content to a file asynchronously, creating directories if needed
386
406
  * @private
@@ -399,4 +419,4 @@ declare class Ecto extends Hookified {
399
419
  private writeFileSync;
400
420
  }
401
421
  //#endregion
402
- export { type BaseEngine, EJS, Ecto, EctoEvents, EctoOptions, type EngineInterface, Handlebars, Liquid, Markdown, Nunjucks, Pug, RenderContext, RenderResult };
422
+ export type { BaseEngine, EngineInterface };
package/dist/ecto.mjs CHANGED
@@ -244,6 +244,16 @@ var Pug = class extends BaseEngine {
244
244
  };
245
245
  //#endregion
246
246
  //#region src/ecto.ts
247
+ /**
248
+ * Hook failure that should reject the render when `throwOnHookError` is set.
249
+ * Hookified has already emitted `error` when any listeners are attached.
250
+ */
251
+ var HookRenderError = class extends Error {
252
+ constructor(cause) {
253
+ super(cause.message, { cause });
254
+ this.name = "HookRenderError";
255
+ }
256
+ };
247
257
  let EctoEvents = /* @__PURE__ */ function(EctoEvents) {
248
258
  EctoEvents["cacheHit"] = "cacheHit";
249
259
  EctoEvents["cacheMiss"] = "cacheMiss";
@@ -272,7 +282,7 @@ var Ecto = class extends Hookified {
272
282
  * @param {EctoOptions} [options] - The options for the ecto engine
273
283
  */
274
284
  constructor(options) {
275
- super();
285
+ super(options);
276
286
  this._ejs = new EJS(options?.engineOptions?.ejs);
277
287
  this._markdown = new Markdown(options?.engineOptions?.markdown);
278
288
  this._pug = new Pug(options?.engineOptions?.pug);
@@ -381,7 +391,8 @@ var Ecto = class extends Hookified {
381
391
  return this._liquid;
382
392
  }
383
393
  /**
384
- * Asynchronously render a template source with data using the specified engine
394
+ * Asynchronously render a template source with data using the specified engine.
395
+ * `beforeRender` can modify the source and data. `afterRender` can modify the returned string.
385
396
  * @param {string} source - The template source string to render
386
397
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
387
398
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -408,13 +419,13 @@ var Ecto = class extends Hookified {
408
419
  filePathOutput,
409
420
  cached: true
410
421
  };
411
- await this.hook("beforeRender", context);
422
+ await this.invokeHook("beforeRender", context);
412
423
  const renderResult = {
413
424
  result: cachedResult,
414
425
  context
415
426
  };
416
- await this.hook("afterRender", renderResult);
417
- await this.writeFile(filePathOutput, renderResult.result);
427
+ await this.invokeHook("afterRender", renderResult);
428
+ await this.writeFile(context.filePathOutput, renderResult.result);
418
429
  return renderResult.result;
419
430
  }
420
431
  this.emit("cacheMiss", `Cache miss for key: ${cacheKey}`);
@@ -427,28 +438,28 @@ var Ecto = class extends Hookified {
427
438
  filePathOutput,
428
439
  cached: false
429
440
  };
430
- await this.hook("beforeRender", context);
441
+ await this.invokeHook("beforeRender", context);
431
442
  const renderEngine = this.getRenderEngine(renderEngineName);
432
443
  renderEngine.rootTemplatePath = context.rootTemplatePath;
433
- let result = await renderEngine.render(context.source, context.data);
444
+ const rendered = await renderEngine.render(context.source, context.data);
434
445
  const renderResult = {
435
- result,
446
+ result: rendered,
436
447
  context
437
448
  };
438
- await this.hook("afterRender", renderResult);
439
- result = renderResult.result;
440
- if (this._cache) await this._cache.set(cacheKey, result);
441
- await this.writeFile(filePathOutput, result);
442
- return result;
449
+ await this.invokeHook("afterRender", renderResult);
450
+ if (this._cache) await this._cache.set(cacheKey, rendered);
451
+ await this.writeFile(context.filePathOutput, renderResult.result);
452
+ return renderResult.result;
443
453
  } catch (error) {
444
- /* v8 ignore next -- @preserve */
454
+ if (error instanceof HookRenderError) throw error.cause;
445
455
  this.emit("error", error);
446
- /* v8 ignore next -- @preserve */
447
456
  return "";
448
457
  }
449
458
  }
450
459
  /**
451
- * Synchronously render a template source with data using the specified engine
460
+ * Synchronously render a template source with data using the specified engine.
461
+ * `beforeRenderSync` can modify the source and data. `afterRenderSync` can modify the returned string.
462
+ * Async hook handlers are skipped.
452
463
  * @param {string} source - The template source string to render
453
464
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
454
465
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -475,13 +486,13 @@ var Ecto = class extends Hookified {
475
486
  filePathOutput,
476
487
  cached: true
477
488
  };
478
- this.hookSync("beforeRenderSync", context);
489
+ this.invokeHookSync("beforeRenderSync", context);
479
490
  const renderResult = {
480
491
  result: cachedResult,
481
492
  context
482
493
  };
483
- this.hookSync("afterRenderSync", renderResult);
484
- this.writeFileSync(filePathOutput, renderResult.result);
494
+ this.invokeHookSync("afterRenderSync", renderResult);
495
+ this.writeFileSync(context.filePathOutput, renderResult.result);
485
496
  return renderResult.result;
486
497
  }
487
498
  this.emit("cacheMiss", `Cache miss for key: ${cacheKey}`);
@@ -494,20 +505,20 @@ var Ecto = class extends Hookified {
494
505
  filePathOutput,
495
506
  cached: false
496
507
  };
497
- this.hookSync("beforeRenderSync", context);
508
+ this.invokeHookSync("beforeRenderSync", context);
498
509
  const renderEngine = this.getRenderEngine(renderEngineName);
499
510
  renderEngine.rootTemplatePath = context.rootTemplatePath;
500
- let result = renderEngine.renderSync(context.source, context.data);
511
+ const rendered = renderEngine.renderSync(context.source, context.data);
501
512
  const renderResult = {
502
- result,
513
+ result: rendered,
503
514
  context
504
515
  };
505
- this.hookSync("afterRenderSync", renderResult);
506
- result = renderResult.result;
507
- if (this._cacheSync) this._cacheSync.set(cacheKey, result);
508
- this.writeFileSync(filePathOutput, result);
509
- return result;
516
+ this.invokeHookSync("afterRenderSync", renderResult);
517
+ if (this._cacheSync) this._cacheSync.set(cacheKey, rendered);
518
+ this.writeFileSync(context.filePathOutput, renderResult.result);
519
+ return renderResult.result;
510
520
  } catch (error) {
521
+ if (error instanceof HookRenderError) throw error.cause;
511
522
  this.emit("error", error);
512
523
  return "";
513
524
  }
@@ -776,9 +787,7 @@ var Ecto = class extends Hookified {
776
787
  case "liquid":
777
788
  result = this._liquid;
778
789
  break;
779
- default:
780
- result = this._ejs;
781
- break;
790
+ default: result = this._ejs;
782
791
  }
783
792
  return result;
784
793
  }
@@ -827,6 +836,57 @@ var Ecto = class extends Hookified {
827
836
  return new Writr(source).body;
828
837
  }
829
838
  /**
839
+ * Run an async render hook.
840
+ * Hookified throws from `emit('error')` when no error listener is attached.
841
+ * A temporary listener keeps that from aborting the render unless
842
+ * `throwOnHookError` is set. Handler failures then reject the render.
843
+ * @param {EctoEvents} event - Hook event name
844
+ * @param {T} payload - Context or result object passed to handlers
845
+ * @returns {Promise<void>}
846
+ */
847
+ async invokeHook(event, payload) {
848
+ if (!this.throwOnHookError) {
849
+ const ignoreHookError = () => {};
850
+ const swallowEmpty = this.listenerCount("error") === 0;
851
+ if (swallowEmpty) this.on("error", ignoreHookError);
852
+ try {
853
+ await this.hook(event, payload);
854
+ } finally {
855
+ if (swallowEmpty) this.off("error", ignoreHookError);
856
+ }
857
+ return;
858
+ }
859
+ try {
860
+ await this.hook(event, payload);
861
+ } catch (error) {
862
+ throw new HookRenderError(error);
863
+ }
864
+ }
865
+ /**
866
+ * Run a synchronous render hook. Async handlers are skipped by Hookified.
867
+ * @param {EctoEvents} event - Hook event name
868
+ * @param {T} payload - Context or result object passed to handlers
869
+ * @returns {void}
870
+ */
871
+ invokeHookSync(event, payload) {
872
+ if (!this.throwOnHookError) {
873
+ const ignoreHookError = () => {};
874
+ const swallowEmpty = this.listenerCount("error") === 0;
875
+ if (swallowEmpty) this.on("error", ignoreHookError);
876
+ try {
877
+ this.hookSync(event, payload);
878
+ } finally {
879
+ if (swallowEmpty) this.off("error", ignoreHookError);
880
+ }
881
+ return;
882
+ }
883
+ try {
884
+ this.hookSync(event, payload);
885
+ } catch (error) {
886
+ throw new HookRenderError(error);
887
+ }
888
+ }
889
+ /**
830
890
  * Write content to a file asynchronously, creating directories if needed
831
891
  * @private
832
892
  * @param {string} [filePath] - The path to write the file to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecto",
3
- "version": "5.1.0",
3
+ "version": "5.1.2",
4
4
  "description": "Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars",
5
5
  "type": "module",
6
6
  "main": "./dist/ecto.mjs",
@@ -42,30 +42,30 @@
42
42
  "liquidjs"
43
43
  ],
44
44
  "dependencies": {
45
- "@jaredwray/fumanchu": "^4.7.2",
45
+ "@jaredwray/fumanchu": "^4.7.4",
46
46
  "cacheable": "^2.5.0",
47
- "ejs": "^6.0.1",
48
- "hookified": "^3.0.2",
49
- "liquidjs": "^10.29.0",
47
+ "ejs": "^7.0.2",
48
+ "hookified": "^3.0.4",
49
+ "liquidjs": "^10.30.0",
50
50
  "nunjucks": "^3.2.4",
51
51
  "pug": "^3.0.4",
52
- "writr": "^6.1.5"
52
+ "writr": "^6.1.6"
53
53
  },
54
54
  "devDependencies": {
55
- "@biomejs/biome": "^2.5.9",
55
+ "@biomejs/biome": "^2.5.15",
56
56
  "@faker-js/faker": "^10.6.0",
57
57
  "@types/ejs": "^3.1.5",
58
- "@types/node": "^24.13.3",
58
+ "@types/node": "^24.19.2",
59
59
  "@types/nunjucks": "^3.2.6",
60
60
  "@types/pug": "^2.0.10",
61
- "@vitest/coverage-v8": "^4.1.11",
62
- "docula": "^3.0.0",
61
+ "@vitest/coverage-v8": "^5.0.5",
62
+ "docula": "^3.0.2",
63
63
  "rimraf": "^6.1.3",
64
- "tsdown": "^0.22.14",
64
+ "tsdown": "^0.23.1",
65
65
  "typescript": "^7.0.2",
66
- "vite": "^8.2.1",
67
- "vitest": "^4.1.11",
68
- "wrangler": "^4.124.0"
66
+ "vite": "^8.3.4",
67
+ "vitest": "^5.0.5",
68
+ "wrangler": "^4.151.2"
69
69
  },
70
70
  "files": [
71
71
  "dist",