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

Commit 9f1efff

Browse files
ggodlewskiSvetoslavTsenov
authored andcommitted
fix(JS/TS): use webpack resolver instead of Node.js resolver (#681)
1 parent 7ff023b commit 9f1efff

File tree

1 file changed

+80
-67
lines changed

1 file changed

+80
-67
lines changed

Diff for: xml-namespace-loader.js

+80-67
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const { parse, relative, join, basename, extname } = require("path");
2+
const { promisify } = require('util');
23
const { convertSlashesInPath } = require("./projectHelpers");
34

45
module.exports = function (source) {
56
this.value = source;
67
const { ignore } = this.query;
8+
const callback = this.async();
79

810
const { XmlParser } = require("tns-core-modules/xml");
911

10-
let namespaces = [];
12+
const resolvePromise = promisify(this.resolve);
13+
const promises = [];
14+
15+
const namespaces = [];
1116
const parser = new XmlParser((event) => {
1217
const { namespace, elementName } = event;
1318
const moduleName = `${namespace}/${elementName}`;
@@ -20,83 +25,91 @@ module.exports = function (source) {
2025
) {
2126
const localNamespacePath = join(this.rootContext, namespace);
2227
const localModulePath = join(localNamespacePath, elementName);
23-
const resolvedPath = tryResolve(localNamespacePath) ||
24-
tryResolve(localModulePath);
25-
26-
if (!resolvedPath) {
27-
const xml = tryResolve(`${localModulePath}.xml`);
28-
if (!xml) {
29-
namespaces.push({ name: namespace, path: namespace });
30-
namespaces.push({ name: moduleName, path: namespace });
31-
32-
return;
33-
} else {
34-
namespaces.push({ name: `${moduleName}.xml`, path: xml });
35-
namespaces.push({ name: moduleName, path: xml });
36-
this.addDependency(xml);
37-
}
38-
39-
const css = tryResolve(`${localModulePath}.css`);
40-
if (css) {
41-
namespaces.push({ name: `${moduleName}.css`, path: css });
42-
this.addDependency(css);
43-
}
44-
45-
return;
46-
}
47-
48-
this.addDependency(resolvedPath);
49-
50-
namespaces.push({ name: namespace, path: resolvedPath });
51-
namespaces.push({ name: moduleName, path: resolvedPath });
52-
53-
const { dir, name } = parse(resolvedPath);
54-
const noExtFilename = join(dir, name);
55-
56-
const xml = tryResolve(`${noExtFilename}.xml`);
57-
if (xml) {
58-
this.addDependency(xml);
59-
namespaces.push({ name: `${moduleName}.xml`, path: xml });
60-
}
61-
62-
const css = tryResolve(`${noExtFilename}.css`);
63-
if (css) {
64-
this.addDependency(css);
65-
namespaces.push({ name: `${moduleName}.css`, path: css });
66-
}
28+
29+
const pathResolved = (resolvedPath) => {
30+
this.addDependency(resolvedPath);
31+
32+
namespaces.push({ name: namespace, path: resolvedPath });
33+
namespaces.push({ name: moduleName, path: resolvedPath });
34+
35+
const { dir, name } = parse(resolvedPath);
36+
const noExtFilename = join(dir, name);
37+
38+
return Promise.all([
39+
resolvePromise(this.context, `${noExtFilename}.xml`)
40+
.then((xml) => {
41+
this.addDependency(xml);
42+
namespaces.push({ name: `${moduleName}.xml`, path: xml });
43+
})
44+
.catch((err) => {}),
45+
46+
resolvePromise(this.context, `${noExtFilename}.css`)
47+
.then((xml) => {
48+
this.addDependency(xml);
49+
namespaces.push({ name: `${moduleName}.css`, path: css });
50+
})
51+
.catch((err) => {})
52+
]);
53+
};
54+
55+
promises.push(resolvePromise(this.context, localNamespacePath)
56+
.then(path => pathResolved(path))
57+
.catch(() => {
58+
return promise = resolvePromise(this.context, localModulePath)
59+
.then(path => pathResolved(path))
60+
.catch(() => {
61+
return Promise.all([
62+
resolvePromise(this.context, `${localModulePath}.xml`)
63+
.then((xml) => {
64+
namespaces.push({ name: `${moduleName}.xml`, path: xml });
65+
namespaces.push({ name: moduleName, path: xml });
66+
this.addDependency(xml);
67+
})
68+
.catch(() => {
69+
namespaces.push({ name: namespace, path: namespace });
70+
namespaces.push({ name: moduleName, path: namespace });
71+
}),
72+
73+
resolvePromise(this.context, `${localModulePath}.css`)
74+
.then((css) => {
75+
namespaces.push({ name: `${moduleName}.css`, path: css });
76+
this.addDependency(css);
77+
})
78+
.catch(() => {})
79+
]);
80+
81+
});
82+
})
83+
);
6784
}
6885
}, undefined, true);
6986

7087
parser.parse(source);
7188

72-
const moduleRegisters = namespaces
73-
.map(convertPath)
74-
.map(n =>
75-
`global.registerModule("${n.name}", function() { return require("${n.path}"); });`
76-
)
77-
.join("");
89+
Promise.all(promises).then(() => {
90+
const moduleRegisters = namespaces
91+
.map(convertPath)
92+
.map(n =>
93+
`global.registerModule("${n.name}", function() { return require("${n.path}"); });`
94+
)
95+
.join("");
96+
97+
// escape special whitespace characters
98+
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
99+
const json = JSON.stringify(source)
100+
.replace(/\u2028/g, '\\u2028')
101+
.replace(/\u2029/g, '\\u2029');
78102

79-
// escape special whitespace characters
80-
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
81-
const json = JSON.stringify(source)
82-
.replace(/\u2028/g, '\\u2028')
83-
.replace(/\u2029/g, '\\u2029');
103+
const wrapped = `${moduleRegisters}\nmodule.exports = ${json}`;
84104

85-
const wrapped = `${moduleRegisters}\nmodule.exports = ${json};`;
105+
callback(null, wrapped);
106+
}).catch((err) => {
107+
callback(err);
108+
})
86109

87-
this.callback(null, wrapped);
88110
}
89111

90112
function convertPath(obj) {
91113
obj.path = convertSlashesInPath(obj.path);
92114
return obj;
93115
}
94-
95-
function tryResolve(path) {
96-
try {
97-
return require.resolve(path);
98-
} catch (e) {
99-
// The path couldn't be resolved
100-
return;
101-
}
102-
}

0 commit comments

Comments
 (0)