@@ -4,11 +4,11 @@ import { path } from "tns-core-modules/file-system";
4
4
5
5
import { NSFileSystem } from "./file-system/ns-file-system" ;
6
6
7
- const extensionsFallbacks = [
8
- [ ".scss" , ".css" ] ,
9
- [ ".sass" , ".css" ] ,
10
- [ ".less" , ".css" ]
11
- ] ;
7
+ const sourceExtensionsMap = {
8
+ ".scss" : ".css" ,
9
+ ".sass" : ".css" ,
10
+ ".less" : ".css"
11
+ } ;
12
12
13
13
@Injectable ( )
14
14
export class FileSystemResourceLoader extends ResourceLoader {
@@ -25,20 +25,21 @@ export class FileSystemResourceLoader extends ResourceLoader {
25
25
}
26
26
27
27
resolve ( url : string ) : string {
28
- const normalizedUrl = this . resolveRelativeUrls ( url ) ;
29
-
30
- if ( this . fs . fileExists ( normalizedUrl ) ) {
31
- return normalizedUrl ;
28
+ const normalizedSourceUrl = this . resolveRelativeUrls ( url ) ;
29
+ const normalizedCompiledFileUrl = normalizedSourceUrl . replace ( / \. \w + $ / , ext => sourceExtensionsMap [ ext ] || ext ) ;
30
+ if ( normalizedCompiledFileUrl !== normalizedSourceUrl && this . fs . fileExists ( normalizedCompiledFileUrl ) ) {
31
+ return normalizedCompiledFileUrl ;
32
32
}
33
-
34
- const { candidates : fallbackCandidates , resource : fallbackResource } =
35
- this . fallbackResolve ( normalizedUrl ) ;
36
-
37
- if ( fallbackResource ) {
38
- return fallbackResource ;
33
+ if ( this . fs . fileExists ( normalizedSourceUrl ) ) {
34
+ return normalizedSourceUrl ;
39
35
}
40
36
41
- throw new Error ( `Could not resolve ${ url } . Looked for: ${ normalizedUrl } , ${ fallbackCandidates } ` ) ;
37
+ if ( normalizedCompiledFileUrl === normalizedSourceUrl ) {
38
+ throw new Error ( `Could not resolve ${ url } . Looked for: ${ normalizedSourceUrl } .` ) ;
39
+ } else {
40
+ throw new Error ( `Could not resolve ${ url } .` +
41
+ `Looked for: ${ normalizedCompiledFileUrl } , ${ normalizedSourceUrl } .` ) ;
42
+ }
42
43
}
43
44
44
45
private resolveRelativeUrls ( url : string ) : string {
@@ -50,23 +51,4 @@ export class FileSystemResourceLoader extends ResourceLoader {
50
51
return url ;
51
52
}
52
53
}
53
-
54
- private fallbackResolve ( url : string ) :
55
- ( { resource : string , candidates : string [ ] } ) {
56
-
57
- const candidates = extensionsFallbacks
58
- . filter ( ( [ extension ] ) => url . endsWith ( extension ) )
59
- . map ( ( [ extension , fallback ] ) =>
60
- this . replaceExtension ( url , extension , fallback ) ) ;
61
-
62
- const resource = candidates . find ( candidate => this . fs . fileExists ( candidate ) ) ;
63
-
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 ;
70
- }
71
54
}
72
-
0 commit comments