This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtns-xml-loader.js
81 lines (63 loc) · 2.23 KB
/
tns-xml-loader.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
var htmlparser = require("htmlparser2");
var UI_PATH = "ui/";
var MODULES = {
"TabViewItem": "ui/tab-view",
"FormattedString": "text/formatted-string",
"Span": "text/span",
"ActionItem": "ui/action-bar",
"NavigationButton": "ui/action-bar",
"SegmentedBarItem": "ui/segmented-bar",
};
var ELEMENT_REGISTRY = "ELEMENT_REGISTRY";
if (!global[ELEMENT_REGISTRY]) {
global[ELEMENT_REGISTRY] = {
"ui/proxy-view-container": "ProxyViewContainer",
"ui/placeholder": "Placeholder"
};
}
function parseResource(source, map) {
this.cacheable();
let templateSource;
try {
templateSource = getTemplateSource(this.resourcePath, source);
} catch(e) {
this.emitWarning(e.message);
return this.callback(null, source, map);
}
if (templateSource === "") {
return this.callback(null, source, map);
}
var parser = new htmlparser.Parser({
onopentag: function (name, attribs) {
// kebab-case to CamelCase
var elementName = name.split("-").map(function (s) { return s[0].toUpperCase() + s.substring(1); }).join("");
// Module path from element name
var modulePath = MODULES[elementName] || UI_PATH +
(elementName.toLowerCase().indexOf("layout") !== -1 ? "layouts/" : "") +
elementName.split(/(?=[A-Z])/).join("-").toLowerCase();
// Update ELEMENT_REGISTRY
global[ELEMENT_REGISTRY][modulePath] = elementName;
}
}, { decodeEntities: true, lowerCaseTags: false });
parser.write(templateSource);
parser.end();
return this.callback(null, source, map);
}
function getTemplateSource(path, source) {
if (isTemplate(path)) {
return source;
} else if (isComponent(path)) {
const templateMatcher = /template\s*:\s*([`'"])((.|\n)*?)\1/;
let match = templateMatcher.exec(source);
return match ? match[2] : "";
} else {
throw new Error(`The NativeScript XML loader must be used with HTML, XML or TypeScript files`);
}
}
function isComponent(resource) {
return /\.ts$/i.test(resource);
}
function isTemplate(resource) {
return /\.html$|\.xml$/i.test(resource);
}
module.exports = parseResource;