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

feat: add xml loader for elements from external namespaces #525

Merged
merged 3 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion templates/webpack.javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module.exports = env => {
].filter(loader => !!loader)
},

{ test: /\.(html|xml)$/, use: "raw-loader" },
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader"},

{
test: /\.css$/,
Expand Down
2 changes: 1 addition & 1 deletion templates/webpack.typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module.exports = env => {
].filter(loader => !!loader)
},

{ test: /\.(html|xml)$/, use: "raw-loader" },
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader"},

{
test: /\.css$/,
Expand Down
32 changes: 32 additions & 0 deletions xml-namespace-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = function(source) {
this.value = source;

const { XmlParser } = require("tns-core-modules/xml");

let namespaces = [];
const parser = new XmlParser((event) => {
const namespace = event.namespace;
if (
namespace &&
!namespace.startsWith("http") &&
namespaces.indexOf(namespace) === -1
) {
namespaces.push(namespace);
}
}, undefined, true);
parser.parse(source);

const registerModules = namespaces
.map(n =>
`global.registerModule("${n}", function() { return require("${n}"); })`
)
.join(";");

const json = JSON.stringify(source)
.replace(/\u2028/g, '\\u2028')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe a comment will be helpful here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion! Added a comment.

.replace(/\u2029/g, '\\u2029');

const wrapped = `${registerModules}\nmodule.exports = ${json}`;

this.callback(null, wrapped);
}