janja 2.0.0 → 2.0.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.
- package/dist/index.cjs.js +148 -131
- package/dist/index.d.ts +31 -24
- package/dist/index.esm.js +148 -131
- package/dist/index.js +148 -131
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -22,13 +22,39 @@ var NodeType;
|
|
|
22
22
|
NodeType["UNKNOWN"] = "UNKNOWN";
|
|
23
23
|
NodeType["UNEXPECTED"] = "UNEXPECTED";
|
|
24
24
|
})(NodeType || (NodeType = {}));
|
|
25
|
-
class
|
|
26
|
-
constructor(
|
|
27
|
-
Object.defineProperty(this, "
|
|
25
|
+
class Traversal {
|
|
26
|
+
constructor() {
|
|
27
|
+
Object.defineProperty(this, "type", {
|
|
28
28
|
enumerable: true,
|
|
29
29
|
configurable: true,
|
|
30
30
|
writable: true,
|
|
31
|
-
value:
|
|
31
|
+
value: null
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "loc", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: null
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "body", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
traverse(cb) {
|
|
47
|
+
this.body?.forEach(cb);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class RootNode extends Traversal {
|
|
51
|
+
constructor(body, loc) {
|
|
52
|
+
super();
|
|
53
|
+
Object.defineProperty(this, "body", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: body
|
|
32
58
|
});
|
|
33
59
|
Object.defineProperty(this, "loc", {
|
|
34
60
|
enumerable: true,
|
|
@@ -44,8 +70,9 @@ class TemplateNode {
|
|
|
44
70
|
});
|
|
45
71
|
}
|
|
46
72
|
}
|
|
47
|
-
class OutputNode {
|
|
73
|
+
class OutputNode extends Traversal {
|
|
48
74
|
constructor(val, loc, strip, exp) {
|
|
75
|
+
super();
|
|
49
76
|
Object.defineProperty(this, "val", {
|
|
50
77
|
enumerable: true,
|
|
51
78
|
configurable: true,
|
|
@@ -78,8 +105,9 @@ class OutputNode {
|
|
|
78
105
|
});
|
|
79
106
|
}
|
|
80
107
|
}
|
|
81
|
-
class TextNode {
|
|
108
|
+
class TextNode extends Traversal {
|
|
82
109
|
constructor(val, loc, strip) {
|
|
110
|
+
super();
|
|
83
111
|
Object.defineProperty(this, "val", {
|
|
84
112
|
enumerable: true,
|
|
85
113
|
configurable: true,
|
|
@@ -106,8 +134,9 @@ class TextNode {
|
|
|
106
134
|
});
|
|
107
135
|
}
|
|
108
136
|
}
|
|
109
|
-
class IfNode {
|
|
137
|
+
class IfNode extends Traversal {
|
|
110
138
|
constructor(test, body, alternatives, loc, strip) {
|
|
139
|
+
super();
|
|
111
140
|
Object.defineProperty(this, "test", {
|
|
112
141
|
enumerable: true,
|
|
113
142
|
configurable: true,
|
|
@@ -145,9 +174,14 @@ class IfNode {
|
|
|
145
174
|
value: NodeType.IF
|
|
146
175
|
});
|
|
147
176
|
}
|
|
177
|
+
traverse(cb) {
|
|
178
|
+
this.body?.forEach(cb);
|
|
179
|
+
this.alternatives?.forEach(node => node.body?.forEach(cb));
|
|
180
|
+
}
|
|
148
181
|
}
|
|
149
|
-
class ElseIfNode {
|
|
182
|
+
class ElseIfNode extends Traversal {
|
|
150
183
|
constructor(test, body, loc, strip) {
|
|
184
|
+
super();
|
|
151
185
|
Object.defineProperty(this, "test", {
|
|
152
186
|
enumerable: true,
|
|
153
187
|
configurable: true,
|
|
@@ -180,8 +214,9 @@ class ElseIfNode {
|
|
|
180
214
|
});
|
|
181
215
|
}
|
|
182
216
|
}
|
|
183
|
-
class ElseNode {
|
|
217
|
+
class ElseNode extends Traversal {
|
|
184
218
|
constructor(body, loc, strip) {
|
|
219
|
+
super();
|
|
185
220
|
Object.defineProperty(this, "body", {
|
|
186
221
|
enumerable: true,
|
|
187
222
|
configurable: true,
|
|
@@ -208,8 +243,9 @@ class ElseNode {
|
|
|
208
243
|
});
|
|
209
244
|
}
|
|
210
245
|
}
|
|
211
|
-
class ForNode {
|
|
246
|
+
class ForNode extends Traversal {
|
|
212
247
|
constructor(loop, body, loc, strip) {
|
|
248
|
+
super();
|
|
213
249
|
Object.defineProperty(this, "loop", {
|
|
214
250
|
enumerable: true,
|
|
215
251
|
configurable: true,
|
|
@@ -242,8 +278,9 @@ class ForNode {
|
|
|
242
278
|
});
|
|
243
279
|
}
|
|
244
280
|
}
|
|
245
|
-
class BreakNode {
|
|
281
|
+
class BreakNode extends Traversal {
|
|
246
282
|
constructor(val, loc, strip) {
|
|
283
|
+
super();
|
|
247
284
|
Object.defineProperty(this, "val", {
|
|
248
285
|
enumerable: true,
|
|
249
286
|
configurable: true,
|
|
@@ -270,8 +307,9 @@ class BreakNode {
|
|
|
270
307
|
});
|
|
271
308
|
}
|
|
272
309
|
}
|
|
273
|
-
class ContinueNode {
|
|
310
|
+
class ContinueNode extends Traversal {
|
|
274
311
|
constructor(val, loc, strip) {
|
|
312
|
+
super();
|
|
275
313
|
Object.defineProperty(this, "val", {
|
|
276
314
|
enumerable: true,
|
|
277
315
|
configurable: true,
|
|
@@ -298,8 +336,9 @@ class ContinueNode {
|
|
|
298
336
|
});
|
|
299
337
|
}
|
|
300
338
|
}
|
|
301
|
-
class UnknownDirectiveNode {
|
|
339
|
+
class UnknownDirectiveNode extends Traversal {
|
|
302
340
|
constructor(name, val, loc, strip) {
|
|
341
|
+
super();
|
|
303
342
|
Object.defineProperty(this, "name", {
|
|
304
343
|
enumerable: true,
|
|
305
344
|
configurable: true,
|
|
@@ -332,8 +371,9 @@ class UnknownDirectiveNode {
|
|
|
332
371
|
});
|
|
333
372
|
}
|
|
334
373
|
}
|
|
335
|
-
class UnexpectedDirectiveNode {
|
|
374
|
+
class UnexpectedDirectiveNode extends Traversal {
|
|
336
375
|
constructor(name, val, loc, strip) {
|
|
376
|
+
super();
|
|
337
377
|
Object.defineProperty(this, "name", {
|
|
338
378
|
enumerable: true,
|
|
339
379
|
configurable: true,
|
|
@@ -366,8 +406,9 @@ class UnexpectedDirectiveNode {
|
|
|
366
406
|
});
|
|
367
407
|
}
|
|
368
408
|
}
|
|
369
|
-
class CommentNode {
|
|
409
|
+
class CommentNode extends Traversal {
|
|
370
410
|
constructor(val, loc, strip) {
|
|
411
|
+
super();
|
|
371
412
|
Object.defineProperty(this, "val", {
|
|
372
413
|
enumerable: true,
|
|
373
414
|
configurable: true,
|
|
@@ -394,8 +435,9 @@ class CommentNode {
|
|
|
394
435
|
});
|
|
395
436
|
}
|
|
396
437
|
}
|
|
397
|
-
class IncludeNode {
|
|
438
|
+
class IncludeNode extends Traversal {
|
|
398
439
|
constructor(val, loc, strip) {
|
|
440
|
+
super();
|
|
399
441
|
Object.defineProperty(this, "val", {
|
|
400
442
|
enumerable: true,
|
|
401
443
|
configurable: true,
|
|
@@ -422,8 +464,9 @@ class IncludeNode {
|
|
|
422
464
|
});
|
|
423
465
|
}
|
|
424
466
|
}
|
|
425
|
-
class BlockNode {
|
|
467
|
+
class BlockNode extends Traversal {
|
|
426
468
|
constructor(val, body, loc, strip) {
|
|
469
|
+
super();
|
|
427
470
|
Object.defineProperty(this, "val", {
|
|
428
471
|
enumerable: true,
|
|
429
472
|
configurable: true,
|
|
@@ -456,8 +499,9 @@ class BlockNode {
|
|
|
456
499
|
});
|
|
457
500
|
}
|
|
458
501
|
}
|
|
459
|
-
class SuperNode {
|
|
502
|
+
class SuperNode extends Traversal {
|
|
460
503
|
constructor(val, loc, strip) {
|
|
504
|
+
super();
|
|
461
505
|
Object.defineProperty(this, "val", {
|
|
462
506
|
enumerable: true,
|
|
463
507
|
configurable: true,
|
|
@@ -484,8 +528,9 @@ class SuperNode {
|
|
|
484
528
|
});
|
|
485
529
|
}
|
|
486
530
|
}
|
|
487
|
-
class MacroNode {
|
|
531
|
+
class MacroNode extends Traversal {
|
|
488
532
|
constructor(val, body, loc, strip) {
|
|
533
|
+
super();
|
|
489
534
|
Object.defineProperty(this, "val", {
|
|
490
535
|
enumerable: true,
|
|
491
536
|
configurable: true,
|
|
@@ -518,8 +563,9 @@ class MacroNode {
|
|
|
518
563
|
});
|
|
519
564
|
}
|
|
520
565
|
}
|
|
521
|
-
class CallerNode {
|
|
566
|
+
class CallerNode extends Traversal {
|
|
522
567
|
constructor(val, loc, strip) {
|
|
568
|
+
super();
|
|
523
569
|
Object.defineProperty(this, "val", {
|
|
524
570
|
enumerable: true,
|
|
525
571
|
configurable: true,
|
|
@@ -546,8 +592,9 @@ class CallerNode {
|
|
|
546
592
|
});
|
|
547
593
|
}
|
|
548
594
|
}
|
|
549
|
-
class CallNode {
|
|
595
|
+
class CallNode extends Traversal {
|
|
550
596
|
constructor(val, body, loc, strip) {
|
|
597
|
+
super();
|
|
551
598
|
Object.defineProperty(this, "val", {
|
|
552
599
|
enumerable: true,
|
|
553
600
|
configurable: true,
|
|
@@ -580,8 +627,9 @@ class CallNode {
|
|
|
580
627
|
});
|
|
581
628
|
}
|
|
582
629
|
}
|
|
583
|
-
class SetNode {
|
|
630
|
+
class SetNode extends Traversal {
|
|
584
631
|
constructor(val, loc, strip) {
|
|
632
|
+
super();
|
|
585
633
|
Object.defineProperty(this, "val", {
|
|
586
634
|
enumerable: true,
|
|
587
635
|
configurable: true,
|
|
@@ -608,8 +656,9 @@ class SetNode {
|
|
|
608
656
|
});
|
|
609
657
|
}
|
|
610
658
|
}
|
|
611
|
-
class CaptureNode {
|
|
659
|
+
class CaptureNode extends Traversal {
|
|
612
660
|
constructor(val, body, loc, strip) {
|
|
661
|
+
super();
|
|
613
662
|
Object.defineProperty(this, "val", {
|
|
614
663
|
enumerable: true,
|
|
615
664
|
configurable: true,
|
|
@@ -718,7 +767,7 @@ function parseBlock(token, parser) {
|
|
|
718
767
|
const val = parser.parseExp(token.expression);
|
|
719
768
|
parser.advance();
|
|
720
769
|
const body = parser.parseUntil(['endblock']);
|
|
721
|
-
if (parser.match('endblock')) {
|
|
770
|
+
if (parser.match(['endblock'])) {
|
|
722
771
|
parser.advance();
|
|
723
772
|
}
|
|
724
773
|
else {
|
|
@@ -742,7 +791,7 @@ function parseCall(token, parser) {
|
|
|
742
791
|
const val = parser.parseExp(token.expression);
|
|
743
792
|
parser.advance();
|
|
744
793
|
const body = parser.parseUntil(['endcall']);
|
|
745
|
-
if (parser.match('endcall')) {
|
|
794
|
+
if (parser.match(['endcall'])) {
|
|
746
795
|
parser.advance();
|
|
747
796
|
}
|
|
748
797
|
else {
|
|
@@ -760,7 +809,7 @@ function parseCapture(token, parser) {
|
|
|
760
809
|
const val = parser.parseExp(token.expression);
|
|
761
810
|
parser.advance();
|
|
762
811
|
const body = parser.parseUntil(['endcapture']);
|
|
763
|
-
if (parser.match('endcapture')) {
|
|
812
|
+
if (parser.match(['endcapture'])) {
|
|
764
813
|
parser.advance();
|
|
765
814
|
}
|
|
766
815
|
else {
|
|
@@ -778,7 +827,7 @@ function parseFor(token, parser) {
|
|
|
778
827
|
const loop = parser.parseExp(token.expression);
|
|
779
828
|
parser.advance();
|
|
780
829
|
const body = parser.parseUntil(['endfor']);
|
|
781
|
-
if (parser.match('endfor')) {
|
|
830
|
+
if (parser.match(['endfor'])) {
|
|
782
831
|
parser.advance();
|
|
783
832
|
}
|
|
784
833
|
else {
|
|
@@ -808,13 +857,13 @@ function parseIf(token, parser) {
|
|
|
808
857
|
parser.advance();
|
|
809
858
|
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
810
859
|
const alternatives = [];
|
|
811
|
-
while (parser.
|
|
860
|
+
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
812
861
|
const branchToken = parser.peek();
|
|
813
862
|
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
814
863
|
? parseElse(branchToken, parser)
|
|
815
864
|
: parseElseIf(branchToken, parser));
|
|
816
865
|
}
|
|
817
|
-
if (parser.match('endif')) {
|
|
866
|
+
if (parser.match(['endif'])) {
|
|
818
867
|
parser.advance();
|
|
819
868
|
}
|
|
820
869
|
else {
|
|
@@ -824,10 +873,9 @@ function parseIf(token, parser) {
|
|
|
824
873
|
}
|
|
825
874
|
function parseElseIf(token, parser) {
|
|
826
875
|
parser.requireExpression(token);
|
|
827
|
-
const test = parser.parseExp(token.expression);
|
|
828
876
|
parser.advance();
|
|
829
|
-
const body = parser.parseUntil(['else', 'elseif', 'elif', 'endif']);
|
|
830
|
-
return new ElseIfNode(
|
|
877
|
+
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
878
|
+
return new ElseIfNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
831
879
|
}
|
|
832
880
|
function parseElse(token, parser) {
|
|
833
881
|
parser.requireNoExpression(token);
|
|
@@ -858,7 +906,7 @@ function parseMacro(token, parser) {
|
|
|
858
906
|
const name = parser.parseExp(token.expression);
|
|
859
907
|
parser.advance();
|
|
860
908
|
const body = parser.parseUntil(['endmacro']);
|
|
861
|
-
if (parser.match('endmacro')) {
|
|
909
|
+
if (parser.match(['endmacro'])) {
|
|
862
910
|
parser.advance();
|
|
863
911
|
}
|
|
864
912
|
else {
|
|
@@ -1091,25 +1139,15 @@ var filters = /*#__PURE__*/Object.freeze({
|
|
|
1091
1139
|
values: values
|
|
1092
1140
|
});
|
|
1093
1141
|
|
|
1094
|
-
async function compileBlock({ val: { value } }, compiler) {
|
|
1095
|
-
if
|
|
1096
|
-
return;
|
|
1097
|
-
}
|
|
1098
|
-
compiler.state.block = value;
|
|
1099
|
-
const blockNode = compiler.blocks.get(value).pop();
|
|
1100
|
-
await compiler.compileNodes(blockNode.body);
|
|
1101
|
-
compiler.blocks.delete(value);
|
|
1102
|
-
compiler.state.block = undefined;
|
|
1142
|
+
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1143
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, `}`);
|
|
1103
1144
|
}
|
|
1104
1145
|
async function compileSuper({ loc }, compiler) {
|
|
1105
1146
|
if (!compiler.state.block) {
|
|
1106
1147
|
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1107
1148
|
return;
|
|
1108
1149
|
}
|
|
1109
|
-
|
|
1110
|
-
if (block) {
|
|
1111
|
-
await compiler.compileNodes(block.body);
|
|
1112
|
-
}
|
|
1150
|
+
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
1113
1151
|
}
|
|
1114
1152
|
const compilers$b = {
|
|
1115
1153
|
[NodeType.BLOCK]: compileBlock,
|
|
@@ -1329,7 +1367,7 @@ const compilers$6 = {
|
|
|
1329
1367
|
};
|
|
1330
1368
|
|
|
1331
1369
|
async function compileInclude({ val: path, loc }, compiler) {
|
|
1332
|
-
compiler.pushRaw(loc, `
|
|
1370
|
+
compiler.pushRaw(loc, `s+=await p["${path.value}"]?.()??"";`);
|
|
1333
1371
|
}
|
|
1334
1372
|
const compilers$5 = {
|
|
1335
1373
|
[NodeType.INCLUDE]: compileInclude,
|
|
@@ -1363,7 +1401,6 @@ const compilers$3 = {
|
|
|
1363
1401
|
|
|
1364
1402
|
async function compileSet({ val, loc }, compiler) {
|
|
1365
1403
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1366
|
-
compiler.pushRaw(null, `});`);
|
|
1367
1404
|
}
|
|
1368
1405
|
const compilers$2 = {
|
|
1369
1406
|
[NodeType.SET]: compileSet,
|
|
@@ -1416,7 +1453,7 @@ const compilerOptions = {
|
|
|
1416
1453
|
const renderOptions = {
|
|
1417
1454
|
...compilerOptions,
|
|
1418
1455
|
globals: {},
|
|
1419
|
-
filters
|
|
1456
|
+
filters,
|
|
1420
1457
|
autoEscape: true,
|
|
1421
1458
|
};
|
|
1422
1459
|
|
|
@@ -2354,7 +2391,7 @@ class Parser extends Tokenizer {
|
|
|
2354
2391
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2355
2392
|
this.expParser = new ExpParser(template);
|
|
2356
2393
|
this.cursor = 0;
|
|
2357
|
-
return new
|
|
2394
|
+
return new RootNode(this.parseUntil(), { start, end });
|
|
2358
2395
|
}
|
|
2359
2396
|
parseUntil(names) {
|
|
2360
2397
|
const nodes = [];
|
|
@@ -2363,7 +2400,8 @@ class Parser extends Tokenizer {
|
|
|
2363
2400
|
const token = this.peek();
|
|
2364
2401
|
if (!token
|
|
2365
2402
|
|| (token.type === TokenType.DIRECTIVE
|
|
2366
|
-
&& names
|
|
2403
|
+
&& names
|
|
2404
|
+
&& this.match(names, token))) {
|
|
2367
2405
|
break;
|
|
2368
2406
|
}
|
|
2369
2407
|
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
@@ -2425,15 +2463,8 @@ class Parser extends Tokenizer {
|
|
|
2425
2463
|
advance() {
|
|
2426
2464
|
this.cursor++;
|
|
2427
2465
|
}
|
|
2428
|
-
match(
|
|
2429
|
-
return
|
|
2430
|
-
}
|
|
2431
|
-
isDirective(names) {
|
|
2432
|
-
const token = this.peek();
|
|
2433
|
-
if (token?.type !== TokenType.DIRECTIVE) {
|
|
2434
|
-
return false;
|
|
2435
|
-
}
|
|
2436
|
-
return names.includes(token.name.toLowerCase());
|
|
2466
|
+
match(names, token = this.peek()) {
|
|
2467
|
+
return names.includes(token?.name);
|
|
2437
2468
|
}
|
|
2438
2469
|
requireExpression({ expression, name, loc }) {
|
|
2439
2470
|
if (!expression) {
|
|
@@ -2462,31 +2493,31 @@ class Compiler extends Context {
|
|
|
2462
2493
|
writable: true,
|
|
2463
2494
|
value: void 0
|
|
2464
2495
|
});
|
|
2465
|
-
Object.defineProperty(this, "
|
|
2496
|
+
Object.defineProperty(this, "state", {
|
|
2466
2497
|
enumerable: true,
|
|
2467
2498
|
configurable: true,
|
|
2468
2499
|
writable: true,
|
|
2469
2500
|
value: void 0
|
|
2470
2501
|
});
|
|
2471
|
-
Object.defineProperty(this, "
|
|
2502
|
+
Object.defineProperty(this, "isRoot", {
|
|
2472
2503
|
enumerable: true,
|
|
2473
2504
|
configurable: true,
|
|
2474
2505
|
writable: true,
|
|
2475
2506
|
value: void 0
|
|
2476
2507
|
});
|
|
2477
|
-
Object.defineProperty(this, "
|
|
2508
|
+
Object.defineProperty(this, "rootNode", {
|
|
2478
2509
|
enumerable: true,
|
|
2479
2510
|
configurable: true,
|
|
2480
2511
|
writable: true,
|
|
2481
2512
|
value: void 0
|
|
2482
2513
|
});
|
|
2483
|
-
Object.defineProperty(this, "
|
|
2514
|
+
Object.defineProperty(this, "partials", {
|
|
2484
2515
|
enumerable: true,
|
|
2485
2516
|
configurable: true,
|
|
2486
2517
|
writable: true,
|
|
2487
2518
|
value: void 0
|
|
2488
2519
|
});
|
|
2489
|
-
Object.defineProperty(this, "
|
|
2520
|
+
Object.defineProperty(this, "blocks", {
|
|
2490
2521
|
enumerable: true,
|
|
2491
2522
|
configurable: true,
|
|
2492
2523
|
writable: true,
|
|
@@ -2497,105 +2528,91 @@ class Compiler extends Context {
|
|
|
2497
2528
|
...options,
|
|
2498
2529
|
};
|
|
2499
2530
|
}
|
|
2500
|
-
async compile(template, root = true, blocks = new Map()) {
|
|
2531
|
+
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2501
2532
|
this.template = template;
|
|
2502
|
-
this.
|
|
2503
|
-
this.partials =
|
|
2533
|
+
this.isRoot = root;
|
|
2534
|
+
this.partials = partials;
|
|
2504
2535
|
this.blocks = blocks;
|
|
2505
|
-
this.
|
|
2536
|
+
this.rootNode = new Parser(this.options).parse(template);
|
|
2506
2537
|
this.state = {};
|
|
2507
2538
|
this.start();
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
if (this.partials.size
|
|
2511
|
-
this.pushRaw(null, `const
|
|
2539
|
+
await this.parseTemplates();
|
|
2540
|
+
if (this.isRoot) {
|
|
2541
|
+
if (this.partials.size) {
|
|
2542
|
+
this.pushRaw(null, `const p={`);
|
|
2512
2543
|
for (const [path, code] of this.partials) {
|
|
2513
|
-
this.pushRaw(null, `"${path}":async
|
|
2544
|
+
this.pushRaw(null, `"${path}":async()=>{`, code, `},`);
|
|
2545
|
+
}
|
|
2546
|
+
this.pushRaw(null, `};`);
|
|
2547
|
+
}
|
|
2548
|
+
if (this.blocks.size) {
|
|
2549
|
+
this.pushRaw(null, `const b={`);
|
|
2550
|
+
for (const [name, blocks] of this.blocks) {
|
|
2551
|
+
this.pushRaw(null, `"${name}":{`, `u:0,`, `s:[`);
|
|
2552
|
+
this.state.block = name;
|
|
2553
|
+
for (const block of blocks) {
|
|
2554
|
+
this.pushRaw(null, `async()=>{`, `let s="";`);
|
|
2555
|
+
await this.compileNodes(block.body);
|
|
2556
|
+
this.pushRaw(null, `return s;`, `},`);
|
|
2557
|
+
}
|
|
2558
|
+
this.state.block = undefined;
|
|
2559
|
+
this.pushRaw(null, `],`, `},`);
|
|
2514
2560
|
}
|
|
2515
2561
|
this.pushRaw(null, `};`);
|
|
2516
2562
|
}
|
|
2517
2563
|
}
|
|
2518
|
-
await this.compileNode(this.
|
|
2564
|
+
await this.compileNode(this.rootNode);
|
|
2519
2565
|
this.end();
|
|
2520
2566
|
return this;
|
|
2521
2567
|
}
|
|
2522
2568
|
async parseTemplates() {
|
|
2523
|
-
const
|
|
2524
|
-
const
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
}
|
|
2530
|
-
const { includes, blocks } = this.collectIncludesAndBlocks();
|
|
2531
|
-
for (const [path, node] of includes) {
|
|
2532
|
-
if (!astMap.has(path)) {
|
|
2533
|
-
try {
|
|
2534
|
-
queue.push({
|
|
2535
|
-
path,
|
|
2536
|
-
node,
|
|
2537
|
-
template: await this.options.loader(path),
|
|
2538
|
-
});
|
|
2539
|
-
}
|
|
2540
|
-
catch {
|
|
2541
|
-
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2542
|
-
return;
|
|
2543
|
-
}
|
|
2569
|
+
const { partials, blocks } = this.collectPartialsAndBlocks();
|
|
2570
|
+
for (const [path, node] of partials) {
|
|
2571
|
+
if (!this.partials.has(path)) {
|
|
2572
|
+
let template;
|
|
2573
|
+
try {
|
|
2574
|
+
template = await this.options.loader(path);
|
|
2544
2575
|
}
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2576
|
+
catch {
|
|
2577
|
+
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2578
|
+
return;
|
|
2579
|
+
}
|
|
2580
|
+
if (template) {
|
|
2581
|
+
await this.compilePartial(path, node, template);
|
|
2550
2582
|
}
|
|
2551
|
-
this.blocks.get(value).push(block);
|
|
2552
2583
|
}
|
|
2553
2584
|
}
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
if (!this.
|
|
2557
|
-
|
|
2585
|
+
for (const block of blocks) {
|
|
2586
|
+
const { val: { value } } = block;
|
|
2587
|
+
if (!this.blocks.has(value)) {
|
|
2588
|
+
this.blocks.set(value, []);
|
|
2558
2589
|
}
|
|
2590
|
+
this.blocks.get(value).push(block);
|
|
2559
2591
|
}
|
|
2560
2592
|
}
|
|
2561
|
-
|
|
2562
|
-
const
|
|
2593
|
+
collectPartialsAndBlocks() {
|
|
2594
|
+
const partials = new Map();
|
|
2563
2595
|
const blocks = [];
|
|
2564
2596
|
const traverse = (node) => {
|
|
2565
2597
|
if (node.type === NodeType.INCLUDE) {
|
|
2566
|
-
if (!
|
|
2567
|
-
|
|
2598
|
+
if (!partials.has(node.val.value)) {
|
|
2599
|
+
partials.set(node.val.value, node);
|
|
2568
2600
|
}
|
|
2569
2601
|
}
|
|
2570
2602
|
if (node.type === NodeType.BLOCK) {
|
|
2571
2603
|
blocks.push(node);
|
|
2572
2604
|
}
|
|
2573
|
-
|
|
2574
|
-
node.children.forEach(traverse);
|
|
2575
|
-
}
|
|
2576
|
-
if (node.type === NodeType.IF) {
|
|
2577
|
-
const ifNode = node;
|
|
2578
|
-
ifNode.body.forEach(traverse);
|
|
2579
|
-
ifNode.alternatives.forEach((alt) => {
|
|
2580
|
-
alt.body.forEach(traverse);
|
|
2581
|
-
});
|
|
2582
|
-
}
|
|
2583
|
-
if (node.type === NodeType.FOR) {
|
|
2584
|
-
node.body.forEach(traverse);
|
|
2585
|
-
}
|
|
2586
|
-
if (node.type === NodeType.BLOCK) {
|
|
2587
|
-
node.body.forEach(traverse);
|
|
2588
|
-
}
|
|
2605
|
+
node.traverse(traverse);
|
|
2589
2606
|
};
|
|
2590
|
-
traverse(this.
|
|
2607
|
+
traverse(this.rootNode);
|
|
2591
2608
|
return {
|
|
2592
|
-
|
|
2609
|
+
partials,
|
|
2593
2610
|
blocks,
|
|
2594
2611
|
};
|
|
2595
2612
|
}
|
|
2596
2613
|
async compileNode(node) {
|
|
2597
2614
|
if (node.type === NodeType.TEMPLATE) {
|
|
2598
|
-
await this.compileNodes(node.
|
|
2615
|
+
await this.compileNodes(node.body);
|
|
2599
2616
|
return;
|
|
2600
2617
|
}
|
|
2601
2618
|
if (this.options.compilers[node.type]) {
|
|
@@ -2609,9 +2626,9 @@ class Compiler extends Context {
|
|
|
2609
2626
|
await this.compileNode(child);
|
|
2610
2627
|
}
|
|
2611
2628
|
}
|
|
2612
|
-
async compilePartial(path, node, template
|
|
2629
|
+
async compilePartial(path, node, template) {
|
|
2613
2630
|
try {
|
|
2614
|
-
const { code } = await new Compiler(this.options).compile(template, false, blocks);
|
|
2631
|
+
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks);
|
|
2615
2632
|
this.partials.set(path, code);
|
|
2616
2633
|
}
|
|
2617
2634
|
catch {
|