-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
103 lines (94 loc) · 3.34 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'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,
ignoreCustomFragments: [ /{{.*?}}/ ]
};
module.exports = function(source) {
var query = loaderUtils.getOptions(this) || {};
var hoganOpts = Object.assign({}, query, { asString: true });
delete hoganOpts.minify;
delete hoganOpts.noShortcut;
delete hoganOpts.clientSide;
delete hoganOpts.tiny;
var render;
if (query.render) {
if (query.clientSide) {
this.callback(new Error('"render" and "clientSide" options are mutually exclusive'));
}
render = typeof hoganOpts.render === 'function' ? hoganOpts.render() : hoganOpts.render;
delete hoganOpts.render;
}
if (this.cacheable) {
this.cacheable();
}
// minify?
if (query.minify) {
// `?minify`
var minifierOptions = minifierDefaults;
// `?{minify:{...}}`
if (Object.prototype.toString.call(query.minify) === '[object Object]') {
minifierOptions = Object.assign({}, minifierOptions, query.minify);
}
source = minifier.minify(source, minifierOptions);
}
var suffix;
if (query.noShortcut) {
suffix = 'return T; }();';
} else if (query.render) {
suffix = 'return T.render(' + JSON.stringify(render) + ');};';
} else {
suffix = 'return T.render.apply(T, arguments); };';
}
if (query.clientSide) {
return 'var H = require("hogan.js");\n' +
'module.exports = function() { ' +
'var src = ' + JSON.stringify(source) + ' \n' +
'var T = H.compile(src, ' + JSON.stringify(hoganOpts) + ');\n' + suffix;
}
if (query.tiny) {
return 'var H = require("hogan.js");\n' +
'module.exports = function() { ' +
'var T = new H.Template(' +
Hogan.compile(source, hoganOpts) +
');' + suffix;
}
return 'var H = require("hogan.js");\n' +
'module.exports = function() { ' +
'var T = new H.Template(' +
Hogan.compile(source, hoganOpts) +
', ' +
JSON.stringify(source) +
', H);' + suffix;
};
module.exports.pitch = function(remainingRequest, precedingRequest, data) {
if (remainingRequest.indexOf('!') >= 0) {
var query = loaderUtils.getOptions(this) || {};
var hoganOpts = Object.assign({}, query);
delete hoganOpts.minify;
delete hoganOpts.noShortcut;
delete hoganOpts.render;
if (this.cacheable) {
this.cacheable();
}
var suffix;
if (query.noShortcut) {
suffix = 'return T; }();';
} else {
suffix = 'return T.render.apply(T, arguments); };';
}
return 'var result = require(' + loaderUtils.stringifyRequest(this, '!!' + remainingRequest) + ')\n' +
'var H = require("hogan.js");\n' +
'module.exports = function() {\n' +
'var T = H.compile(result, ' + JSON.stringify(hoganOpts) + ');\n' +
suffix;
}
};