forked from deepsweet/mustache-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (50 loc) · 1.54 KB
/
index.js
File metadata and controls
62 lines (50 loc) · 1.54 KB
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
55
56
57
58
59
60
61
62
'use strict';
var loaderUtils = require('loader-utils');
var Hogan = require('hogan.js');
var minifier = require('html-minifier');
// https://github.com/kangax/html-minifier#options-quick-reference
var minifierDefaults = {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
caseSensitive: true
};
// :)
var extend = function(target, source) {
target = JSON.parse(JSON.stringify(target));
Object.keys(source).forEach(function(key) {
target[key] = source[key];
});
return target;
};
module.exports = function(source) {
var query = loaderUtils.parseQuery(this.query);
if (this.cacheable) {
this.cacheable();
}
// minify?
if (query.minify) {
// `?minify`
var minifierOptions = minifierDefaults;
// `?{minify:{...}}`
if (Object.prototype.toString.call(query.minify) === '[object Object]') {
minifierOptions = extend(minifierOptions, query.minify);
}
source = minifier.minify(source, minifierOptions);
}
var suffix;
if (query.noShortcut) {
suffix = 'return T; }();';
} else {
suffix = 'return T.render.apply(T, arguments); };';
}
return 'var H = require("hogan.js");\n' +
'module.exports = function() { ' +
'var T = new H.Template(' +
Hogan.compile(source, { asString: true }) +
', ' +
JSON.stringify(source) +
', H);' + suffix;
};