@@ -67,12 +67,16 @@ export class InlineResourcesMetadataTransformer implements MetadataTransformer {
6767 arg [ 'template' ] = loader . get ( arg [ 'templateUrl' ] ) ;
6868 delete arg . templateUrl ;
6969 }
70- if ( arg [ 'styleUrls' ] ) {
71- const styleUrls = arg [ 'styleUrls' ] ;
72- if ( Array . isArray ( styleUrls ) ) {
73- arg [ 'styles' ] = styleUrls . map ( styleUrl => loader . get ( styleUrl ) ) ;
74- delete arg . styleUrls ;
75- }
70+
71+ const styles = arg [ 'styles' ] || [ ] ;
72+ const styleUrls = arg [ 'styleUrls' ] || [ ] ;
73+ if ( ! Array . isArray ( styles ) ) throw new Error ( 'styles should be an array' ) ;
74+ if ( ! Array . isArray ( styleUrls ) ) throw new Error ( 'styleUrls should be an array' ) ;
75+
76+ styles . push ( ...styleUrls . map ( styleUrl => loader . get ( styleUrl ) ) ) ;
77+ if ( styles . length > 0 ) {
78+ arg [ 'styles' ] = styles ;
79+ delete arg . styleUrls ;
7680 }
7781
7882 return arg ;
@@ -262,49 +266,59 @@ function updateComponentProperties(
262266 // argument
263267 return args ;
264268 }
265- const newArgument = ts . updateObjectLiteral (
266- componentArg , ts . visitNodes ( componentArg . properties , ( node : ts . ObjectLiteralElementLike ) => {
267- if ( ! ts . isPropertyAssignment ( node ) ) {
268- // Error: unsupported
269- return node ;
270- }
271269
272- if ( ts . isComputedPropertyName ( node . name ) ) {
273- // computed names are not supported
274- return node ;
270+ const newProperties : ts . ObjectLiteralElementLike [ ] = [ ] ;
271+ const newStyleExprs : ts . Expression [ ] = [ ] ;
272+ componentArg . properties . forEach ( prop => {
273+ if ( ! ts . isPropertyAssignment ( prop ) || ts . isComputedPropertyName ( prop . name ) ) {
274+ newProperties . push ( prop ) ;
275+ return ;
276+ }
277+
278+ switch ( prop . name . text ) {
279+ case 'styles' :
280+ if ( ! ts . isArrayLiteralExpression ( prop . initializer ) ) {
281+ throw new Error ( 'styles takes an array argument' ) ;
275282 }
283+ newStyleExprs . push ( ...prop . initializer . elements ) ;
284+ break ;
276285
277- const name = node . name . text ;
278- switch ( name ) {
279- case 'styleUrls' :
280- if ( ! ts . isArrayLiteralExpression ( node . initializer ) ) {
281- // Error: unsupported
282- return node ;
283- }
284- const styleUrls = node . initializer . elements ;
285-
286- return ts . updatePropertyAssignment (
287- node , ts . createIdentifier ( 'styles' ) ,
288- ts . createArrayLiteral ( ts . visitNodes ( styleUrls , ( expr : ts . Expression ) => {
289- if ( ts . isStringLiteral ( expr ) ) {
290- const styles = loader . get ( expr . text ) ;
291- return ts . createLiteral ( styles ) ;
292- }
293- return expr ;
294- } ) ) ) ;
295-
296-
297- case 'templateUrl' :
298- if ( ts . isStringLiteral ( node . initializer ) ) {
299- const template = loader . get ( node . initializer . text ) ;
300- return ts . updatePropertyAssignment (
301- node , ts . createIdentifier ( 'template' ) , ts . createLiteral ( template ) ) ;
302- }
303- return node ;
304-
305- default :
306- return node ;
286+ case 'styleUrls' :
287+ if ( ! ts . isArrayLiteralExpression ( prop . initializer ) ) {
288+ throw new Error ( 'styleUrls takes an array argument' ) ;
289+ }
290+ newStyleExprs . push ( ...prop . initializer . elements . map ( ( expr : ts . Expression ) => {
291+ if ( ! ts . isStringLiteral ( expr ) && ! ts . isNoSubstitutionTemplateLiteral ( expr ) ) {
292+ throw new Error (
293+ 'Can only accept string literal arguments to styleUrls. ' + PRECONDITIONS_TEXT ) ;
294+ }
295+ const styles = loader . get ( expr . text ) ;
296+ return ts . createLiteral ( styles ) ;
297+ } ) ) ;
298+ break ;
299+
300+ case 'templateUrl' :
301+ if ( ! ts . isStringLiteral ( prop . initializer ) &&
302+ ! ts . isNoSubstitutionTemplateLiteral ( prop . initializer ) ) {
303+ throw new Error (
304+ 'Can only accept a string literal argument to templateUrl. ' + PRECONDITIONS_TEXT ) ;
307305 }
308- } ) ) ;
309- return ts . createNodeArray < ts . Expression > ( [ newArgument ] ) ;
306+ const template = loader . get ( prop . initializer . text ) ;
307+ newProperties . push ( ts . updatePropertyAssignment (
308+ prop , ts . createIdentifier ( 'template' ) , ts . createLiteral ( template ) ) ) ;
309+ break ;
310+
311+ default :
312+ newProperties . push ( prop ) ;
313+ }
314+ } ) ;
315+
316+ // Add the non-inline styles
317+ if ( newStyleExprs . length > 0 ) {
318+ const newStyles = ts . createPropertyAssignment (
319+ ts . createIdentifier ( 'styles' ) , ts . createArrayLiteral ( newStyleExprs ) ) ;
320+ newProperties . push ( newStyles ) ;
321+ }
322+
323+ return ts . createNodeArray ( [ ts . updateObjectLiteral ( componentArg , newProperties ) ] ) ;
310324}
0 commit comments