@@ -7,6 +7,7 @@ import { isEventOrGesture } from "../../core/bindable";
77import { File , path , knownFolders } from "../../../file-system" ;
88import { getBindingOptions , bindingConstants } from "../binding-builder" ;
99import { resolveFileName } from "../../../file-system/file-name-resolver" ;
10+ import { profile } from "tns-core-modules/profiling" ;
1011import * as debugModule from "../../../utils/debug" ;
1112import * as platform from "../../../platform" ;
1213
@@ -24,14 +25,9 @@ const CODEFILE = "codeFile";
2425const CSSFILE = "cssFile" ;
2526const IMPORT = "import" ;
2627
27- export function getComponentModule ( elementName : string , namespace : string , attributes : Object , exports : Object , moduleNamePath ?: string ) : ComponentModule {
28+ const createComponentInstance = profile ( "createComponentInstance" , ( elementName : string , namespace : string ) : { instance : View , instanceModule : Object } => {
2829 var instance : View ;
2930 var instanceModule : Object ;
30- var componentModule : ComponentModule ;
31-
32- // Support lower-case-dashed component declaration in the XML (https://github.com/NativeScript/NativeScript/issues/309).
33- elementName = elementName . split ( "-" ) . map ( s => { return s [ 0 ] . toUpperCase ( ) + s . substring ( 1 ) } ) . join ( "" ) ;
34-
3531 // Get module id.
3632 var moduleId = MODULES [ elementName ] || UI_PATH +
3733 ( elementName . toLowerCase ( ) . indexOf ( "layout" ) !== - 1 ? "layouts/" : "" ) +
@@ -70,7 +66,10 @@ export function getComponentModule(elementName: string, namespace: string, attri
7066 throw new debug . ScopeError ( ex , "Module '" + moduleId + "' not found for element '" + ( namespace ? namespace + ":" : "" ) + elementName + "'." ) ;
7167 }
7268
73- let cssApplied = false ;
69+ return { instance, instanceModule } ;
70+ } ) ;
71+
72+ const getComponentModuleExports = profile ( "getComponentModuleExports" , ( instance : View , moduleExports : Object , attributes : Object ) : Object => {
7473 if ( attributes ) {
7574 if ( attributes [ IMPORT ] ) {
7675 let importPath = attributes [ IMPORT ] . trim ( ) ;
@@ -79,39 +78,43 @@ export function getComponentModule(elementName: string, namespace: string, attri
7978 importPath = path . join ( knownFolders . currentApp ( ) . path , importPath . replace ( "~/" , "" ) ) ;
8079 }
8180
82- exports = global . loadModule ( importPath ) ;
83- ( < any > instance ) . exports = exports ;
81+ moduleExports = global . loadModule ( importPath ) ;
82+ ( < any > instance ) . exports = moduleExports ;
8483 }
8584
86- // if (instance instanceof Page) {
87- if ( attributes [ CODEFILE ] ) {
88- let codeFilePath = attributes [ CODEFILE ] . trim ( ) ;
89- if ( codeFilePath . indexOf ( "~/" ) === 0 ) {
90- codeFilePath = path . join ( knownFolders . currentApp ( ) . path , codeFilePath . replace ( "~/" , "" ) ) ;
91- }
85+ if ( attributes [ CODEFILE ] ) {
86+ let codeFilePath = attributes [ CODEFILE ] . trim ( ) ;
87+ if ( codeFilePath . indexOf ( "~/" ) === 0 ) {
88+ codeFilePath = path . join ( knownFolders . currentApp ( ) . path , codeFilePath . replace ( "~/" , "" ) ) ;
89+ }
9290
93- const codeFilePathWithExt = codeFilePath . indexOf ( ".js" ) !== - 1 ? codeFilePath : `${ codeFilePath } .js` ;
94- if ( File . exists ( codeFilePathWithExt ) ) {
95- exports = global . loadModule ( codeFilePath ) ;
96- ( < any > instance ) . exports = exports ;
97- } else {
98- throw new Error ( `Code file with path "${ codeFilePathWithExt } " cannot be found!` ) ;
99- }
91+ const codeFilePathWithExt = codeFilePath . indexOf ( ".js" ) !== - 1 ? codeFilePath : `${ codeFilePath } .js` ;
92+ if ( File . exists ( codeFilePathWithExt ) ) {
93+ moduleExports = global . loadModule ( codeFilePath ) ;
94+ ( < any > instance ) . exports = moduleExports ;
95+ } else {
96+ throw new Error ( `Code file with path "${ codeFilePathWithExt } " cannot be found!` ) ;
10097 }
98+ }
99+ }
100+ return moduleExports ;
101+ } ) ;
101102
102- if ( attributes [ CSSFILE ] && typeof ( < any > instance ) . addCssFile === "function" ) {
103- let cssFilePath = attributes [ CSSFILE ] . trim ( ) ;
104- if ( cssFilePath . indexOf ( "~/" ) === 0 ) {
105- cssFilePath = path . join ( knownFolders . currentApp ( ) . path , cssFilePath . replace ( "~/" , "" ) ) ;
106- }
107- if ( File . exists ( cssFilePath ) ) {
108- ( < any > instance ) . addCssFile ( cssFilePath ) ;
109- cssApplied = true ;
110- } else {
111- throw new Error ( `Css file with path "${ cssFilePath } " cannot be found!` ) ;
112- }
103+ const applyComponentCss = profile ( "applyComponentCss" , ( instance : View , moduleNamePath : string , attributes : Object ) => {
104+ let cssApplied = false ;
105+ if ( attributes ) {
106+ if ( attributes [ CSSFILE ] && typeof ( < any > instance ) . addCssFile === "function" ) {
107+ let cssFilePath = attributes [ CSSFILE ] . trim ( ) ;
108+ if ( cssFilePath . indexOf ( "~/" ) === 0 ) {
109+ cssFilePath = path . join ( knownFolders . currentApp ( ) . path , cssFilePath . replace ( "~/" , "" ) ) ;
110+ }
111+ if ( File . exists ( cssFilePath ) ) {
112+ ( < any > instance ) . addCssFile ( cssFilePath ) ;
113+ cssApplied = true ;
114+ } else {
115+ throw new Error ( `Css file with path "${ cssFilePath } " cannot be found!` ) ;
113116 }
114- // }
117+ }
115118 }
116119
117120 if ( typeof ( < any > instance ) . addCssFile === "function" ) { //instance instanceof Page) {
@@ -129,7 +132,9 @@ export function getComponentModule(elementName: string, namespace: string, attri
129132 ( < any > instance ) . _refreshCss ( ) ;
130133 }
131134 }
135+ } ) ;
132136
137+ const applyComponentAttributes = profile ( "applyComponentAttributes" , ( instance : View , instanceModule : Object , moduleExports : Object , attributes : Object ) => {
133138 if ( instance && instanceModule ) {
134139 for ( let attr in attributes ) {
135140
@@ -157,16 +162,28 @@ export function getComponentModule(elementName: string, namespace: string, attri
157162 }
158163
159164 if ( subObj !== undefined && subObj !== null ) {
160- setPropertyValue ( subObj , instanceModule , exports , subPropName , attrValue ) ;
165+ setPropertyValue ( subObj , instanceModule , moduleExports , subPropName , attrValue ) ;
161166 }
162167 } else {
163- setPropertyValue ( instance , instanceModule , exports , attr , attrValue ) ;
168+ setPropertyValue ( instance , instanceModule , moduleExports , attr , attrValue ) ;
164169 }
165170 }
171+ }
172+ } ) ;
173+
174+ export function getComponentModule ( elementName : string , namespace : string , attributes : Object , moduleExports : Object , moduleNamePath ?: string ) : ComponentModule {
175+ // Support lower-case-dashed component declaration in the XML (https://github.com/NativeScript/NativeScript/issues/309).
176+ elementName = elementName . split ( "-" ) . map ( s => { return s [ 0 ] . toUpperCase ( ) + s . substring ( 1 ) } ) . join ( "" ) ;
166177
178+ const { instance, instanceModule } = createComponentInstance ( elementName , namespace ) ;
179+ moduleExports = getComponentModuleExports ( instance , moduleExports , attributes ) ;
180+ applyComponentCss ( instance , moduleNamePath , attributes ) ;
181+ applyComponentAttributes ( instance , instanceModule , moduleExports , attributes ) ;
182+
183+ var componentModule ;
184+ if ( instance && instanceModule ) {
167185 componentModule = { component : instance , exports : instanceModule } ;
168186 }
169-
170187 return componentModule ;
171188}
172189
0 commit comments