You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
import nunjucks from 'nunjucks'
|
|
import { minify } from 'html-minifier'
|
|
|
|
import { asset, media } from './helpers/engine.js'
|
|
|
|
/**
|
|
* engine - handle eta.js
|
|
*
|
|
* @author Björn Hase <me@herr-hase.wtf>
|
|
* @license http://opensource.org/licenses/MIT The MIT License
|
|
* @link https://gitea.node001.net/HerrHase/happy-site-webpack-plugin.git
|
|
*
|
|
*/
|
|
class Engine {
|
|
|
|
constructor(views, site, options) {
|
|
|
|
// merge options
|
|
this._options = Object.assign({}, {
|
|
autoescapes: true,
|
|
throwOnUndefined: true
|
|
}, options)
|
|
|
|
this.nunjucks = nunjucks.configure(views, this._options)
|
|
this.nunjucks.addFilter('media', function(options) {
|
|
return media(options)
|
|
})
|
|
|
|
// adding defaults for view, function and data from config.yml
|
|
this._defaults = {
|
|
site: site,
|
|
asset: asset
|
|
}
|
|
}
|
|
|
|
/**
|
|
* render
|
|
*
|
|
* @param {string} view
|
|
* @param {object} data
|
|
* @return {string}
|
|
*
|
|
*/
|
|
render(view, data) {
|
|
data = Object.assign({}, data, this._defaults)
|
|
|
|
return minify(this.nunjucks.render(view, data), {
|
|
removeComments: true,
|
|
collapseWhitespace: true
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
export default Engine |