-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
129 lines (104 loc) · 3.67 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var path = require('path');
var loaderUtils = require('loader-utils');
var attributeParser = require('./lib/attributeParser');
var macroParser = require('./lib/macroParser');
// Try getting underscore first, then lodash
var _;
try {
_ = require('underscore');
} catch (e) {
_ = require('lodash');
}
// Extendable arguments
var macros = _.extend({}, require('./lib/macros'));
module.exports = function(content) {
this.cacheable && this.cacheable();
var callback = this.async();
// Default arguments
var root,
parseMacros = true,
engine = false,
withImports = false,
attributes = ['img:src'],
parseDynamicRoutes = false;
// Parse arguments
var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query || '?');
if (_.isObject(query)) {
root = query.root;
// Apply template settings
_.each(_.pick(query, 'interpolate', 'escape', 'evaluate'), function(value, key) {
_.templateSettings[key] = new RegExp(value, 'g');
});
// Apply template variable
if (query.variable !== undefined) {
_.templateSettings.variable = query.variable;
}
// Set tag+attribute to parse for external resources
if (query.attributes !== undefined) {
attributes = _.isArray(query.attributes) ? query.attributes : [];
}
// Parse / ignore macros
if (query.parseMacros !== undefined) {
parseMacros = !!query.parseMacros;
}
// Template engine
if (query.engine !== undefined) {
engine = query.engine;
}
// Template settings imports (on by default for lodash)
if (query.withImports !== undefined) {
withImports = query.withImports;
} else if (engine === 'lodash') {
withImports = true;
}
// Prepend a html comment with the filename in it
if (query.prependFilenameComment) {
var filenameRelative = path.relative(query.prependFilenameComment, this.resource);
content = "\n<!-- " + filenameRelative + " -->\n" + content;
}
// Check if dynamic routes must be parsed
if (query.parseDynamicRoutes !== undefined) {
parseDynamicRoutes = !!query.parseDynamicRoutes;
}
// support macros in loader options. because it isn't support customer options from webpack@4
if (_.isObject(query.macros)) {
_.extend(macros, query.macros);
}
}
// Include additional macros
if (this.options && _.isObject(this.options.macros)) {
_.extend(macros, this.options.macros);
}
// Parse macros
if (parseMacros) {
var macrosContext = macroParser(content, function (macro) {
return _.isFunction(macros[macro]);
}, 'MACRO');
content = macrosContext.replaceMatches(content);
}
// Parse attributes
var attributesContext = attributeParser(content, function (tag, attr) {
return attributes.indexOf(tag + ':' + attr) != -1;
}, 'ATTRIBUTE', root, parseDynamicRoutes);
content = attributesContext.replaceMatches(content);
// Compile template
var source = _.template(content).source;
// Resolve macros
if (parseMacros) {
source = macrosContext.resolveMacros(source, macros);
}
// Resolve attributes
source = attributesContext.resolveAttributes(source);
// Build the module export, optionally with template imports
if (withImports) {
source = 'module.exports = Function(_.keys(_.templateSettings.imports), \'return \' + ' + source + '.toString()).apply(undefined, _.values(_.templateSettings.imports));\n';
} else {
source = 'module.exports = ' + source + ';\n';
}
// Explicitly require the engine, otherwise it will rely on the global _
if (engine) {
source = 'var _ = require(\'' + engine + '\');\n' + source;
}
callback(null, source);
};
module.exports._ = _;