-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathresource-loader.ts
54 lines (45 loc) · 1.79 KB
/
resource-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Injectable } from "@angular/core";
import { ResourceLoader } from "@angular/compiler";
import { path } from "tns-core-modules/file-system";
import { NSFileSystem } from "./file-system/ns-file-system";
const sourceExtensionsMap = {
".scss": ".css",
".sass": ".css",
".less": ".css"
};
@Injectable()
export class FileSystemResourceLoader extends ResourceLoader {
constructor(private fs: NSFileSystem) {
super();
}
get(url: string): Promise<string> {
const resolvedPath = this.resolve(url);
const templateFile = this.fs.fileFromPath(resolvedPath);
return templateFile.readText();
}
resolve(url: string): string {
const normalizedSourceUrl = this.resolveRelativeUrls(url);
const normalizedCompiledFileUrl = normalizedSourceUrl.replace(/\.\w+$/, ext => sourceExtensionsMap[ext] || ext);
if (normalizedCompiledFileUrl !== normalizedSourceUrl && this.fs.fileExists(normalizedCompiledFileUrl)) {
return normalizedCompiledFileUrl;
}
if (this.fs.fileExists(normalizedSourceUrl)) {
return normalizedSourceUrl;
}
if (normalizedCompiledFileUrl === normalizedSourceUrl) {
throw new Error(`Could not resolve ${url}. Looked for: ${normalizedSourceUrl}.`);
} else {
throw new Error(`Could not resolve ${url}.` +
`Looked for: ${normalizedCompiledFileUrl}, ${normalizedSourceUrl}.`);
}
}
private resolveRelativeUrls(url: string): string {
// Angular assembles absolute URLs and prefixes them with //
if (url.indexOf("/") !== 0) {
// Resolve relative URLs based on the app root.
return path.join(this.fs.currentApp().path, url);
} else {
return url;
}
}
}