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

Commit 3e0f381

Browse files
authored
fix-next: register custom components at build time (#529)
1 parent c7117d7 commit 3e0f381

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

Diff for: register-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
require("tns-core-modules/bundle-entry-points");
2-
const context = require.context("~/", true, /(root|page|fragment)\.(xml|css|js|ts|scss)$/);
2+
const context = require.context("~/", true, /(root|page)\.(xml|css|js|ts|scss)$/);
33
global.registerWebpackModules(context);
44

Diff for: xml-namespace-loader.js

+47-7
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,74 @@
1+
const { parse, relative, join, basename, extname } = require("path");
2+
13
module.exports = function(source) {
24
this.value = source;
35

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

68
let namespaces = [];
79
const parser = new XmlParser((event) => {
8-
const namespace = event.namespace;
10+
const { namespace, elementName } = event;
11+
912
if (
1013
namespace &&
1114
!namespace.startsWith("http") &&
12-
namespaces.indexOf(namespace) === -1
15+
!namespaces.some(n => n.name === namespace)
1316
) {
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+
}
1545
}
1646
}, undefined, true);
47+
1748
parser.parse(source);
1849

19-
const registerModules = namespaces
50+
const moduleRegisters = namespaces
2051
.map(n =>
21-
`global.registerModule("${n}", function() { return require("${n}"); })`
52+
`global.registerModule("${n.name}", function() { return require("${n.path}"); });`
2253
)
23-
.join(";");
54+
.join("");
2455

2556
// escape special whitespace characters
2657
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
2758
const json = JSON.stringify(source)
2859
.replace(/\u2028/g, '\\u2028')
2960
.replace(/\u2029/g, '\\u2029');
3061

31-
const wrapped = `${registerModules}\nmodule.exports = ${json}`;
62+
const wrapped = `${moduleRegisters}\nmodule.exports = ${json}`;
3263

3364
this.callback(null, wrapped);
3465
}
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

Comments
 (0)