forked from alsolovyev/webpack-nunjucks-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (48 loc) · 1.58 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const path = require('path')
const { Environment, FileSystemLoader } = require('nunjucks')
const log = require('webpack-log')({ name: 'nunjucks' })
const schema = require('./schema')
const iconFn = require('./functions/icon')
/**
* Transpiles nunjucks files into a string.
* https://webpack.js.org/contribute/writing-a-loader
*
* @requires nunjucks
* @requires html-loader
*
* @todo Hide common code (njk config)
*
* @param {String} source - the contents of the raw resource
* @returns {String} a valid HTML string
*/
module.exports = function(source) {
const callback = this.async()
const { autoescape, context, templates } = this.getOptions(schema)
const env = new Environment(
new FileSystemLoader(path.resolve(this.rootContext, templates || '')),
{
autoescape: autoescape || true
}
)
// Add a file as dependency of the loader result in order to make them watchable.
env.on('load', (name, source) => this.addDependency(source.path))
// Add a global value that will be available to all templates.
env.addGlobal('icon', iconFn)
// Transpile
env.renderString(source, context, (error, HTMLString) => {
if (error) {
const [ location, description ] = error.message.split('\n ')
/**
* Do not emit an error to prevent the "HTML Webpack"
* plugin from outputting useless text to the terminal.
*/
callback(null, `<pre style='font-size:30px'>${ error.message }</pre>`)
process.stdout.write('\n')
log.error(description)
log.error(location)
} else {
callback(null, HTMLString)
}
})
return
}