Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 05f33ed

Browse files
authored
fix: register elements from embedded templates (#56)
Make the tns-xml-loader parse Angular templates from TypeScript files. fixes #55
1 parent a208a85 commit 05f33ed

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

Diff for: tns-xml-loader.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,59 @@ if (!global[ELEMENT_REGISTRY]) {
2020
};
2121
}
2222

23-
module.exports = function (source, map) {
23+
function parseResource(source, map) {
2424
this.cacheable();
2525

26-
var loader = this;
26+
let templateSource;
27+
try {
28+
templateSource = getTemplateSource(this.resourcePath, source);
29+
} catch(e) {
30+
this.emitWarning(e.message);
31+
return this.callback(null, source, map);
32+
}
33+
34+
if (templateSource === "") {
35+
return this.callback(null, source, map);
36+
}
2737

2838
var parser = new htmlparser.Parser({
2939
onopentag: function (name, attribs) {
3040
// kebab-case to CamelCase
3141
var elementName = name.split("-").map(function (s) { return s[0].toUpperCase() + s.substring(1); }).join("");
42+
3243
// Module path from element name
3344
var modulePath = MODULES[elementName] || UI_PATH +
3445
(elementName.toLowerCase().indexOf("layout") !== -1 ? "layouts/" : "") +
3546
elementName.split(/(?=[A-Z])/).join("-").toLowerCase();
47+
3648
// Update ELEMENT_REGISTRY
3749
global[ELEMENT_REGISTRY][modulePath] = elementName;
3850
}
3951
}, { decodeEntities: true, lowerCaseTags: false });
40-
parser.write(source);
52+
53+
parser.write(templateSource);
4154
parser.end();
4255

43-
this.callback(null, source, map);
44-
};
56+
return this.callback(null, source, map);
57+
}
58+
59+
function getTemplateSource(path, source) {
60+
if (isTemplate(path)) {
61+
return source;
62+
} else if (isComponent(path)) {
63+
const templateMatcher = /template\s*:\s*([`'"])((.|\n)*?)\1/;
64+
return templateMatcher.test(source) ? source.replace(templateMatcher, "$2") : "";
65+
} else {
66+
throw new Error(`The NativeScript XML loader must be used with HTML, XML or TypeScript files`);
67+
}
68+
}
69+
70+
function isComponent(resource) {
71+
return /\.ts$/i.test(resource);
72+
}
73+
74+
function isTemplate(resource) {
75+
return /\.html$|\.xml$/i.test(resource);
76+
}
77+
78+
module.exports = parseResource;

Diff for: webpack.common.js.angular.template

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function (platform, destinationApp) {
4747
]),
4848

4949
// Exclude explicitly required but never declared in XML elements.
50-
// Loader nativescript-dev-webpack/tns-xml-loader should be added for *.xml/html files.
50+
// Loader nativescript-dev-webpack/tns-xml-loader should be added for *.xml/html and *.ts files.
5151
new nsWebpack.ExcludeUnusedElementsPlugin(),
5252

5353
//Angular AOT compiler
@@ -109,7 +109,7 @@ module.exports = function (platform, destinationApp) {
109109
test: /\.html$|\.xml$/,
110110
loaders: [
111111
"raw-loader",
112-
'nativescript-dev-webpack/tns-xml-loader'
112+
"nativescript-dev-webpack/tns-xml-loader",
113113
]
114114
},
115115
// Root app.css file gets extracted with bundled dependencies
@@ -133,6 +133,7 @@ module.exports = function (platform, destinationApp) {
133133
{
134134
test: /\.ts$/,
135135
loaders: [
136+
"nativescript-dev-webpack/tns-xml-loader",
136137
"nativescript-dev-webpack/tns-aot-loader",
137138
"@ngtools/webpack",
138139
]

0 commit comments

Comments
 (0)