@@ -180,42 +180,37 @@ function visitComponentMetadata(
180
180
importName ,
181
181
) ;
182
182
case 'styles' :
183
+ case 'styleUrl' :
183
184
case 'styleUrls' :
184
- if ( ! ts . isArrayLiteralExpression ( node . initializer ) ) {
185
+ const isInlineStyle = name === 'styles' ;
186
+ let styles : Iterable < ts . Expression > ;
187
+
188
+ if ( ts . isStringLiteralLike ( node . initializer ) ) {
189
+ styles = [
190
+ transformInlineStyleLiteral (
191
+ node . initializer ,
192
+ nodeFactory ,
193
+ isInlineStyle ,
194
+ inlineStyleFileExtension ,
195
+ resourceImportDeclarations ,
196
+ moduleKind ,
197
+ ) as ts . StringLiteralLike ,
198
+ ] ;
199
+ } else if ( ts . isArrayLiteralExpression ( node . initializer ) ) {
200
+ styles = ts . visitNodes ( node . initializer . elements , ( node ) =>
201
+ transformInlineStyleLiteral (
202
+ node ,
203
+ nodeFactory ,
204
+ isInlineStyle ,
205
+ inlineStyleFileExtension ,
206
+ resourceImportDeclarations ,
207
+ moduleKind ,
208
+ ) ,
209
+ ) as ts . NodeArray < ts . Expression > ;
210
+ } else {
185
211
return node ;
186
212
}
187
213
188
- const isInlineStyle = name === 'styles' ;
189
- const styles = ts . visitNodes ( node . initializer . elements , ( node ) => {
190
- if ( ! ts . isStringLiteral ( node ) && ! ts . isNoSubstitutionTemplateLiteral ( node ) ) {
191
- return node ;
192
- }
193
-
194
- let url ;
195
- if ( isInlineStyle ) {
196
- if ( inlineStyleFileExtension ) {
197
- const data = Buffer . from ( node . text ) . toString ( 'base64' ) ;
198
- const containingFile = node . getSourceFile ( ) . fileName ;
199
- // app.component.ts.css?ngResource!=!@ngtools/webpack/src/loaders/inline-resource.js?data=...!app.component.ts
200
- url =
201
- `${ containingFile } .${ inlineStyleFileExtension } ?${ NG_COMPONENT_RESOURCE_QUERY } ` +
202
- `!=!${ InlineAngularResourceLoaderPath } ?data=${ encodeURIComponent (
203
- data ,
204
- ) } !${ containingFile } `;
205
- } else {
206
- return nodeFactory . createStringLiteral ( node . text ) ;
207
- }
208
- } else {
209
- url = getResourceUrl ( node ) ;
210
- }
211
-
212
- if ( ! url ) {
213
- return node ;
214
- }
215
-
216
- return createResourceImport ( nodeFactory , url , resourceImportDeclarations , moduleKind ) ;
217
- } ) as ts . NodeArray < ts . Expression > ;
218
-
219
214
// Styles should be placed first
220
215
if ( isInlineStyle ) {
221
216
styleReplacements . unshift ( ...styles ) ;
@@ -229,9 +224,44 @@ function visitComponentMetadata(
229
224
}
230
225
}
231
226
227
+ function transformInlineStyleLiteral (
228
+ node : ts . Node ,
229
+ nodeFactory : ts . NodeFactory ,
230
+ isInlineStyle : boolean ,
231
+ inlineStyleFileExtension : string | undefined ,
232
+ resourceImportDeclarations : ts . ImportDeclaration [ ] ,
233
+ moduleKind : ts . ModuleKind ,
234
+ ) {
235
+ if ( ! ts . isStringLiteralLike ( node ) ) {
236
+ return node ;
237
+ }
238
+
239
+ if ( ! isInlineStyle ) {
240
+ const url = getResourceUrl ( node ) ;
241
+
242
+ return url
243
+ ? createResourceImport ( nodeFactory , url , resourceImportDeclarations , moduleKind )
244
+ : node ;
245
+ }
246
+
247
+ if ( ! inlineStyleFileExtension ) {
248
+ return nodeFactory . createStringLiteral ( node . text ) ;
249
+ }
250
+
251
+ const data = Buffer . from ( node . text ) . toString ( 'base64' ) ;
252
+ const containingFile = node . getSourceFile ( ) . fileName ;
253
+
254
+ // app.component.ts.css?ngResource!=!@ngtools/webpack/src/loaders/inline-resource.js?data=...!app.component.ts
255
+ const url =
256
+ `${ containingFile } .${ inlineStyleFileExtension } ?${ NG_COMPONENT_RESOURCE_QUERY } ` +
257
+ `!=!${ InlineAngularResourceLoaderPath } ?data=${ encodeURIComponent ( data ) } !${ containingFile } ` ;
258
+
259
+ return createResourceImport ( nodeFactory , url , resourceImportDeclarations , moduleKind ) ;
260
+ }
261
+
232
262
export function getResourceUrl ( node : ts . Node ) : string | null {
233
263
// only analyze strings
234
- if ( ! ts . isStringLiteral ( node ) && ! ts . isNoSubstitutionTemplateLiteral ( node ) ) {
264
+ if ( ! ts . isStringLiteralLike ( node ) ) {
235
265
return null ;
236
266
}
237
267
0 commit comments