@@ -28,7 +28,7 @@ function findDomains(
28
28
return domains ;
29
29
}
30
30
31
- function inputTemplate ( provider : string ) {
31
+ function inputTemplateAppModule ( provider : string ) {
32
32
/* eslint-disable max-len */
33
33
return tags . stripIndent `
34
34
export class AppModule {
@@ -56,72 +56,127 @@ function inputTemplate(provider: string) {
56
56
}], null, null); })();
57
57
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(AppModule, { declarations: [AppComponent], imports: [BrowserModule,
58
58
NgOptimizedImage] }); })();
59
- ` ;
59
+ ` ;
60
+ }
61
+
62
+ function inputTemplateComponent ( provider : string ) {
63
+ /* eslint-disable max-len */
64
+ return tags . stripIndent `
65
+ export class AppComponent {
66
+ title = 'angular-cli-testbed';
67
+ static ɵfac = function AppComponent_Factory(t) { return new (t || AppComponent)(); };
68
+ static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: AppComponent, selectors: [["app-root"]], standalone: true, features: [i0.ɵɵProvidersFeature([
69
+ ${ provider }
70
+ ]), i0.ɵɵStandaloneFeature], decls: 2, vars: 0, template: function AppComponent_Template(rf, ctx) { if (rf & 1) {
71
+ i0.ɵɵelementStart(0, "div");
72
+ i0.ɵɵtext(1, "Hello world");
73
+ i0.ɵɵelementEnd();
74
+ } } });
75
+ }
76
+ (function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AppComponent, [{
77
+ type: Component,
78
+ args: [{ selector: 'app-root', imports: [NgOptimizedImage, NgSwitchCase, NgSwitchDefault, NgSwitch], standalone: true, providers: [
79
+ ${ provider }
80
+ ], template: "<div>Hello world</div>\n\n" }]
81
+ }], null, null); })();
82
+ ` ;
83
+ }
84
+
85
+ function runSharedTests ( template : ( povider : string ) => string ) {
86
+ it ( 'should find a domain when a built-in loader is used with a string-literal-like argument' , ( ) => {
87
+ // Intentionally inconsistent use of quote styles in this data structure:
88
+ const builtInLoaders : Array < [ string , string ] > = [
89
+ [ 'provideCloudflareLoader("www.cloudflaredomain.com")' , 'www.cloudflaredomain.com' ] ,
90
+ [
91
+ "provideCloudinaryLoader('https://www.cloudinarydomain.net')" ,
92
+ 'https://www.cloudinarydomain.net' ,
93
+ ] ,
94
+ [ 'provideImageKitLoader("www.imageKitdomain.com")' , 'www.imageKitdomain.com' ] ,
95
+ [ 'provideImgixLoader(`www.imgixdomain.com/images/`)' , 'www.imgixdomain.com/images/' ] ,
96
+ ] ;
97
+ for ( const loader of builtInLoaders ) {
98
+ const input = template ( loader [ 0 ] ) ;
99
+ const result = Array . from ( findDomains ( input ) ) ;
100
+ expect ( result . length ) . toBe ( 1 ) ;
101
+ expect ( result [ 0 ] ) . toBe ( loader [ 1 ] ) ;
102
+ }
103
+ } ) ;
104
+
105
+ it ( 'should find a domain in a custom loader function with a template literal' , ( ) => {
106
+ const customLoader = tags . stripIndent `
107
+ {
108
+ provide: IMAGE_LOADER,
109
+ useValue: (config: ImageLoaderConfig) => {
110
+ return ${ '`https://customLoaderTemplate.com/images?src=${config.src}&width=${config.width}`' } ;
111
+ },
112
+ },` ;
113
+ const input = template ( customLoader ) ;
114
+ const result = Array . from ( findDomains ( input ) ) ;
115
+ expect ( result . length ) . toBe ( 1 ) ;
116
+ expect ( result [ 0 ] ) . toBe ( 'https://customLoaderTemplate.com/' ) ;
117
+ } ) ;
118
+
119
+ it ( 'should find a domain when provider is alongside other providers' , ( ) => {
120
+ const customLoader = tags . stripIndent `
121
+ {
122
+ provide: SOME_OTHER_PROVIDER,
123
+ useValue: (config: ImageLoaderConfig) => {
124
+ return "https://notacustomloaderstring.com/images?src=" + config.src + "&width=" + config.width;
125
+ },
126
+ },
127
+ provideNotARealLoader("https://www.foo.com"),
128
+ {
129
+ provide: IMAGE_LOADER,
130
+ useValue: (config: ImageLoaderConfig) => {
131
+ return ${ '`https://customloadertemplate.com/images?src=${config.src}&width=${config.width}`' } ;
132
+ },
133
+ },
134
+ {
135
+ provide: YET_ANOTHER_PROVIDER,
136
+ useValue: (config: ImageLoaderConfig) => {
137
+ return ${ '`https://notacustomloadertemplate.com/images?src=${config.src}&width=${config.width}`' } ;
138
+ },
139
+ },` ;
140
+ const input = template ( customLoader ) ;
141
+ const result = Array . from ( findDomains ( input ) ) ;
142
+ expect ( result . length ) . toBe ( 1 ) ;
143
+ expect ( result [ 0 ] ) . toBe ( 'https://customloadertemplate.com/' ) ;
144
+ } ) ;
60
145
}
61
146
62
147
describe ( '@ngtools/webpack transformers' , ( ) => {
63
- describe ( 'find_image_domains' , ( ) => {
64
- it ( 'should find a domain when a built-in loader is used with a string-literal-like argument' , ( ) => {
65
- // Intentionally inconsistent use of quote styles in this data structure:
66
- const builtInLoaders : Array < [ string , string ] > = [
67
- [ 'provideCloudflareLoader("www.cloudflaredomain.com")' , 'www.cloudflaredomain.com' ] ,
68
- [
69
- "provideCloudinaryLoader('https://www.cloudinarydomain.net')" ,
70
- 'https://www.cloudinarydomain.net' ,
71
- ] ,
72
- [ 'provideImageKitLoader("www.imageKitdomain.com")' , 'www.imageKitdomain.com' ] ,
73
- [ 'provideImgixLoader(`www.imgixdomain.com/images/`)' , 'www.imgixdomain.com/images/' ] ,
74
- ] ;
75
- for ( const loader of builtInLoaders ) {
76
- const input = inputTemplate ( loader [ 0 ] ) ;
77
- const result = Array . from ( findDomains ( input ) ) ;
78
- expect ( result . length ) . toBe ( 1 ) ;
79
- expect ( result [ 0 ] ) . toBe ( loader [ 1 ] ) ;
80
- }
81
- } ) ;
148
+ describe ( 'find_image_domains (app module)' , ( ) => {
149
+ runSharedTests ( inputTemplateAppModule ) ;
150
+ runSharedTests ( inputTemplateComponent ) ;
82
151
83
152
it ( 'should not find a domain when a built-in loader is used with a variable' , ( ) => {
84
- const input = inputTemplate ( `provideCloudflareLoader(myImageCDN)` ) ;
153
+ const input = inputTemplateAppModule ( `provideCloudflareLoader(myImageCDN)` ) ;
85
154
const result = Array . from ( findDomains ( input ) ) ;
86
155
expect ( result . length ) . toBe ( 0 ) ;
87
156
} ) ;
88
157
89
158
it ( 'should not find a domain when a built-in loader is used with an expression' , ( ) => {
90
- const input = inputTemplate (
159
+ const input = inputTemplateAppModule (
91
160
`provideCloudflareLoader("https://www." + (dev ? "dev." : "") + "cloudinarydomain.net")` ,
92
161
) ;
93
162
const result = Array . from ( findDomains ( input ) ) ;
94
163
expect ( result . length ) . toBe ( 0 ) ;
95
164
} ) ;
96
165
97
166
it ( 'should not find a domain when a built-in loader is used with a template literal' , ( ) => {
98
- const input = inputTemplate (
167
+ const input = inputTemplateAppModule (
99
168
'provideCloudflareLoader(`https://www.${dev ? "dev." : ""}cloudinarydomain.net`)' ,
100
169
) ;
101
170
const result = Array . from ( findDomains ( input ) ) ;
102
171
expect ( result . length ) . toBe ( 0 ) ;
103
172
} ) ;
104
173
105
174
it ( 'should not find a domain in a function that is not a built-in loader' , ( ) => {
106
- const input = inputTemplate ( 'provideNotARealLoader("https://www.foo.com")' ) ;
175
+ const input = inputTemplateAppModule ( 'provideNotARealLoader("https://www.foo.com")' ) ;
107
176
const result = Array . from ( findDomains ( input ) ) ;
108
177
expect ( result . length ) . toBe ( 0 ) ;
109
178
} ) ;
110
179
111
- it ( 'should find a domain in a custom loader function with a template literal' , ( ) => {
112
- const customLoader = tags . stripIndent `
113
- {
114
- provide: IMAGE_LOADER,
115
- useValue: (config: ImageLoaderConfig) => {
116
- return ${ '`https://customLoaderTemplate.com/images?src=${config.src}&width=${config.width}`' } ;
117
- },
118
- },` ;
119
- const input = inputTemplate ( customLoader ) ;
120
- const result = Array . from ( findDomains ( input ) ) ;
121
- expect ( result . length ) . toBe ( 1 ) ;
122
- expect ( result [ 0 ] ) . toBe ( 'https://customLoaderTemplate.com/' ) ;
123
- } ) ;
124
-
125
180
it ( 'should find a domain in a custom loader function with string concatenation' , ( ) => {
126
181
const customLoader = tags . stripIndent `
127
182
{
@@ -130,7 +185,7 @@ describe('@ngtools/webpack transformers', () => {
130
185
return "https://customLoaderString.com/images?src=" + config.src + "&width=" + config.width;
131
186
},
132
187
},` ;
133
- const input = inputTemplate ( customLoader ) ;
188
+ const input = inputTemplateAppModule ( customLoader ) ;
134
189
const result = Array . from ( findDomains ( input ) ) ;
135
190
expect ( result . length ) . toBe ( 1 ) ;
136
191
expect ( result [ 0 ] ) . toBe ( 'https://customLoaderString.com/' ) ;
@@ -144,36 +199,9 @@ describe('@ngtools/webpack transformers', () => {
144
199
return "https://customLoaderString.com/images?src=" + config.src + "&width=" + config.width;
145
200
},
146
201
},` ;
147
- const input = inputTemplate ( customLoader ) ;
202
+ const input = inputTemplateAppModule ( customLoader ) ;
148
203
const result = Array . from ( findDomains ( input ) ) ;
149
204
expect ( result . length ) . toBe ( 0 ) ;
150
205
} ) ;
151
-
152
- it ( 'should find a domain when provider is alongside other providers' , ( ) => {
153
- const customLoader = tags . stripIndent `
154
- {
155
- provide: SOME_OTHER_PROVIDER,
156
- useValue: (config: ImageLoaderConfig) => {
157
- return "https://notacustomloaderstring.com/images?src=" + config.src + "&width=" + config.width;
158
- },
159
- },
160
- provideNotARealLoader("https://www.foo.com"),
161
- {
162
- provide: IMAGE_LOADER,
163
- useValue: (config: ImageLoaderConfig) => {
164
- return ${ '`https://customloadertemplate.com/images?src=${config.src}&width=${config.width}`' } ;
165
- },
166
- },
167
- {
168
- provide: YET_ANOTHER_PROVIDER,
169
- useValue: (config: ImageLoaderConfig) => {
170
- return ${ '`https://notacustomloadertemplate.com/images?src=${config.src}&width=${config.width}`' } ;
171
- },
172
- },` ;
173
- const input = inputTemplate ( customLoader ) ;
174
- const result = Array . from ( findDomains ( input ) ) ;
175
- expect ( result . length ) . toBe ( 1 ) ;
176
- expect ( result [ 0 ] ) . toBe ( 'https://customloadertemplate.com/' ) ;
177
- } ) ;
178
206
} ) ;
179
207
} ) ;
0 commit comments