Skip to content

Commit b7ada12

Browse files
beatytRomakita
authored andcommitted
feat(engines): add eta engine
1 parent 5306644 commit b7ada12

10 files changed

Lines changed: 136 additions & 2 deletions

File tree

docs/docs/templating.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ If `dustjs-helpers` is installed, `dustjs-linkedin` will not be used by consolid
8484
| [dust](https://github.com/linkedin/dustjs) | [`npm install dustjs-helpers`](https://www.npmjs.com/package/dustjs-helpers) (2) or<br>[`npm install dustjs-linkedin`](https://www.npmjs.com/package/dustjs-linkedin) (3) | [(website)](http://linkedin.github.io/dustjs/) |
8585
| [ect](https://github.com/baryshev/ect) | [`npm install ect`](https://www.npmjs.com/package/ect) | [(website)](http://ectjs.com/) |
8686
| [ejs](https://github.com/mde/ejs) | [`npm install ejs`](https://www.npmjs.com/package/ejs) | [(website)](http://ejs.co/) |
87+
| [eta](https://eta.js.org/) | [`npm install eta`](https://www.npmjs.com/package/eta) | [(website)](http://ejs.co/) |
8788
| [hamlet](https://github.com/gregwebs/hamlet.js) | [`npm install hamlet`](https://www.npmjs.com/package/hamlet) | - |
8889
| [hamljs](https://github.com/visionmedia/haml.js) | [`npm install hamljs`](https://www.npmjs.com/package/hamljs) | - |
8990
| [haml-coffee](https://github.com/netzpirat/haml-coffee) | [`npm install haml-coffee`](https://www.npmjs.com/package/haml-coffee) | - |

packages/engines/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"ect": "^0.5.9",
4545
"ejs": "^3.1.6",
4646
"eslint": "^8.12.0",
47+
"eta": "3.2.0",
4748
"haml": "^0.4.3",
4849
"haml-coffee": "^1.14.1",
4950
"hamlet": "^0.3.3",

packages/engines/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ If `dustjs-helpers` is installed, `dustjs-linkedin` will not be used by consolid
5555
| [dust](https://github.com/linkedin/dustjs) | [`npm install dustjs-helpers`](https://www.npmjs.com/package/dustjs-helpers) (2) or<br>[`npm install dustjs-linkedin`](https://www.npmjs.com/package/dustjs-linkedin) (3) | [(website)](http://linkedin.github.io/dustjs/) |
5656
| [ect](https://github.com/baryshev/ect) | [`npm install ect`](https://www.npmjs.com/package/ect) | [(website)](http://ectjs.com/) |
5757
| [ejs](https://github.com/mde/ejs) | [`npm install ejs`](https://www.npmjs.com/package/ejs) | [(website)](http://ejs.co/) |
58+
| [eta](https://eta.js.org/) | [`npm install eta`](https://www.npmjs.com/package/eta) | [(website)](http://ejs.co/) |
5859
| [hamlet](https://github.com/gregwebs/hamlet.js) | [`npm install hamlet`](https://www.npmjs.com/package/hamlet) | - |
5960
| [hamljs](https://github.com/visionmedia/haml.js) | [`npm install hamljs`](https://www.npmjs.com/package/hamljs) | - |
6061
| [haml-coffee](https://github.com/netzpirat/haml-coffee) | [`npm install haml-coffee`](https://www.npmjs.com/package/haml-coffee) | - |

packages/engines/src/components/Engine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {Type} from "@tsed/core";
12
import {cache, getCachedEngine, importEngine, read, readPartials} from "../utils/cache";
23

34
export interface ViewEngineOptions {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {expect} from "chai";
2+
import {renderFile} from "ejs";
3+
import {dirname} from "node:path";
4+
import {getEngineFixture} from "../../test/getEngineFixture";
5+
import {EtaEngine} from "./EtaEngine";
6+
7+
describe("EtaEngine", () => {
8+
it("should render the given content", async () => {
9+
const {render, locals, $compile, template} = await getEngineFixture({
10+
token: EtaEngine
11+
});
12+
13+
await render();
14+
15+
expect(await render()).to.eq("<p>Tobi</p>\n");
16+
expect($compile()).to.have.been.callCount(2);
17+
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false});
18+
});
19+
20+
it("should render the given content (by string - no cache)", async () => {
21+
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine});
22+
await render();
23+
24+
expect(await render()).to.eq("<p>Tobi</p>\n");
25+
expect($compile()).to.have.been.callCount(2);
26+
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false});
27+
});
28+
it("should render the given content (by string - with cache)", async () => {
29+
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine, cache: true});
30+
await render();
31+
32+
expect(await render()).to.eq("<p>Tobi</p>\n");
33+
expect($compile()).to.have.been.callCount(2);
34+
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: true});
35+
});
36+
it("should render the given content (by file - no cache)", async () => {
37+
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({token: EtaEngine, useTemplateName: true});
38+
39+
await renderFile({
40+
views: dirname(path)
41+
});
42+
43+
const content = await renderFile({
44+
views: dirname(path)
45+
});
46+
47+
expect(content).to.eq("<p>Tobi</p>\n");
48+
expect($compileFile()).to.have.been.callCount(2);
49+
expect($compileFile()).to.have.been.calledWithExactly("user", {
50+
...locals,
51+
partials: undefined,
52+
filename: "user",
53+
cache: false,
54+
views: dirname(path)
55+
});
56+
});
57+
it("should render the given content (by file - with cache)", async () => {
58+
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({
59+
token: EtaEngine,
60+
useTemplateName: true,
61+
cache: true
62+
});
63+
64+
await renderFile({
65+
views: dirname(path)
66+
});
67+
const content = await renderFile();
68+
69+
expect(content).to.eq("<p>Tobi</p>\n");
70+
expect($compileFile()).to.have.been.callCount(1);
71+
expect($compileFile()).to.have.been.calledWithExactly("user", {
72+
...locals,
73+
partials: undefined,
74+
filename: "user",
75+
cache: true,
76+
views: dirname(path)
77+
});
78+
});
79+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {ViewEngine} from "../decorators/viewEngine";
2+
import {Engine, EngineOptions} from "./Engine";
3+
4+
@ViewEngine("eta", {
5+
requires: "eta"
6+
})
7+
export class EtaEngine extends Engine {
8+
protected cache = new Map();
9+
10+
protected getEngine(options: EngineOptions) {
11+
const root = options.root || options.views;
12+
const key = String(root || "default");
13+
14+
if (!this.cache.get(key)) {
15+
const {Eta} = this.engine;
16+
17+
this.cache.set(
18+
key,
19+
new Eta({
20+
...options,
21+
views: root
22+
})
23+
);
24+
}
25+
26+
return this.cache.get(key);
27+
}
28+
29+
protected $compile(template: string, options: any) {
30+
const eta = this.getEngine(options);
31+
32+
return () => eta.renderStringAsync(template, options);
33+
}
34+
35+
protected $compileFile(file: string, options: EngineOptions): Promise<(options: EngineOptions) => Promise<string>> {
36+
const eta = this.getEngine(options);
37+
38+
return Promise.resolve(() => eta.renderAsync(file, options));
39+
}
40+
}

packages/engines/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from "./components/DotEngine";
88
export * from "./components/DustEngine";
99
export * from "./components/EctEngine";
1010
export * from "./components/EjsEngine";
11+
export * from "./components/EtaEngine";
1112
export * from "./components/Engine";
1213
export * from "./components/HamlCoffeeEngine";
1314
export * from "./components/HamlEngine";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><%= it.user.name %></p>

packages/engines/test/getEngineFixture.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ interface EngineFixtureOptions {
1212
token: string | typeof Engine;
1313
cache?: boolean;
1414
templateName?: string;
15+
useTemplateName?: boolean;
1516
}
1617

17-
export async function getEngineFixture({token, cache = false, templateName = "user"}: EngineFixtureOptions) {
18+
export async function getEngineFixture({token, cache = false, useTemplateName = false, templateName = "user"}: EngineFixtureOptions) {
1819
const engine = engines.get(token)!;
1920

2021
await engine.$onInit();
@@ -54,7 +55,7 @@ export async function getEngineFixture({token, cache = false, templateName = "us
5455
});
5556
},
5657
renderFile(options: any = {}) {
57-
return engine.renderFile(path, {
58+
return engine.renderFile(useTemplateName ? templateName : path, {
5859
cache,
5960
...locals,
6061
...options

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6532,6 +6532,7 @@ __metadata:
65326532
ect: "npm:^0.5.9"
65336533
ejs: "npm:^3.1.6"
65346534
eslint: "npm:^8.12.0"
6535+
eta: "npm:3.2.0"
65356536
filedirname: "npm:^2.7.0"
65366537
fs-extra: "npm:11.1.1"
65376538
haml: "npm:^0.4.3"
@@ -14965,6 +14966,13 @@ __metadata:
1496514966
languageName: node
1496614967
linkType: hard
1496714968

14969+
"eta@npm:3.2.0":
14970+
version: 3.2.0
14971+
resolution: "eta@npm:3.2.0"
14972+
checksum: 10/f4aa8ff6b9a4adf3d5db83255793be0136bdbcf2341154ad64c019bacc04d69e10218e54764af572eb8ba016996b8cbac9dc7a6fc2e9eb5005a0323bab883692
14973+
languageName: node
14974+
linkType: hard
14975+
1496814976
"eta@npm:^2.0.1":
1496914977
version: 2.0.1
1497014978
resolution: "eta@npm:2.0.1"

0 commit comments

Comments
 (0)