@@ -274,9 +274,36 @@ function _getResourceRequest(element: ts.Expression, sourceFile: ts.SourceFile)
274
274
function _replaceResources ( refactor : TypeScriptFileRefactor ) : void {
275
275
const sourceFile = refactor . sourceFile ;
276
276
277
+ _getResourceNodes ( refactor )
278
+ // Get the full text of the initializer.
279
+ . forEach ( ( node : ts . PropertyAssignment ) => {
280
+ const key = _getContentOfKeyLiteral ( sourceFile , node . name ) ;
281
+
282
+ if ( key == 'templateUrl' ) {
283
+ refactor . replaceNode ( node ,
284
+ `template: require(${ _getResourceRequest ( node . initializer , sourceFile ) } )` ) ;
285
+ } else if ( key == 'styleUrls' ) {
286
+ const arr = < ts . ArrayLiteralExpression [ ] > (
287
+ refactor . findAstNodes ( node , ts . SyntaxKind . ArrayLiteralExpression , false ) ) ;
288
+ if ( ! arr || arr . length == 0 || arr [ 0 ] . elements . length == 0 ) {
289
+ return ;
290
+ }
291
+
292
+ const initializer = arr [ 0 ] . elements . map ( ( element : ts . Expression ) => {
293
+ return _getResourceRequest ( element , sourceFile ) ;
294
+ } ) ;
295
+ refactor . replaceNode ( node , `styles: [require(${ initializer . join ( '), require(' ) } )]` ) ;
296
+ }
297
+ } ) ;
298
+ }
299
+
300
+
301
+ function _getResourceNodes ( refactor : TypeScriptFileRefactor ) {
302
+ const { sourceFile } = refactor ;
303
+
277
304
// Find all object literals.
278
- refactor . findAstNodes ( sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
279
- // Get all their property assignments.
305
+ return refactor . findAstNodes ( sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
306
+ // Get all their property assignments.
280
307
. map ( node => refactor . findAstNodes ( node , ts . SyntaxKind . PropertyAssignment ) )
281
308
// Flatten into a single array (from an array of array<property assignments>).
282
309
. reduce ( ( prev , curr ) => curr ? prev . concat ( curr ) : prev , [ ] )
@@ -288,27 +315,38 @@ function _replaceResources(refactor: TypeScriptFileRefactor): void {
288
315
return false ;
289
316
}
290
317
return key == 'templateUrl' || key == 'styleUrls' ;
291
- } )
292
- // Get the full text of the initializer.
293
- . forEach ( ( node : ts . PropertyAssignment ) => {
294
- const key = _getContentOfKeyLiteral ( sourceFile , node . name ) ;
318
+ } ) ;
319
+ }
320
+
321
+
322
+ function _getResourcesUrls ( refactor : TypeScriptFileRefactor ) : string [ ] {
323
+ return _getResourceNodes ( refactor )
324
+ . reduce ( ( acc : string [ ] , node : ts . PropertyAssignment ) => {
325
+ const key = _getContentOfKeyLiteral ( refactor . sourceFile , node . name ) ;
295
326
296
327
if ( key == 'templateUrl' ) {
297
- refactor . replaceNode ( node ,
298
- `template: require(${ _getResourceRequest ( node . initializer , sourceFile ) } )` ) ;
328
+ const url = ( node . initializer as ts . StringLiteral ) . text ;
329
+ if ( url ) {
330
+ acc . push ( url ) ;
331
+ }
299
332
} else if ( key == 'styleUrls' ) {
300
333
const arr = < ts . ArrayLiteralExpression [ ] > (
301
334
refactor . findAstNodes ( node , ts . SyntaxKind . ArrayLiteralExpression , false ) ) ;
302
335
if ( ! arr || arr . length == 0 || arr [ 0 ] . elements . length == 0 ) {
303
336
return ;
304
337
}
305
338
306
- const initializer = arr [ 0 ] . elements . map ( ( element : ts . Expression ) => {
307
- return _getResourceRequest ( element , sourceFile ) ;
339
+ arr [ 0 ] . elements . forEach ( ( element : ts . Expression ) => {
340
+ if ( element . kind == ts . SyntaxKind . StringLiteral ) {
341
+ const url = ( element as ts . StringLiteral ) . text ;
342
+ if ( url ) {
343
+ acc . push ( url ) ;
344
+ }
345
+ }
308
346
} ) ;
309
- refactor . replaceNode ( node , `styles: [require(${ initializer . join ( '), require(' ) } )]` ) ;
310
347
}
311
- } ) ;
348
+ return acc ;
349
+ } , [ ] ) ;
312
350
}
313
351
314
352
@@ -362,6 +400,12 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }) {
362
400
plugin . diagnose ( sourceFileName ) ;
363
401
}
364
402
} )
403
+ . then ( ( ) => {
404
+ // Add resources as dependencies.
405
+ _getResourcesUrls ( refactor ) . forEach ( ( url : string ) => {
406
+ this . addDependency ( path . resolve ( path . dirname ( sourceFileName ) , url ) ) ;
407
+ } ) ;
408
+ } )
365
409
. then ( ( ) => {
366
410
// Force a few compiler options to make sure we get the result we want.
367
411
const compilerOptions : ts . CompilerOptions = Object . assign ( { } , plugin . compilerOptions , {
0 commit comments