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

Commit d2e97ff

Browse files
committed
feat: add xml loader for elements from external namespaces
1 parent dd5fe11 commit d2e97ff

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-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

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
const json = JSON.stringify(source)
26+
.replace(/\u2028/g, '\\u2028')
27+
.replace(/\u2029/g, '\\u2029');
28+
29+
const wrapped = `${registerModules}\nmodule.exports = ${json}`;
30+
31+
this.callback(null, wrapped);
32+
}

0 commit comments

Comments
 (0)