Skip to content

Commit

Permalink
fix(common): Fix views extension mapping and add cache in production …
Browse files Browse the repository at this point in the history
…profile
  • Loading branch information
Romakita committed Oct 1, 2020
1 parent b6ca396 commit 487456f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/socketio/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const rootDir = __dirname;
}],
socketIO: {},
views: {
path: `${rootDir}/../views`,
root: `${rootDir}/../views`,
viewEngine: "ejs",
extensions: {
html: "ejs"
Expand Down
19 changes: 18 additions & 1 deletion packages/common/src/config/interfaces/PlatformViewsSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,25 @@ export type PlatformViewsEngineOptions = {
};

export interface PlatformViewsSettings {
path?: string;
/**
* Views directory.
*/
root?: string;
/**
* Enable cache. Ts.ED enable cache in PRODUCTION profile by default.
*/
cache?: boolean;
/**
* Provide extensions mapping to match the expected engines.
*/
extensions?: Partial<PlatformViewsExtensionsTypes>;
/**
* Default view engine extension.
* Allow omitting extension when using View decorator or render method.
*/
viewEngine?: string;
/**
* Options mapping for each engine.
*/
options?: Partial<PlatformViewsEngineOptions>;
}
3 changes: 2 additions & 1 deletion packages/common/src/platform/services/PlatformViews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("PlatformViews", () => {
const result = await platformViews.render("views.ejs");

expect(result).to.equal("HTML");
expect(platformViews.consolidate.ejs).to.have.been.calledWithExactly("views.ejs", {global: "global"});
expect(platformViews.consolidate.ejs).to.have.been.calledWithExactly("views.ejs", {cache: false, global: "global"});
});
it("should render a template without extension", async () => {
const platformViews = PlatformTest.get<PlatformViews>(PlatformViews);
Expand All @@ -43,6 +43,7 @@ describe("PlatformViews", () => {

expect(result).to.equal("HTML");
expect(platformViews.consolidate.ejs).to.have.been.calledWithExactly("views.ejs", {
cache: false,
global: "global",
test: "test"
});
Expand Down
20 changes: 12 additions & 8 deletions packages/common/src/platform/services/PlatformViews.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Env} from "@tsed/core";
import {Constant, Injectable} from "@tsed/di";
import * as cons from "consolidate";
import * as Fs from "fs";
Expand All @@ -13,8 +14,14 @@ import {
*/
@Injectable()
export class PlatformViews {
@Constant("views.path", `${process.cwd()}/views`)
readonly path: string;
@Constant("env")
env: Env;

@Constant("views.root", `${process.cwd()}/views`)
readonly root: string;

@Constant("views.cache")
readonly cache: boolean;

@Constant("views.viewEngine", "ejs")
readonly viewEngine: string;
Expand All @@ -30,7 +37,7 @@ export class PlatformViews {
$onInit() {
this.extensionsMap = new Map(
Object.entries({
hsb: "handlebars",
hbs: "handlebars",
ejs: "ejs",
...this.extensions
})
Expand Down Expand Up @@ -69,10 +76,7 @@ export class PlatformViews {
throw new Error(`Engine not found for the ".${extension}" file extension`);
}

return render(this.resolve(viewPath), {
...engineOptions,
...options
});
return render(this.resolve(viewPath), Object.assign({cache: this.cache || this.env === Env.PROD}, engineOptions, options));
}

protected resolve(viewPath: string) {
Expand All @@ -83,7 +87,7 @@ export class PlatformViews {
return (
[
viewPath,
resolve(join(this.path, viewPath)),
resolve(join(this.root, viewPath)),
resolve(join(process.cwd(), "views", viewPath)),
resolve(join(process.cwd(), "public", viewPath))
].find((file) => Fs.existsSync(file)) || viewPath
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-express/test/app/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const rootDir = __dirname;
"/": `${rootDir}/public`
},
views: {
path: `${rootDir}/views`,
root: `${rootDir}/views`,
extensions: {
ejs: "ejs"
}
Expand Down
1 change: 0 additions & 1 deletion packages/platform-koa/src/services/PlatformKoaRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from "@tsed/common";
import {Configuration, Inject} from "@tsed/di";
import * as send from "koa-send";
import {resolve} from "path";
import {staticsMiddleware} from "../middlewares/staticsMiddleware";
import {getMulter} from "../utils/multer";

Expand Down
2 changes: 1 addition & 1 deletion packages/platform-koa/test/app/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const rootDir = __dirname;
"/": `${rootDir}/public`
},
views: {
path: `${rootDir}/views`,
root: `${rootDir}/views`,
extensions: {
ejs: "ejs"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/socketio/test/app/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const rootDir = resolve(__dirname);
"/": "${rootDir}/views"
},
views: {
path: `${rootDir}/views`,
root: `${rootDir}/views`,
extensions: {
html: "ejs"
},
Expand Down

0 comments on commit 487456f

Please sign in to comment.