forked from seldo/nunjucks-hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
50 lines (36 loc) · 1.04 KB
/
index.cjs
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
const Nunjucks = require('nunjucks')
const debug = require('debug')
const log = debug('@modernpoacher/nunjucks')
log('`@modernpoacher/nunjucks` is awake')
let ENVIRONMENT
function compile (src, options = {}, next = null) {
log('compile')
const template = Nunjucks.compile(src, ENVIRONMENT || (ENVIRONMENT = options.environment), (Reflect.has(options, 'filename') ? Reflect.get(options, 'filename') : null))
return (next instanceof Function)
? next(null, (context, options, next) => template.render(context, next))
: (context) => template.render(context)
}
function prepare (options = {}, next = () => {}) {
log('prepare')
const {
path,
compileOptions = {}
} = options
options.compileOptions = {
...compileOptions,
environment: ENVIRONMENT || configure(path, { watch: false })
}
return next()
}
function configure (path, options = { watch: false }) {
log('configure')
return (
ENVIRONMENT = Nunjucks.configure(path, options)
)
}
module.exports = {
...Nunjucks,
compile,
prepare,
configure
}