Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add eta engine #2557

Open
wants to merge 2 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ If `dustjs-helpers` is installed, `dustjs-linkedin` will not be used by consolid
| [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/) |
| [ect](https://github.com/baryshev/ect) | [`npm install ect`](https://www.npmjs.com/package/ect) | [(website)](http://ectjs.com/) |
| [ejs](https://github.com/mde/ejs) | [`npm install ejs`](https://www.npmjs.com/package/ejs) | [(website)](http://ejs.co/) |
| [eta](https://eta.js.org/) | [`npm install eta`](https://www.npmjs.com/package/eta) | [(website)](http://ejs.co/) |
| [hamlet](https://github.com/gregwebs/hamlet.js) | [`npm install hamlet`](https://www.npmjs.com/package/hamlet) | - |
| [hamljs](https://github.com/visionmedia/haml.js) | [`npm install hamljs`](https://www.npmjs.com/package/hamljs) | - |
| [haml-coffee](https://github.com/netzpirat/haml-coffee) | [`npm install haml-coffee`](https://www.npmjs.com/package/haml-coffee) | - |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@
]
},
"packageManager": "yarn@4.1.0"
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
"jest": "^29.2.0"
},
"peerDependencies": {}
}
}
2 changes: 1 addition & 1 deletion packages/di/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
"optional": false
}
}
}
}
7 changes: 3 additions & 4 deletions packages/engines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ect": "^0.5.9",
"ejs": "^3.1.6",
"eslint": "^8.12.0",
"filedirname": "^2.7.0",
"eta": "3.2.0",
"haml": "^0.4.3",
"haml-coffee": "^1.14.1",
"hamlet": "^0.3.3",
Expand Down Expand Up @@ -86,6 +86,5 @@
"decorators",
"engines",
"view"
],
"peerDependencies": {}
}
]
}
1 change: 1 addition & 0 deletions packages/engines/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ If `dustjs-helpers` is installed, `dustjs-linkedin` will not be used by consolid
| [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/) |
| [ect](https://github.com/baryshev/ect) | [`npm install ect`](https://www.npmjs.com/package/ect) | [(website)](http://ectjs.com/) |
| [ejs](https://github.com/mde/ejs) | [`npm install ejs`](https://www.npmjs.com/package/ejs) | [(website)](http://ejs.co/) |
| [eta](https://eta.js.org/) | [`npm install eta`](https://www.npmjs.com/package/eta) | [(website)](http://ejs.co/) |
| [hamlet](https://github.com/gregwebs/hamlet.js) | [`npm install hamlet`](https://www.npmjs.com/package/hamlet) | - |
| [hamljs](https://github.com/visionmedia/haml.js) | [`npm install hamljs`](https://www.npmjs.com/package/hamljs) | - |
| [haml-coffee](https://github.com/netzpirat/haml-coffee) | [`npm install haml-coffee`](https://www.npmjs.com/package/haml-coffee) | - |
Expand Down
1 change: 1 addition & 0 deletions packages/engines/src/components/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Type} from "@tsed/core";
import {cache, getCachedEngine, importEngine, read, readPartials} from "../utils/cache";

export interface ViewEngineOptions {
Expand Down
80 changes: 80 additions & 0 deletions packages/engines/src/components/EtaEngine.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {expect} from "chai";
import {dirname} from "node:path";
import {getEngineFixture} from "../../test/getEngineFixture";
import {EtaEngine} from "./EtaEngine";

describe("EtaEngine", () => {
it("should render the given content", async () => {
const {render, locals, $compile, template} = await getEngineFixture({
token: EtaEngine
});

await render();

expect(await render()).to.eq("<p>Tobi</p>\n");
expect($compile()).to.have.been.callCount(2);
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false});
});

it("should render the given content (by string - no cache)", async () => {
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine});
await render();

expect(await render()).to.eq("<p>Tobi</p>\n");
expect($compile()).to.have.been.callCount(2);
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false});
});
it("should render the given content (by string - with cache)", async () => {
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine, cache: true});
await render();

expect(await render()).to.eq("<p>Tobi</p>\n");
expect($compile()).to.have.been.callCount(2);
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: true});
});
it("should render the given content (by file - no cache)", async () => {
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({
token: EtaEngine,
useTemplateName: true
});

await renderFile({
views: dirname(path)
});

const content = await renderFile({
views: dirname(path)
});

expect(content).to.eq("<p>Tobi</p>\n");
expect($compileFile()).to.have.been.callCount(2);
expect($compileFile()).to.have.been.calledWithExactly("user", {
...locals,
partials: undefined,
filename: "user",
cache: false,
views: dirname(path)
});
});
it("should render the given content (by file - with cache - with entire file path)", async () => {
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({
token: EtaEngine,
cache: true
});

await renderFile({
root: dirname(path)
});
const content = await renderFile();

expect(content).to.eq("<p>Tobi</p>\n");
expect($compileFile()).to.have.been.callCount(1);
expect($compileFile()).to.have.been.calledWithExactly(path, {
cache: true,
user: {name: "Tobi"},
root: dirname(path),
filename: path,
partials: undefined
});
});
});
45 changes: 45 additions & 0 deletions packages/engines/src/components/EtaEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {ViewEngine} from "../decorators/viewEngine";
import {Engine, EngineOptions} from "./Engine";

@ViewEngine("eta", {
requires: "eta"
})
export class EtaEngine extends Engine {
protected cache = new Map();

protected getEngine(options: EngineOptions) {
const root = options.root;
const key = String(root || "default");

if (!this.cache.get(key)) {
const {Eta} = this.engine;

this.cache.set(
key,
new Eta({
...options,
views: root
})
);
}

return this.cache.get(key);
}

protected $compile(template: string, options: any) {
const eta = this.getEngine(options);

return () => eta.renderStringAsync(template, options);
}

protected $compileFile(file: string, options: EngineOptions): Promise<(options: EngineOptions) => Promise<string>> {
const root = options.views || options.root;
const templateName = file.replace(root, "");
const eta = this.getEngine({
...options,
root
});

return Promise.resolve(() => eta.renderAsync(templateName, options));
}
}
1 change: 1 addition & 0 deletions packages/engines/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./components/DotEngine";
export * from "./components/DustEngine";
export * from "./components/EctEngine";
export * from "./components/EjsEngine";
export * from "./components/EtaEngine";
export * from "./components/Engine";
export * from "./components/HamlCoffeeEngine";
export * from "./components/HamlEngine";
Expand Down
1 change: 1 addition & 0 deletions packages/engines/test/fixtures/eta/user.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= it.user.name %></p>
5 changes: 3 additions & 2 deletions packages/engines/test/getEngineFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ interface EngineFixtureOptions {
token: string | typeof Engine;
cache?: boolean;
templateName?: string;
useTemplateName?: boolean;
}

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

await engine.$onInit();
Expand Down Expand Up @@ -54,7 +55,7 @@ export async function getEngineFixture({token, cache = false, templateName = "us
});
},
renderFile(options: any = {}) {
return engine.renderFile(path, {
return engine.renderFile(useTemplateName ? templateName : path, {
cache,
...locals,
...options
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
"apollo-server-core": ">=3.0.0",
"graphql": ">15.0.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/graphql/graphql-ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"@tsed/logger": ">=6.2.2",
"graphql-ws": ">=5.14.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/graphql/typegraphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
"graphql": ">=15.0.0",
"type-graphql": ">=1.0.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/adapters-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"ioredis-mock": ">=8.2.2",
"uuid": "8.3.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/adapters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/ioredis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"ioredis": ">=5.2.3",
"ioredis-mock": ">=8.2.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/mikro-orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"@tsed/di": "7.61.2",
"@tsed/logger": ">=6.2.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
"@tsed/schema": "7.61.2",
"mongoose": ">=6.0.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/objection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"tsed",
"prisma"
]
}
}
2 changes: 1 addition & 1 deletion packages/orm/testing-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
"@tsed/mongoose": "7.61.2",
"mongoose": ">=6.0.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/orm/typeorm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
"publishConfig": {
"distTag": "deprecated"
}
}
}
2 changes: 1 addition & 1 deletion packages/perf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
"jest": "^29.2.0"
},
"peerDependencies": {}
}
}
2 changes: 1 addition & 1 deletion packages/platform/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-exceptions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-log-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-middlewares/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"optional": true
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-params/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-response-filter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-serverless-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
"optional": false
}
}
}
}
2 changes: 1 addition & 1 deletion packages/platform/platform-serverless-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
"@tsed/openspec": "7.61.2",
"@tsed/schema": "7.61.2"
}
}
}
Loading
Loading