Skip to content

Commit 11eff10

Browse files
beatytRomakita
authored andcommitted
feat(engines): add eta engine
1 parent 37cdc0c commit 11eff10

72 files changed

Lines changed: 352 additions & 215 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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) | - |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,4 @@
220220
]
221221
},
222222
"packageManager": "yarn@4.1.0"
223-
}
223+
}

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
"jest": "^29.2.0"
3333
},
3434
"peerDependencies": {}
35-
}
35+
}

packages/di/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
"optional": false
5050
}
5151
}
52-
}
52+
}

packages/engines/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"ect": "^0.5.9",
4242
"ejs": "^3.1.6",
4343
"eslint": "^8.12.0",
44-
"filedirname": "^2.7.0",
44+
"eta": "3.2.0",
4545
"haml": "^0.4.3",
4646
"haml-coffee": "^1.14.1",
4747
"hamlet": "^0.3.3",
@@ -86,6 +86,5 @@
8686
"decorators",
8787
"engines",
8888
"view"
89-
],
90-
"peerDependencies": {}
91-
}
89+
]
90+
}

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

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";

0 commit comments

Comments
 (0)