Transform snippets into a map in the browser for faster access
This commit is contained in:
parent
64855bbdf0
commit
861d48dbf3
|
@ -51,10 +51,16 @@ module.exports.processJS = (handlebars, context) => {
|
||||||
// This is required to allow the final `return` statement to be valid.
|
// This is required to allow the final `return` statement to be valid.
|
||||||
const js = iifeWrapper(atob(handlebars))
|
const js = iifeWrapper(atob(handlebars))
|
||||||
|
|
||||||
|
// Transform snippets into an object for faster access
|
||||||
|
let snippetMap = {}
|
||||||
|
for (let snippet of context.snippets || []) {
|
||||||
|
snippetMap[snippet.name] = snippet.code
|
||||||
|
}
|
||||||
|
|
||||||
// Our $ context function gets a value from context.
|
// Our $ context function gets a value from context.
|
||||||
// We clone the context to avoid mutation in the binding affecting real
|
// We clone the context to avoid mutation in the binding affecting real
|
||||||
// app context.
|
// app context.
|
||||||
const clonedContext = cloneDeep(context)
|
const clonedContext = cloneDeep({ ...context, snippets: null })
|
||||||
const sandboxContext = {
|
const sandboxContext = {
|
||||||
$: path => getContextValue(path, clonedContext),
|
$: path => getContextValue(path, clonedContext),
|
||||||
helpers: getJsHelperList(),
|
helpers: getJsHelperList(),
|
||||||
|
@ -64,9 +70,7 @@ module.exports.processJS = (handlebars, context) => {
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
get: function (_, name) {
|
get: function (_, name) {
|
||||||
// This will error if the snippet doesn't exist, but that's intended
|
return eval(iifeWrapper(snippetMap[name]))
|
||||||
const snippet = (context.snippets || []).find(x => x.name === name)
|
|
||||||
return eval(iifeWrapper(snippet.code))
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue