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