@@ -20,25 +20,59 @@ if (!global[ELEMENT_REGISTRY]) {
20
20
} ;
21
21
}
22
22
23
- module . exports = function ( source , map ) {
23
+ function parseResource ( source , map ) {
24
24
this . cacheable ( ) ;
25
25
26
- var loader = this ;
26
+ let templateSource ;
27
+ try {
28
+ templateSource = getTemplateSource ( this . resourcePath , source ) ;
29
+ } catch ( e ) {
30
+ this . emitWarning ( e . message ) ;
31
+ return this . callback ( null , source , map ) ;
32
+ }
33
+
34
+ if ( templateSource === "" ) {
35
+ return this . callback ( null , source , map ) ;
36
+ }
27
37
28
38
var parser = new htmlparser . Parser ( {
29
39
onopentag : function ( name , attribs ) {
30
40
// kebab-case to CamelCase
31
41
var elementName = name . split ( "-" ) . map ( function ( s ) { return s [ 0 ] . toUpperCase ( ) + s . substring ( 1 ) ; } ) . join ( "" ) ;
42
+
32
43
// Module path from element name
33
44
var modulePath = MODULES [ elementName ] || UI_PATH +
34
45
( elementName . toLowerCase ( ) . indexOf ( "layout" ) !== - 1 ? "layouts/" : "" ) +
35
46
elementName . split ( / (? = [ A - Z ] ) / ) . join ( "-" ) . toLowerCase ( ) ;
47
+
36
48
// Update ELEMENT_REGISTRY
37
49
global [ ELEMENT_REGISTRY ] [ modulePath ] = elementName ;
38
50
}
39
51
} , { decodeEntities : true , lowerCaseTags : false } ) ;
40
- parser . write ( source ) ;
52
+
53
+ parser . write ( templateSource ) ;
41
54
parser . end ( ) ;
42
55
43
- this . callback ( null , source , map ) ;
44
- } ;
56
+ return this . callback ( null , source , map ) ;
57
+ }
58
+
59
+ function getTemplateSource ( path , source ) {
60
+ if ( isTemplate ( path ) ) {
61
+ return source ;
62
+ } else if ( isComponent ( path ) ) {
63
+ const templateMatcher = / t e m p l a t e \s * : \s * ( [ ` ' " ] ) ( ( .| \n ) * ?) \1/ ;
64
+ return templateMatcher . test ( source ) ? source . replace ( templateMatcher , "$2" ) : "" ;
65
+ } else {
66
+ throw new Error ( `The NativeScript XML loader must be used with HTML, XML or TypeScript files` ) ;
67
+ }
68
+ }
69
+
70
+ function isComponent ( resource ) {
71
+ return / \. t s $ / i. test ( resource ) ;
72
+ }
73
+
74
+ function isTemplate ( resource ) {
75
+ return / \. h t m l $ | \. x m l $ / i. test ( resource ) ;
76
+ }
77
+
78
+ module . exports = parseResource ;
0 commit comments