-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
53 lines (40 loc) · 1.23 KB
/
loader.js
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
import Asset from "./loader/asset.js";
// import LoadError from "./loader/error.js";
const { readFile } = Deno;
/**
* Loads files from arbitrary locations, typically a URL, and caches
* them similarly to how Deno caches locally imported JavaScript files.
* The Loader object attempts to extend this functionality to everything
* in Saur, especially in the CLI, allowing template files and static
* assets to be required into the project without needing to have a copy
* of the source code locally.
*/
export default class Loader {
constructor(options = {}) {
this.Processor = options.processor;
this.reader = options.reader || readFile;
this.base = options.base;
}
process(body) {
const { Processor } = this;
if (!Processor) {
return body;
}
const processor = new Processor(body);
return processor.process();
}
async require(path, caching = true) {
const asset = new Asset(path, this.base);
if (asset.local) {
const file = await this.reader(path);
return file;
}
if (caching && asset.cached) {
return asset.body();
}
const response = await fetch(asset.url);
const body = await response.text();
asset.cache({ body });
return body;
}
}