@tsed/platform-views 7.0.0-alpha.2 → 7.0.0-beta.11

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.
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.View = void 0;
4
+ var schema_1 = require("@tsed/schema");
5
+ Object.defineProperty(exports, "View", { enumerable: true, get: function () { return schema_1.View; } });
6
+ //# sourceMappingURL=view.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/decorators/view.ts"],"names":[],"mappings":";;;AAAA,uCAAkC;AAA1B,8FAAA,IAAI,OAAA","sourcesContent":["export {View} from \"@tsed/schema\";\n"]}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PLATFORM_VIEWS_EXTENSIONS = void 0;
4
+ exports.PLATFORM_VIEWS_EXTENSIONS = {
5
+ atpl: "atpl",
6
+ bracket: "bracket",
7
+ dot: "dot",
8
+ dust: "dust",
9
+ ect: "ect",
10
+ ejs: "ejs",
11
+ haml: "haml",
12
+ "haml-coffee": "haml-coffee",
13
+ hamlet: "hamlet",
14
+ hbs: "handlebars",
15
+ handlebars: "handlebars",
16
+ hogan: "hogan",
17
+ htmling: "htmling",
18
+ jazz: "jazz",
19
+ jqtpl: "jqtpl",
20
+ just: "just",
21
+ kernel: "kernel",
22
+ liquid: "liquid",
23
+ liquor: "liquor",
24
+ lodash: "lodash",
25
+ mote: "mote",
26
+ mustache: "mustache",
27
+ nunjucks: "nunjucks",
28
+ plates: "plates",
29
+ pug: "pug",
30
+ qejs: "qejs",
31
+ ractive: "ractive",
32
+ razor: "razor",
33
+ jsx: "react",
34
+ slm: "slm",
35
+ squirelly: "squirelly",
36
+ swig: "swig",
37
+ teacup: "teacup",
38
+ templayed: "templayed",
39
+ toffee: "toffee",
40
+ twig: "twig",
41
+ underscore: "underscore",
42
+ vash: "vash",
43
+ velocityjs: "velocityjs",
44
+ walrus: "walrus",
45
+ whiskers: "whiskers"
46
+ };
47
+ //# sourceMappingURL=PlatformViewsSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformViewsSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformViewsSettings.ts"],"names":[],"mappings":";;;AAEa,QAAA,yBAAyB,GAAG;IACvC,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,aAAa,EAAE,aAAa;IAC5B,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,YAAY;IACjB,UAAU,EAAE,YAAY;IACxB,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,KAAK;IACV,SAAS,EAAE,WAAW;IACtB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,SAAS,EAAE,WAAW;IACtB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;CACrB,CAAC","sourcesContent":["import {BaseContext} from \"@tsed/di\";\n\nexport const PLATFORM_VIEWS_EXTENSIONS = {\n atpl: \"atpl\",\n bracket: \"bracket\",\n dot: \"dot\",\n dust: \"dust\",\n ect: \"ect\",\n ejs: \"ejs\",\n haml: \"haml\",\n \"haml-coffee\": \"haml-coffee\",\n hamlet: \"hamlet\",\n hbs: \"handlebars\",\n handlebars: \"handlebars\",\n hogan: \"hogan\",\n htmling: \"htmling\",\n jazz: \"jazz\",\n jqtpl: \"jqtpl\",\n just: \"just\",\n kernel: \"kernel\",\n liquid: \"liquid\",\n liquor: \"liquor\",\n lodash: \"lodash\",\n mote: \"mote\",\n mustache: \"mustache\",\n nunjucks: \"nunjucks\",\n plates: \"plates\",\n pug: \"pug\",\n qejs: \"qejs\",\n ractive: \"ractive\",\n razor: \"razor\",\n jsx: \"react\",\n slm: \"slm\",\n squirelly: \"squirelly\",\n swig: \"swig\",\n teacup: \"teacup\",\n templayed: \"templayed\",\n toffee: \"toffee\",\n twig: \"twig\",\n underscore: \"underscore\",\n vash: \"vash\",\n velocityjs: \"velocityjs\",\n walrus: \"walrus\",\n whiskers: \"whiskers\"\n};\n\nexport type PlatformViewsExtensionsTypes = Record<string, string>;\n\nexport interface PlatformViewsEngineOptions extends Record<string, unknown> {\n requires?: any;\n}\n\nexport interface PlatformRenderOptions extends Record<string, unknown> {\n $ctx: BaseContext;\n}\n\nexport interface PlatformViewEngine {\n options: PlatformViewsEngineOptions;\n\n render(path: string, options: PlatformRenderOptions): Promise<string>;\n}\n\nexport interface PlatformViewsSettings {\n disabled?: boolean;\n /**\n * Views directory.\n */\n root?: string;\n /**\n * Enable cache. Ts.ED enable cache in PRODUCTION profile by default.\n */\n cache?: boolean;\n /**\n * Provide extensions mapping to match the expected engines.\n */\n extensions?: Partial<PlatformViewsExtensionsTypes>;\n /**\n * Default view engine extension.\n * Allow omitting extension when using View decorator or render method.\n */\n viewEngine?: string;\n /**\n * Options mapping for each engine.\n */\n options?: Record<string, PlatformViewsEngineOptions>;\n}\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n /**\n * Object to configure Views engines with Consolidate. See more on [View engine](/docs/template-engine.md).\n */\n views: PlatformViewsSettings;\n }\n }\n}\n"]}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ /**
3
+ * @file Automatically generated by barrelsby.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const tslib_1 = require("tslib");
7
+ tslib_1.__exportStar(require("./decorators/view"), exports);
8
+ tslib_1.__exportStar(require("./domain/PlatformViewsSettings"), exports);
9
+ tslib_1.__exportStar(require("./services/PlatformViews"), exports);
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,4DAAkC;AAClC,yEAA+C;AAC/C,mEAAyC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/view\";\nexport * from \"./domain/PlatformViewsSettings\";\nexport * from \"./services/PlatformViews\";\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var _PlatformViews_extensions, _PlatformViews_engines, _PlatformViews_cachePaths;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.PlatformViews = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const core_1 = require("@tsed/core");
7
+ const di_1 = require("@tsed/di");
8
+ const engines_1 = require("@tsed/engines");
9
+ const fs_1 = tslib_1.__importDefault(require("fs"));
10
+ const path_1 = require("path");
11
+ const PlatformViewsSettings_1 = require("../domain/PlatformViewsSettings");
12
+ async function patchEJS(ejs) {
13
+ if (!ejs) {
14
+ const mod = await Promise.resolve().then(() => tslib_1.__importStar(require("ejs")));
15
+ ejs = mod.default;
16
+ }
17
+ return {
18
+ compile(str, { client, ...options }) {
19
+ return ejs.compile(str, options);
20
+ }
21
+ };
22
+ }
23
+ /**
24
+ * @platform
25
+ */
26
+ let PlatformViews = class PlatformViews {
27
+ constructor() {
28
+ _PlatformViews_extensions.set(this, void 0);
29
+ _PlatformViews_engines.set(this, new Map());
30
+ _PlatformViews_cachePaths.set(this, new Map());
31
+ }
32
+ async $onInit() {
33
+ if (!this.disabled) {
34
+ tslib_1.__classPrivateFieldSet(this, _PlatformViews_extensions, new Map(Object.entries({
35
+ ...PlatformViewsSettings_1.PLATFORM_VIEWS_EXTENSIONS,
36
+ ...this.extensionsOptions
37
+ })), "f");
38
+ await this.loadEngines();
39
+ }
40
+ }
41
+ async loadEngines() {
42
+ engines_1.requires.set("ejs", await patchEJS(engines_1.requires.get("ejs")));
43
+ tslib_1.__classPrivateFieldGet(this, _PlatformViews_extensions, "f").forEach((engineType) => {
44
+ if (engines_1.engines.has(engineType)) {
45
+ const options = this.getEngineOptions(engineType);
46
+ if (options.requires) {
47
+ engines_1.requires.set(engineType, options.requires);
48
+ }
49
+ this.registerEngine(engineType, {
50
+ options,
51
+ render: (0, engines_1.getEngine)(engineType)
52
+ });
53
+ }
54
+ });
55
+ }
56
+ getEngines() {
57
+ return [...tslib_1.__classPrivateFieldGet(this, _PlatformViews_extensions, "f").entries()].map(([extension, engineType]) => {
58
+ const engine = this.getEngine(this.getExtension(engineType));
59
+ return {
60
+ extension,
61
+ engine
62
+ };
63
+ });
64
+ }
65
+ registerEngine(engineType, engine) {
66
+ tslib_1.__classPrivateFieldGet(this, _PlatformViews_engines, "f").set(engineType, engine);
67
+ return this;
68
+ }
69
+ getEngine(type) {
70
+ return tslib_1.__classPrivateFieldGet(this, _PlatformViews_engines, "f").get(tslib_1.__classPrivateFieldGet(this, _PlatformViews_extensions, "f").get(type) || type);
71
+ }
72
+ getEngineOptions(engineType) {
73
+ return (0, core_1.getValue)(this.engineOptions, engineType, {});
74
+ }
75
+ async render(viewPath, options = {}) {
76
+ const { path, extension } = tslib_1.__classPrivateFieldGet(this, _PlatformViews_cachePaths, "f").get(viewPath) || tslib_1.__classPrivateFieldGet(this, _PlatformViews_cachePaths, "f").set(viewPath, this.resolve(viewPath)).get(viewPath);
77
+ const engine = this.getEngine(extension);
78
+ if (!engine) {
79
+ throw new Error(`Engine not found to render the following "${viewPath}"`);
80
+ }
81
+ return engine.render(path, Object.assign({ cache: this.cache || this.env === core_1.Env.PROD }, engine.options, options));
82
+ }
83
+ getExtension(viewPath) {
84
+ return ((0, path_1.extname)(viewPath) || this.viewEngine).replace(/\./, "");
85
+ }
86
+ resolve(viewPath) {
87
+ const extension = this.getExtension(viewPath);
88
+ viewPath = viewPath.replace((0, path_1.extname)(viewPath), "") + "." + extension;
89
+ const path = [
90
+ viewPath,
91
+ (0, path_1.resolve)((0, path_1.join)(this.root, viewPath)),
92
+ (0, path_1.resolve)((0, path_1.join)(process.cwd(), "views", viewPath)),
93
+ (0, path_1.resolve)((0, path_1.join)(process.cwd(), "public", viewPath))
94
+ ].find((file) => fs_1.default.existsSync(file)) || viewPath;
95
+ return {
96
+ path,
97
+ extension
98
+ };
99
+ }
100
+ };
101
+ _PlatformViews_extensions = new WeakMap(), _PlatformViews_engines = new WeakMap(), _PlatformViews_cachePaths = new WeakMap();
102
+ tslib_1.__decorate([
103
+ (0, di_1.Constant)("env"),
104
+ tslib_1.__metadata("design:type", String)
105
+ ], PlatformViews.prototype, "env", void 0);
106
+ tslib_1.__decorate([
107
+ (0, di_1.Constant)("views.root", `${process.cwd()}/views`),
108
+ tslib_1.__metadata("design:type", String)
109
+ ], PlatformViews.prototype, "root", void 0);
110
+ tslib_1.__decorate([
111
+ (0, di_1.Constant)("views.cache"),
112
+ tslib_1.__metadata("design:type", Boolean)
113
+ ], PlatformViews.prototype, "cache", void 0);
114
+ tslib_1.__decorate([
115
+ (0, di_1.Constant)("views.disabled", false),
116
+ tslib_1.__metadata("design:type", String)
117
+ ], PlatformViews.prototype, "disabled", void 0);
118
+ tslib_1.__decorate([
119
+ (0, di_1.Constant)("views.viewEngine", "ejs"),
120
+ tslib_1.__metadata("design:type", String)
121
+ ], PlatformViews.prototype, "viewEngine", void 0);
122
+ tslib_1.__decorate([
123
+ (0, di_1.Constant)("views.extensions", {}),
124
+ tslib_1.__metadata("design:type", Object)
125
+ ], PlatformViews.prototype, "extensionsOptions", void 0);
126
+ tslib_1.__decorate([
127
+ (0, di_1.Constant)("views.options", {}),
128
+ tslib_1.__metadata("design:type", Object)
129
+ ], PlatformViews.prototype, "engineOptions", void 0);
130
+ PlatformViews = tslib_1.__decorate([
131
+ (0, di_1.Module)({
132
+ views: {
133
+ exists: true
134
+ }
135
+ })
136
+ ], PlatformViews);
137
+ exports.PlatformViews = PlatformViews;
138
+ //# sourceMappingURL=PlatformViews.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformViews.js","sourceRoot":"","sources":["../../../src/services/PlatformViews.ts"],"names":[],"mappings":";;;;;AAAA,qCAAyC;AACzC,iCAA0C;AAC1C,2CAA2D;AAC3D,oDAAoB;AACpB,+BAA4C;AAC5C,2EAKyC;AAEzC,KAAK,UAAU,QAAQ,CAAC,GAAQ;IAC9B,IAAI,CAAC,GAAG,EAAE;QACR,MAAM,GAAG,GAAG,gEAAa,KAAK,GAAC,CAAC;QAChC,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC;KACnB;IAED,OAAO;QACL,OAAO,CAAC,GAAW,EAAE,EAAC,MAAM,EAAE,GAAG,OAAO,EAAM;YAC5C,OAAO,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACnC,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;AAMH,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAsBE,4CAAiC;QACjC,iCAAW,IAAI,GAAG,EAA8B,EAAC;QACjD,oCAAc,IAAI,GAAG,EAA6C,EAAC;IA4FrE,CAAC;IA1FC,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YA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MAAM,EAAE,IAAI;SACb;KACF,CAAC;GACW,aAAa,CAoHzB;AApHY,sCAAa","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {Constant, Module} from \"@tsed/di\";\nimport {engines, getEngine, requires} from \"@tsed/engines\";\nimport Fs from \"fs\";\nimport {extname, join, resolve} from \"path\";\nimport {\n PLATFORM_VIEWS_EXTENSIONS,\n PlatformViewEngine,\n PlatformViewsEngineOptions,\n PlatformViewsExtensionsTypes\n} from \"../domain/PlatformViewsSettings\";\n\nasync function patchEJS(ejs: any) {\n if (!ejs) {\n const mod = await import(\"ejs\");\n ejs = mod.default;\n }\n\n return {\n compile(str: string, {client, ...options}: any) {\n return ejs.compile(str, options);\n }\n };\n}\n\n/**\n * @platform\n */\n@Module({\n views: {\n exists: true\n }\n})\nexport class PlatformViews {\n @Constant(\"env\")\n env: Env;\n\n @Constant(\"views.root\", `${process.cwd()}/views`)\n readonly root: string;\n\n @Constant(\"views.cache\")\n readonly cache: boolean;\n\n @Constant(\"views.disabled\", false)\n readonly disabled: string;\n\n @Constant(\"views.viewEngine\", \"ejs\")\n readonly viewEngine: string;\n\n @Constant(\"views.extensions\", {})\n protected extensionsOptions: PlatformViewsExtensionsTypes;\n\n @Constant(\"views.options\", {})\n protected engineOptions: Record<string, PlatformViewsEngineOptions>;\n\n #extensions: Map<string, string>;\n #engines = new Map<string, PlatformViewEngine>();\n #cachePaths = new Map<string, {path: string; extension: string}>();\n\n async $onInit() {\n if (!this.disabled) {\n this.#extensions = new Map(\n Object.entries({\n ...PLATFORM_VIEWS_EXTENSIONS,\n ...this.extensionsOptions\n })\n );\n\n await this.loadEngines();\n }\n }\n\n async loadEngines() {\n requires.set(\"ejs\", await patchEJS(requires.get(\"ejs\")));\n\n this.#extensions.forEach((engineType) => {\n if (engines.has(engineType)) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n requires.set(engineType, options.requires);\n }\n\n this.registerEngine(engineType, {\n options,\n render: getEngine(engineType)\n });\n }\n });\n }\n\n getEngines() {\n return [...this.#extensions.entries()].map(([extension, engineType]) => {\n const engine = this.getEngine(this.getExtension(engineType))!;\n\n return {\n extension,\n engine\n };\n });\n }\n\n registerEngine(engineType: string, engine: PlatformViewEngine) {\n this.#engines.set(engineType, engine);\n\n return this;\n }\n\n getEngine(type: string) {\n return this.#engines.get(this.#extensions.get(type) || type);\n }\n\n getEngineOptions(engineType: string): PlatformViewsEngineOptions {\n return getValue(this.engineOptions, engineType, {});\n }\n\n async render(viewPath: string, options: any = {}): Promise<string> {\n const {path, extension} = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath)!;\n const engine = this.getEngine(extension);\n\n if (!engine) {\n throw new Error(`Engine not found to render the following \"${viewPath}\"`);\n }\n\n return engine.render(path, Object.assign({cache: this.cache || this.env === Env.PROD}, engine.options, options));\n }\n\n protected getExtension(viewPath: string) {\n return (extname(viewPath) || this.viewEngine).replace(/\\./, \"\");\n }\n\n protected resolve(viewPath: string) {\n const extension = this.getExtension(viewPath);\n\n viewPath = viewPath.replace(extname(viewPath), \"\") + \".\" + extension;\n\n const path =\n [\n viewPath,\n resolve(join(this.root, viewPath)),\n resolve(join(process.cwd(), \"views\", viewPath)),\n resolve(join(process.cwd(), \"public\", viewPath))\n ].find((file) => Fs.existsSync(file)) || viewPath;\n\n return {\n path,\n extension\n };\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export { View } from "@tsed/schema";
2
+ //# sourceMappingURL=view.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/decorators/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC","sourcesContent":["export {View} from \"@tsed/schema\";\n"]}
@@ -0,0 +1,44 @@
1
+ export const PLATFORM_VIEWS_EXTENSIONS = {
2
+ atpl: "atpl",
3
+ bracket: "bracket",
4
+ dot: "dot",
5
+ dust: "dust",
6
+ ect: "ect",
7
+ ejs: "ejs",
8
+ haml: "haml",
9
+ "haml-coffee": "haml-coffee",
10
+ hamlet: "hamlet",
11
+ hbs: "handlebars",
12
+ handlebars: "handlebars",
13
+ hogan: "hogan",
14
+ htmling: "htmling",
15
+ jazz: "jazz",
16
+ jqtpl: "jqtpl",
17
+ just: "just",
18
+ kernel: "kernel",
19
+ liquid: "liquid",
20
+ liquor: "liquor",
21
+ lodash: "lodash",
22
+ mote: "mote",
23
+ mustache: "mustache",
24
+ nunjucks: "nunjucks",
25
+ plates: "plates",
26
+ pug: "pug",
27
+ qejs: "qejs",
28
+ ractive: "ractive",
29
+ razor: "razor",
30
+ jsx: "react",
31
+ slm: "slm",
32
+ squirelly: "squirelly",
33
+ swig: "swig",
34
+ teacup: "teacup",
35
+ templayed: "templayed",
36
+ toffee: "toffee",
37
+ twig: "twig",
38
+ underscore: "underscore",
39
+ vash: "vash",
40
+ velocityjs: "velocityjs",
41
+ walrus: "walrus",
42
+ whiskers: "whiskers"
43
+ };
44
+ //# sourceMappingURL=PlatformViewsSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformViewsSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformViewsSettings.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,aAAa,EAAE,aAAa;IAC5B,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,YAAY;IACjB,UAAU,EAAE,YAAY;IACxB,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,KAAK;IACV,SAAS,EAAE,WAAW;IACtB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,SAAS,EAAE,WAAW;IACtB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;CACrB,CAAC","sourcesContent":["import {BaseContext} from \"@tsed/di\";\n\nexport const PLATFORM_VIEWS_EXTENSIONS = {\n atpl: \"atpl\",\n bracket: \"bracket\",\n dot: \"dot\",\n dust: \"dust\",\n ect: \"ect\",\n ejs: \"ejs\",\n haml: \"haml\",\n \"haml-coffee\": \"haml-coffee\",\n hamlet: \"hamlet\",\n hbs: \"handlebars\",\n handlebars: \"handlebars\",\n hogan: \"hogan\",\n htmling: \"htmling\",\n jazz: \"jazz\",\n jqtpl: \"jqtpl\",\n just: \"just\",\n kernel: \"kernel\",\n liquid: \"liquid\",\n liquor: \"liquor\",\n lodash: \"lodash\",\n mote: \"mote\",\n mustache: \"mustache\",\n nunjucks: \"nunjucks\",\n plates: \"plates\",\n pug: \"pug\",\n qejs: \"qejs\",\n ractive: \"ractive\",\n razor: \"razor\",\n jsx: \"react\",\n slm: \"slm\",\n squirelly: \"squirelly\",\n swig: \"swig\",\n teacup: \"teacup\",\n templayed: \"templayed\",\n toffee: \"toffee\",\n twig: \"twig\",\n underscore: \"underscore\",\n vash: \"vash\",\n velocityjs: \"velocityjs\",\n walrus: \"walrus\",\n whiskers: \"whiskers\"\n};\n\nexport type PlatformViewsExtensionsTypes = Record<string, string>;\n\nexport interface PlatformViewsEngineOptions extends Record<string, unknown> {\n requires?: any;\n}\n\nexport interface PlatformRenderOptions extends Record<string, unknown> {\n $ctx: BaseContext;\n}\n\nexport interface PlatformViewEngine {\n options: PlatformViewsEngineOptions;\n\n render(path: string, options: PlatformRenderOptions): Promise<string>;\n}\n\nexport interface PlatformViewsSettings {\n disabled?: boolean;\n /**\n * Views directory.\n */\n root?: string;\n /**\n * Enable cache. Ts.ED enable cache in PRODUCTION profile by default.\n */\n cache?: boolean;\n /**\n * Provide extensions mapping to match the expected engines.\n */\n extensions?: Partial<PlatformViewsExtensionsTypes>;\n /**\n * Default view engine extension.\n * Allow omitting extension when using View decorator or render method.\n */\n viewEngine?: string;\n /**\n * Options mapping for each engine.\n */\n options?: Record<string, PlatformViewsEngineOptions>;\n}\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n /**\n * Object to configure Views engines with Consolidate. See more on [View engine](/docs/template-engine.md).\n */\n views: PlatformViewsSettings;\n }\n }\n}\n"]}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./decorators/view.js";
5
+ export * from "./domain/PlatformViewsSettings.js";
6
+ export * from "./services/PlatformViews.js";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,mBAAmB,CAAC;AAClC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,0BAA0B,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/view\";\nexport * from \"./domain/PlatformViewsSettings\";\nexport * from \"./services/PlatformViews\";\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,135 @@
1
+ var _PlatformViews_extensions, _PlatformViews_engines, _PlatformViews_cachePaths;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet, __decorate, __metadata } from "tslib";
3
+ import { Env, getValue } from "@tsed/core";
4
+ import { Constant, Module } from "@tsed/di";
5
+ import { engines, getEngine, requires } from "@tsed/engines";
6
+ import Fs from "fs";
7
+ import { extname, join, resolve } from "path";
8
+ import { PLATFORM_VIEWS_EXTENSIONS } from "../domain/PlatformViewsSettings.js";
9
+ async function patchEJS(ejs) {
10
+ if (!ejs) {
11
+ const mod = await import("ejs");
12
+ ejs = mod.default;
13
+ }
14
+ return {
15
+ compile(str, { client, ...options }) {
16
+ return ejs.compile(str, options);
17
+ }
18
+ };
19
+ }
20
+ /**
21
+ * @platform
22
+ */
23
+ let PlatformViews = class PlatformViews {
24
+ constructor() {
25
+ _PlatformViews_extensions.set(this, void 0);
26
+ _PlatformViews_engines.set(this, new Map());
27
+ _PlatformViews_cachePaths.set(this, new Map());
28
+ }
29
+ async $onInit() {
30
+ if (!this.disabled) {
31
+ __classPrivateFieldSet(this, _PlatformViews_extensions, new Map(Object.entries({
32
+ ...PLATFORM_VIEWS_EXTENSIONS,
33
+ ...this.extensionsOptions
34
+ })), "f");
35
+ await this.loadEngines();
36
+ }
37
+ }
38
+ async loadEngines() {
39
+ requires.set("ejs", await patchEJS(requires.get("ejs")));
40
+ __classPrivateFieldGet(this, _PlatformViews_extensions, "f").forEach((engineType) => {
41
+ if (engines.has(engineType)) {
42
+ const options = this.getEngineOptions(engineType);
43
+ if (options.requires) {
44
+ requires.set(engineType, options.requires);
45
+ }
46
+ this.registerEngine(engineType, {
47
+ options,
48
+ render: getEngine(engineType)
49
+ });
50
+ }
51
+ });
52
+ }
53
+ getEngines() {
54
+ return [...__classPrivateFieldGet(this, _PlatformViews_extensions, "f").entries()].map(([extension, engineType]) => {
55
+ const engine = this.getEngine(this.getExtension(engineType));
56
+ return {
57
+ extension,
58
+ engine
59
+ };
60
+ });
61
+ }
62
+ registerEngine(engineType, engine) {
63
+ __classPrivateFieldGet(this, _PlatformViews_engines, "f").set(engineType, engine);
64
+ return this;
65
+ }
66
+ getEngine(type) {
67
+ return __classPrivateFieldGet(this, _PlatformViews_engines, "f").get(__classPrivateFieldGet(this, _PlatformViews_extensions, "f").get(type) || type);
68
+ }
69
+ getEngineOptions(engineType) {
70
+ return getValue(this.engineOptions, engineType, {});
71
+ }
72
+ async render(viewPath, options = {}) {
73
+ const { path, extension } = __classPrivateFieldGet(this, _PlatformViews_cachePaths, "f").get(viewPath) || __classPrivateFieldGet(this, _PlatformViews_cachePaths, "f").set(viewPath, this.resolve(viewPath)).get(viewPath);
74
+ const engine = this.getEngine(extension);
75
+ if (!engine) {
76
+ throw new Error(`Engine not found to render the following "${viewPath}"`);
77
+ }
78
+ return engine.render(path, Object.assign({ cache: this.cache || this.env === Env.PROD }, engine.options, options));
79
+ }
80
+ getExtension(viewPath) {
81
+ return (extname(viewPath) || this.viewEngine).replace(/\./, "");
82
+ }
83
+ resolve(viewPath) {
84
+ const extension = this.getExtension(viewPath);
85
+ viewPath = viewPath.replace(extname(viewPath), "") + "." + extension;
86
+ const path = [
87
+ viewPath,
88
+ resolve(join(this.root, viewPath)),
89
+ resolve(join(process.cwd(), "views", viewPath)),
90
+ resolve(join(process.cwd(), "public", viewPath))
91
+ ].find((file) => Fs.existsSync(file)) || viewPath;
92
+ return {
93
+ path,
94
+ extension
95
+ };
96
+ }
97
+ };
98
+ _PlatformViews_extensions = new WeakMap(), _PlatformViews_engines = new WeakMap(), _PlatformViews_cachePaths = new WeakMap();
99
+ __decorate([
100
+ Constant("env"),
101
+ __metadata("design:type", String)
102
+ ], PlatformViews.prototype, "env", void 0);
103
+ __decorate([
104
+ Constant("views.root", `${process.cwd()}/views`),
105
+ __metadata("design:type", String)
106
+ ], PlatformViews.prototype, "root", void 0);
107
+ __decorate([
108
+ Constant("views.cache"),
109
+ __metadata("design:type", Boolean)
110
+ ], PlatformViews.prototype, "cache", void 0);
111
+ __decorate([
112
+ Constant("views.disabled", false),
113
+ __metadata("design:type", String)
114
+ ], PlatformViews.prototype, "disabled", void 0);
115
+ __decorate([
116
+ Constant("views.viewEngine", "ejs"),
117
+ __metadata("design:type", String)
118
+ ], PlatformViews.prototype, "viewEngine", void 0);
119
+ __decorate([
120
+ Constant("views.extensions", {}),
121
+ __metadata("design:type", Object)
122
+ ], PlatformViews.prototype, "extensionsOptions", void 0);
123
+ __decorate([
124
+ Constant("views.options", {}),
125
+ __metadata("design:type", Object)
126
+ ], PlatformViews.prototype, "engineOptions", void 0);
127
+ PlatformViews = __decorate([
128
+ Module({
129
+ views: {
130
+ exists: true
131
+ }
132
+ })
133
+ ], PlatformViews);
134
+ export { PlatformViews };
135
+ //# sourceMappingURL=PlatformViews.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformViews.js","sourceRoot":"","sources":["../../../src/services/PlatformViews.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAC,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAC,MAAM,MAAM,CAAC;AAC5C,OAAO,EACL,yBAAyB,EAI1B,MAAM,iCAAiC,CAAC;AAEzC,KAAK,UAAU,QAAQ,CAAC,GAAQ;IAC9B,IAAI,CAAC,GAAG,EAAE;QACR,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,CAAC;QAChC,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC;KACnB;IAED,OAAO;QACL,OAAO,CAAC,GAAW,EAAE,EAAC,MAAM,EAAE,GAAG,OAAO,EAAM;YAC5C,OAAO,GAAG,CAAC,OAAO,C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AAK,CAAC;;iDACR;AAG5B;IADC,QAAQ,CAAC,kBAAkB,EAAE,EAAE,CAAC;;wDACyB;AAG1D;IADC,QAAQ,CAAC,eAAe,EAAE,EAAE,CAAC;;oDACsC;AApBzD,aAAa;IALzB,MAAM,CAAC;QACN,KAAK,EAAE;YACL,MAAM,EAAE,IAAI;SACb;KACF,CAAC;GACW,aAAa,CAoHzB;SApHY,aAAa","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {Constant, Module} from \"@tsed/di\";\nimport {engines, getEngine, requires} from \"@tsed/engines\";\nimport Fs from \"fs\";\nimport {extname, join, resolve} from \"path\";\nimport {\n PLATFORM_VIEWS_EXTENSIONS,\n PlatformViewEngine,\n PlatformViewsEngineOptions,\n PlatformViewsExtensionsTypes\n} from \"../domain/PlatformViewsSettings\";\n\nasync function patchEJS(ejs: any) {\n if (!ejs) {\n const mod = await import(\"ejs\");\n ejs = mod.default;\n }\n\n return {\n compile(str: string, {client, ...options}: any) {\n return ejs.compile(str, options);\n }\n };\n}\n\n/**\n * @platform\n */\n@Module({\n views: {\n exists: true\n }\n})\nexport class PlatformViews {\n @Constant(\"env\")\n env: Env;\n\n @Constant(\"views.root\", `${process.cwd()}/views`)\n readonly root: string;\n\n @Constant(\"views.cache\")\n readonly cache: boolean;\n\n @Constant(\"views.disabled\", false)\n readonly disabled: string;\n\n @Constant(\"views.viewEngine\", \"ejs\")\n readonly viewEngine: string;\n\n @Constant(\"views.extensions\", {})\n protected extensionsOptions: PlatformViewsExtensionsTypes;\n\n @Constant(\"views.options\", {})\n protected engineOptions: Record<string, PlatformViewsEngineOptions>;\n\n #extensions: Map<string, string>;\n #engines = new Map<string, PlatformViewEngine>();\n #cachePaths = new Map<string, {path: string; extension: string}>();\n\n async $onInit() {\n if (!this.disabled) {\n this.#extensions = new Map(\n Object.entries({\n ...PLATFORM_VIEWS_EXTENSIONS,\n ...this.extensionsOptions\n })\n );\n\n await this.loadEngines();\n }\n }\n\n async loadEngines() {\n requires.set(\"ejs\", await patchEJS(requires.get(\"ejs\")));\n\n this.#extensions.forEach((engineType) => {\n if (engines.has(engineType)) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n requires.set(engineType, options.requires);\n }\n\n this.registerEngine(engineType, {\n options,\n render: getEngine(engineType)\n });\n }\n });\n }\n\n getEngines() {\n return [...this.#extensions.entries()].map(([extension, engineType]) => {\n const engine = this.getEngine(this.getExtension(engineType))!;\n\n return {\n extension,\n engine\n };\n });\n }\n\n registerEngine(engineType: string, engine: PlatformViewEngine) {\n this.#engines.set(engineType, engine);\n\n return this;\n }\n\n getEngine(type: string) {\n return this.#engines.get(this.#extensions.get(type) || type);\n }\n\n getEngineOptions(engineType: string): PlatformViewsEngineOptions {\n return getValue(this.engineOptions, engineType, {});\n }\n\n async render(viewPath: string, options: any = {}): Promise<string> {\n const {path, extension} = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath)!;\n const engine = this.getEngine(extension);\n\n if (!engine) {\n throw new Error(`Engine not found to render the following \"${viewPath}\"`);\n }\n\n return engine.render(path, Object.assign({cache: this.cache || this.env === Env.PROD}, engine.options, options));\n }\n\n protected getExtension(viewPath: string) {\n return (extname(viewPath) || this.viewEngine).replace(/\\./, \"\");\n }\n\n protected resolve(viewPath: string) {\n const extension = this.getExtension(viewPath);\n\n viewPath = viewPath.replace(extname(viewPath), \"\") + \".\" + extension;\n\n const path =\n [\n viewPath,\n resolve(join(this.root, viewPath)),\n resolve(join(process.cwd(), \"views\", viewPath)),\n resolve(join(process.cwd(), \"public\", viewPath))\n ].find((file) => Fs.existsSync(file)) || viewPath;\n\n return {\n path,\n extension\n };\n }\n}\n"]}
@@ -0,0 +1 @@
1
+ export { View } from "@tsed/schema";
@@ -54,6 +54,7 @@ export interface PlatformViewEngine {
54
54
  render(path: string, options: PlatformRenderOptions): Promise<string>;
55
55
  }
56
56
  export interface PlatformViewsSettings {
57
+ disabled?: boolean;
57
58
  /**
58
59
  * Views directory.
59
60
  */
@@ -1,5 +1,6 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
1
4
  export * from "./decorators/view";
2
5
  export * from "./domain/PlatformViewsSettings";
3
6
  export * from "./services/PlatformViews";
4
- export * from "./errors/TemplateRenderError";
5
- export * from "./utils/renderView";
@@ -8,15 +8,12 @@ export declare class PlatformViews {
8
8
  env: Env;
9
9
  readonly root: string;
10
10
  readonly cache: boolean;
11
+ readonly disabled: string;
11
12
  readonly viewEngine: string;
12
13
  protected extensionsOptions: PlatformViewsExtensionsTypes;
13
14
  protected engineOptions: Record<string, PlatformViewsEngineOptions>;
14
15
  $onInit(): Promise<void>;
15
- /**
16
- * @deprecated
17
- */
18
- loadFromConsolidate(): Promise<void>;
19
- loadFromTsedEngines(): Promise<void>;
16
+ loadEngines(): Promise<void>;
20
17
  getEngines(): {
21
18
  extension: string;
22
19
  engine: PlatformViewEngine;
package/package.json CHANGED
@@ -1,46 +1,62 @@
1
1
  {
2
2
  "name": "@tsed/platform-views",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.0.0-beta.11",
4
4
  "description": "Views module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
7
- "main": "./lib/index.js",
8
- "typings": "./lib/index.d.ts",
7
+ "main": "./lib/cjs/index.js",
8
+ "module": "./lib/esm/index.js",
9
+ "typings": "./lib/types/index.d.ts",
9
10
  "exports": {
10
- "require": "./lib/index.js",
11
- "default": "./lib/index.modern.js"
11
+ "types": "./lib/types/index.d.ts",
12
+ "import": "./lib/esm/index.js",
13
+ "require": "./lib/cjs/index.js",
14
+ "default": "./lib/esm/index.js"
12
15
  },
13
16
  "scripts": {
14
- "build": "microbundle --target node --no-compress --format modern,cjs --tsconfig ./tsconfig.compile.json"
17
+ "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
18
+ "build:cjs": "tsc --build tsconfig.compile.json",
19
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
20
+ "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
15
21
  },
16
22
  "dependencies": {
17
- "consolidate": "^0.16.0",
23
+ "@tsed/engines": "7.0.0-beta.11",
18
24
  "ejs": "^3.1.5",
19
- "tslib": "2.2.0"
25
+ "tslib": "2.3.1"
20
26
  },
21
27
  "devDependencies": {
22
- "@tsed/core": "7.0.0-alpha.2",
23
- "@tsed/di": "7.0.0-alpha.2",
24
- "@tsed/engines": "^1.1.5",
25
- "@tsed/exceptions": "7.0.0-alpha.2",
26
- "@tsed/schema": "7.0.0-alpha.2",
27
- "@types/consolidate": "0.14.0"
28
+ "@tsed/core": "7.0.0-beta.11",
29
+ "@tsed/di": "7.0.0-beta.11",
30
+ "@tsed/exceptions": "7.0.0-beta.11",
31
+ "@tsed/schema": "7.0.0-beta.11",
32
+ "@types/consolidate": "0.14.1",
33
+ "@types/ejs": "3.1.0"
28
34
  },
29
35
  "peerDependencies": {
30
- "@tsed/core": "^7.0.0-alpha.2",
31
- "@tsed/di": "^7.0.0-alpha.2",
32
- "@tsed/engines": "^1.1.5",
33
- "@tsed/exceptions": "^7.0.0-alpha.2",
34
- "@tsed/schema": "^7.0.0-alpha.2"
36
+ "@tsed/core": "^7.0.0-beta.11",
37
+ "@tsed/di": "^7.0.0-beta.11",
38
+ "@tsed/exceptions": "^7.0.0-beta.11",
39
+ "@tsed/schema": "^7.0.0-beta.11"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "@tsed/core": {
43
+ "optional": false
44
+ },
45
+ "@tsed/di": {
46
+ "optional": false
47
+ },
48
+ "@tsed/exceptions": {
49
+ "optional": false
50
+ },
51
+ "@tsed/schema": {
52
+ "optional": false
53
+ }
35
54
  },
36
55
  "repository": "https://github.com/tsedio/tsed",
37
56
  "bugs": {
38
57
  "url": "https://github.com/tsedio/tsed/issues"
39
58
  },
40
- "homepage": "https://github.com/tsedio/tsed/tree/alpha/packages/platform/platform-views",
59
+ "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-views",
41
60
  "author": "Romain Lenzotti",
42
- "license": "MIT",
43
- "publishConfig": {
44
- "tag": "alpha"
45
- }
61
+ "license": "MIT"
46
62
  }
package/readme.md CHANGED
@@ -36,18 +36,17 @@ npm install --save @tsed/core
36
36
  ```
37
37
 
38
38
  ## Contributors
39
+
39
40
  Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html).
40
41
 
41
42
  <a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
42
43
 
43
-
44
44
  ## Backers
45
45
 
46
46
  Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
47
47
 
48
48
  <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
49
49
 
50
-
51
50
  ## Sponsors
52
51
 
53
52
  Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]