|
| 1 | +const { parse, relative, join, basename, extname } = require("path"); |
| 2 | + |
1 | 3 | module.exports = function(source) {
|
2 | 4 | this.value = source;
|
3 | 5 |
|
4 | 6 | const { XmlParser } = require("tns-core-modules/xml");
|
5 | 7 |
|
6 | 8 | let namespaces = [];
|
7 | 9 | const parser = new XmlParser((event) => {
|
8 |
| - const namespace = event.namespace; |
| 10 | + const { namespace, elementName } = event; |
| 11 | + |
9 | 12 | if (
|
10 | 13 | namespace &&
|
11 | 14 | !namespace.startsWith("http") &&
|
12 |
| - namespaces.indexOf(namespace) === -1 |
| 15 | + !namespaces.some(n => n.name === namespace) |
13 | 16 | ) {
|
14 |
| - namespaces.push(namespace); |
| 17 | + const localNamespacePath = join(this.rootContext, namespace); |
| 18 | + const localModulePath = join(localNamespacePath, elementName); |
| 19 | + const resolvedPath = tryResolve(localNamespacePath) || |
| 20 | + tryResolve(localModulePath) || |
| 21 | + namespace; |
| 22 | + |
| 23 | + this.addDependency(resolvedPath); |
| 24 | + namespaces.push({ name: namespace, path: resolvedPath }); |
| 25 | + |
| 26 | + const moduleName = `${namespace}/${elementName}`; |
| 27 | + namespaces.push({ name: moduleName, path: resolvedPath }); |
| 28 | + |
| 29 | + const { dir, name } = parse(resolvedPath); |
| 30 | + const noExtFilename = join(dir, name); |
| 31 | + |
| 32 | + const xmlFile = `${noExtFilename}.xml`; |
| 33 | + const xmlFileResolved = tryResolve(xmlFile); |
| 34 | + if (xmlFileResolved) { |
| 35 | + this.addDependency(xmlFileResolved); |
| 36 | + namespaces.push({ name: `${moduleName}.xml`, path: xmlFileResolved }); |
| 37 | + } |
| 38 | + |
| 39 | + const cssFile = `${noExtFilename}.css`; |
| 40 | + const cssFileResolved = tryResolve(cssFile); |
| 41 | + if (cssFileResolved) { |
| 42 | + this.addDependency(cssFileResolved); |
| 43 | + namespaces.push({ name: `${moduleName}.css`, path: cssFileResolved }); |
| 44 | + } |
15 | 45 | }
|
16 | 46 | }, undefined, true);
|
| 47 | + |
17 | 48 | parser.parse(source);
|
18 | 49 |
|
19 |
| - const registerModules = namespaces |
| 50 | + const moduleRegisters = namespaces |
20 | 51 | .map(n =>
|
21 |
| - `global.registerModule("${n}", function() { return require("${n}"); })` |
| 52 | + `global.registerModule("${n.name}", function() { return require("${n.path}"); });` |
22 | 53 | )
|
23 |
| - .join(";"); |
| 54 | + .join(""); |
24 | 55 |
|
25 | 56 | // escape special whitespace characters
|
26 | 57 | // 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 | 58 | const json = JSON.stringify(source)
|
28 | 59 | .replace(/\u2028/g, '\\u2028')
|
29 | 60 | .replace(/\u2029/g, '\\u2029');
|
30 | 61 |
|
31 |
| - const wrapped = `${registerModules}\nmodule.exports = ${json}`; |
| 62 | + const wrapped = `${moduleRegisters}\nmodule.exports = ${json}`; |
32 | 63 |
|
33 | 64 | this.callback(null, wrapped);
|
34 | 65 | }
|
| 66 | + |
| 67 | +function tryResolve(path) { |
| 68 | + try { |
| 69 | + return require.resolve(path); |
| 70 | + } catch(e) { |
| 71 | + // The path couldn't be resolved |
| 72 | + return; |
| 73 | + } |
| 74 | +} |
0 commit comments