diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index e850f841..71f8c09d 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -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$/, diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index d1f89b28..b8a34f92 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -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$/, diff --git a/xml-namespace-loader.js b/xml-namespace-loader.js new file mode 100644 index 00000000..8b3627cb --- /dev/null +++ b/xml-namespace-loader.js @@ -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); +}