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.js
CHANGED
|
@@ -25,13 +25,39 @@
|
|
|
25
25
|
NodeType["UNKNOWN"] = "UNKNOWN";
|
|
26
26
|
NodeType["UNEXPECTED"] = "UNEXPECTED";
|
|
27
27
|
})(NodeType || (NodeType = {}));
|
|
28
|
-
class
|
|
29
|
-
constructor(
|
|
30
|
-
Object.defineProperty(this, "
|
|
28
|
+
class Traversal {
|
|
29
|
+
constructor() {
|
|
30
|
+
Object.defineProperty(this, "type", {
|
|
31
31
|
enumerable: true,
|
|
32
32
|
configurable: true,
|
|
33
33
|
writable: true,
|
|
34
|
-
value:
|
|
34
|
+
value: null
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "loc", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: null
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(this, "body", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
traverse(cb) {
|
|
50
|
+
this.body?.forEach(cb);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class RootNode extends Traversal {
|
|
54
|
+
constructor(body, loc) {
|
|
55
|
+
super();
|
|
56
|
+
Object.defineProperty(this, "body", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: body
|
|
35
61
|
});
|
|
36
62
|
Object.defineProperty(this, "loc", {
|
|
37
63
|
enumerable: true,
|
|
@@ -47,8 +73,9 @@
|
|
|
47
73
|
});
|
|
48
74
|
}
|
|
49
75
|
}
|
|
50
|
-
class OutputNode {
|
|
76
|
+
class OutputNode extends Traversal {
|
|
51
77
|
constructor(val, loc, strip, exp) {
|
|
78
|
+
super();
|
|
52
79
|
Object.defineProperty(this, "val", {
|
|
53
80
|
enumerable: true,
|
|
54
81
|
configurable: true,
|
|
@@ -81,8 +108,9 @@
|
|
|
81
108
|
});
|
|
82
109
|
}
|
|
83
110
|
}
|
|
84
|
-
class TextNode {
|
|
111
|
+
class TextNode extends Traversal {
|
|
85
112
|
constructor(val, loc, strip) {
|
|
113
|
+
super();
|
|
86
114
|
Object.defineProperty(this, "val", {
|
|
87
115
|
enumerable: true,
|
|
88
116
|
configurable: true,
|
|
@@ -109,8 +137,9 @@
|
|
|
109
137
|
});
|
|
110
138
|
}
|
|
111
139
|
}
|
|
112
|
-
class IfNode {
|
|
140
|
+
class IfNode extends Traversal {
|
|
113
141
|
constructor(test, body, alternatives, loc, strip) {
|
|
142
|
+
super();
|
|
114
143
|
Object.defineProperty(this, "test", {
|
|
115
144
|
enumerable: true,
|
|
116
145
|
configurable: true,
|
|
@@ -148,9 +177,14 @@
|
|
|
148
177
|
value: NodeType.IF
|
|
149
178
|
});
|
|
150
179
|
}
|
|
180
|
+
traverse(cb) {
|
|
181
|
+
this.body?.forEach(cb);
|
|
182
|
+
this.alternatives?.forEach(node => node.body?.forEach(cb));
|
|
183
|
+
}
|
|
151
184
|
}
|
|
152
|
-
class ElseIfNode {
|
|
185
|
+
class ElseIfNode extends Traversal {
|
|
153
186
|
constructor(test, body, loc, strip) {
|
|
187
|
+
super();
|
|
154
188
|
Object.defineProperty(this, "test", {
|
|
155
189
|
enumerable: true,
|
|
156
190
|
configurable: true,
|
|
@@ -183,8 +217,9 @@
|
|
|
183
217
|
});
|
|
184
218
|
}
|
|
185
219
|
}
|
|
186
|
-
class ElseNode {
|
|
220
|
+
class ElseNode extends Traversal {
|
|
187
221
|
constructor(body, loc, strip) {
|
|
222
|
+
super();
|
|
188
223
|
Object.defineProperty(this, "body", {
|
|
189
224
|
enumerable: true,
|
|
190
225
|
configurable: true,
|
|
@@ -211,8 +246,9 @@
|
|
|
211
246
|
});
|
|
212
247
|
}
|
|
213
248
|
}
|
|
214
|
-
class ForNode {
|
|
249
|
+
class ForNode extends Traversal {
|
|
215
250
|
constructor(loop, body, loc, strip) {
|
|
251
|
+
super();
|
|
216
252
|
Object.defineProperty(this, "loop", {
|
|
217
253
|
enumerable: true,
|
|
218
254
|
configurable: true,
|
|
@@ -245,8 +281,9 @@
|
|
|
245
281
|
});
|
|
246
282
|
}
|
|
247
283
|
}
|
|
248
|
-
class BreakNode {
|
|
284
|
+
class BreakNode extends Traversal {
|
|
249
285
|
constructor(val, loc, strip) {
|
|
286
|
+
super();
|
|
250
287
|
Object.defineProperty(this, "val", {
|
|
251
288
|
enumerable: true,
|
|
252
289
|
configurable: true,
|
|
@@ -273,8 +310,9 @@
|
|
|
273
310
|
});
|
|
274
311
|
}
|
|
275
312
|
}
|
|
276
|
-
class ContinueNode {
|
|
313
|
+
class ContinueNode extends Traversal {
|
|
277
314
|
constructor(val, loc, strip) {
|
|
315
|
+
super();
|
|
278
316
|
Object.defineProperty(this, "val", {
|
|
279
317
|
enumerable: true,
|
|
280
318
|
configurable: true,
|
|
@@ -301,8 +339,9 @@
|
|
|
301
339
|
});
|
|
302
340
|
}
|
|
303
341
|
}
|
|
304
|
-
class UnknownDirectiveNode {
|
|
342
|
+
class UnknownDirectiveNode extends Traversal {
|
|
305
343
|
constructor(name, val, loc, strip) {
|
|
344
|
+
super();
|
|
306
345
|
Object.defineProperty(this, "name", {
|
|
307
346
|
enumerable: true,
|
|
308
347
|
configurable: true,
|
|
@@ -335,8 +374,9 @@
|
|
|
335
374
|
});
|
|
336
375
|
}
|
|
337
376
|
}
|
|
338
|
-
class UnexpectedDirectiveNode {
|
|
377
|
+
class UnexpectedDirectiveNode extends Traversal {
|
|
339
378
|
constructor(name, val, loc, strip) {
|
|
379
|
+
super();
|
|
340
380
|
Object.defineProperty(this, "name", {
|
|
341
381
|
enumerable: true,
|
|
342
382
|
configurable: true,
|
|
@@ -369,8 +409,9 @@
|
|
|
369
409
|
});
|
|
370
410
|
}
|
|
371
411
|
}
|
|
372
|
-
class CommentNode {
|
|
412
|
+
class CommentNode extends Traversal {
|
|
373
413
|
constructor(val, loc, strip) {
|
|
414
|
+
super();
|
|
374
415
|
Object.defineProperty(this, "val", {
|
|
375
416
|
enumerable: true,
|
|
376
417
|
configurable: true,
|
|
@@ -397,8 +438,9 @@
|
|
|
397
438
|
});
|
|
398
439
|
}
|
|
399
440
|
}
|
|
400
|
-
class IncludeNode {
|
|
441
|
+
class IncludeNode extends Traversal {
|
|
401
442
|
constructor(val, loc, strip) {
|
|
443
|
+
super();
|
|
402
444
|
Object.defineProperty(this, "val", {
|
|
403
445
|
enumerable: true,
|
|
404
446
|
configurable: true,
|
|
@@ -425,8 +467,9 @@
|
|
|
425
467
|
});
|
|
426
468
|
}
|
|
427
469
|
}
|
|
428
|
-
class BlockNode {
|
|
470
|
+
class BlockNode extends Traversal {
|
|
429
471
|
constructor(val, body, loc, strip) {
|
|
472
|
+
super();
|
|
430
473
|
Object.defineProperty(this, "val", {
|
|
431
474
|
enumerable: true,
|
|
432
475
|
configurable: true,
|
|
@@ -459,8 +502,9 @@
|
|
|
459
502
|
});
|
|
460
503
|
}
|
|
461
504
|
}
|
|
462
|
-
class SuperNode {
|
|
505
|
+
class SuperNode extends Traversal {
|
|
463
506
|
constructor(val, loc, strip) {
|
|
507
|
+
super();
|
|
464
508
|
Object.defineProperty(this, "val", {
|
|
465
509
|
enumerable: true,
|
|
466
510
|
configurable: true,
|
|
@@ -487,8 +531,9 @@
|
|
|
487
531
|
});
|
|
488
532
|
}
|
|
489
533
|
}
|
|
490
|
-
class MacroNode {
|
|
534
|
+
class MacroNode extends Traversal {
|
|
491
535
|
constructor(val, body, loc, strip) {
|
|
536
|
+
super();
|
|
492
537
|
Object.defineProperty(this, "val", {
|
|
493
538
|
enumerable: true,
|
|
494
539
|
configurable: true,
|
|
@@ -521,8 +566,9 @@
|
|
|
521
566
|
});
|
|
522
567
|
}
|
|
523
568
|
}
|
|
524
|
-
class CallerNode {
|
|
569
|
+
class CallerNode extends Traversal {
|
|
525
570
|
constructor(val, loc, strip) {
|
|
571
|
+
super();
|
|
526
572
|
Object.defineProperty(this, "val", {
|
|
527
573
|
enumerable: true,
|
|
528
574
|
configurable: true,
|
|
@@ -549,8 +595,9 @@
|
|
|
549
595
|
});
|
|
550
596
|
}
|
|
551
597
|
}
|
|
552
|
-
class CallNode {
|
|
598
|
+
class CallNode extends Traversal {
|
|
553
599
|
constructor(val, body, loc, strip) {
|
|
600
|
+
super();
|
|
554
601
|
Object.defineProperty(this, "val", {
|
|
555
602
|
enumerable: true,
|
|
556
603
|
configurable: true,
|
|
@@ -583,8 +630,9 @@
|
|
|
583
630
|
});
|
|
584
631
|
}
|
|
585
632
|
}
|
|
586
|
-
class SetNode {
|
|
633
|
+
class SetNode extends Traversal {
|
|
587
634
|
constructor(val, loc, strip) {
|
|
635
|
+
super();
|
|
588
636
|
Object.defineProperty(this, "val", {
|
|
589
637
|
enumerable: true,
|
|
590
638
|
configurable: true,
|
|
@@ -611,8 +659,9 @@
|
|
|
611
659
|
});
|
|
612
660
|
}
|
|
613
661
|
}
|
|
614
|
-
class CaptureNode {
|
|
662
|
+
class CaptureNode extends Traversal {
|
|
615
663
|
constructor(val, body, loc, strip) {
|
|
664
|
+
super();
|
|
616
665
|
Object.defineProperty(this, "val", {
|
|
617
666
|
enumerable: true,
|
|
618
667
|
configurable: true,
|
|
@@ -721,7 +770,7 @@ ${output
|
|
|
721
770
|
const val = parser.parseExp(token.expression);
|
|
722
771
|
parser.advance();
|
|
723
772
|
const body = parser.parseUntil(['endblock']);
|
|
724
|
-
if (parser.match('endblock')) {
|
|
773
|
+
if (parser.match(['endblock'])) {
|
|
725
774
|
parser.advance();
|
|
726
775
|
}
|
|
727
776
|
else {
|
|
@@ -745,7 +794,7 @@ ${output
|
|
|
745
794
|
const val = parser.parseExp(token.expression);
|
|
746
795
|
parser.advance();
|
|
747
796
|
const body = parser.parseUntil(['endcall']);
|
|
748
|
-
if (parser.match('endcall')) {
|
|
797
|
+
if (parser.match(['endcall'])) {
|
|
749
798
|
parser.advance();
|
|
750
799
|
}
|
|
751
800
|
else {
|
|
@@ -763,7 +812,7 @@ ${output
|
|
|
763
812
|
const val = parser.parseExp(token.expression);
|
|
764
813
|
parser.advance();
|
|
765
814
|
const body = parser.parseUntil(['endcapture']);
|
|
766
|
-
if (parser.match('endcapture')) {
|
|
815
|
+
if (parser.match(['endcapture'])) {
|
|
767
816
|
parser.advance();
|
|
768
817
|
}
|
|
769
818
|
else {
|
|
@@ -781,7 +830,7 @@ ${output
|
|
|
781
830
|
const loop = parser.parseExp(token.expression);
|
|
782
831
|
parser.advance();
|
|
783
832
|
const body = parser.parseUntil(['endfor']);
|
|
784
|
-
if (parser.match('endfor')) {
|
|
833
|
+
if (parser.match(['endfor'])) {
|
|
785
834
|
parser.advance();
|
|
786
835
|
}
|
|
787
836
|
else {
|
|
@@ -811,13 +860,13 @@ ${output
|
|
|
811
860
|
parser.advance();
|
|
812
861
|
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
813
862
|
const alternatives = [];
|
|
814
|
-
while (parser.
|
|
863
|
+
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
815
864
|
const branchToken = parser.peek();
|
|
816
865
|
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
817
866
|
? parseElse(branchToken, parser)
|
|
818
867
|
: parseElseIf(branchToken, parser));
|
|
819
868
|
}
|
|
820
|
-
if (parser.match('endif')) {
|
|
869
|
+
if (parser.match(['endif'])) {
|
|
821
870
|
parser.advance();
|
|
822
871
|
}
|
|
823
872
|
else {
|
|
@@ -827,10 +876,9 @@ ${output
|
|
|
827
876
|
}
|
|
828
877
|
function parseElseIf(token, parser) {
|
|
829
878
|
parser.requireExpression(token);
|
|
830
|
-
const test = parser.parseExp(token.expression);
|
|
831
879
|
parser.advance();
|
|
832
|
-
const body = parser.parseUntil(['else', 'elseif', 'elif', 'endif']);
|
|
833
|
-
return new ElseIfNode(
|
|
880
|
+
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
881
|
+
return new ElseIfNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
834
882
|
}
|
|
835
883
|
function parseElse(token, parser) {
|
|
836
884
|
parser.requireNoExpression(token);
|
|
@@ -861,7 +909,7 @@ ${output
|
|
|
861
909
|
const name = parser.parseExp(token.expression);
|
|
862
910
|
parser.advance();
|
|
863
911
|
const body = parser.parseUntil(['endmacro']);
|
|
864
|
-
if (parser.match('endmacro')) {
|
|
912
|
+
if (parser.match(['endmacro'])) {
|
|
865
913
|
parser.advance();
|
|
866
914
|
}
|
|
867
915
|
else {
|
|
@@ -1094,25 +1142,15 @@ ${output
|
|
|
1094
1142
|
values: values
|
|
1095
1143
|
});
|
|
1096
1144
|
|
|
1097
|
-
async function compileBlock({ val: { value } }, compiler) {
|
|
1098
|
-
if
|
|
1099
|
-
return;
|
|
1100
|
-
}
|
|
1101
|
-
compiler.state.block = value;
|
|
1102
|
-
const blockNode = compiler.blocks.get(value).pop();
|
|
1103
|
-
await compiler.compileNodes(blockNode.body);
|
|
1104
|
-
compiler.blocks.delete(value);
|
|
1105
|
-
compiler.state.block = undefined;
|
|
1145
|
+
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1146
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, `}`);
|
|
1106
1147
|
}
|
|
1107
1148
|
async function compileSuper({ loc }, compiler) {
|
|
1108
1149
|
if (!compiler.state.block) {
|
|
1109
1150
|
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1110
1151
|
return;
|
|
1111
1152
|
}
|
|
1112
|
-
|
|
1113
|
-
if (block) {
|
|
1114
|
-
await compiler.compileNodes(block.body);
|
|
1115
|
-
}
|
|
1153
|
+
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
1116
1154
|
}
|
|
1117
1155
|
const compilers$b = {
|
|
1118
1156
|
[NodeType.BLOCK]: compileBlock,
|
|
@@ -1332,7 +1370,7 @@ ${output
|
|
|
1332
1370
|
};
|
|
1333
1371
|
|
|
1334
1372
|
async function compileInclude({ val: path, loc }, compiler) {
|
|
1335
|
-
compiler.pushRaw(loc, `
|
|
1373
|
+
compiler.pushRaw(loc, `s+=await p["${path.value}"]?.()??"";`);
|
|
1336
1374
|
}
|
|
1337
1375
|
const compilers$5 = {
|
|
1338
1376
|
[NodeType.INCLUDE]: compileInclude,
|
|
@@ -1366,7 +1404,6 @@ ${output
|
|
|
1366
1404
|
|
|
1367
1405
|
async function compileSet({ val, loc }, compiler) {
|
|
1368
1406
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1369
|
-
compiler.pushRaw(null, `});`);
|
|
1370
1407
|
}
|
|
1371
1408
|
const compilers$2 = {
|
|
1372
1409
|
[NodeType.SET]: compileSet,
|
|
@@ -1419,7 +1456,7 @@ ${output
|
|
|
1419
1456
|
const renderOptions = {
|
|
1420
1457
|
...compilerOptions,
|
|
1421
1458
|
globals: {},
|
|
1422
|
-
filters
|
|
1459
|
+
filters,
|
|
1423
1460
|
autoEscape: true,
|
|
1424
1461
|
};
|
|
1425
1462
|
|
|
@@ -2357,7 +2394,7 @@ ${output
|
|
|
2357
2394
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2358
2395
|
this.expParser = new ExpParser(template);
|
|
2359
2396
|
this.cursor = 0;
|
|
2360
|
-
return new
|
|
2397
|
+
return new RootNode(this.parseUntil(), { start, end });
|
|
2361
2398
|
}
|
|
2362
2399
|
parseUntil(names) {
|
|
2363
2400
|
const nodes = [];
|
|
@@ -2366,7 +2403,8 @@ ${output
|
|
|
2366
2403
|
const token = this.peek();
|
|
2367
2404
|
if (!token
|
|
2368
2405
|
|| (token.type === TokenType.DIRECTIVE
|
|
2369
|
-
&& names
|
|
2406
|
+
&& names
|
|
2407
|
+
&& this.match(names, token))) {
|
|
2370
2408
|
break;
|
|
2371
2409
|
}
|
|
2372
2410
|
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
@@ -2428,15 +2466,8 @@ ${output
|
|
|
2428
2466
|
advance() {
|
|
2429
2467
|
this.cursor++;
|
|
2430
2468
|
}
|
|
2431
|
-
match(
|
|
2432
|
-
return
|
|
2433
|
-
}
|
|
2434
|
-
isDirective(names) {
|
|
2435
|
-
const token = this.peek();
|
|
2436
|
-
if (token?.type !== TokenType.DIRECTIVE) {
|
|
2437
|
-
return false;
|
|
2438
|
-
}
|
|
2439
|
-
return names.includes(token.name.toLowerCase());
|
|
2469
|
+
match(names, token = this.peek()) {
|
|
2470
|
+
return names.includes(token?.name);
|
|
2440
2471
|
}
|
|
2441
2472
|
requireExpression({ expression, name, loc }) {
|
|
2442
2473
|
if (!expression) {
|
|
@@ -2465,31 +2496,31 @@ ${output
|
|
|
2465
2496
|
writable: true,
|
|
2466
2497
|
value: void 0
|
|
2467
2498
|
});
|
|
2468
|
-
Object.defineProperty(this, "
|
|
2499
|
+
Object.defineProperty(this, "state", {
|
|
2469
2500
|
enumerable: true,
|
|
2470
2501
|
configurable: true,
|
|
2471
2502
|
writable: true,
|
|
2472
2503
|
value: void 0
|
|
2473
2504
|
});
|
|
2474
|
-
Object.defineProperty(this, "
|
|
2505
|
+
Object.defineProperty(this, "isRoot", {
|
|
2475
2506
|
enumerable: true,
|
|
2476
2507
|
configurable: true,
|
|
2477
2508
|
writable: true,
|
|
2478
2509
|
value: void 0
|
|
2479
2510
|
});
|
|
2480
|
-
Object.defineProperty(this, "
|
|
2511
|
+
Object.defineProperty(this, "rootNode", {
|
|
2481
2512
|
enumerable: true,
|
|
2482
2513
|
configurable: true,
|
|
2483
2514
|
writable: true,
|
|
2484
2515
|
value: void 0
|
|
2485
2516
|
});
|
|
2486
|
-
Object.defineProperty(this, "
|
|
2517
|
+
Object.defineProperty(this, "partials", {
|
|
2487
2518
|
enumerable: true,
|
|
2488
2519
|
configurable: true,
|
|
2489
2520
|
writable: true,
|
|
2490
2521
|
value: void 0
|
|
2491
2522
|
});
|
|
2492
|
-
Object.defineProperty(this, "
|
|
2523
|
+
Object.defineProperty(this, "blocks", {
|
|
2493
2524
|
enumerable: true,
|
|
2494
2525
|
configurable: true,
|
|
2495
2526
|
writable: true,
|
|
@@ -2500,105 +2531,91 @@ ${output
|
|
|
2500
2531
|
...options,
|
|
2501
2532
|
};
|
|
2502
2533
|
}
|
|
2503
|
-
async compile(template, root = true, blocks = new Map()) {
|
|
2534
|
+
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2504
2535
|
this.template = template;
|
|
2505
|
-
this.
|
|
2506
|
-
this.partials =
|
|
2536
|
+
this.isRoot = root;
|
|
2537
|
+
this.partials = partials;
|
|
2507
2538
|
this.blocks = blocks;
|
|
2508
|
-
this.
|
|
2539
|
+
this.rootNode = new Parser(this.options).parse(template);
|
|
2509
2540
|
this.state = {};
|
|
2510
2541
|
this.start();
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
if (this.partials.size
|
|
2514
|
-
this.pushRaw(null, `const
|
|
2542
|
+
await this.parseTemplates();
|
|
2543
|
+
if (this.isRoot) {
|
|
2544
|
+
if (this.partials.size) {
|
|
2545
|
+
this.pushRaw(null, `const p={`);
|
|
2515
2546
|
for (const [path, code] of this.partials) {
|
|
2516
|
-
this.pushRaw(null, `"${path}":async
|
|
2547
|
+
this.pushRaw(null, `"${path}":async()=>{`, code, `},`);
|
|
2548
|
+
}
|
|
2549
|
+
this.pushRaw(null, `};`);
|
|
2550
|
+
}
|
|
2551
|
+
if (this.blocks.size) {
|
|
2552
|
+
this.pushRaw(null, `const b={`);
|
|
2553
|
+
for (const [name, blocks] of this.blocks) {
|
|
2554
|
+
this.pushRaw(null, `"${name}":{`, `u:0,`, `s:[`);
|
|
2555
|
+
this.state.block = name;
|
|
2556
|
+
for (const block of blocks) {
|
|
2557
|
+
this.pushRaw(null, `async()=>{`, `let s="";`);
|
|
2558
|
+
await this.compileNodes(block.body);
|
|
2559
|
+
this.pushRaw(null, `return s;`, `},`);
|
|
2560
|
+
}
|
|
2561
|
+
this.state.block = undefined;
|
|
2562
|
+
this.pushRaw(null, `],`, `},`);
|
|
2517
2563
|
}
|
|
2518
2564
|
this.pushRaw(null, `};`);
|
|
2519
2565
|
}
|
|
2520
2566
|
}
|
|
2521
|
-
await this.compileNode(this.
|
|
2567
|
+
await this.compileNode(this.rootNode);
|
|
2522
2568
|
this.end();
|
|
2523
2569
|
return this;
|
|
2524
2570
|
}
|
|
2525
2571
|
async parseTemplates() {
|
|
2526
|
-
const
|
|
2527
|
-
const
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
}
|
|
2533
|
-
const { includes, blocks } = this.collectIncludesAndBlocks();
|
|
2534
|
-
for (const [path, node] of includes) {
|
|
2535
|
-
if (!astMap.has(path)) {
|
|
2536
|
-
try {
|
|
2537
|
-
queue.push({
|
|
2538
|
-
path,
|
|
2539
|
-
node,
|
|
2540
|
-
template: await this.options.loader(path),
|
|
2541
|
-
});
|
|
2542
|
-
}
|
|
2543
|
-
catch {
|
|
2544
|
-
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2545
|
-
return;
|
|
2546
|
-
}
|
|
2572
|
+
const { partials, blocks } = this.collectPartialsAndBlocks();
|
|
2573
|
+
for (const [path, node] of partials) {
|
|
2574
|
+
if (!this.partials.has(path)) {
|
|
2575
|
+
let template;
|
|
2576
|
+
try {
|
|
2577
|
+
template = await this.options.loader(path);
|
|
2547
2578
|
}
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2579
|
+
catch {
|
|
2580
|
+
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2581
|
+
return;
|
|
2582
|
+
}
|
|
2583
|
+
if (template) {
|
|
2584
|
+
await this.compilePartial(path, node, template);
|
|
2553
2585
|
}
|
|
2554
|
-
this.blocks.get(value).push(block);
|
|
2555
2586
|
}
|
|
2556
2587
|
}
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
if (!this.
|
|
2560
|
-
|
|
2588
|
+
for (const block of blocks) {
|
|
2589
|
+
const { val: { value } } = block;
|
|
2590
|
+
if (!this.blocks.has(value)) {
|
|
2591
|
+
this.blocks.set(value, []);
|
|
2561
2592
|
}
|
|
2593
|
+
this.blocks.get(value).push(block);
|
|
2562
2594
|
}
|
|
2563
2595
|
}
|
|
2564
|
-
|
|
2565
|
-
const
|
|
2596
|
+
collectPartialsAndBlocks() {
|
|
2597
|
+
const partials = new Map();
|
|
2566
2598
|
const blocks = [];
|
|
2567
2599
|
const traverse = (node) => {
|
|
2568
2600
|
if (node.type === NodeType.INCLUDE) {
|
|
2569
|
-
if (!
|
|
2570
|
-
|
|
2601
|
+
if (!partials.has(node.val.value)) {
|
|
2602
|
+
partials.set(node.val.value, node);
|
|
2571
2603
|
}
|
|
2572
2604
|
}
|
|
2573
2605
|
if (node.type === NodeType.BLOCK) {
|
|
2574
2606
|
blocks.push(node);
|
|
2575
2607
|
}
|
|
2576
|
-
|
|
2577
|
-
node.children.forEach(traverse);
|
|
2578
|
-
}
|
|
2579
|
-
if (node.type === NodeType.IF) {
|
|
2580
|
-
const ifNode = node;
|
|
2581
|
-
ifNode.body.forEach(traverse);
|
|
2582
|
-
ifNode.alternatives.forEach((alt) => {
|
|
2583
|
-
alt.body.forEach(traverse);
|
|
2584
|
-
});
|
|
2585
|
-
}
|
|
2586
|
-
if (node.type === NodeType.FOR) {
|
|
2587
|
-
node.body.forEach(traverse);
|
|
2588
|
-
}
|
|
2589
|
-
if (node.type === NodeType.BLOCK) {
|
|
2590
|
-
node.body.forEach(traverse);
|
|
2591
|
-
}
|
|
2608
|
+
node.traverse(traverse);
|
|
2592
2609
|
};
|
|
2593
|
-
traverse(this.
|
|
2610
|
+
traverse(this.rootNode);
|
|
2594
2611
|
return {
|
|
2595
|
-
|
|
2612
|
+
partials,
|
|
2596
2613
|
blocks,
|
|
2597
2614
|
};
|
|
2598
2615
|
}
|
|
2599
2616
|
async compileNode(node) {
|
|
2600
2617
|
if (node.type === NodeType.TEMPLATE) {
|
|
2601
|
-
await this.compileNodes(node.
|
|
2618
|
+
await this.compileNodes(node.body);
|
|
2602
2619
|
return;
|
|
2603
2620
|
}
|
|
2604
2621
|
if (this.options.compilers[node.type]) {
|
|
@@ -2612,9 +2629,9 @@ ${output
|
|
|
2612
2629
|
await this.compileNode(child);
|
|
2613
2630
|
}
|
|
2614
2631
|
}
|
|
2615
|
-
async compilePartial(path, node, template
|
|
2632
|
+
async compilePartial(path, node, template) {
|
|
2616
2633
|
try {
|
|
2617
|
-
const { code } = await new Compiler(this.options).compile(template, false, blocks);
|
|
2634
|
+
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks);
|
|
2618
2635
|
this.partials.set(path, code);
|
|
2619
2636
|
}
|
|
2620
2637
|
catch {
|