@@ -10,6 +10,7 @@ import * as ts from 'typescript';
10
10
export function replaceResources (
11
11
shouldTransform : ( fileName : string ) => boolean ,
12
12
getTypeChecker : ( ) => ts . TypeChecker ,
13
+ directTemplateLoading = false ,
13
14
) : ts . TransformerFactory < ts . SourceFile > {
14
15
15
16
return ( context : ts . TransformationContext ) => {
@@ -19,7 +20,7 @@ export function replaceResources(
19
20
if ( ts . isClassDeclaration ( node ) ) {
20
21
node . decorators = ts . visitNodes (
21
22
node . decorators ,
22
- ( node : ts . Decorator ) => visitDecorator ( node , typeChecker ) ,
23
+ ( node : ts . Decorator ) => visitDecorator ( node , typeChecker , directTemplateLoading ) ,
23
24
) ;
24
25
}
25
26
@@ -34,7 +35,10 @@ export function replaceResources(
34
35
} ;
35
36
}
36
37
37
- function visitDecorator ( node : ts . Decorator , typeChecker : ts . TypeChecker ) : ts . Decorator {
38
+ function visitDecorator (
39
+ node : ts . Decorator ,
40
+ typeChecker : ts . TypeChecker ,
41
+ directTemplateLoading : boolean ) : ts . Decorator {
38
42
if ( ! isComponentDecorator ( node , typeChecker ) ) {
39
43
return node ;
40
44
}
@@ -56,7 +60,8 @@ function visitDecorator(node: ts.Decorator, typeChecker: ts.TypeChecker): ts.Dec
56
60
// visit all properties
57
61
let properties = ts . visitNodes (
58
62
objectExpression . properties ,
59
- ( node : ts . ObjectLiteralElementLike ) => visitComponentMetadata ( node , styleReplacements ) ,
63
+ ( node : ts . ObjectLiteralElementLike ) =>
64
+ visitComponentMetadata ( node , styleReplacements , directTemplateLoading ) ,
60
65
) ;
61
66
62
67
// replace properties with updated properties
@@ -83,6 +88,7 @@ function visitDecorator(node: ts.Decorator, typeChecker: ts.TypeChecker): ts.Dec
83
88
function visitComponentMetadata (
84
89
node : ts . ObjectLiteralElementLike ,
85
90
styleReplacements : ts . Expression [ ] ,
91
+ directTemplateLoading : boolean ,
86
92
) : ts . ObjectLiteralElementLike | undefined {
87
93
if ( ! ts . isPropertyAssignment ( node ) || ts . isComputedPropertyName ( node . name ) ) {
88
94
return node ;
@@ -98,7 +104,7 @@ function visitComponentMetadata(
98
104
return ts . updatePropertyAssignment (
99
105
node ,
100
106
ts . createIdentifier ( 'template' ) ,
101
- createRequireExpression ( node . initializer ) ,
107
+ createRequireExpression ( node . initializer , directTemplateLoading ? '!raw-loader!' : '' ) ,
102
108
) ;
103
109
104
110
case 'styles' :
@@ -133,13 +139,13 @@ function visitComponentMetadata(
133
139
}
134
140
}
135
141
136
- export function getResourceUrl ( node : ts . Expression ) : string | null {
142
+ export function getResourceUrl ( node : ts . Expression , loader = '' ) : string | null {
137
143
// only analyze strings
138
144
if ( ! ts . isStringLiteral ( node ) && ! ts . isNoSubstitutionTemplateLiteral ( node ) ) {
139
145
return null ;
140
146
}
141
147
142
- return `${ / ^ \. ? \. \/ / . test ( node . text ) ? '' : './' } ${ node . text } ` ;
148
+ return `${ loader } ${ / ^ \. ? \. \/ / . test ( node . text ) ? '' : './' } ${ node . text } ` ;
143
149
}
144
150
145
151
function isComponentDecorator ( node : ts . Node , typeChecker : ts . TypeChecker ) : node is ts . Decorator {
@@ -155,8 +161,8 @@ function isComponentDecorator(node: ts.Node, typeChecker: ts.TypeChecker): node
155
161
return false ;
156
162
}
157
163
158
- function createRequireExpression ( node : ts . Expression ) : ts . Expression {
159
- const url = getResourceUrl ( node ) ;
164
+ function createRequireExpression ( node : ts . Expression , loader = '' ) : ts . Expression {
165
+ const url = getResourceUrl ( node , loader ) ;
160
166
if ( ! url ) {
161
167
return node ;
162
168
}
0 commit comments