Skip to content

Commit

Permalink
Adds support for methods: compileFileAsync, render, renderFile, rende…
Browse files Browse the repository at this point in the history
…rFileAsync
  • Loading branch information
webketje committed Oct 6, 2022
1 parent 4012864 commit 72311e9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
57 changes: 49 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,58 @@
const fs = require('fs')
const ejs = require('ejs')

exports.name = 'ejs'
exports.outputFormat = 'html'
exports.compile = ejs.compile
exports.compileClient = function (source, options) {
const transformer = {
name: 'ejs',
outputFormat: 'html',
}

transformer.compile = function (source, options) {
return ejs.compile(source, { ...options, async: false, client: false })
}

transformer.compileClient = function (source, options) {
return ejs.compile(source, { ...options, async: false, client: true }).toString()
}

transformer.compileFile = function (path, options) {
options = options || {}
options.client = true
return exports.compile(source, options).toString()
options.filename = options.filename || path
return transformer.compile(fs.readFileSync(path, 'utf8'), options)
}

exports.compileFile = function (path, options) {
transformer.compileFileAsync = function (path, options) {
options = options || {}
options.filename = options.filename || path
return exports.compile(fs.readFileSync(path, 'utf8'), options)
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (error, source) => {
if (error) {
reject(error)
}

resolve(transformer.compile(source, options))
})
})
}

transformer.render = function (source, options, locals) {
return ejs.render(source, locals, { ...options, async: false })
}

transformer.renderFile = function (path, options, locals = null) {
options.filename = options.filename || path
return ejs.render(fs.readFileSync(path, 'utf8'), locals, { ...options, async: false })
}

transformer.renderFileAsync = function (path, options, locals = null) {
return new Promise((resolve, reject) => {
ejs.renderFile(path, locals, options, (error, result) => {
if (error) {
reject(error)
}

resolve(result)
})
})
}

module.exports = transformer
2 changes: 1 addition & 1 deletion test/input.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ul>
<@ pets.forEach(function(pet){ @>
<@ include pet @>
<@- include('pet', { pet }) @>
<@ }) @>
</ul>

0 comments on commit 72311e9

Please sign in to comment.