-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
outOfOrderRender.js
81 lines (70 loc) · 1.96 KB
/
outOfOrderRender.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* This class defers any `bundleGet` calls to a post-build transform step,
* to allow `getBundle` to be called before all of the `css` additions have been processed
*/
class OutOfOrderRender {
static SPLIT_REGEX = /(\/\*__EleventyBundle:[^:]*:[^:]*:[^:]*:EleventyBundle__\*\/)/;
static SEPARATOR = ":";
constructor(content) {
this.content = content;
this.managers = {};
}
static getAssetKey(type, name, bucket) {
if(Array.isArray(bucket)) {
bucket = bucket.join(",");
} else if(typeof bucket === "string") {
} else {
bucket = "";
}
return `/*__EleventyBundle:${type}:${name}:${bucket || "default"}:EleventyBundle__*/`
}
setAssetManager(name, assetManager) {
this.managers[name] = assetManager;
}
setOutputDirectory(dir) {
this.outputDirectory = dir;
}
setBundleDirectory(dir) {
this.bundleDirectory = dir;
}
normalizeMatch(match) {
if(match.startsWith("/*__EleventyBundle:")) {
let [prefix, type, name, bucket, suffix] = match.split(OutOfOrderRender.SEPARATOR);
return { type, name, bucket };
}
return match;
}
findAll() {
let matches = this.content.split(OutOfOrderRender.SPLIT_REGEX);
let ret = [];
for(let match of matches) {
ret.push(this.normalizeMatch(match));
}
return ret;
}
setWriteToFileSystem(isWrite) {
this.writeToFileSystem = isWrite;
}
replaceAll(url) {
let matches = this.findAll();
return matches.map(match => {
if(typeof match === "string") {
return match;
}
let {type, name, bucket} = match;
if(!this.managers[name]) {
throw new Error(`No asset manager found for ${name}. Known keys: ${Object.keys(this.managers)}`);
}
if(type === "get") {
return this.managers[name].getForPage(url, bucket);
} else if(type === "file") {
return this.managers[name].writeBundle(url, bucket, {
output: this.outputDirectory,
bundle: this.bundleDirectory,
write: this.writeToFileSystem,
});
}
return "";
}).join("");
}
}
module.exports = OutOfOrderRender;