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

Commit

Permalink
fix: register elements from embedded templates (#56)
Browse files Browse the repository at this point in the history
Make the tns-xml-loader parse Angular templates from TypeScript files.

fixes #55
  • Loading branch information
sis0k0 authored Jan 18, 2017
1 parent a208a85 commit 05f33ed
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
44 changes: 39 additions & 5 deletions tns-xml-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,59 @@ if (!global[ELEMENT_REGISTRY]) {
};
}

module.exports = function (source, map) {
function parseResource(source, map) {
this.cacheable();

var loader = this;
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(source);

parser.write(templateSource);
parser.end();

this.callback(null, source, map);
};
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/;
return templateMatcher.test(source) ? source.replace(templateMatcher, "$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;
5 changes: 3 additions & 2 deletions webpack.common.js.angular.template
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function (platform, destinationApp) {
]),

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

//Angular AOT compiler
Expand Down Expand Up @@ -109,7 +109,7 @@ module.exports = function (platform, destinationApp) {
test: /\.html$|\.xml$/,
loaders: [
"raw-loader",
'nativescript-dev-webpack/tns-xml-loader'
"nativescript-dev-webpack/tns-xml-loader",
]
},
// Root app.css file gets extracted with bundled dependencies
Expand All @@ -133,6 +133,7 @@ module.exports = function (platform, destinationApp) {
{
test: /\.ts$/,
loaders: [
"nativescript-dev-webpack/tns-xml-loader",
"nativescript-dev-webpack/tns-aot-loader",
"@ngtools/webpack",
]
Expand Down

0 comments on commit 05f33ed

Please sign in to comment.