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

Commit 19bbc7e

Browse files
sis0k0manoldonev
authored andcommitted
feat: add xml loader for elements from external namespaces (#525)
* feat: add xml loader for elements from external namespaces * refactor: add comment about special chars
1 parent d395fa3 commit 19bbc7e

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Diff for: templates/webpack.javascript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ module.exports = env => {
151151
].filter(loader => !!loader)
152152
},
153153

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

156156
{
157157
test: /\.css$/,

Diff for: templates/webpack.typescript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ module.exports = env => {
151151
].filter(loader => !!loader)
152152
},
153153

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

156156
{
157157
test: /\.css$/,

Diff for: xml-namespace-loader.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = function(source) {
2+
this.value = source;
3+
4+
const { XmlParser } = require("tns-core-modules/xml");
5+
6+
let namespaces = [];
7+
const parser = new XmlParser((event) => {
8+
const namespace = event.namespace;
9+
if (
10+
namespace &&
11+
!namespace.startsWith("http") &&
12+
namespaces.indexOf(namespace) === -1
13+
) {
14+
namespaces.push(namespace);
15+
}
16+
}, undefined, true);
17+
parser.parse(source);
18+
19+
const registerModules = namespaces
20+
.map(n =>
21+
`global.registerModule("${n}", function() { return require("${n}"); })`
22+
)
23+
.join(";");
24+
25+
// escape special whitespace characters
26+
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
27+
const json = JSON.stringify(source)
28+
.replace(/\u2028/g, '\\u2028')
29+
.replace(/\u2029/g, '\\u2029');
30+
31+
const wrapped = `${registerModules}\nmodule.exports = ${json}`;
32+
33+
this.callback(null, wrapped);
34+
}

0 commit comments

Comments
 (0)