Prefer registered modules in builder and component-builder.

Avoid breakage in bundled scenarios.
This commit is contained in:
Hristo Deshev
2016-06-22 18:39:10 +03:00
parent f6f0af2947
commit 5c3bd121ad
2 changed files with 26 additions and 12 deletions

View File

@@ -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 { function loadCustomComponent(componentPath: string, componentName?: string, attributes?: Object, context?: Object, parentPage?: Page): ComponentModule {
var result: ComponentModule; var result: ComponentModule;
componentPath = componentPath.replace("~/", ""); componentPath = componentPath.replace("~/", "");
const moduleName = componentPath + "/" + componentName;
var fullComponentPathFilePathWithoutExt = componentPath; var fullComponentPathFilePathWithoutExt = componentPath;
@@ -74,9 +75,14 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
var jsFilePath = resolveFileName(fullComponentPathFilePathWithoutExt, "js"); var jsFilePath = resolveFileName(fullComponentPathFilePathWithoutExt, "js");
var subExports = context; var subExports = context;
if (jsFilePath) { if (global.moduleExists(moduleName)) {
// Custom components with XML and code // Component has registered code module.
subExports = global.loadModule(jsFilePath) subExports = global.loadModule(moduleName);
} else {
if (jsFilePath) {
// Component has code file.
subExports = global.loadModule(jsFilePath)
}
} }
result = loadInternal(xmlFilePath, subExports); result = loadInternal(xmlFilePath, subExports);

View File

@@ -2,7 +2,7 @@
import {Page} from "ui/page"; import {Page} from "ui/page";
import {View, isEventOrGesture} from "ui/core/view"; import {View, isEventOrGesture} from "ui/core/view";
import {ComponentModule} from "ui/builder/component-builder"; import {ComponentModule} from "ui/builder/component-builder";
import {File, Folder, path, knownFolders} from "file-system"; import {File, path, knownFolders} from "file-system";
import {getBindingOptions, bindingConstants} from "./binding-builder"; import {getBindingOptions, bindingConstants} from "./binding-builder";
import * as debugModule from "utils/debug"; import * as debugModule from "utils/debug";
import * as platformModule from "platform"; import * as platformModule from "platform";
@@ -50,18 +50,26 @@ export function getComponentModule(elementName: string, namespace: string, attri
try { try {
if (isString(namespace)) { if (isString(namespace)) {
var pathInsideTNSModules = path.join(knownFolders.currentApp().path, "tns_modules", namespace); if (global.moduleExists(namespace)) {
moduleId = namespace;
if (Folder.exists(pathInsideTNSModules)) {
moduleId = pathInsideTNSModules;
} else { } else {
// We expect module at root level in the app. var pathInsideTNSModules = path.join(knownFolders.currentApp().path, "tns_modules", namespace);
moduleId = path.join(knownFolders.currentApp().path, 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. if (!instanceModule) {
instanceModule = global.loadModule(moduleId); // Require module by module id.
instanceModule = global.loadModule(moduleId);
}
// Get the component type from module. // Get the component type from module.
var instanceType = instanceModule[elementName] || Object; var instanceType = instanceModule[elementName] || Object;