@@ -24,7 +24,24 @@ export class FileSystemResourceLoader extends ResourceLoader {
24
24
return templateFile . readText ( ) ;
25
25
}
26
26
27
- resolveRelativeUrls ( url : string ) : string {
27
+ resolve ( url : string ) : string {
28
+ const normalizedUrl = this . resolveRelativeUrls ( url ) ;
29
+
30
+ if ( this . fs . fileExists ( normalizedUrl ) ) {
31
+ return normalizedUrl ;
32
+ }
33
+
34
+ const { candidates : fallbackCandidates , resource : fallbackResource } =
35
+ this . fallbackResolve ( normalizedUrl ) ;
36
+
37
+ if ( fallbackResource ) {
38
+ return fallbackResource ;
39
+ }
40
+
41
+ throw new Error ( `Could not resolve ${ url } . Looked for: ${ normalizedUrl } , ${ fallbackCandidates } ` ) ;
42
+ }
43
+
44
+ private resolveRelativeUrls ( url : string ) : string {
28
45
// Angular assembles absolute URLs and prefixes them with //
29
46
if ( url . indexOf ( "/" ) !== 0 ) {
30
47
// Resolve relative URLs based on the app root.
@@ -34,26 +51,22 @@ export class FileSystemResourceLoader extends ResourceLoader {
34
51
}
35
52
}
36
53
37
- resolve ( url : string ) {
38
- const normalizedUrl = this . resolveRelativeUrls ( url ) ;
54
+ private fallbackResolve ( url : string ) :
55
+ ( { resource : string , candidates : string [ ] } ) {
39
56
40
- if ( this . fs . fileExists ( normalizedUrl ) ) {
41
- return normalizedUrl ;
42
- }
57
+ const candidates = extensionsFallbacks
58
+ . filter ( ( [ extension ] ) => url . endsWith ( extension ) )
59
+ . map ( ( [ extension , fallback ] ) =>
60
+ this . replaceExtension ( url , extension , fallback ) ) ;
43
61
44
- const fallbackCandidates = [ ] ;
45
- extensionsFallbacks . forEach ( ( [ extension , fallback ] ) => {
46
- if ( normalizedUrl . endsWith ( extension ) ) {
47
- fallbackCandidates . push ( normalizedUrl . substr ( 0 , normalizedUrl . length - extension . length ) + fallback ) ;
48
- }
49
- } ) ;
50
-
51
- for ( let i = 0 ; i < fallbackCandidates . length ; i ++ ) {
52
- if ( this . fs . fileExists ( fallbackCandidates [ i ] ) ) {
53
- return fallbackCandidates [ i ] ;
54
- }
55
- }
62
+ const resource = candidates . find ( candidate => this . fs . fileExists ( candidate ) ) ;
56
63
57
- throw new Error ( `Could not resolve ${ url } . Looked for: ${ normalizedUrl } , ${ fallbackCandidates } ` ) ;
64
+ return { candidates, resource } ;
65
+ }
66
+
67
+ private replaceExtension ( fileName : string , oldExtension : string , newExtension : string ) : string {
68
+ const baseName = fileName . substr ( 0 , fileName . length - oldExtension . length ) ;
69
+ return baseName + newExtension ;
58
70
}
59
71
}
72
+
0 commit comments