@@ -24,7 +24,24 @@ export class FileSystemResourceLoader extends ResourceLoader {
2424 return templateFile . readText ( ) ;
2525 }
2626
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 {
2845 // Angular assembles absolute URLs and prefixes them with //
2946 if ( url . indexOf ( "/" ) !== 0 ) {
3047 // Resolve relative URLs based on the app root.
@@ -34,26 +51,22 @@ export class FileSystemResourceLoader extends ResourceLoader {
3451 }
3552 }
3653
37- resolve ( url : string ) {
38- const normalizedUrl = this . resolveRelativeUrls ( url ) ;
54+ private fallbackResolve ( url : string ) :
55+ ( { resource : string , candidates : string [ ] } ) {
3956
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 ) ) ;
4361
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 ) ) ;
5663
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 ;
5870 }
5971}
72+
0 commit comments