@@ -7,6 +7,7 @@ import { isEventOrGesture } from "../../core/bindable";
7
7
import { File , path , knownFolders } from "../../../file-system" ;
8
8
import { getBindingOptions , bindingConstants } from "../binding-builder" ;
9
9
import { resolveFileName } from "../../../file-system/file-name-resolver" ;
10
+ import { profile } from "tns-core-modules/profiling" ;
10
11
import * as debugModule from "../../../utils/debug" ;
11
12
import * as platform from "../../../platform" ;
12
13
@@ -24,14 +25,9 @@ const CODEFILE = "codeFile";
24
25
const CSSFILE = "cssFile" ;
25
26
const IMPORT = "import" ;
26
27
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 } => {
28
29
var instance : View ;
29
30
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
-
35
31
// Get module id.
36
32
var moduleId = MODULES [ elementName ] || UI_PATH +
37
33
( elementName . toLowerCase ( ) . indexOf ( "layout" ) !== - 1 ? "layouts/" : "" ) +
@@ -70,7 +66,10 @@ export function getComponentModule(elementName: string, namespace: string, attri
70
66
throw new debug . ScopeError ( ex , "Module '" + moduleId + "' not found for element '" + ( namespace ? namespace + ":" : "" ) + elementName + "'." ) ;
71
67
}
72
68
73
- let cssApplied = false ;
69
+ return { instance, instanceModule } ;
70
+ } ) ;
71
+
72
+ const getComponentModuleExports = profile ( "getComponentModuleExports" , ( instance : View , moduleExports : Object , attributes : Object ) : Object => {
74
73
if ( attributes ) {
75
74
if ( attributes [ IMPORT ] ) {
76
75
let importPath = attributes [ IMPORT ] . trim ( ) ;
@@ -79,39 +78,43 @@ export function getComponentModule(elementName: string, namespace: string, attri
79
78
importPath = path . join ( knownFolders . currentApp ( ) . path , importPath . replace ( "~/" , "" ) ) ;
80
79
}
81
80
82
- exports = global . loadModule ( importPath ) ;
83
- ( < any > instance ) . exports = exports ;
81
+ moduleExports = global . loadModule ( importPath ) ;
82
+ ( < any > instance ) . exports = moduleExports ;
84
83
}
85
84
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
+ }
92
90
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!` ) ;
100
97
}
98
+ }
99
+ }
100
+ return moduleExports ;
101
+ } ) ;
101
102
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!` ) ;
113
116
}
114
- // }
117
+ }
115
118
}
116
119
117
120
if ( typeof ( < any > instance ) . addCssFile === "function" ) { //instance instanceof Page) {
@@ -129,7 +132,9 @@ export function getComponentModule(elementName: string, namespace: string, attri
129
132
( < any > instance ) . _refreshCss ( ) ;
130
133
}
131
134
}
135
+ } ) ;
132
136
137
+ const applyComponentAttributes = profile ( "applyComponentAttributes" , ( instance : View , instanceModule : Object , moduleExports : Object , attributes : Object ) => {
133
138
if ( instance && instanceModule ) {
134
139
for ( let attr in attributes ) {
135
140
@@ -157,16 +162,28 @@ export function getComponentModule(elementName: string, namespace: string, attri
157
162
}
158
163
159
164
if ( subObj !== undefined && subObj !== null ) {
160
- setPropertyValue ( subObj , instanceModule , exports , subPropName , attrValue ) ;
165
+ setPropertyValue ( subObj , instanceModule , moduleExports , subPropName , attrValue ) ;
161
166
}
162
167
} else {
163
- setPropertyValue ( instance , instanceModule , exports , attr , attrValue ) ;
168
+ setPropertyValue ( instance , instanceModule , moduleExports , attr , attrValue ) ;
164
169
}
165
170
}
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 ( "" ) ;
166
177
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 ) {
167
185
componentModule = { component : instance , exports : instanceModule } ;
168
186
}
169
-
170
187
return componentModule ;
171
188
}
172
189
0 commit comments