-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource-loader.ts
45 lines (36 loc) · 1.45 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
import { ResourceLoader } from 'https://jspm.dev/@angular/compiler@11';
import { Injector } from 'https://jspm.dev/@angular/core@11';
import { join } from "https://deno.land/[email protected]/path/posix.ts";
import { RESOURCE_PATH } from './bootstrap.ts';
const { readFile } = Deno;
const decoder = new TextDecoder();
export class DenoFileSystemResourceLoader extends ResourceLoader {
private readonly filesCache: Map<string, string> = new Map<string, string>()
private readonly resourcePath: string;
// @ts-ignore
constructor(private readonly injector: Injector) {
super();
this.resourcePath = this.injector.get(RESOURCE_PATH);
}
resolve(url: string, baseUrl: string): string {
// Angular assembles absolute URL's and prefixes them with //
if (url.indexOf('/') !== 0) {
// Resolve relative URL's based on the app root.
return join(baseUrl, url);
} else {
return url;
}
}
get(url: string, aa?: any): Promise<string> {
const appDir = this.resourcePath || '';
const filePath = this.resolve(url, appDir);
if (this.filesCache.has(filePath)) {
return Promise.resolve(this.filesCache.get(filePath) + '');
}
return readFile(filePath).then(source => {
const template: string = decoder.decode(source)
this.filesCache.set(filePath, template)
return template;
});
}
}