-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource-base.ts
57 lines (51 loc) · 1.44 KB
/
source-base.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
55
56
57
import { Source } from "./source.js";
export class SourceBase<F extends Source.Fragment = Source.Fragment> implements Source<F> {
#fragments: F[] | undefined = undefined;
#fragmentMap: Map<string, F> | undefined = undefined;
/**
* Called to parse all fragments in this source.
*/
protected parse(): F[] {
return [];
}
/**
* Called to create an updated version of this source.
*/
update?(context: Source.UpdateContext): Source.UpdateResult;
/**
* Get an array of filenames that this source is compiled to.
*
* If this is not implemented or returns an empty array,
* fragment ids from this source are added to the
* manifest as global fragments.
*/
getOutputFilenames?(): string[];
/**
* An array of all fragments in this source.
*/
get fragments(): readonly F[] {
if (this.#fragments === undefined) {
this.#fragments = this.parse ? this.parse() : [];
}
return this.#fragments;
}
/**
* A map of ids to fragments.
*
* Note that this may not contain all fragments if there are any duplicate fragment ids.
*/
get fragmentMap(): ReadonlyMap<string, F> {
if (this.#fragmentMap === undefined) {
const map = new Map<string, F>();
const fragments = this.fragments;
for (let i = 0; i < fragments.length; i++) {
const fragment = fragments[i];
if (fragment.fragmentId !== undefined) {
map.set(fragment.fragmentId, fragment);
}
}
this.#fragmentMap = map;
}
return this.#fragmentMap;
}
}