@@ -15,12 +15,12 @@ export function replaceResources(
15
15
return ( context : ts . TransformationContext ) => {
16
16
const typeChecker = getTypeChecker ( ) ;
17
17
const resourceImportDeclarations : ts . ImportDeclaration [ ] = [ ] ;
18
-
18
+ const moduleKind = context . getCompilerOptions ( ) . module ;
19
19
const visitNode : ts . Visitor = ( node : ts . Node ) => {
20
20
if ( ts . isClassDeclaration ( node ) ) {
21
21
const decorators = ts . visitNodes ( node . decorators , node =>
22
22
ts . isDecorator ( node )
23
- ? visitDecorator ( node , typeChecker , directTemplateLoading , resourceImportDeclarations )
23
+ ? visitDecorator ( node , typeChecker , directTemplateLoading , resourceImportDeclarations , moduleKind )
24
24
: node ,
25
25
) ;
26
26
@@ -68,6 +68,7 @@ function visitDecorator(
68
68
typeChecker : ts . TypeChecker ,
69
69
directTemplateLoading : boolean ,
70
70
resourceImportDeclarations : ts . ImportDeclaration [ ] ,
71
+ moduleKind ?: ts . ModuleKind ,
71
72
) : ts . Decorator {
72
73
if ( ! isComponentDecorator ( node , typeChecker ) ) {
73
74
return node ;
@@ -90,7 +91,7 @@ function visitDecorator(
90
91
// visit all properties
91
92
let properties = ts . visitNodes ( objectExpression . properties , node =>
92
93
ts . isObjectLiteralElementLike ( node )
93
- ? visitComponentMetadata ( node , styleReplacements , directTemplateLoading , resourceImportDeclarations )
94
+ ? visitComponentMetadata ( node , styleReplacements , directTemplateLoading , resourceImportDeclarations , moduleKind )
94
95
: node ,
95
96
) ;
96
97
@@ -117,6 +118,7 @@ function visitComponentMetadata(
117
118
styleReplacements : ts . Expression [ ] ,
118
119
directTemplateLoading : boolean ,
119
120
resourceImportDeclarations : ts . ImportDeclaration [ ] ,
121
+ moduleKind ?: ts . ModuleKind ,
120
122
) : ts . ObjectLiteralElementLike | undefined {
121
123
if ( ! ts . isPropertyAssignment ( node ) || ts . isComputedPropertyName ( node . name ) ) {
122
124
return node ;
@@ -128,7 +130,12 @@ function visitComponentMetadata(
128
130
return undefined ;
129
131
130
132
case 'templateUrl' :
131
- const importName = createResourceImport ( node . initializer , directTemplateLoading ? '!raw-loader!' : '' , resourceImportDeclarations ) ;
133
+ const importName = createResourceImport (
134
+ node . initializer ,
135
+ directTemplateLoading ? '!raw-loader!' : '' ,
136
+ resourceImportDeclarations ,
137
+ moduleKind ,
138
+ ) ;
132
139
if ( ! importName ) {
133
140
return node ;
134
141
}
@@ -154,7 +161,7 @@ function visitComponentMetadata(
154
161
return ts . createLiteral ( node . text ) ;
155
162
}
156
163
157
- return createResourceImport ( node , undefined , resourceImportDeclarations ) || node ;
164
+ return createResourceImport ( node , undefined , resourceImportDeclarations , moduleKind ) || node ;
158
165
} ) ;
159
166
160
167
// Styles should be placed first
@@ -170,28 +177,6 @@ function visitComponentMetadata(
170
177
}
171
178
}
172
179
173
- export function createResourceImport (
174
- node : ts . Node ,
175
- loader : string | undefined ,
176
- resourceImportDeclarations : ts . ImportDeclaration [ ] ,
177
- ) : ts . Identifier | null {
178
- const url = getResourceUrl ( node , loader ) ;
179
- if ( ! url ) {
180
- return null ;
181
- }
182
-
183
- const importName = ts . createIdentifier ( `__NG_CLI_RESOURCE__${ resourceImportDeclarations . length } ` ) ;
184
-
185
- resourceImportDeclarations . push ( ts . createImportDeclaration (
186
- undefined ,
187
- undefined ,
188
- ts . createImportClause ( importName , undefined ) ,
189
- ts . createLiteral ( url ) ,
190
- ) ) ;
191
-
192
- return importName ;
193
- }
194
-
195
180
export function getResourceUrl ( node : ts . Node , loader = '' ) : string | null {
196
181
// only analyze strings
197
182
if ( ! ts . isStringLiteral ( node ) && ! ts . isNoSubstitutionTemplateLiteral ( node ) ) {
@@ -214,6 +199,41 @@ function isComponentDecorator(node: ts.Node, typeChecker: ts.TypeChecker): node
214
199
return false ;
215
200
}
216
201
202
+ function createResourceImport (
203
+ node : ts . Node ,
204
+ loader : string | undefined ,
205
+ resourceImportDeclarations : ts . ImportDeclaration [ ] ,
206
+ moduleKind = ts . ModuleKind . ES2015 ,
207
+ ) : ts . Identifier | ts . Expression | null {
208
+ const url = getResourceUrl ( node , loader ) ;
209
+ if ( ! url ) {
210
+ return null ;
211
+ }
212
+
213
+ const urlLiteral = ts . createLiteral ( url ) ;
214
+
215
+ if ( moduleKind < ts . ModuleKind . ES2015 ) {
216
+ return ts . createPropertyAccess (
217
+ ts . createCall (
218
+ ts . createIdentifier ( 'require' ) ,
219
+ [ ] ,
220
+ [ urlLiteral ] ,
221
+ ) ,
222
+ 'default' ,
223
+ ) ;
224
+ } else {
225
+ const importName = ts . createIdentifier ( `__NG_CLI_RESOURCE__${ resourceImportDeclarations . length } ` ) ;
226
+ resourceImportDeclarations . push ( ts . createImportDeclaration (
227
+ undefined ,
228
+ undefined ,
229
+ ts . createImportClause ( importName , undefined ) ,
230
+ urlLiteral ,
231
+ ) ) ;
232
+
233
+ return importName ;
234
+ }
235
+ }
236
+
217
237
interface DecoratorOrigin {
218
238
name : string ;
219
239
module : string ;
0 commit comments