Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
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
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
34 changes: 34 additions & 0 deletions xml-namespace-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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(";");

// escape special whitespace characters
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
const json = JSON.stringify(source)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');

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

this.callback(null, wrapped);
}