From 9bc277f81f189c7806e0a869de969d8994951201 Mon Sep 17 00:00:00 2001 From: Grzegorz Godlewski Date: Sat, 29 Sep 2018 15:24:03 +0200 Subject: [PATCH] fix: use webpack resolver in xml-namespace-loader insead of nodejs resolver --- xml-namespace-loader.js | 147 ++++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 67 deletions(-) diff --git a/xml-namespace-loader.js b/xml-namespace-loader.js index 2cb83c3a..48f5c83c 100644 --- a/xml-namespace-loader.js +++ b/xml-namespace-loader.js @@ -1,13 +1,18 @@ const { parse, relative, join, basename, extname } = require("path"); +const { promisify } = require('util'); const { convertSlashesInPath } = require("./projectHelpers"); module.exports = function (source) { this.value = source; const { ignore } = this.query; + const callback = this.async(); const { XmlParser } = require("tns-core-modules/xml"); - let namespaces = []; + const resolvePromise = promisify(this.resolve); + const promises = []; + + const namespaces = []; const parser = new XmlParser((event) => { const { namespace, elementName } = event; const moduleName = `${namespace}/${elementName}`; @@ -20,83 +25,91 @@ module.exports = function (source) { ) { const localNamespacePath = join(this.rootContext, namespace); const localModulePath = join(localNamespacePath, elementName); - const resolvedPath = tryResolve(localNamespacePath) || - tryResolve(localModulePath); - - if (!resolvedPath) { - const xml = tryResolve(`${localModulePath}.xml`); - if (!xml) { - namespaces.push({ name: namespace, path: namespace }); - namespaces.push({ name: moduleName, path: namespace }); - - return; - } else { - namespaces.push({ name: `${moduleName}.xml`, path: xml }); - namespaces.push({ name: moduleName, path: xml }); - this.addDependency(xml); - } - - const css = tryResolve(`${localModulePath}.css`); - if (css) { - namespaces.push({ name: `${moduleName}.css`, path: css }); - this.addDependency(css); - } - - return; - } - - this.addDependency(resolvedPath); - - namespaces.push({ name: namespace, path: resolvedPath }); - namespaces.push({ name: moduleName, path: resolvedPath }); - - const { dir, name } = parse(resolvedPath); - const noExtFilename = join(dir, name); - - const xml = tryResolve(`${noExtFilename}.xml`); - if (xml) { - this.addDependency(xml); - namespaces.push({ name: `${moduleName}.xml`, path: xml }); - } - - const css = tryResolve(`${noExtFilename}.css`); - if (css) { - this.addDependency(css); - namespaces.push({ name: `${moduleName}.css`, path: css }); - } + + const pathResolved = (resolvedPath) => { + this.addDependency(resolvedPath); + + namespaces.push({ name: namespace, path: resolvedPath }); + namespaces.push({ name: moduleName, path: resolvedPath }); + + const { dir, name } = parse(resolvedPath); + const noExtFilename = join(dir, name); + + return Promise.all([ + resolvePromise(this.context, `${noExtFilename}.xml`) + .then((xml) => { + this.addDependency(xml); + namespaces.push({ name: `${moduleName}.xml`, path: xml }); + }) + .catch((err) => {}), + + resolvePromise(this.context, `${noExtFilename}.css`) + .then((xml) => { + this.addDependency(xml); + namespaces.push({ name: `${moduleName}.css`, path: css }); + }) + .catch((err) => {}) + ]); + }; + + promises.push(resolvePromise(this.context, localNamespacePath) + .then(path => pathResolved(path)) + .catch(() => { + return promise = resolvePromise(this.context, localModulePath) + .then(path => pathResolved(path)) + .catch(() => { + return Promise.all([ + resolvePromise(this.context, `${localModulePath}.xml`) + .then((xml) => { + namespaces.push({ name: `${moduleName}.xml`, path: xml }); + namespaces.push({ name: moduleName, path: xml }); + this.addDependency(xml); + }) + .catch(() => { + namespaces.push({ name: namespace, path: namespace }); + namespaces.push({ name: moduleName, path: namespace }); + }), + + resolvePromise(this.context, `${localModulePath}.css`) + .then((css) => { + namespaces.push({ name: `${moduleName}.css`, path: css }); + this.addDependency(css); + }) + .catch(() => {}) + ]); + + }); + }) + ); } }, undefined, true); parser.parse(source); - const moduleRegisters = namespaces - .map(convertPath) - .map(n => - `global.registerModule("${n.name}", function() { return require("${n.path}"); });` - ) - .join(""); + Promise.all(promises).then(() => { + const moduleRegisters = namespaces + .map(convertPath) + .map(n => + `global.registerModule("${n.name}", function() { return require("${n.path}"); });` + ) + .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'); - // 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 = `${moduleRegisters}\nmodule.exports = ${json}`; - const wrapped = `${moduleRegisters}\nmodule.exports = ${json};`; + callback(null, wrapped); + }).catch((err) => { + callback(err); + }) - this.callback(null, wrapped); } function convertPath(obj) { obj.path = convertSlashesInPath(obj.path); return obj; } - -function tryResolve(path) { - try { - return require.resolve(path); - } catch (e) { - // The path couldn't be resolved - return; - } -}