forked from zapthedingbat/drawio-obsidian
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup-plugin-inline.js
44 lines (38 loc) · 1.1 KB
/
rollup-plugin-inline.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
import * as fs from "fs";
const inlinePrefix = "inline!";
const base64Prefix = "base64!";
export default function inline() {
const sources = new Map();
return {
name: "rollup-plugin-inline",
resolveId: (sourcePath) => {
if (sourcePath.startsWith(inlinePrefix)) {
const nextSourcePath = sourcePath.slice(inlinePrefix.length);
sources.set(nextSourcePath, "inline");
return nextSourcePath;
}
if (sourcePath.startsWith(base64Prefix)) {
const nextSourcePath = sourcePath.slice(base64Prefix.length);
sources.set(nextSourcePath, "base64");
return nextSourcePath;
}
return null;
},
transform: (codeContent, id) => {
if (sources.has(id)) {
const transform = sources.get(id);
const source =
transform === "base64"
? fs.readFileSync(id).toString("base64")
: codeContent;
const code = `export default ${JSON.stringify(source)};`;
const map = { mappings: "" };
return {
code,
map,
};
}
return null;
},
};
}