forked from NativeScript/nativescript-dev-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-namespace-loader.js
100 lines (80 loc) · 3.27 KB
/
xml-namespace-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const { parse, relative, join, basename, extname } = require("path");
const { convertSlashesInPath } = require("./projectHelpers");
module.exports = function (source) {
this.value = source;
const { XmlParser } = require("tns-core-modules/xml");
let namespaces = [];
const parser = new XmlParser((event) => {
const { namespace, elementName } = event;
const moduleName = `${namespace}/${elementName}`;
if (
namespace &&
!namespace.startsWith("http") &&
!namespaces.some(n => n.name === moduleName)
) {
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 });
}
}
}, undefined, true);
parser.parse(source);
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');
const wrapped = `${moduleRegisters}\nmodule.exports = ${json}`;
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;
}
}