-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (25 loc) · 889 Bytes
/
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
const cons = require('consolidate')
const loaderUtils = require('loader-utils')
const extname = require('path').extname
module.exports = function (content) {
this.cacheable && this.cacheable()
const callback = this.async()
const opt = loaderUtils.getOptions(this)
function exportContent(content) {
callback(null, opt.raw ? content : 'module.exports = ' + JSON.stringify(content))
}
// with no engine given, use the file extension as engine
opt.engine = opt.engine || extname(this.request).substr(1).toLowerCase()
if (!cons[opt.engine]) {
return callback(new Error(
'Template engine \'' + opt.engine + '\' ' +
'isn\'t available in Consolidate.js'
))
}
// for relative includes
opt.filename = this.resourcePath
cons[opt.engine].render(content, opt, function (err, html) {
if (err) return callback(err)
exportContent(html)
})
}