Skip to content

Commit

Permalink
feat: allow async
Browse files Browse the repository at this point in the history
  • Loading branch information
lubomirblazekcz committed Aug 13, 2022
1 parent a9d8f52 commit 2cd37bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function processData(paths, data = {}) {
return context
}

const renderTemplate = (filename, content, options) => {
const renderTemplate = async(filename, content, options) => {
const output = {}
const context = processData(options.data, options.globals)

Expand Down Expand Up @@ -79,17 +79,15 @@ const renderTemplate = (filename, content, options) => {
lodash.merge(context, JSON.parse(fs.readFileSync(filename + '.json').toString()))
}

try {
output.content = Twig.twig({
async: true,
data: content,
path: filename,
namespaces: options.namespaces,
rethrow: true
}).render(context)
} catch (error) {
output.content = await Twig.twig({
allowAsync: true,
data: content,
path: filename,
namespaces: options.namespaces,
rethrow: true
}).renderAsync(context).catch((error) => {
output.error = error
}
})

return output
}
Expand Down Expand Up @@ -117,7 +115,7 @@ const plugin = (options = {}) => {
return content
}

const render = renderTemplate(filename, content, options)
const render = await renderTemplate(filename, content, options)

if (render.error) {
if (!server) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vituum/vite-plugin-twig",
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"main": "index.js",
"dependencies": {
Expand Down

0 comments on commit 2cd37bd

Please sign in to comment.