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

fix: register elements from embedded templates #56

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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