-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (31 loc) · 1006 Bytes
/
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
var loaderUtils = require('loader-utils');
var textTags = ['title', 'style', 'script'];
module.exports = function (str) {
if (this.cacheable) {
this.cacheable();
}
var query = loaderUtils.parseQuery(this.query);
var tag = query.tag || 'div';
var asText = query.asText || textTags.indexOf(tag) !== -1;
var attrs = query.attrs || [];
var out = [str];
out.push('var elm = document.createElement(' + JSON.stringify(tag) + ');');
// extract attributes
attrs.forEach(function (attr) {
var colon = attr.indexOf(':');
if (colon !== -1) {
var name = attr.substring(0, colon);
var value = attr.substring(colon + 1);
out.push('elm.setAttribute(' + JSON.stringify(name) + ', ' + JSON.stringify(value) + ');');
}
});
// set element body
if (asText) {
out.push('elm.appendChild(document.createTextNode(module.exports));');
} else {
out.push('elm.innerHTML = module.exports;');
}
// re-assign module.exports
out.push('module.exports = elm;');
return out.join('\n');
};