@brilab-mailer/template-handlebars 0.0.4 → 0.0.5-1
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.
|
@@ -5,10 +5,14 @@ interface HandlebarsConfig extends MailerTemplateEngineExtendsOptionsType {
|
|
|
5
5
|
export declare class HandlebarsTemplateEngine implements MailerTemplateEngine {
|
|
6
6
|
private readonly options;
|
|
7
7
|
private readonly hbs;
|
|
8
|
+
private readonly templateCache;
|
|
9
|
+
private readonly layoutCache;
|
|
8
10
|
private readonly templatesDir;
|
|
9
11
|
private readonly layoutsDir;
|
|
12
|
+
private readonly partialsDir;
|
|
10
13
|
constructor(options: HandlebarsConfig);
|
|
11
14
|
private registerI18nHelpers;
|
|
15
|
+
private registerPartialsHelpers;
|
|
12
16
|
private loadTemplate;
|
|
13
17
|
private loadLayout;
|
|
14
18
|
render(templateKey: string, context?: Record<string, any>, options?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlebars-template.engine.d.ts","sourceRoot":"","sources":["../../src/lib/handlebars-template.engine.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,oBAAoB,EACpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,sCAAsC,EACtC,MAAM,0BAA0B,CAAC;AAKlC,UAAU,gBACT,SAAQ,sCAAsC;CAAG;AAElD,qBACa,wBAAyB,YAAW,oBAAoB;
|
|
1
|
+
{"version":3,"file":"handlebars-template.engine.d.ts","sourceRoot":"","sources":["../../src/lib/handlebars-template.engine.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,oBAAoB,EACpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,sCAAsC,EACtC,MAAM,0BAA0B,CAAC;AAKlC,UAAU,gBACT,SAAQ,sCAAsC;CAAG;AAElD,qBACa,wBAAyB,YAAW,oBAAoB;IAYnE,OAAO,CAAC,QAAQ,CAAC,OAAO;IAXzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAuB;IAE3C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAkD;IAChF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAkD;IAE9E,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAInB,OAAO,EAAE,gBAAgB;IAkB3C,OAAO,CAAC,mBAAmB;IAiB3B,OAAO,CAAC,uBAAuB;IAkC/B,OAAO,CAAC,YAAY;IAapB,OAAO,CAAC,UAAU;IAaZ,MAAM,CACX,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EACjC,OAAO,GAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAA6B,GACxD,OAAO,CAAC,MAAM,CAAC;CASlB"}
|
|
@@ -7,19 +7,27 @@ import Handlebars from 'handlebars';
|
|
|
7
7
|
let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
|
|
8
8
|
options;
|
|
9
9
|
hbs = Handlebars.create();
|
|
10
|
+
templateCache = new Map();
|
|
11
|
+
layoutCache = new Map();
|
|
10
12
|
templatesDir;
|
|
11
13
|
layoutsDir;
|
|
14
|
+
partialsDir;
|
|
12
15
|
constructor(options) {
|
|
13
16
|
this.options = options;
|
|
14
|
-
this.registerI18nHelpers();
|
|
15
17
|
const rootDir = this.options?.rootDir ?? process.cwd();
|
|
16
18
|
const templateDir = this.options?.templateDir ?? '';
|
|
17
19
|
const baseDir = path.join(rootDir, templateDir);
|
|
18
20
|
this.templatesDir = baseDir;
|
|
19
21
|
const layoutsFolder = options?.folders?.layouts || 'layouts';
|
|
20
22
|
this.layoutsDir = path.join(baseDir, layoutsFolder);
|
|
23
|
+
const partialsFolder = options?.folders?.partials || 'partials';
|
|
24
|
+
this.partialsDir = path.join(baseDir, partialsFolder);
|
|
25
|
+
this.registerI18nHelpers();
|
|
26
|
+
this.registerPartialsHelpers();
|
|
21
27
|
}
|
|
22
28
|
registerI18nHelpers() {
|
|
29
|
+
if (!this.hbs)
|
|
30
|
+
return;
|
|
23
31
|
const translator = this.options?.translator;
|
|
24
32
|
if (!translator)
|
|
25
33
|
return;
|
|
@@ -34,20 +42,55 @@ let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
|
|
|
34
42
|
return value;
|
|
35
43
|
});
|
|
36
44
|
}
|
|
45
|
+
registerPartialsHelpers() {
|
|
46
|
+
if (!this.hbs)
|
|
47
|
+
return;
|
|
48
|
+
const partialsPath = this.partialsDir;
|
|
49
|
+
if (!fs.existsSync(partialsPath))
|
|
50
|
+
return;
|
|
51
|
+
const registered = new Set();
|
|
52
|
+
const registerRecursive = (dir, namespace = '') => {
|
|
53
|
+
const files = fs.readdirSync(dir, { withFileTypes: true });
|
|
54
|
+
for (const file of files) {
|
|
55
|
+
const fullPath = path.join(dir, file.name);
|
|
56
|
+
if (file.isDirectory()) {
|
|
57
|
+
registerRecursive(fullPath, `${namespace}${file.name}/`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (!file.name.toLowerCase().endsWith('.hbs'))
|
|
61
|
+
continue;
|
|
62
|
+
const name = `${namespace}${path.basename(file.name, '.hbs')}`;
|
|
63
|
+
if (registered.has(name))
|
|
64
|
+
continue;
|
|
65
|
+
const content = fs.readFileSync(fullPath, 'utf8');
|
|
66
|
+
registered.add(name);
|
|
67
|
+
this.hbs.registerPartial(name, content);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
registerRecursive(partialsPath);
|
|
71
|
+
}
|
|
37
72
|
loadTemplate(key) {
|
|
73
|
+
if (this.templateCache.has(key))
|
|
74
|
+
return this.templateCache.get(key);
|
|
38
75
|
const filePath = path.join(this.templatesDir, `${key}.hbs`);
|
|
39
76
|
if (!fs.existsSync(filePath)) {
|
|
40
77
|
throw new Error(`Template not found: ${filePath}`);
|
|
41
78
|
}
|
|
42
79
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
43
|
-
|
|
80
|
+
const compiled = this.hbs.compile(source);
|
|
81
|
+
this.templateCache.set(key, compiled);
|
|
82
|
+
return compiled;
|
|
44
83
|
}
|
|
45
84
|
loadLayout(key) {
|
|
85
|
+
if (this.layoutCache.has(key))
|
|
86
|
+
return this.layoutCache.get(key);
|
|
46
87
|
const filePath = path.join(this.layoutsDir, `${key}.hbs`);
|
|
47
88
|
if (!fs.existsSync(filePath))
|
|
48
89
|
return null;
|
|
49
90
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
50
|
-
|
|
91
|
+
const compiled = this.hbs.compile(source);
|
|
92
|
+
this.layoutCache.set(key, compiled);
|
|
93
|
+
return compiled;
|
|
51
94
|
}
|
|
52
95
|
async render(templateKey, context = {}, options = { layoutKey: 'default' }) {
|
|
53
96
|
const template = this.loadTemplate(templateKey);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brilab-mailer/template-handlebars",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5-1",
|
|
4
4
|
"author": "Bohdan Radchenko <radchenkobs@gmail.com>",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@nestjs/common": "^10.0.0",
|
|
28
28
|
"@nestjs/config": "^3.0.0",
|
|
29
|
-
"@brilab-mailer/contracts": "^0.0.
|
|
30
|
-
"@brilab-mailer/core": "^0.0.
|
|
29
|
+
"@brilab-mailer/contracts": "^0.0.5-1",
|
|
30
|
+
"@brilab-mailer/core": "^0.0.5-1"
|
|
31
31
|
},
|
|
32
32
|
"peerDependenciesMeta": {
|
|
33
33
|
"@brilab-mailer/contracts": {
|