UNPKG

4.6 kBJavaScriptView Raw
1const { parse, relative, join, basename, extname } = require("path");
2const { promisify } = require('util');
3const { convertSlashesInPath } = require("./projectHelpers");
4
5module.exports = function (source) {
6 this.value = source;
7 const { ignore } = this.query;
8 const callback = this.async();
9
10 const { XmlParser } = require("tns-core-modules/xml");
11
12 const resolvePromise = promisify(this.resolve);
13 const promises = [];
14
15 const namespaces = [];
16 const parser = new XmlParser((event) => {
17 const { namespace, elementName } = event;
18 const moduleName = `${namespace}/${elementName}`;
19
20 if (
21 namespace &&
22 !namespace.startsWith("http") &&
23 !namespaces.some(n => n.name === moduleName) &&
24 (!ignore || !moduleName.match(ignore))
25 ) {
26 const localNamespacePath = join(this.rootContext, namespace);
27 const localModulePath = join(localNamespacePath, elementName);
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 );
84 }
85 }, undefined, true);
86
87 parser.parse(source);
88
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');
102
103 const wrapped = `${moduleRegisters}\nmodule.exports = ${json}`;
104
105 callback(null, wrapped);
106 }).catch((err) => {
107 callback(err);
108 })
109
110}
111
112function convertPath(obj) {
113 obj.path = convertSlashesInPath(obj.path);
114 return obj;
115}