diff --git a/tns-core-modules/ui/builder/builder.ts b/tns-core-modules/ui/builder/builder.ts index 73d3dfa18..c5614d16b 100644 --- a/tns-core-modules/ui/builder/builder.ts +++ b/tns-core-modules/ui/builder/builder.ts @@ -60,6 +60,7 @@ function parseInternal(value: string, context: any, uri?: string): ComponentModu function loadCustomComponent(componentPath: string, componentName?: string, attributes?: Object, context?: Object, parentPage?: Page): ComponentModule { var result: ComponentModule; componentPath = componentPath.replace("~/", ""); + const moduleName = componentPath + "/" + componentName; var fullComponentPathFilePathWithoutExt = componentPath; @@ -74,9 +75,14 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr var jsFilePath = resolveFileName(fullComponentPathFilePathWithoutExt, "js"); var subExports = context; - if (jsFilePath) { - // Custom components with XML and code - subExports = global.loadModule(jsFilePath) + if (global.moduleExists(moduleName)) { + // Component has registered code module. + subExports = global.loadModule(moduleName); + } else { + if (jsFilePath) { + // Component has code file. + subExports = global.loadModule(jsFilePath) + } } result = loadInternal(xmlFilePath, subExports); diff --git a/tns-core-modules/ui/builder/component-builder.ts b/tns-core-modules/ui/builder/component-builder.ts index b82740b2e..aca848ecf 100644 --- a/tns-core-modules/ui/builder/component-builder.ts +++ b/tns-core-modules/ui/builder/component-builder.ts @@ -50,18 +50,26 @@ export function getComponentModule(elementName: string, namespace: string, attri try { if (isString(namespace)) { - var pathInsideTNSModules = path.join(knownFolders.currentApp().path, "tns_modules", namespace); - - if (Folder.exists(pathInsideTNSModules)) { - moduleId = pathInsideTNSModules; + if (global.moduleExists(namespace)) { + moduleId = namespace; } else { - // We expect module at root level in the app. - moduleId = path.join(knownFolders.currentApp().path, namespace); + var pathInsideTNSModules = path.join(knownFolders.currentApp().path, "tns_modules", namespace); + + try { + // module inside tns_modules + instanceModule = require(pathInsideTNSModules); + moduleId = pathInsideTNSModules; + } catch (e) { + // module at root level in the app. + moduleId = path.join(knownFolders.currentApp().path, namespace); + } } } - // Require module by module id. - instanceModule = global.loadModule(moduleId); + if (!instanceModule) { + // Require module by module id. + instanceModule = global.loadModule(moduleId); + } // Get the component type from module. var instanceType = instanceModule[elementName] || Object;