-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
96 lines (78 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const app = require('./package.json')
const { registerCoreHelpers } = require('./helpers')
const isPromise = (obj) => !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
function asyncHelpers(hbs) {
const handlebars = hbs.create(),
asyncCompiler = class extends hbs.JavaScriptCompiler {
constructor() {
super()
this.compiler = asyncCompiler
}
mergeSource(varDeclarations) {
const sources = super.mergeSource(varDeclarations)
sources.prepend('return (async () => {')
sources.add(' })()')
return sources
}
appendToBuffer(source, location, explicit) {
// Force a source as this simplifies the merge logic.
if (!Array.isArray(source)) {
source = [source]
}
source = this.source.wrap(source, location)
if (this.environment.isSimple) {
return ['return await ', source, ';']
} if (explicit) {
// This is a case where the buffer operation occurs as a child of another
// construct, generally braces. We have to explicitly output these buffer
// operations to ensure that the emitted code goes in the correct location.
return ['buffer += await ', source, ';']
}
source.appendToBuffer = true
source.prepend('await ')
return source
}
}
handlebars.JavaScriptCompiler = asyncCompiler
const _compile = handlebars.compile,
_template = handlebars.VM.template,
_escapeExpression = handlebars.escapeExpression,
escapeExpression = function(value) {
if(isPromise(value)) {
return value.then((v) => _escapeExpression(v))
}
return _escapeExpression(value)
}
function lookupProperty(containerLookupProperty) {
return function(parent, propertyName) {
if (isPromise(parent)) {
return parent.then((p) => containerLookupProperty(p, propertyName))
}
return containerLookupProperty(parent, propertyName)
}
}
handlebars.template = function(spec) {
spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => {
// const main = await spec.main
container.escapeExpression = escapeExpression
container.lookupProperty = lookupProperty(container.lookupProperty)
if(depths.length == 0){
depths = [data.root]
}
const v = spec.main(container, context, container.helpers, container.partials, data, blockParams, depths)
return v
}
return _template(spec, handlebars)
}
handlebars.compile = function(template, options) {
const compiled = _compile.apply(handlebars, [template, { ...options }])
return function(context, execOptions) {
context = context || {}
return compiled.call(handlebars, context, execOptions)
}
}
handlebars.ASYNC_VERSION = app.version
registerCoreHelpers(handlebars)
return handlebars
}
module.exports = asyncHelpers